From 90e82e9740cc1f824fdcdae40c9cbe9b17c2206b Mon Sep 17 00:00:00 2001 From: anonymous Date: Sun, 21 Apr 2024 17:22:36 +0300 Subject: [PATCH] faudio: Fix XNA_GetSongEnded false positives Now that we release buffer lock before executing callbacks, some code from another thread can get scheduled between mutex unlock and callback call and observe empty buffer queue right before callback puts a new buffer there. XNA_Song determines the end of the song by simply checking if the queue is empty, which may lead to random playback breaks. Not sure how to properly check for the song end but the buffer decoding function also zeroes out total samples counter when it finishes a EOS-flagged buffer so checking if that counter is zero should cut it... probably? --- src/FAudio_platform_win32.c | 2 +- src/XNA_Song.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FAudio_platform_win32.c b/src/FAudio_platform_win32.c index cd07e5d8c..4f7be92b1 100644 --- a/src/FAudio_platform_win32.c +++ b/src/FAudio_platform_win32.c @@ -1055,7 +1055,7 @@ FAUDIOAPI uint32_t XNA_GetSongEnded() return 1; } FAudioSourceVoice_GetState(songVoice, &state, 0); - return state.BuffersQueued == 0; + return state.BuffersQueued == 0 && state.SamplesPlayed == 0; } FAUDIOAPI void XNA_EnableVisualization(uint32_t enable) diff --git a/src/XNA_Song.c b/src/XNA_Song.c index 982ff904a..a8755833c 100644 --- a/src/XNA_Song.c +++ b/src/XNA_Song.c @@ -325,7 +325,7 @@ FAUDIOAPI uint32_t XNA_GetSongEnded() return 1; } FAudioSourceVoice_GetState(songVoice, &state, 0); - return state.BuffersQueued == 0; + return state.BuffersQueued == 0 && state.SamplesPlayed == 0; } FAUDIOAPI void XNA_EnableVisualization(uint32_t enable)