Mixer function fixups from H266

git-svn-id: https://svn.eduke32.com/eduke32@7120 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-10-25 23:32:35 +00:00
parent 1c007c56c5
commit 6d226cf0da
3 changed files with 88 additions and 90 deletions

View file

@ -31,8 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef MULTIVC_H_
#define MULTIVC_H_
#include "limits.h"
#include "inttypes.h"
#include "multivoc.h"
#define VOC_8BIT 0x0
@ -56,7 +54,41 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define MV_MUSIC_PRIORITY INT_MAX
#define MIX_VOLUME(volume) ((max(0, min((volume), 255)) * (MV_MAXVOLUME + 1)) >> 8)
#define MV_VOLUME(src) (int)((float)(src) * volume)
template <typename T>
static inline conditional_t< is_signed<T>::value, make_unsigned_t<T>, make_signed_t<T> > MV_FLIP_SIGNEDNESS(T src)
{
static constexpr make_unsigned_t<T> msb = ((make_unsigned_t<T>)1) << (sizeof(T) * CHAR_BIT - 1u);
return src ^ msb;
}
template <typename T>
static inline enable_if_t<is_signed<T>::value, T> MV_VOLUME(T src, float volume)
{
return (T)Blrintf((float)src * volume);
}
template <typename T>
static inline enable_if_t<is_unsigned<T>::value, T> MV_VOLUME(T src, float volume)
{
return MV_FLIP_SIGNEDNESS(MV_VOLUME(MV_FLIP_SIGNEDNESS(src), volume));
}
struct split16_t
{
explicit split16_t(uint16_t x) : v{x} {}
uint8_t l() const
{
return (v & 0x00FFu);
}
uint8_t h() const
{
return (v & 0xFF00u) >> CHAR_BIT;
}
private:
uint16_t v;
};
#define MV_MIXBUFFERSIZE 256
#define MV_NUMBEROFBUFFERS 16

View file

@ -36,10 +36,12 @@ void MV_Mix16BitMono(uint32_t position, uint32_t rate, const char *start, uint32
while (length--)
{
int const sample0 = MV_VOLUME(source[position >> 16]);
uint8_t const usample0 = MV_VOLUME(source[position >> 16], volume);
position += rate;
*dest = (int16_t)clamp(MV_LeftVolume[sample0] + *dest, INT16_MIN, INT16_MAX);
*dest = (int16_t)clamp(MV_LeftVolume[usample0] + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}
@ -55,12 +57,14 @@ void MV_Mix16BitStereo(uint32_t position, uint32_t rate, const char *start, uint
while (length--)
{
int const sample0 = MV_VOLUME(source[position >> 16]);
uint8_t const usample0 = MV_VOLUME(source[position >> 16], volume);
position += rate;
*dest = (int16_t)clamp(MV_LeftVolume[sample0] + *dest, INT16_MIN, INT16_MAX);
*dest = (int16_t)clamp(MV_LeftVolume[usample0] + *dest, INT16_MIN, INT16_MAX);
*(dest + (MV_RightChannelOffset >> 1))
= (int16_t)clamp(MV_RightVolume[sample0] + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
= (int16_t)clamp(MV_RightVolume[usample0] + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}
@ -71,25 +75,18 @@ void MV_Mix16BitStereo(uint32_t position, uint32_t rate, const char *start, uint
// 16-bit mono source, 16-bit mono output
void MV_Mix16BitMono16(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume)
{
auto const source = (uint16_t const *)start;
auto const source = (int16_t const *)start;
int16_t * dest = (int16_t *)MV_MixDestination;
while (length--)
{
int const sample0 = MV_VOLUME(source[position >> 16]);
#ifdef BIGENDIAN
int sample0l = sample0 >> 8;
int sample0h = (sample0 & 255) ^ 128;
#else
int sample0l = sample0 & 255;
int sample0h = (sample0 >> 8) ^ 128;
#endif
int16_t const isample0 = B_LITTLE16(source[position >> 16]);
split16_t const usample0{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample0, volume))};
position += rate;
sample0l = MV_LeftVolume[sample0l] >> 8;
sample0h = MV_LeftVolume[sample0h];
*dest = (int16_t)clamp(sample0l + sample0h + 128 + *dest, INT16_MIN, INT16_MAX);
int32_t const sample0 = (MV_LeftVolume[usample0.l()] >> 8) + MV_LeftVolume[usample0.h()] + 128;
*dest = (int16_t)clamp(sample0 + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}
@ -101,31 +98,21 @@ void MV_Mix16BitMono16(uint32_t position, uint32_t rate, const char *start, uint
// 16-bit mono source, 16-bit stereo output
void MV_Mix16BitStereo16(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume)
{
auto const source = (uint16_t const *)start;
auto const source = (int16_t const *)start;
int16_t * dest = (int16_t *)MV_MixDestination;
while (length--)
{
int const sample0 = MV_VOLUME(source[position >> 16]);
int16_t const isample0 = B_LITTLE16(source[position >> 16]);
split16_t const usample0{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample0, volume))};
position += rate;
#ifdef BIGENDIAN
int sample0l = sample0 >> 8;
int sample0h = (sample0 & 255) ^ 128;
#else
int sample0l = sample0 & 255;
int sample0h = (sample0 >> 8) ^ 128;
#endif
int const sample1l = MV_RightVolume[sample0l] >> 8;
int const sample1h = MV_RightVolume[sample0h];
sample0l = MV_LeftVolume[sample0l] >> 8;
sample0h = MV_LeftVolume[sample0h];
*dest = (int16_t)clamp(sample0l + sample0h + 128 + *dest, INT16_MIN, INT16_MAX);
int32_t const sample0 = (MV_LeftVolume[usample0.l()] >> 8) + MV_LeftVolume[usample0.h()] + 128;
int32_t const sample1 = (MV_RightVolume[usample0.l()] >> 8) + MV_RightVolume[usample0.h()] + 128;
*dest = (int16_t)clamp(sample0 + *dest, INT16_MIN, INT16_MAX);
*(dest + (MV_RightChannelOffset >> 1))
= (int16_t)clamp(sample1l + sample1h + 128 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
= (int16_t)clamp(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}

View file

@ -36,11 +36,12 @@ void MV_Mix16BitMono8Stereo(uint32_t position, uint32_t rate, const char *start,
while (length--)
{
int const sample0 = MV_VOLUME(source[(position >> 16) << 1]);
int const sample1 = MV_VOLUME(source[((position >> 16) << 1) + 1]);
uint8_t const usample0 = MV_VOLUME(source[(position >> 16) << 1], volume);
uint8_t const usample1 = MV_VOLUME(source[((position >> 16) << 1) + 1], volume);
position += rate;
*dest = (int16_t)clamp(((MV_LeftVolume[sample0] + MV_LeftVolume[sample1]) >> 1) + *dest, INT16_MIN, INT16_MAX);
*dest = (int16_t)clamp(((MV_LeftVolume[usample0] + MV_LeftVolume[usample1]) >> 1) + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}
@ -56,13 +57,15 @@ void MV_Mix16BitStereo8Stereo(uint32_t position, uint32_t rate, const char *star
while (length--)
{
int const sample0 = MV_VOLUME(source[(position >> 16) << 1]);
int const sample1 = MV_VOLUME(source[((position >> 16) << 1) + 1]);
uint8_t const usample0 = MV_VOLUME(source[(position >> 16) << 1], volume);
uint8_t const usample1 = MV_VOLUME(source[((position >> 16) << 1) + 1], volume);
position += rate;
*dest = (int16_t)clamp(MV_LeftVolume[sample0] + *dest, INT16_MIN, INT16_MAX);
*dest = (int16_t)clamp(MV_LeftVolume[usample0] + *dest, INT16_MIN, INT16_MAX);
*(dest + (MV_RightChannelOffset >> 1))
= (int16_t)clamp(MV_RightVolume[sample1] + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
= (int16_t)clamp(MV_RightVolume[usample1] + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}
@ -73,35 +76,22 @@ void MV_Mix16BitStereo8Stereo(uint32_t position, uint32_t rate, const char *star
// 16-bit stereo source, 16-bit mono output
void MV_Mix16BitMono16Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume)
{
auto const source = (uint16_t const *)start;
auto const source = (int16_t const *)start;
int16_t * dest = (int16_t *)MV_MixDestination;
while (length--)
{
int sample0 = MV_VOLUME(source[(position >> 16) << 1]);
int sample1 = MV_VOLUME(source[((position >> 16) << 1) + 1]);
#ifdef BIGENDIAN
int sample0l = sample0 >> 8;
int sample0h = (sample0 & 255) ^ 128;
int sample1l = sample1 >> 8;
int sample1h = (sample1 & 255) ^ 128;
#else
int sample0l = sample0 & 255;
int sample0h = (sample0 >> 8) ^ 128;
int sample1l = sample1 & 255;
int sample1h = (sample1 >> 8) ^ 128;
#endif
int16_t const isample0 = B_LITTLE16(source[(position >> 16) << 1]);
int16_t const isample1 = B_LITTLE16(source[((position >> 16) << 1) + 1]);
split16_t const usample0{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample0, volume))};
split16_t const usample1{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample1, volume))};
position += rate;
sample0l = MV_LeftVolume[sample0l] >> 8;
sample0h = MV_LeftVolume[sample0h];
sample0 = sample0l + sample0h + 128;
sample1l = MV_LeftVolume[sample1l] >> 8;
sample1h = MV_LeftVolume[sample1h];
sample1 = sample1l + sample1h + 128;
int32_t const sample0 = (MV_LeftVolume[usample0.l()] >> 8) + MV_LeftVolume[usample0.h()] + 128;
int32_t const sample1 = (MV_LeftVolume[usample1.l()] >> 8) + MV_LeftVolume[usample1.h()] + 128;
*dest = (int16_t)clamp(((sample0 + sample1) >> 1) + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}
@ -112,35 +102,24 @@ void MV_Mix16BitMono16Stereo(uint32_t position, uint32_t rate, const char *start
// 16-bit stereo source, 16-bit stereo output
void MV_Mix16BitStereo16Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume)
{
auto const source = (uint16_t const *)start;
auto const source = (int16_t const *)start;
int16_t * dest = (int16_t *)MV_MixDestination;
while (length--)
{
int sample0 = MV_VOLUME(source[(position >> 16) << 1]);
int sample1 = MV_VOLUME(source[((position >> 16) << 1) + 1]);
#ifdef BIGENDIAN
int sample0l = sample0 >> 8;
int sample0h = (sample0 & 255) ^ 128;
int sample1l = sample1 >> 8;
int sample1h = (sample1 & 255) ^ 128;
#else
int sample0l = sample0 & 255;
int sample0h = (sample0 >> 8) ^ 128;
int sample1l = sample1 & 255;
int sample1h = (sample1 >> 8) ^ 128;
#endif
int16_t const isample0 = B_LITTLE16(source[(position >> 16) << 1]);
int16_t const isample1 = B_LITTLE16(source[((position >> 16) << 1) + 1]);
split16_t const usample0{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample0, volume))};
split16_t const usample1{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample1, volume))};
position += rate;
sample0l = MV_LeftVolume[sample0l] >> 8;
sample0h = MV_LeftVolume[sample0h];
sample1l = MV_RightVolume[sample1l] >> 8;
sample1h = MV_RightVolume[sample1h];
*dest = (int16_t)clamp(sample0l + sample0h + 128 + *dest, INT16_MIN, INT16_MAX);
int32_t const sample0 = (MV_LeftVolume[usample0.l()] >> 8) + MV_LeftVolume[usample0.h()] + 128;
int32_t const sample1 = (MV_RightVolume[usample1.l()] >> 8) + MV_RightVolume[usample1.h()] + 128;
*dest = (int16_t)clamp(sample0 + *dest, INT16_MIN, INT16_MAX);
*(dest + (MV_RightChannelOffset >> 1))
= (int16_t)clamp(sample1l + sample1h + 128 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
= (int16_t)clamp(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1;
}