From 3a347e04ed166d77cbb23f1d7426de6da3f3efc9 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sat, 29 Aug 2020 17:24:11 +1000 Subject: [PATCH] Fix a bug where a sound is never marked as not playing. --- research/ma_engine.c | 2 +- research/ma_engine.h | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/research/ma_engine.c b/research/ma_engine.c index e3981fce..edcc61b5 100644 --- a/research/ma_engine.c +++ b/research/ma_engine.c @@ -98,7 +98,7 @@ int main(int argc, char** argv) ma_sound_start(&sound); //ma_sleep(1000); - ma_sound_set_looping(&sound2, MA_TRUE); + //ma_sound_set_looping(&sound2, MA_TRUE); ma_sound_set_volume(&sound2, 0.5f); ma_sound_start(&sound2); diff --git a/research/ma_engine.h b/research/ma_engine.h index 63b5d6f4..cd643d4d 100644 --- a/research/ma_engine.h +++ b/research/ma_engine.h @@ -999,6 +999,7 @@ MA_API ma_result ma_sound_set_fade_point_in_milliseconds(ma_sound* pSound, ma_ui MA_API ma_result ma_sound_set_fade_point_auto_reset(ma_sound* pSound, ma_uint32 fadePointIndex, ma_bool32 autoReset); MA_API ma_result ma_sound_set_start_delay(ma_sound* pSound, ma_uint64 delayInMilliseconds); MA_API ma_result ma_sound_set_stop_delay(ma_sound* pSound, ma_uint64 delayInMilliseconds); +MA_API ma_bool32 ma_sound_is_playing(const ma_sound* pSound); MA_API ma_bool32 ma_sound_at_end(const ma_sound* pSound); MA_API ma_result ma_sound_get_time_in_frames(const ma_sound* pSound, ma_uint64* pTimeInFrames); MA_API ma_result ma_sound_seek_to_pcm_frame(ma_sound* pSound, ma_uint64 frameIndex); /* Just a wrapper around ma_data_source_seek_to_pcm_frame(). */ @@ -5355,6 +5356,12 @@ static void ma_engine_mix_sound(ma_engine* pEngine, ma_sound_group* pGroup, ma_s the mixer to optimize the volume = 0 case, and let the effect do it's own internal optimizations in non-audible cases. */ result = ma_mixer_mix_data_source(&pGroup->mixer, pSound->pDataSource, offsetInFrames, (frameCount - offsetInFrames), &framesProcessed, pSound->volume, &pSound->effect, pSound->isLooping); + + /* If we reached the end of the sound we'll want to mark it as at the end and stop it. This should never be returned for looping sounds. */ + if (result == MA_AT_END) { + ma_sound_stop_internal(pSound); + c89atomic_exchange_32(&pSound->atEnd, MA_TRUE); /* This will be set to false in ma_sound_start(). */ + } /* For the benefit of the main effect we need to ensure the local time is updated explicitly. This is required for allowing time-based effects to @@ -5365,12 +5372,6 @@ static void ma_engine_mix_sound(ma_engine* pEngine, ma_sound_group* pGroup, ma_s ma_engine_effect_set_time(&pSound->effect, currentTimeInFrames); } - /* If we reached the end of the sound we'll want to mark it as at the end and stop it. This should never be returned for looping sounds. */ - if (result == MA_AT_END) { - ma_sound_stop_internal(pSound); - c89atomic_exchange_32(&pSound->atEnd, MA_TRUE); /* This will be set to false in ma_sound_start(). */ - } - pSound->runningTimeInEngineFrames += offsetInFrames + framesProcessed; } else { /* The sound hasn't started yet. Just keep advancing time forward, but leave the data source alone. */ @@ -6339,6 +6340,15 @@ MA_API ma_result ma_sound_set_stop_delay(ma_sound* pSound, ma_uint64 delayInMill return MA_SUCCESS; } +MA_API ma_bool32 ma_sound_is_playing(const ma_sound* pSound) +{ + if (pSound == NULL) { + return MA_FALSE; + } + + return pSound->isPlaying; +} + MA_API ma_bool32 ma_sound_at_end(const ma_sound* pSound) { if (pSound == NULL) {