From b9a9ea497c970f6dd6a0b210adca96f3f288091e Mon Sep 17 00:00:00 2001 From: h0neybadger Date: Mon, 4 Jan 2021 11:08:57 +0100 Subject: [PATCH] Fix switch audio delay with SDL_ClearQueuedAudio --- switch/src/io.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/switch/src/io.cpp b/switch/src/io.cpp index 9f6ab56..3104bd4 100644 --- a/switch/src/io.cpp +++ b/switch/src/io.cpp @@ -291,6 +291,16 @@ void IO::AudioCB(int16_t *buf, size_t samples_count) else buf[x] = (int16_t)sample; } + + int audio_queued_size = SDL_GetQueuedAudioSize(this->sdl_audio_device_id); + if(audio_queued_size > 16000) + { + // clear audio queue to avoid big audio delay + // average values are close to 13000 bytes + CHIAKI_LOGW(this->log, "Triggering SDL_ClearQueuedAudio with queue size = %d", audio_queued_size); + SDL_ClearQueuedAudio(this->sdl_audio_device_id); + } + int success = SDL_QueueAudio(this->sdl_audio_device_id, buf, sizeof(int16_t) * samples_count * 2); if(success != 0) CHIAKI_LOGE(this->log, "SDL_QueueAudio failed: %s\n", SDL_GetError());