Sound mixer: Avoid buffer starvation if attempting to play empty source

This commit is contained in:
Iliyas Jorio 2022-05-15 10:50:18 +02:00
parent 801d7e8d1e
commit 3f8eeba1a3
1 changed files with 15 additions and 6 deletions

View File

@ -439,6 +439,13 @@ void Source::SetInterpolation(bool newInterpolation)
void Source::Play() 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(); gMixer.Lock();
state = CM_STATE_PLAYING; state = CM_STATE_PLAYING;
if (!active) if (!active)
@ -528,16 +535,18 @@ void WavStream::RewindImplementation()
idx = 0; idx = 0;
} }
void WavStream::FillBuffer(int16_t* dst, int len) void WavStream::FillBuffer(int16_t* dst, int fillLength)
{ {
int x, n; int x, n;
len /= 2; fillLength /= 2;
while (len > 0) while (fillLength > 0)
{ {
n = MIN(len, length - idx); n = MIN(fillLength, length - idx);
len -= n;
fillLength -= n;
if (bigEndian && bitdepth == 16 && channels == 1) if (bigEndian && bitdepth == 16 && channels == 1)
{ {
WAV_PROCESS_LOOP({ 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 // Loop back and continue filling buffer if we didn't fill the buffer
if (len > 0) if (fillLength > 0)
{ {
idx = 0; idx = 0;
} }