From 8e9d64f0d6b36b9fab92272b981ffce617094934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Casta=C3=B1eda?= Date: Sat, 5 Dec 2020 19:54:53 +0100 Subject: [PATCH] FileGameSound: fix stuck in pause when ramped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I346d7b93fa8507451ee46856ad6618acd6e2d609 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3469 Reviewed-by: Jérôme Duval --- src/kits/game/FileGameSound.cpp | 119 ++++++++++++++------------------ 1 file changed, 50 insertions(+), 69 deletions(-) diff --git a/src/kits/game/FileGameSound.cpp b/src/kits/game/FileGameSound.cpp index dae021e98a..4fcae04cfa 100644 --- a/src/kits/game/FileGameSound.cpp +++ b/src/kits/game/FileGameSound.cpp @@ -188,79 +188,60 @@ BFileGameSound::FillBuffer(void* inBuffer, size_t inByteCount) char* buffer = (char*)inBuffer; size_t out_offset = 0; - while (inByteCount > 0 && !fPaused) { - if (!fPaused || fPausing) { - if (fPlayPosition == 0 || fPlayPosition >= fBufferSize) { - if (!Load()) + while (inByteCount > 0 && (!fPaused || fPausing != NULL)) { + if (fPlayPosition == 0 || fPlayPosition >= fBufferSize) { + if (!Load()) + break; + } + + size_t bytes = fBufferSize - fPlayPosition; + + if (bytes > inByteCount) + bytes = inByteCount; + + if (fPausing != NULL) { + Lock(); + + bool rampDone = false; + + switch(Format().format) { + case gs_audio_format::B_GS_U8: + rampDone = ::FillBuffer(fPausing, + (uint8*)&buffer[out_offset], + (uint8*)&fBuffer[fPlayPosition], &bytes); + break; + + case gs_audio_format::B_GS_S16: + rampDone = ::FillBuffer(fPausing, + (int16*)&buffer[out_offset], + (int16*)&fBuffer[fPlayPosition], &bytes); + break; + + case gs_audio_format::B_GS_S32: + rampDone = ::FillBuffer(fPausing, + (int32*)&buffer[out_offset], + (int32*)&fBuffer[fPlayPosition], &bytes); + break; + + case gs_audio_format::B_GS_F: + rampDone = ::FillBuffer(fPausing, + (float*)&buffer[out_offset], + (float*)&fBuffer[fPlayPosition], &bytes); break; } - if (fPausing) { - Lock(); - - bool rampDone = false; - size_t bytes = fBufferSize - fPlayPosition; - - if (bytes > inByteCount) { - bytes = inByteCount; - } - - // Fill the requested buffer, stopping if the paused flag is set - - switch(Format().format) { - case gs_audio_format::B_GS_U8: - rampDone = ::FillBuffer(fPausing, - (uint8*)&buffer[out_offset], - (uint8*)&fBuffer[fPlayPosition], &bytes); - break; - - case gs_audio_format::B_GS_S16: - rampDone = ::FillBuffer(fPausing, - (int16*)&buffer[out_offset], - (int16*)&fBuffer[fPlayPosition], &bytes); - break; - - case gs_audio_format::B_GS_S32: - rampDone = ::FillBuffer(fPausing, - (int32*)&buffer[out_offset], - (int32*)&fBuffer[fPlayPosition], &bytes); - break; - - case gs_audio_format::B_GS_F: - rampDone = ::FillBuffer(fPausing, - (float*)&buffer[out_offset], - (float*)&fBuffer[fPlayPosition], &bytes); - break; - } - - inByteCount -= bytes; - out_offset += bytes; - fPlayPosition += bytes; - - // We finished ramping - if (rampDone) { - - // Need to be able to stop asap when pause flag is flipped. - while (fPlayPosition < fBufferSize && (inByteCount > 0)) { - buffer[out_offset++] = fBuffer[fPlayPosition++]; - inByteCount--; - } - - delete fPausing; - fPausing = NULL; - } - - Unlock(); - } else { - - // Need to be able to stop asap when the pause flag is flipped. - while (fPlayPosition < fBufferSize && (!fPaused || fPausing) - && (inByteCount > 0)) { - buffer[out_offset++] = fBuffer[fPlayPosition++]; - inByteCount--; - } + if (rampDone) { + delete fPausing; + fPausing = NULL; } - } + + Unlock(); + } else + memcpy(&buffer[out_offset], &fBuffer[fPlayPosition], bytes); + + inByteCount -= bytes; + out_offset += bytes; + fPlayPosition += bytes; } // Fill the rest with silence