Change audiolib mixing functions to just use a return value instead of setting a global to indicate the new mixing buffer position

git-svn-id: https://svn.eduke32.com/eduke32@7285 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-12-15 01:40:03 +00:00
parent c3f88abf73
commit 465e19909f
4 changed files with 52 additions and 40 deletions

View file

@ -112,7 +112,7 @@ typedef struct VoiceNode
playbackstatus (*GetSound)(struct VoiceNode *); playbackstatus (*GetSound)(struct VoiceNode *);
void (*mix)(struct VoiceNode const *, uint32_t); uint32_t (*mix)(struct VoiceNode const *, uint32_t);
const char *sound; const char *sound;
@ -215,20 +215,19 @@ void MV_ReleaseXAVoice(VoiceNode *voice);
void MV_ReleaseXMPVoice(VoiceNode *voice); void MV_ReleaseXMPVoice(VoiceNode *voice);
// implemented in mix.c // implemented in mix.c
void MV_Mix16BitMono(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitMono(struct VoiceNode const *voice, uint32_t length);
void MV_Mix16BitStereo(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitStereo(struct VoiceNode const *voice, uint32_t length);
void MV_Mix16BitMono16(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitMono16(struct VoiceNode const *voice, uint32_t length);
void MV_Mix16BitStereo16(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitStereo16(struct VoiceNode const *voice, uint32_t length);
void MV_16BitReverb(char const *src, char *dest, const int16_t *volume, int32_t count); void MV_16BitReverb(char const *src, char *dest, const int16_t *volume, int32_t count);
// implemented in mixst.c // implemented in mixst.c
void MV_Mix16BitMono8Stereo(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitMono8Stereo(struct VoiceNode const *voice, uint32_t length);
void MV_Mix16BitStereo8Stereo(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitStereo8Stereo(struct VoiceNode const *voice, uint32_t length);
void MV_Mix16BitMono16Stereo(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitMono16Stereo(struct VoiceNode const *voice, uint32_t length);
void MV_Mix16BitStereo16Stereo(struct VoiceNode const *voice, uint32_t length); uint32_t MV_Mix16BitStereo16Stereo(struct VoiceNode const *voice, uint32_t length);
extern char *MV_MixDestination; // pointer to the next output sample extern char *MV_MixDestination; // pointer to the next output sample
extern uint32_t MV_MixPosition; // return value of where the source pointer got to
extern const int16_t *MV_LeftVolume; extern const int16_t *MV_LeftVolume;
extern const int16_t *MV_RightVolume; extern const int16_t *MV_RightVolume;
extern int32_t MV_SampleSize; extern int32_t MV_SampleSize;

View file

@ -28,7 +28,7 @@
*/ */
// 8-bit mono source, 16-bit mono output // 8-bit mono source, 16-bit mono output
void MV_Mix16BitMono(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitMono(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (uint8_t const *)voice->sound; auto const source = (uint8_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -37,7 +37,7 @@ void MV_Mix16BitMono(struct VoiceNode const * const voice, uint32_t length)
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
uint8_t const usample0 = SCALE_SAMPLE(source[position >> 16], volume); uint8_t const usample0 = SCALE_SAMPLE(source[position >> 16], volume);
@ -46,13 +46,15 @@ void MV_Mix16BitMono(struct VoiceNode const * const voice, uint32_t length)
*dest = (int16_t)clamp(SCALE_SAMPLE(MV_LeftVolume[usample0], MV_GlobalVolume) + *dest, INT16_MIN, INT16_MAX); *dest = (int16_t)clamp(SCALE_SAMPLE(MV_LeftVolume[usample0], MV_GlobalVolume) + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
// 8-bit mono source, 16-bit stereo output // 8-bit mono source, 16-bit stereo output
void MV_Mix16BitStereo(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitStereo(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (uint8_t const *)voice->sound; auto const source = (uint8_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -61,7 +63,7 @@ void MV_Mix16BitStereo(struct VoiceNode const * const voice, uint32_t length)
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
uint8_t const usample0 = SCALE_SAMPLE(source[position >> 16], volume); uint8_t const usample0 = SCALE_SAMPLE(source[position >> 16], volume);
@ -72,13 +74,15 @@ void MV_Mix16BitStereo(struct VoiceNode const * const voice, uint32_t length)
= (int16_t)clamp(SCALE_SAMPLE(MV_RightVolume[usample0], MV_GlobalVolume) + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); = (int16_t)clamp(SCALE_SAMPLE(MV_RightVolume[usample0], MV_GlobalVolume) + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
// 16-bit mono source, 16-bit mono output // 16-bit mono source, 16-bit mono output
void MV_Mix16BitMono16(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitMono16(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (int16_t const *)voice->sound; auto const source = (int16_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -87,7 +91,7 @@ void MV_Mix16BitMono16(struct VoiceNode const * const voice, uint32_t length)
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
int16_t const isample0 = B_LITTLE16(source[position >> 16]); int16_t const isample0 = B_LITTLE16(source[position >> 16]);
split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))}; split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))};
@ -100,13 +104,15 @@ void MV_Mix16BitMono16(struct VoiceNode const * const voice, uint32_t length)
*dest = (int16_t)clamp(sample0 + *dest, INT16_MIN, INT16_MAX); *dest = (int16_t)clamp(sample0 + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
// 16-bit mono source, 16-bit stereo output // 16-bit mono source, 16-bit stereo output
void MV_Mix16BitStereo16(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitStereo16(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (int16_t const *)voice->sound; auto const source = (int16_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -115,7 +121,7 @@ void MV_Mix16BitStereo16(struct VoiceNode const * const voice, uint32_t length)
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
int16_t const isample0 = B_LITTLE16(source[position >> 16]); int16_t const isample0 = B_LITTLE16(source[position >> 16]);
split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))}; split16_t const usample0{FLIP_SIGN(SCALE_SAMPLE(isample0, volume))};
@ -132,9 +138,11 @@ void MV_Mix16BitStereo16(struct VoiceNode const * const voice, uint32_t length)
= (int16_t)clamp(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); = (int16_t)clamp(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
void MV_16BitReverb(char const *src, char *dest, const int16_t *volume, int32_t count) void MV_16BitReverb(char const *src, char *dest, const int16_t *volume, int32_t count)
@ -156,5 +164,6 @@ void MV_16BitReverb(char const *src, char *dest, const int16_t *volume, int32_t
sample0l = (volume)[sample0l] >> 8; sample0l = (volume)[sample0l] >> 8;
sample0h = (volume)[sample0h]; sample0h = (volume)[sample0h];
*output++ = (int16_t)(sample0l + sample0h + 128); *output++ = (int16_t)(sample0l + sample0h + 128);
} while (--count > 0); }
while (--count > 0);
} }

View file

@ -28,7 +28,7 @@
*/ */
// 8-bit stereo source, 16-bit mono output // 8-bit stereo source, 16-bit mono output
void MV_Mix16BitMono8Stereo(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitMono8Stereo(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (uint8_t const *)voice->sound; auto const source = (uint8_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -37,7 +37,7 @@ void MV_Mix16BitMono8Stereo(struct VoiceNode const * const voice, uint32_t lengt
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
uint8_t const usample0 = SCALE_SAMPLE(source[(position >> 16) << 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); uint8_t const usample1 = SCALE_SAMPLE(source[((position >> 16) << 1) + 1], volume);
@ -48,13 +48,15 @@ void MV_Mix16BitMono8Stereo(struct VoiceNode const * const voice, uint32_t lengt
SCALE_SAMPLE(MV_LeftVolume[usample1], MV_GlobalVolume)) >> 1) + *dest, INT16_MIN, INT16_MAX); SCALE_SAMPLE(MV_LeftVolume[usample1], MV_GlobalVolume)) >> 1) + *dest, INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
// 8-bit stereo source, 16-bit stereo output // 8-bit stereo source, 16-bit stereo output
void MV_Mix16BitStereo8Stereo(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitStereo8Stereo(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (uint8_t const *)voice->sound; auto const source = (uint8_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -63,7 +65,7 @@ void MV_Mix16BitStereo8Stereo(struct VoiceNode const * const voice, uint32_t len
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
uint8_t const usample0 = SCALE_SAMPLE(source[(position >> 16) << 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); uint8_t const usample1 = SCALE_SAMPLE(source[((position >> 16) << 1) + 1], volume);
@ -75,13 +77,15 @@ void MV_Mix16BitStereo8Stereo(struct VoiceNode const * const voice, uint32_t len
= (int16_t)clamp(SCALE_SAMPLE(MV_RightVolume[usample1], MV_GlobalVolume) + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); = (int16_t)clamp(SCALE_SAMPLE(MV_RightVolume[usample1], MV_GlobalVolume) + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
// 16-bit stereo source, 16-bit mono output // 16-bit stereo source, 16-bit mono output
void MV_Mix16BitMono16Stereo(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitMono16Stereo(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (int16_t const *)voice->sound; auto const source = (int16_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -90,7 +94,7 @@ void MV_Mix16BitMono16Stereo(struct VoiceNode const * const voice, uint32_t leng
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
int16_t const isample0 = B_LITTLE16(source[(position >> 16) << 1]); int16_t const isample0 = B_LITTLE16(source[(position >> 16) << 1]);
int16_t const isample1 = B_LITTLE16(source[((position >> 16) << 1) + 1]); int16_t const isample1 = B_LITTLE16(source[((position >> 16) << 1) + 1]);
@ -107,13 +111,15 @@ void MV_Mix16BitMono16Stereo(struct VoiceNode const * const voice, uint32_t leng
*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; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }
// 16-bit stereo source, 16-bit stereo output // 16-bit stereo source, 16-bit stereo output
void MV_Mix16BitStereo16Stereo(struct VoiceNode const * const voice, uint32_t length) uint32_t MV_Mix16BitStereo16Stereo(struct VoiceNode const * const voice, uint32_t length)
{ {
auto const source = (int16_t const *)voice->sound; auto const source = (int16_t const *)voice->sound;
auto dest = (int16_t *)MV_MixDestination; auto dest = (int16_t *)MV_MixDestination;
@ -122,7 +128,7 @@ void MV_Mix16BitStereo16Stereo(struct VoiceNode const * const voice, uint32_t le
uint32_t const rate = voice->RateScale; uint32_t const rate = voice->RateScale;
float const volume = voice->volume; float const volume = voice->volume;
while (length--) do
{ {
int16_t const isample0 = B_LITTLE16(source[(position >> 16) << 1]); int16_t const isample0 = B_LITTLE16(source[(position >> 16) << 1]);
int16_t const isample1 = B_LITTLE16(source[((position >> 16) << 1) + 1]); int16_t const isample1 = B_LITTLE16(source[((position >> 16) << 1) + 1]);
@ -141,7 +147,9 @@ void MV_Mix16BitStereo16Stereo(struct VoiceNode const * const voice, uint32_t le
= (int16_t)clamp(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX); = (int16_t)clamp(sample1 + *(dest + (MV_RightChannelOffset >> 1)), INT16_MIN, INT16_MAX);
dest += MV_SampleSize >> 1; dest += MV_SampleSize >> 1;
} }
while (--length);
MV_MixPosition = position;
MV_MixDestination = (char *)dest; MV_MixDestination = (char *)dest;
return position;
} }

View file

@ -90,8 +90,6 @@ const int16_t *MV_RightVolume;
int32_t MV_SampleSize = 1; int32_t MV_SampleSize = 1;
int32_t MV_RightChannelOffset; int32_t MV_RightChannelOffset;
uint32_t MV_MixPosition;
int32_t MV_ErrorCode = MV_NotInstalled; int32_t MV_ErrorCode = MV_NotInstalled;
float MV_GlobalVolume = 1.f; float MV_GlobalVolume = 1.f;
@ -182,11 +180,9 @@ static bool MV_Mix(VoiceNode *voice, int const buffer)
if (voice->priority == FX_MUSIC_PRIORITY) if (voice->priority == FX_MUSIC_PRIORITY)
MV_GlobalVolume = 1.f; MV_GlobalVolume = 1.f;
if (voice->mix) voice->position = voice->mix(voice, voclength);
voice->mix(voice, voclength);
MV_GlobalVolume = gv; MV_GlobalVolume = gv;
voice->position = MV_MixPosition;
length -= voclength; length -= voclength;