diff --git a/src/FAudioGMS.c b/src/FAudioGMS.c index 3c95c02..7d89787 100644 --- a/src/FAudioGMS.c +++ b/src/FAudioGMS.c @@ -332,11 +332,14 @@ static inline FAudioGMS_EffectChain *FAudioGMS_INTERNAL_LookupEffectChain(uint32 static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance *instance); static void FAudioGMS_INTERNAL_SoundInstance_Play(FAudioGMS_SoundInstance *instance); static void FAudioGMS_INTERNAL_SoundInstance_Stop(FAudioGMS_SoundInstance *instance); +static void FAudioGMS_INTERNAL_SoundInstance_Destroy(FAudioGMS_SoundInstance *instance); static void FAudioGMS_INTERNAL_OnBufferEndCallback( FAudioVoiceCallback *callback, FAudioGMS_SoundInstance *instance) { + uint32_t i; + if (instance->isStatic) { FAudioGMS_INTERNAL_SoundInstance_Stop(instance); @@ -360,6 +363,27 @@ static void FAudioGMS_INTERNAL_OnBufferEndCallback( FAudioGMS_INTERNAL_SoundInstance_Play(instance->queuedSoundInstance); instance->queuedSoundInstance = NULL; } + + if (instance->destroyOnFinish) + { + /* this is a callback so we don't want to immediately destroy and screw up data */ + instance->destroyTimerActive = 1; + instance->destroyTimer = 0; + + if (instance->voice.effectChainAttached) + { + /* If we have active reverb we don't want to clean up until the decay time is over + */ + for (i = 0; i < instance->voice.effectChain->effectCount; i += 1) + { + if (instance->voice.effectChain->effectTypes[i] == EffectType_Reverb) + { + instance->destroyTimer += instance->voice.effectChain->effectParameters[i] + .reverbParameters.DecayTime; + } + } + } + } } } @@ -707,7 +731,7 @@ static FAudioGMS_SoundInstance *FAudioGMS_INTERNAL_SoundInstance_Init( &instance->format, FAUDIO_VOICE_USEFILTER, FAUDIO_DEFAULT_FREQ_RATIO, - isStatic ? NULL : &device->voiceCallbacks, + &device->voiceCallbacks, &instance->voice.sends, NULL); @@ -2025,43 +2049,13 @@ void FAudioGMS_Update() FAudioGMS_INTERNAL_SoundInstance_SetVolume(instance, volume); } - if (instance->destroyOnFinish) + if (instance->destroyTimerActive) { - FAudioVoiceState state; - FAudioSourceVoice_GetState( - instance->voice.handle, - &state, - FAUDIO_VOICE_NOSAMPLESPLAYED); + instance->destroyTimer -= device->timestep; - if (instance->destroyTimerActive) + if (instance->destroyTimer <= 0) { - instance->destroyTimer -= device->timestep; - - if (instance->destroyTimer <= 0) - { - FAudioGMS_INTERNAL_SoundInstance_Destroy(instance); - } - } - else if (state.BuffersQueued == 0) - { - if (instance->voice.effectChainAttached) - { - /* If we have active reverb we don't want to clean up until the decay time is over */ - for (j = 0; j < instance->voice.effectChain->effectCount; j += 1) - { - if (instance->voice.effectChain->effectTypes[j] == EffectType_Reverb) - { - instance->destroyTimerActive = 1; - instance->destroyTimer += - instance->voice.effectChain->effectParameters[j] - .reverbParameters.DecayTime; - } - } - } - else - { - FAudioGMS_INTERNAL_SoundInstance_Destroy(instance); - } + FAudioGMS_INTERNAL_SoundInstance_Destroy(instance); } } }