1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "audio_fsm.hpp"
#include <stdint.h>
#include <future>
#include <memory>
#include <variant>
#include "audio_sink.hpp"
#include "esp_log.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "audio_converter.hpp"
#include "audio_decoder.hpp"
#include "audio_events.hpp"
#include "bluetooth.hpp"
#include "bt_audio_output.hpp"
#include "event_queue.hpp"
#include "fatfs_audio_input.hpp"
#include "future_fetcher.hpp"
#include "i2s_audio_output.hpp"
#include "i2s_dac.hpp"
#include "idf_additions.h"
#include "nvs.hpp"
#include "sample.hpp"
#include "service_locator.hpp"
#include "system_events.hpp"
#include "track.hpp"
#include "track_queue.hpp"
#include "wm8523.hpp"
namespace audio {
[[maybe_unused]] static const char kTag[] = "audio_fsm";
std::shared_ptr<system_fsm::ServiceLocator> AudioState::sServices;
std::shared_ptr<FatfsAudioInput> AudioState::sFileSource;
std::unique_ptr<Decoder> AudioState::sDecoder;
std::shared_ptr<SampleConverter> AudioState::sSampleConverter;
std::shared_ptr<I2SAudioOutput> AudioState::sI2SOutput;
std::shared_ptr<BluetoothAudioOutput> AudioState::sBtOutput;
std::shared_ptr<IAudioOutput> AudioState::sOutput;
std::optional<database::TrackId> AudioState::sCurrentTrack;
void AudioState::react(const system_fsm::KeyLockChanged& ev) {
if (ev.locking && sServices) {
sServices->nvs().AmpCurrentVolume(sOutput->GetVolume());
}
}
void AudioState::react(const StepUpVolume& ev) {
if (sOutput->AdjustVolumeUp()) {
events::Ui().Dispatch(VolumeChanged{
.percent = sOutput->GetVolumePct(),
.db = sOutput->GetVolumeDb(),
});
}
}
void AudioState::react(const StepDownVolume& ev) {
if (sOutput->AdjustVolumeDown()) {
events::Ui().Dispatch(VolumeChanged{
.percent = sOutput->GetVolumePct(),
.db = sOutput->GetVolumeDb(),
});
}
}
void AudioState::react(const system_fsm::HasPhonesChanged& ev) {
if (ev.has_headphones) {
ESP_LOGI(kTag, "headphones in!");
} else {
ESP_LOGI(kTag, "headphones out!");
}
}
void AudioState::react(const ChangeMaxVolume& ev) {
ESP_LOGI(kTag, "new max volume %i db",
(ev.new_max - drivers::wm8523::kLineLevelReferenceVolume) / 4);
sI2SOutput->SetMaxVolume(ev.new_max);
sServices->nvs().AmpMaxVolume(ev.new_max);
}
void AudioState::react(const OutputModeChanged& ev) {
// TODO: handle SetInUse
ESP_LOGI(kTag, "output mode changed");
auto new_mode = sServices->nvs().OutputMode();
sOutput->SetMode(IAudioOutput::Modes::kOff);
switch (new_mode) {
case drivers::NvsStorage::Output::kBluetooth:
sOutput = sBtOutput;
break;
case drivers::NvsStorage::Output::kHeadphones:
sOutput = sI2SOutput;
break;
}
sOutput->SetMode(IAudioOutput::Modes::kOnPaused);
}
auto AudioState::playTrack(database::TrackId id) -> void {
sCurrentTrack = id;
sServices->bg_worker().Dispatch<void>([=]() {
auto db = sServices->database().lock();
if (!db) {
return;
}
sFileSource->SetPath(db->getTrackPath(id));
});
}
namespace states {
void Uninitialised::react(const system_fsm::BootComplete& ev) {
sServices = ev.services;
constexpr size_t kDrainBufferSize =
drivers::kI2SBufferLengthFrames * sizeof(sample::Sample) * 2 * 8;
ESP_LOGI(kTag, "allocating drain buffer, size %u KiB",
kDrainBufferSize / 1024);
StreamBufferHandle_t stream = xStreamBufferCreateWithCaps(
kDrainBufferSize, sizeof(sample::Sample) * 2, MALLOC_CAP_DMA);
sFileSource.reset(
new FatfsAudioInput(sServices->tag_parser(), sServices->bg_worker()));
sI2SOutput.reset(new I2SAudioOutput(stream, sServices->gpios()));
sBtOutput.reset(new BluetoothAudioOutput(stream, sServices->bluetooth()));
auto& nvs = sServices->nvs();
sI2SOutput->SetMaxVolume(nvs.AmpMaxVolume());
sI2SOutput->SetVolume(nvs.AmpCurrentVolume());
if (sServices->nvs().OutputMode() ==
drivers::NvsStorage::Output::kHeadphones) {
sOutput = sI2SOutput;
} else {
sOutput = sBtOutput;
}
sOutput->SetMode(IAudioOutput::Modes::kOnPaused);
sSampleConverter.reset(new SampleConverter());
sSampleConverter->SetOutput(sOutput);
Decoder::Start(sFileSource, sSampleConverter);
transit<Standby>();
}
void Standby::react(const PlayFile& ev) {
sFileSource->SetPath(ev.filename);
}
void Playback::react(const PlayFile& ev) {
sFileSource->SetPath(ev.filename);
}
void Standby::react(const internal::InputFileOpened& ev) {
transit<Playback>();
}
void Standby::react(const QueueUpdate& ev) {
auto current_track = sServices->track_queue().current();
if (!current_track || (sCurrentTrack && *sCurrentTrack == *current_track)) {
return;
}
playTrack(*current_track);
}
void Standby::react(const TogglePlayPause& ev) {
if (sCurrentTrack) {
transit<Playback>();
}
}
void Playback::entry() {
ESP_LOGI(kTag, "beginning playback");
sOutput->SetMode(IAudioOutput::Modes::kOnPlaying);
events::System().Dispatch(PlaybackStarted{});
events::Ui().Dispatch(PlaybackStarted{});
}
void Playback::exit() {
ESP_LOGI(kTag, "finishing playback");
// TODO(jacqueline): Second case where it's useful to wait for the i2s buffer
// to drain.
vTaskDelay(pdMS_TO_TICKS(10));
sOutput->SetMode(IAudioOutput::Modes::kOnPaused);
// Stash the current volume now, in case it changed during playback, since we
// might be powering off soon.
sServices->nvs().AmpCurrentVolume(sOutput->GetVolume());
events::System().Dispatch(PlaybackFinished{});
events::Ui().Dispatch(PlaybackFinished{});
}
void Playback::react(const system_fsm::HasPhonesChanged& ev) {
if (!ev.has_headphones) {
transit<Standby>();
}
}
void Playback::react(const QueueUpdate& ev) {
if (!ev.current_changed) {
return;
}
auto current_track = sServices->track_queue().current();
if (!current_track) {
sFileSource->SetPath();
sCurrentTrack.reset();
transit<Standby>();
return;
}
playTrack(*current_track);
}
void Playback::react(const TogglePlayPause& ev) {
transit<Standby>();
}
void Playback::react(const PlaybackUpdate& ev) {
ESP_LOGI(kTag, "elapsed: %lu, total: %lu", ev.seconds_elapsed,
ev.track->duration);
}
void Playback::react(const internal::InputFileOpened& ev) {}
void Playback::react(const internal::InputFileClosed& ev) {}
void Playback::react(const internal::InputFileFinished& ev) {
ESP_LOGI(kTag, "finished playing file");
sServices->track_queue().next();
if (!sServices->track_queue().current()) {
transit<Standby>();
}
}
void Playback::react(const internal::AudioPipelineIdle& ev) {
transit<Standby>();
}
} // namespace states
} // namespace audio
FSM_INITIAL_STATE(audio::AudioState, audio::states::Uninitialised)
|