From 6513d9f3e3d3863ff06bbedb8330d26790df391c Mon Sep 17 00:00:00 2001 From: terminx Date: Thu, 25 Oct 2018 23:32:41 +0000 Subject: [PATCH] Make ASS's mixing functions take a pointer to a voice instead of passing a bunch of parameters separately git-svn-id: https://svn.eduke32.com/eduke32@7121 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/audiolib/src/_multivc.h | 29 +++++++-------- source/audiolib/src/mix.cpp | 50 +++++++++++++++----------- source/audiolib/src/mixst.cpp | 62 ++++++++++++++++++-------------- source/audiolib/src/multivoc.cpp | 2 +- 4 files changed, 82 insertions(+), 61 deletions(-) diff --git a/source/audiolib/src/_multivc.h b/source/audiolib/src/_multivc.h index 271668646..f97ffc39c 100644 --- a/source/audiolib/src/_multivc.h +++ b/source/audiolib/src/_multivc.h @@ -56,21 +56,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define MIX_VOLUME(volume) ((max(0, min((volume), 255)) * (MV_MAXVOLUME + 1)) >> 8) template -static inline conditional_t< is_signed::value, make_unsigned_t, make_signed_t > MV_FLIP_SIGNEDNESS(T src) +static inline conditional_t< is_signed::value, make_unsigned_t, make_signed_t > FLIP_SIGN(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) +static inline enable_if_t::value, T> SCALE_SAMPLE(T src, float volume) { return (T)Blrintf((float)src * volume); } template -static inline enable_if_t::value, T> MV_VOLUME(T src, float volume) +static inline enable_if_t::value, T> SCALE_SAMPLE(T src, float volume) { - return MV_FLIP_SIGNEDNESS(MV_VOLUME(MV_FLIP_SIGNEDNESS(src), volume)); + return FLIP_SIGN(SCALE_SAMPLE(FLIP_SIGN(src), volume)); } struct split16_t @@ -110,7 +110,7 @@ typedef struct VoiceNode playbackstatus (*GetSound)(struct VoiceNode *voice); - void (*mix)(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); + void (*mix)(struct VoiceNode *voice, uint32_t length); const char *sound; @@ -186,7 +186,8 @@ extern Pan MV_PanTable[ MV_NUMPANPOSITIONS ][ MV_MAXVOLUME + 1 ]; extern int32_t MV_ErrorCode; extern int32_t MV_Installed; extern int32_t MV_MixRate; -typedef char HARSH_CLIP_TABLE_8[ MV_NUMVOICES * 256 ]; + +using HARSH_CLIP_TABLE_8 = char[MV_NUMVOICES * 256]; #define MV_SetErrorCode(status) MV_ErrorCode = (status); @@ -213,17 +214,17 @@ void MV_ReleaseXAVoice(VoiceNode *voice); void MV_ReleaseXMPVoice(VoiceNode *voice); // implemented in mix.c -void MV_Mix16BitMono(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); -void MV_Mix16BitStereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); -void MV_Mix16BitMono16(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); -void MV_Mix16BitStereo16(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); +void MV_Mix16BitMono(struct VoiceNode *voice, uint32_t length); +void MV_Mix16BitStereo(struct VoiceNode *voice, uint32_t length); +void MV_Mix16BitMono16(struct VoiceNode *voice, uint32_t length); +void MV_Mix16BitStereo16(struct VoiceNode *voice, uint32_t length); void MV_16BitReverb( char const *src, char *dest, int16_t *volume, int32_t count ); // implemented in mixst.c -void MV_Mix16BitMono8Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); -void MV_Mix16BitStereo8Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); -void MV_Mix16BitMono16Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); -void MV_Mix16BitStereo16Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume); +void MV_Mix16BitMono8Stereo(struct VoiceNode *voice, uint32_t length); +void MV_Mix16BitStereo8Stereo(struct VoiceNode *voice, uint32_t length); +void MV_Mix16BitMono16Stereo(struct VoiceNode *voice, uint32_t length); +void MV_Mix16BitStereo16Stereo(struct VoiceNode *voice, uint32_t length); extern char *MV_MixDestination; // pointer to the next output sample extern uint32_t MV_MixPosition; // return value of where the source pointer got to diff --git a/source/audiolib/src/mix.cpp b/source/audiolib/src/mix.cpp index fa53d2665..405e2e98f 100644 --- a/source/audiolib/src/mix.cpp +++ b/source/audiolib/src/mix.cpp @@ -29,19 +29,21 @@ */ // 8-bit mono source, 16-bit mono output -void MV_Mix16BitMono(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume) +void MV_Mix16BitMono(struct VoiceNode *voice, uint32_t length) { - auto const source = (uint8_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (uint8_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { - uint8_t const usample0 = MV_VOLUME(source[position >> 16], volume); + uint8_t const usample0 = SCALE_SAMPLE(source[position >> 16], volume); position += rate; *dest = (int16_t)clamp(MV_LeftVolume[usample0] + *dest, INT16_MIN, INT16_MAX); - dest += MV_SampleSize >> 1; } @@ -50,21 +52,23 @@ void MV_Mix16BitMono(uint32_t position, uint32_t rate, const char *start, uint32 } // 8-bit mono source, 16-bit stereo output -void MV_Mix16BitStereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume) +void MV_Mix16BitStereo(struct VoiceNode *voice, uint32_t length) { - auto const source = (uint8_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (uint8_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { - uint8_t const usample0 = MV_VOLUME(source[position >> 16], volume); + uint8_t const usample0 = SCALE_SAMPLE(source[position >> 16], volume); position += rate; *dest = (int16_t)clamp(MV_LeftVolume[usample0] + *dest, INT16_MIN, INT16_MAX); *(dest + (MV_RightChannelOffset >> 1)) = (int16_t)clamp(MV_RightVolume[usample0] + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); - dest += MV_SampleSize >> 1; } @@ -73,21 +77,24 @@ 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) +void MV_Mix16BitMono16(struct VoiceNode *voice, uint32_t length) { - auto const source = (int16_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (int16_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { int16_t const isample0 = B_LITTLE16(source[position >> 16]); - split16_t const usample0{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample0, volume))}; + split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))}; position += rate; 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 = (int16_t)clamp(sample0 + *dest, INT16_MIN, INT16_MAX); dest += MV_SampleSize >> 1; } @@ -96,24 +103,27 @@ 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) +void MV_Mix16BitStereo16(struct VoiceNode *voice, uint32_t length) { - auto const source = (int16_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (int16_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { int16_t const isample0 = B_LITTLE16(source[position >> 16]); - split16_t const usample0{MV_FLIP_SIGNEDNESS(MV_VOLUME(isample0, volume))}; + split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))}; position += rate; 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(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 8ce95a3fd..54ef14c77 100644 --- a/source/audiolib/src/mixst.cpp +++ b/source/audiolib/src/mixst.cpp @@ -21,23 +21,25 @@ #include "_multivc.h" /* - position = offset of starting sample in start - rate = resampling increment - start = sound data length = count of samples to mix + position = offset of starting sample in source + rate = resampling increment volume = direct volume adjustment, 1.0 = no change */ // 8-bit stereo source, 16-bit mono output -void MV_Mix16BitMono8Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume) +void MV_Mix16BitMono8Stereo(struct VoiceNode *voice, uint32_t length) { - auto const source = (uint8_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (uint8_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { - uint8_t const usample0 = MV_VOLUME(source[(position >> 16) << 1], volume); - uint8_t const usample1 = MV_VOLUME(source[((position >> 16) << 1) + 1], volume); + uint8_t const usample0 = SCALE_SAMPLE(source[(position >> 16) << 1], volume); + uint8_t const usample1 = SCALE_SAMPLE(source[((position >> 16) << 1) + 1], volume); position += rate; @@ -50,22 +52,24 @@ void MV_Mix16BitMono8Stereo(uint32_t position, uint32_t rate, const char *start, } // 8-bit stereo source, 16-bit stereo output -void MV_Mix16BitStereo8Stereo(uint32_t position, uint32_t rate, const char *start, uint32_t length, float volume) +void MV_Mix16BitStereo8Stereo(struct VoiceNode *voice, uint32_t length) { - auto const source = (uint8_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (uint8_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { - uint8_t const usample0 = MV_VOLUME(source[(position >> 16) << 1], volume); - uint8_t const usample1 = MV_VOLUME(source[((position >> 16) << 1) + 1], volume); + uint8_t const usample0 = SCALE_SAMPLE(source[(position >> 16) << 1], volume); + uint8_t const usample1 = SCALE_SAMPLE(source[((position >> 16) << 1) + 1], volume); position += rate; *dest = (int16_t)clamp(MV_LeftVolume[usample0] + *dest, INT16_MIN, INT16_MAX); *(dest + (MV_RightChannelOffset >> 1)) = (int16_t)clamp(MV_RightVolume[usample1] + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); - dest += MV_SampleSize >> 1; } @@ -74,24 +78,27 @@ 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) +void MV_Mix16BitMono16Stereo(struct VoiceNode *voice, uint32_t length) { - auto const source = (int16_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (int16_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { 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))}; + split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))}; + split16_t const usample1{FLIP_SIGN(SCALE_SAMPLE(isample1, volume))}; position += rate; 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 = (int16_t)clamp(((sample0 + sample1) >> 1) + *dest, INT16_MIN, INT16_MAX); dest += MV_SampleSize >> 1; } @@ -100,26 +107,29 @@ 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) +void MV_Mix16BitStereo16Stereo(struct VoiceNode *voice, uint32_t length) { - auto const source = (int16_t const *)start; - int16_t * dest = (int16_t *)MV_MixDestination; + auto const source = (int16_t const *)voice->sound; + int16_t * dest = (int16_t *)MV_MixDestination; + uint32_t position = voice->position; + uint32_t const rate = voice->RateScale; + float const volume = voice->volume; while (length--) { 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))}; + split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))}; + split16_t const usample1{FLIP_SIGN(SCALE_SAMPLE(isample1, volume))}; position += rate; 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(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); - dest += MV_SampleSize >> 1; } diff --git a/source/audiolib/src/multivoc.cpp b/source/audiolib/src/multivoc.cpp index b385d7268..3c7d126cd 100644 --- a/source/audiolib/src/multivoc.cpp +++ b/source/audiolib/src/multivoc.cpp @@ -177,7 +177,7 @@ static void MV_Mix(VoiceNode *voice, int const buffer) voclength = length; if (voice->mix) - voice->mix(position, rate, start, voclength, voice->volume); + voice->mix(voice, voclength); voice->position = MV_MixPosition;