summaryrefslogtreecommitdiff
path: root/src/tangara/audio/audio_fsm.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-09-12 10:44:26 +1000
committerjacqueline <me@jacqueline.id.au>2024-09-12 10:44:26 +1000
commitc51709f99ff5456a5863ca39ff893f823a3642d4 (patch)
tree4b2262b6451834dfb0e197fcc7c64fd3ea0f0569 /src/tangara/audio/audio_fsm.cpp
parent542ebc65317ac4744a4b96c3131dace5bda10314 (diff)
downloadtangara-fw-c51709f99ff5456a5863ca39ff893f823a3642d4.tar.gz
Pause and unpause the current audio output in response to TTS
Diffstat (limited to 'src/tangara/audio/audio_fsm.cpp')
-rw-r--r--src/tangara/audio/audio_fsm.cpp26
1 files changed, 23 insertions, 3 deletions
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<IAudioOutput::Format> AudioState::sDrainFormat;
StreamCues AudioState::sStreamCues;
bool AudioState::sIsPaused = true;
+bool AudioState::sIsTtsPlaying = false;
auto AudioState::emitPlaybackUpdate(bool paused) -> void {
std::optional<uint32_t> 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<states::Playback>() || 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<drivers::OutputBuffers>(
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);
}