From 4f08a0838aeea3022ea915bebb34e42ac7f2b47c Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 10 Feb 2025 16:23:13 +1100 Subject: [PATCH 1/2] Change output mode when bluetooth connects/disconnects --- src/tangara/audio/audio_fsm.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/tangara/audio/audio_fsm.cpp b/src/tangara/audio/audio_fsm.cpp index 07d04d3f..d479542a 100644 --- a/src/tangara/audio/audio_fsm.cpp +++ b/src/tangara/audio/audio_fsm.cpp @@ -288,6 +288,8 @@ void AudioState::react(const system_fsm::BluetoothEvent& ev) { if (bt.connectionState() != drivers::Bluetooth::ConnectionState::kConnected) { // If BT Disconnected, move to standby state + events::Audio().Dispatch(audio::OutputModeChanged{ + .set_to = drivers::NvsStorage::Output::kHeadphones}); transit(); return; } @@ -295,6 +297,8 @@ void AudioState::react(const system_fsm::BluetoothEvent& ev) { if (!dev) { return; } + events::Audio().Dispatch(audio::OutputModeChanged{ + .set_to = drivers::NvsStorage::Output::kBluetooth}); sBtOutput->SetVolume(sServices->nvs().BluetoothVolume(dev->mac)); events::Ui().Dispatch(VolumeChanged{ .percent = sOutput->GetVolumePct(), @@ -386,13 +390,14 @@ void AudioState::react(const OutputModeChanged& ev) { if (ev.set_to) { new_mode = *ev.set_to; } - sOutput->mode(IAudioOutput::Modes::kOff); switch (new_mode) { case drivers::NvsStorage::Output::kBluetooth: sOutput = sBtOutput; + sI2SOutput->mode(IAudioOutput::Modes::kOff); break; case drivers::NvsStorage::Output::kHeadphones: sOutput = sI2SOutput; + sBtOutput->mode(IAudioOutput::Modes::kOff); break; } sSampleProcessor->SetOutput(sOutput); @@ -429,8 +434,8 @@ auto AudioState::updateTrackData(std::string uri, }); } -auto AudioState::updateSavedPosition(std::string uri, uint32_t position) - -> void { +auto AudioState::updateSavedPosition(std::string uri, + uint32_t position) -> void { updateTrackData( uri, [=](database::TrackData& data) { data.last_position = position; }); } @@ -486,13 +491,13 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) { sI2SOutput->SetVolume(nvs.AmpCurrentVolume()); sI2SOutput->SetVolumeImbalance(nvs.AmpLeftBias()); + // Always set to headphones output initially + // Connecting bluetooth will change this + sOutput = sI2SOutput; if (sServices->nvs().OutputMode() == - drivers::NvsStorage::Output::kHeadphones) { - sOutput = sI2SOutput; - } else { + drivers::NvsStorage::Output::kBluetooth) { // Ensure Bluetooth gets enabled if it's the default sink. sServices->bluetooth().enable(true); - sOutput = sBtOutput; } sOutput->mode(IAudioOutput::Modes::kOnPaused); From 285c4928af79c1f5844277464832f060ded77f85 Mon Sep 17 00:00:00 2001 From: ailurux Date: Tue, 11 Feb 2025 12:43:01 +1100 Subject: [PATCH 2/2] Only change output mode if the new output mode is different --- src/tangara/audio/audio_fsm.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/tangara/audio/audio_fsm.cpp b/src/tangara/audio/audio_fsm.cpp index d479542a..7854818d 100644 --- a/src/tangara/audio/audio_fsm.cpp +++ b/src/tangara/audio/audio_fsm.cpp @@ -390,16 +390,20 @@ void AudioState::react(const OutputModeChanged& ev) { if (ev.set_to) { new_mode = *ev.set_to; } + std::shared_ptr new_output; switch (new_mode) { case drivers::NvsStorage::Output::kBluetooth: - sOutput = sBtOutput; - sI2SOutput->mode(IAudioOutput::Modes::kOff); + new_output = sBtOutput; break; case drivers::NvsStorage::Output::kHeadphones: - sOutput = sI2SOutput; - sBtOutput->mode(IAudioOutput::Modes::kOff); + new_output = sI2SOutput; break; } + if (new_output == sOutput) { + return; + } + sOutput->mode(IAudioOutput::Modes::kOff); + sOutput = new_output; sSampleProcessor->SetOutput(sOutput); updateOutputMode();