From deb0e53a15b61e2a12af8c4118f106064a7dd610 Mon Sep 17 00:00:00 2001 From: David Reid Date: Thu, 11 Aug 2022 17:05:07 +1000 Subject: [PATCH] Try fixing a bug where it's possible for old audio data to be output. Public issue https://github.com/mackron/miniaudio/issues/506 --- miniaudio.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/miniaudio.h b/miniaudio.h index 54cb3dde..a128ca94 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -17880,6 +17880,11 @@ static void ma_device__on_data(ma_device* pDevice, void* pFramesOut, const void* { MA_ASSERT(pDevice != NULL); + /* Don't read more data from the client if we're in the process of stopping. */ + if (ma_device_get_state(pDevice) == ma_device_state_stopping) { + return; + } + if (pDevice->noFixedSizedCallback) { /* Fast path. Not using a fixed sized callback. Process directly from the specified buffers. */ ma_device__on_data_inner(pDevice, pFramesOut, pFramesIn, frameCount); @@ -40670,6 +40675,15 @@ MA_API ma_result ma_device_stop(ma_device* pDevice) ma_event_wait(&pDevice->stopEvent); result = MA_SUCCESS; } + + /* + This is a safety measure to ensure the internal buffer has been cleared so any leftover + does not get played the next time the device starts. Ideally this should be drained by + the backend first. + */ + pDevice->playback.intermediaryBufferLen = 0; + pDevice->playback.inputCacheConsumed = 0; + pDevice->playback.inputCacheRemaining = 0; } ma_mutex_unlock(&pDevice->startStopLock);