change destroyOnFinish behavior to use OnBufferEnd

main
cosmonaut 2021-11-11 14:45:21 -08:00
parent 6edd9ebc94
commit 3568028dc3
1 changed files with 29 additions and 35 deletions

View File

@ -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);
}
}
}