[client] audio: stop playback immediately if new playback is started

If a new playback is started while the previous playback is still flushing,
we simply allow the stream to continue playing and effectively cancel the
flush. In general this is not safe because there may not be enough data in
the buffer to avoid underrunning. We could handle this better later by
trying to insert the right number of silent samples into the buffer, but
for now just completely stop the previous stream before starting the new
one.
This commit is contained in:
Chris Spencer 2022-01-24 21:12:02 +00:00 committed by Geoffrey McRae
parent 68b42e1c1a
commit b34b253814

View File

@ -176,20 +176,10 @@ void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format,
LG_LOCK(audio.playback.lock); LG_LOCK(audio.playback.lock);
static int lastChannels = 0;
static int lastSampleRate = 0;
if (audio.playback.state != STREAM_STATE_STOP) if (audio.playback.state != STREAM_STATE_STOP)
{ {
if (channels == lastChannels && sampleRate == lastSampleRate) // Stop the current playback immediately. Even if the format is compatible,
{ // we may not have enough data left in the buffers to avoid underrunning
// if the stream was still draining and the format matches, return the
// stream to the run state
if (audio.playback.state == STREAM_STATE_DRAIN)
audio.playback.state = STREAM_STATE_RUN;
goto no_change;
}
playbackStopNL(); playbackStopNL();
} }
@ -197,9 +187,6 @@ void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format,
audio.playback.buffer = ringbuffer_new(bufferFrames, audio.playback.buffer = ringbuffer_new(bufferFrames,
channels * sizeof(uint16_t)); channels * sizeof(uint16_t));
lastChannels = channels;
lastSampleRate = sampleRate;
audio.playback.sampleRate = sampleRate; audio.playback.sampleRate = sampleRate;
audio.playback.stride = channels * sizeof(uint16_t); audio.playback.stride = channels * sizeof(uint16_t);
audio.playback.state = STREAM_STATE_SETUP; audio.playback.state = STREAM_STATE_SETUP;
@ -223,7 +210,6 @@ void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format,
audio.playback.state = STREAM_STATE_SETUP; audio.playback.state = STREAM_STATE_SETUP;
no_change:
LG_UNLOCK(audio.playback.lock); LG_UNLOCK(audio.playback.lock);
} }