From 3f8eeba1a36ef43163debfc19ad9965a8ead3596 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Sun, 15 May 2022 10:50:18 +0200 Subject: [PATCH] Sound mixer: Avoid buffer starvation if attempting to play empty source --- src/SoundMixer/cmixer.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/SoundMixer/cmixer.cpp b/src/SoundMixer/cmixer.cpp index c6166f8..58b9cdf 100644 --- a/src/SoundMixer/cmixer.cpp +++ b/src/SoundMixer/cmixer.cpp @@ -439,6 +439,13 @@ void Source::SetInterpolation(bool newInterpolation) void Source::Play() { + if (length == 0) + { + // Don't attempt to play an empty source as this would result + // in instant starvation when filling mixer buffer + return; + } + gMixer.Lock(); state = CM_STATE_PLAYING; if (!active) @@ -528,16 +535,18 @@ void WavStream::RewindImplementation() idx = 0; } -void WavStream::FillBuffer(int16_t* dst, int len) +void WavStream::FillBuffer(int16_t* dst, int fillLength) { int x, n; - len /= 2; + fillLength /= 2; - while (len > 0) + while (fillLength > 0) { - n = MIN(len, length - idx); - len -= n; + n = MIN(fillLength, length - idx); + + fillLength -= n; + if (bigEndian && bitdepth == 16 && channels == 1) { WAV_PROCESS_LOOP({ @@ -581,7 +590,7 @@ void WavStream::FillBuffer(int16_t* dst, int len) }); } // Loop back and continue filling buffer if we didn't fill the buffer - if (len > 0) + if (fillLength > 0) { idx = 0; }