From 0e04346b8c7aedf55b3acfb7dd9301abbc3358c5 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 9 Nov 2021 15:32:22 -0800 Subject: [PATCH] fix effect chains making sounds mono --- src/FAudioGMS.c | 119 +++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 56 deletions(-) diff --git a/src/FAudioGMS.c b/src/FAudioGMS.c index d8de43f..09a7b7c 100644 --- a/src/FAudioGMS.c +++ b/src/FAudioGMS.c @@ -506,6 +506,57 @@ void FAudioGMS_Init(double spatialDistanceScale, double timestep) fflush(stdout); } +double FAudioGMS_StaticSound_LoadWAV(char *filePath) +{ + RETURN_ON_NULL_DEVICE(-1.0) + drwav_uint64 frameCount; + + FAudioGMS_StaticSound *sound = SDL_malloc(sizeof(FAudioGMS_StaticSound)); + float *pSampleData = drwav_open_file_and_read_pcm_frames_f32( + filePath, + &sound->channels, + &sound->samplesPerSecond, + &frameCount, + NULL); + if (pSampleData == NULL) + { + Log("Error opening WAV file: "); + Log(filePath); + SDL_free(sound); + return -1; + } + + sound->buffer.AudioBytes = (uint32_t)(frameCount * sound->channels * sizeof(float)); + sound->buffer.Flags = 0; + sound->buffer.LoopBegin = 0; + sound->buffer.LoopCount = 0; + sound->buffer.LoopLength = 0; + sound->buffer.PlayBegin = 0; + sound->buffer.PlayLength = frameCount; + sound->buffer.pAudioData = (uint8_t *)pSampleData; + sound->buffer.pContext = NULL; + + sound->lengthInSeconds = frameCount / sound->samplesPerSecond; + + if (device->staticSoundIndexStack.count > 0) + { + sound->id = IdStack_Pop(&device->staticSoundIndexStack); + } + else + { + sound->id = device->staticSoundCount; + + device->staticSounds = SDL_realloc( + device->staticSounds, + (device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound *)); + device->staticSoundCount += 1; + } + + device->staticSounds[sound->id] = sound; + + return (double)sound->id; +} + /* Taken from * https://github.com/FNA-XNA/FNA/blob/master/src/Audio/SoundEffectInstance.cs */ @@ -563,61 +614,8 @@ static void SetPanMatrixCoefficients(FAudioGMS_SoundInstance *instance) } } -double FAudioGMS_StaticSound_LoadWAV(char *filePath) +static void FAudioGMS_INTERNAl_SoundInstance_ApplyPan(FAudioGMS_SoundInstance* instance) { - RETURN_ON_NULL_DEVICE(-1.0) - drwav_uint64 frameCount; - - FAudioGMS_StaticSound *sound = SDL_malloc(sizeof(FAudioGMS_StaticSound)); - float *pSampleData = drwav_open_file_and_read_pcm_frames_f32( - filePath, - &sound->channels, - &sound->samplesPerSecond, - &frameCount, - NULL); - if (pSampleData == NULL) - { - Log("Error opening WAV file: "); - Log(filePath); - SDL_free(sound); - return -1; - } - - sound->buffer.AudioBytes = (uint32_t)(frameCount * sound->channels * sizeof(float)); - sound->buffer.Flags = 0; - sound->buffer.LoopBegin = 0; - sound->buffer.LoopCount = 0; - sound->buffer.LoopLength = 0; - sound->buffer.PlayBegin = 0; - sound->buffer.PlayLength = frameCount; - sound->buffer.pAudioData = (uint8_t *)pSampleData; - sound->buffer.pContext = NULL; - - sound->lengthInSeconds = frameCount / sound->samplesPerSecond; - - if (device->staticSoundIndexStack.count > 0) - { - sound->id = IdStack_Pop(&device->staticSoundIndexStack); - } - else - { - sound->id = device->staticSoundCount; - - device->staticSounds = SDL_realloc( - device->staticSounds, - (device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound *)); - device->staticSoundCount += 1; - } - - device->staticSounds[sound->id] = sound; - - return (double)sound->id; -} - -static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance *instance, float pan) -{ - pan = SDL_max(-1.0, SDL_min(1.0, pan)); - instance->pan = pan; SetPanMatrixCoefficients(instance); FAudioVoice_SetOutputMatrix( @@ -629,6 +627,13 @@ static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance *ins 0); } +static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance *instance, float pan) +{ + pan = SDL_max(-1.0, SDL_min(1.0, pan)); + instance->pan = pan; + FAudioGMS_INTERNAl_SoundInstance_ApplyPan(instance); +} + static void FAudioGMS_INTERNAL_SoundInstance_UpdatePitch(FAudioGMS_SoundInstance *instance) { float doppler = 1.0f; @@ -1750,7 +1755,7 @@ static void FAudioGMS_INTERNAL_Voice_SetEffectGain(FAudioGMS_Voice *voice, float float *outputMatrix = SDL_stack_alloc(float, voice->effectSends.SendCount); outputMatrix[0] = effectGain; - if (voice->effectSends.SendCount == 2) + if (voice->handle->outputChannels == 2) { outputMatrix[1] = effectGain; } @@ -1758,7 +1763,7 @@ static void FAudioGMS_INTERNAL_Voice_SetEffectGain(FAudioGMS_Voice *voice, float FAudioVoice_SetOutputMatrix( voice->handle, voice->effectVoice, - voice->effectSends.SendCount, + voice->handle->outputChannels, 1, outputMatrix, 0); @@ -1926,6 +1931,8 @@ void FAudioGMS_SoundInstance_SetEffectChain( if (instance != NULL && effectChain != NULL) { FAudioGMS_INTERNAL_SetEffectChain(&instance->voice, effectChain, effectGain); + /* re-apply pan after setting chain because sends may have changed */ + FAudioGMS_INTERNAl_SoundInstance_ApplyPan(instance); } }