From b34b25381436343516d02951e1c463602af68bf5 Mon Sep 17 00:00:00 2001 From: Chris Spencer <spencercw@gmail.com> Date: Mon, 24 Jan 2022 21:12:02 +0000 Subject: [PATCH] [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. --- client/src/audio.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/client/src/audio.c b/client/src/audio.c index fb250196..c630367d 100644 --- a/client/src/audio.c +++ b/client/src/audio.c @@ -176,20 +176,10 @@ void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format, LG_LOCK(audio.playback.lock); - static int lastChannels = 0; - static int lastSampleRate = 0; - if (audio.playback.state != STREAM_STATE_STOP) { - if (channels == lastChannels && sampleRate == lastSampleRate) - { - // 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; - } - + // Stop the current playback immediately. Even if the format is compatible, + // we may not have enough data left in the buffers to avoid underrunning playbackStopNL(); } @@ -197,9 +187,6 @@ void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format, audio.playback.buffer = ringbuffer_new(bufferFrames, channels * sizeof(uint16_t)); - lastChannels = channels; - lastSampleRate = sampleRate; - audio.playback.sampleRate = sampleRate; audio.playback.stride = channels * sizeof(uint16_t); 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; -no_change: LG_UNLOCK(audio.playback.lock); }