From 6d226cf0da4bc0c081fc09c2ac8746d5d53daef1 Mon Sep 17 00:00:00 2001 From: terminx Date: Thu, 25 Oct 2018 23:32:35 +0000 Subject: [PATCH] Mixer function fixups from H266 git-svn-id: https://svn.eduke32.com/eduke32@7120 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/audiolib/src/_multivc.h | 38 ++++++++++++++-- source/audiolib/src/mix.cpp | 59 ++++++++++--------------- source/audiolib/src/mixst.cpp | 81 +++++++++++++--------------------- 3 files changed, 88 insertions(+), 90 deletions(-) diff --git a/source/audiolib/src/_multivc.h b/source/audiolib/src/_multivc.h index e23691c85..271668646 100644 --- a/source/audiolib/src/_multivc.h +++ b/source/audiolib/src/_multivc.h @@ -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 +static inline conditional_t< is_signed::value, make_unsigned_t, make_signed_t > MV_FLIP_SIGNEDNESS(T src) +{ + static constexpr make_unsigned_t msb = ((make_unsigned_t)1) << (sizeof(T) * CHAR_BIT - 1u); + return src ^ msb; +} + +template +static inline enable_if_t::value, T> MV_VOLUME(T src, float volume) +{ + return (T)Blrintf((float)src * volume); +} +template +static inline enable_if_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 diff --git a/source/audiolib/src/mix.cpp b/source/audiolib/src/mix.cpp index 4bc29abda..fa53d2665 100644 --- a/source/audiolib/src/mix.cpp +++ b/source/audiolib/src/mix.cpp @@ -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; } diff --git a/source/audiolib/src/mixst.cpp b/source/audiolib/src/mixst.cpp index d737ebf36..8ce95a3fd 100644 --- a/source/audiolib/src/mixst.cpp +++ b/source/audiolib/src/mixst.cpp @@ -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; }