From c51709f99ff5456a5863ca39ff893f823a3642d4 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 12 Sep 2024 10:44:26 +1000 Subject: Pause and unpause the current audio output in response to TTS --- src/tangara/audio/audio_fsm.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/tangara/audio/audio_fsm.cpp') diff --git a/src/tangara/audio/audio_fsm.cpp b/src/tangara/audio/audio_fsm.cpp index dac04f75..1daf568e 100644 --- a/src/tangara/audio/audio_fsm.cpp +++ b/src/tangara/audio/audio_fsm.cpp @@ -76,6 +76,7 @@ std::optional AudioState::sDrainFormat; StreamCues AudioState::sStreamCues; bool AudioState::sIsPaused = true; +bool AudioState::sIsTtsPlaying = false; auto AudioState::emitPlaybackUpdate(bool paused) -> void { std::optional position; @@ -191,6 +192,11 @@ void AudioState::react(const TogglePlayPause& ev) { } } +void AudioState::react(const TtsPlaybackChanged& ev) { + sIsTtsPlaying = ev.is_playing; + updateOutputMode(); +} + void AudioState::react(const internal::DecodingFinished& ev) { // If we just finished playing whatever's at the front of the queue, then we // need to advanve and start playing the next one ASAP in order to continue @@ -369,8 +375,8 @@ void AudioState::react(const OutputModeChanged& ev) { sOutput = sI2SOutput; break; } - sOutput->mode(IAudioOutput::Modes::kOnPaused); sSampleProcessor->SetOutput(sOutput); + updateOutputMode(); // Bluetooth volume isn't 'changed' until we've connected to a device. if (new_mode == drivers::NvsStorage::Output::kHeadphones) { @@ -381,6 +387,14 @@ void AudioState::react(const OutputModeChanged& ev) { } } +auto AudioState::updateOutputMode() -> void { + if (is_in_state() || sIsTtsPlaying) { + sOutput->mode(IAudioOutput::Modes::kOnPlaying); + } else { + sOutput->mode(IAudioOutput::Modes::kOnPaused); + } +} + auto AudioState::commitVolume() -> void { auto mode = sServices->nvs().OutputMode(); auto vol = sOutput->GetVolume(); @@ -402,6 +416,7 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) { sDrainBuffers = std::make_unique( kTrackDrainLatencySamples, kSystemDrainLatencySamples); + sDrainBuffers->first.suspend(true); sStreamFactory.reset( new FatfsStreamFactory(sServices->database(), sServices->tag_parser())); @@ -454,6 +469,10 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) { static const char kQueueKey[] = "audio:queue"; static const char kCurrentFileKey[] = "audio:current"; +auto Standby::entry() -> void { + updateOutputMode(); +} + void Standby::react(const system_fsm::KeyLockChanged& ev) { if (!ev.locking) { return; @@ -539,7 +558,8 @@ static void heartbeat(TimerHandle_t) { void Playback::entry() { ESP_LOGI(kTag, "audio output resumed"); - sOutput->mode(IAudioOutput::Modes::kOnPlaying); + sDrainBuffers->first.suspend(false); + updateOutputMode(); emitPlaybackUpdate(false); if (!sHeartbeatTimer) { @@ -552,7 +572,7 @@ void Playback::entry() { void Playback::exit() { ESP_LOGI(kTag, "audio output paused"); xTimerStop(sHeartbeatTimer, portMAX_DELAY); - sOutput->mode(IAudioOutput::Modes::kOnPaused); + sDrainBuffers->first.suspend(true); emitPlaybackUpdate(true); } -- cgit v1.2.3