From 7fc90b86a846ab6fc0efae85ecd0075d869ea004 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 19 Nov 2025 13:58:23 +0200 Subject: [PATCH] Make audio source switching instant with async initialization When switching audio output source between HDMI and USB, the HDMI audio device (hw:0,0) can take 18-31 seconds to initialize due to hardware characteristics of the TC358743 chip. This caused the UI to freeze during source changes. Changes: - Move startAudio() to background goroutine in SetAudioOutputSource - Move SaveConfig() to background goroutine to avoid blocking on disk I/O - Return immediately after updating in-memory config - Audio will initialize in background while UI remains responsive The in-memory config is updated synchronously so subsequent calls see the new source immediately. Both async operations are protected by their respective mutexes (audioMutex, configLock). --- audio.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/audio.go b/audio.go index 6bc1c4fa..88e35af8 100644 --- a/audio.go +++ b/audio.go @@ -278,14 +278,22 @@ func SetAudioOutputSource(source string) error { return nil } - stopOutputAudio() config.AudioOutputSource = source - if err := startAudio(); err != nil { - audioLogger.Error().Err(err).Str("source", source).Msg("Failed to start audio output after source change") - } + go func() { + stopOutputAudio() + if err := startAudio(); err != nil { + audioLogger.Error().Err(err).Str("source", source).Msg("Failed to start audio output after source change") + } + }() - return SaveConfig() + go func() { + if err := SaveConfig(); err != nil { + audioLogger.Error().Err(err).Msg("Failed to save config after audio source change") + } + }() + + return nil } func RestartAudioOutput() error {