-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampler_app.cpp
More file actions
301 lines (265 loc) · 10 KB
/
Copy pathsampler_app.cpp
File metadata and controls
301 lines (265 loc) · 10 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "sampler_app.h"
#include <Arduino.h>
#include <esp_system.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "codec_es8388.h"
#include "storage_sd.h"
namespace {
constexpr UBaseType_t kAudioTaskPriority = 6;
constexpr UBaseType_t kLoaderTaskPriority = 4;
constexpr UBaseType_t kUiTaskPriority = 2;
// Above the UI so streams stay fed; the reader yields every 10 ms of SD work.
constexpr UBaseType_t kSdReaderTaskPriority = 3;
constexpr BaseType_t kSdReaderTaskCore = 0;
constexpr uint32_t kDiagnosticsIntervalMs = 1000;
constexpr BaseType_t kAudioTaskCore = 1;
constexpr BaseType_t kLoaderTaskCore = 0;
constexpr BaseType_t kUiTaskCore = 0;
constexpr uint16_t kTriggerQueueLength = 32;
constexpr uint16_t kLoaderCommandQueueLength = 12;
constexpr uint16_t kUiStatusQueueLength = 16;
constexpr uint16_t kAudioTaskStackWords = 6144;
constexpr uint16_t kLoaderTaskStackWords = 6144;
constexpr uint16_t kUiTaskStackWords = 8192;
// Preloading several MiB of samples at the 4 MHz SD fallback takes seconds.
constexpr uint32_t kBootRebuildTimeoutMs = 60000;
const char *startupTitleForResetReason() {
const esp_reset_reason_t reason = esp_reset_reason();
if (reason == ESP_RST_SW || reason == ESP_RST_PANIC || reason == ESP_RST_TASK_WDT) {
return "Updating firmware...";
}
return "Starting...";
}
} // namespace
void SamplerApp::setup() {
initializePlatform();
initializeHardware();
initializeRuntimeDefaults();
loadStorageAndSettings();
initializeInteractiveModules();
if (!startTasks()) {
return;
}
if (!requestLoaderRebuildAndWait(kBootRebuildTimeoutMs)) {
return;
}
if (!CodecES8388::unmute()) {
Serial.println("Audio: codec unmute failed");
return;
}
renderBootScreen(false);
prepareCallbacksAndBootFlow();
}
void SamplerApp::initializePlatform() {
Serial.begin(115200);
delay(200);
}
void SamplerApp::initializeHardware() {
CodecES8388::init();
if (display_.begin()) {
display_.renderStartupMessage(startupTitleForResetReason(), "Please wait...");
}
}
void SamplerApp::initializeRuntimeDefaults() {
runtime_.applyDefaultSettings();
display_.setAudio(&audio_);
bootScreenFlow_.begin(&display_, &input_, &ui_);
renderBootScreen(true);
}
void SamplerApp::loadStorageAndSettings() {
const bool sdReady = StorageSD::init();
if (sdReady) {
SampleLibrary::loadFromSd(catalog_, [](void *context) {
static_cast<SamplerApp *>(context)->renderBootScreen(true);
}, this);
renderBootScreen(true);
runtime_.loadSettingsFromSd();
} else {
SampleLibrary::clear(catalog_);
runtime_.applyDefaultSettings();
}
renderBootScreen(true);
}
void SamplerApp::initializeInteractiveModules() {
input_.begin();
runtime_.setCatalog(&catalog_);
audio_.setSampleCatalog(&catalog_);
ui_.begin(catalog_.names, catalog_.paths, catalog_.count, catalog_.validation);
midi_.begin(&ui_);
runtime_.applyAssignmentsToUi(ui_, catalog_);
ui_.clearUnsavedChanges();
}
void SamplerApp::prepareCallbacksAndBootFlow() {
playbackRouter_.begin(&ui_, &catalog_, &runtime_, &triggerEngine_);
saveService_.begin(
&ui_, &catalog_, &runtime_, &triggerEngine_, loaderCommandQueue_, uiStatusQueue_);
callbackBinder_.begin(&ui_, &midi_, &playbackRouter_, &saveService_, loaderCommandQueue_);
bootScreenFlow_.waitForDismissOrTimeout();
ui_.forceMainScreen();
callbackBinder_.bindUiAndMidiCallbacks();
display_.renderUi(ui_);
}
bool SamplerApp::startTasks() {
loaderCommandQueue_ = xQueueCreate(kLoaderCommandQueueLength, sizeof(LoaderCommand));
if (!loaderCommandQueue_) {
return false;
}
uiStatusQueue_ = xQueueCreate(kUiStatusQueueLength, sizeof(UiStatusEvent));
if (!uiStatusQueue_) {
return false;
}
// Start I2S while the codec is still soft-muted. The audio task calls
// begin() too, but Audio::begin() is idempotent.
if (!audio_.begin()) {
Serial.println("Audio: initialization failed; outputs remain muted");
return false;
}
// SD reads for streamed samples run on core 0, so the audio task on core 1
// never waits for the card. Without the task, playback still works but
// reads block the audio task.
if (!audio_.startStreamReader(kSdReaderTaskPriority, kSdReaderTaskCore)) {
Serial.println("Audio: SD reader task failed; streaming from the audio task");
}
if (!triggerEngine_.begin(
&audio_,
kAudioTaskPriority,
kAudioTaskCore,
kTriggerQueueLength,
kAudioTaskStackWords,
uiStatusQueue_)) {
return false;
}
const BaseType_t loaderTaskOk = xTaskCreatePinnedToCore(loaderTaskEntry,
"sample_loader",
kLoaderTaskStackWords,
this,
kLoaderTaskPriority,
&loaderTaskHandle_,
kLoaderTaskCore);
if (loaderTaskOk != pdPASS) {
return false;
}
const BaseType_t uiTaskOk = xTaskCreatePinnedToCore(uiTaskEntry,
"ui_task",
kUiTaskStackWords,
this,
kUiTaskPriority,
&uiTaskHandle_,
kUiTaskCore);
if (uiTaskOk != pdPASS) {
return false;
}
return true;
}
void SamplerApp::renderBootScreen(bool loading) {
bootScreenFlow_.render(loading,
catalog_.count,
runtime_.assignedSamplesCount(),
runtime_.ramUsagePercent(), catalog_.checkedCount, catalog_.rejectedCount);
}
void SamplerApp::loop() {
vTaskDelay(pdMS_TO_TICKS(1000));
}
void SamplerApp::logStreamingDiagnostics() {
// Runs once per second from the UI task on core 0: the audio task keeps
// core 1 busy while voices play. Report only seconds with new problems, so
// a quiet log means playback kept up.
const Audio::StreamingDiagnostics now = audio_.streamingDiagnostics();
const Audio::StreamingDiagnostics &last = loggedDiagnostics_;
const uint32_t sdBytesPerSecond = now.sdBytesRead - lastSdBytesRead_;
lastSdBytesRead_ = now.sdBytesRead;
if (now.i2sUnderrunCount == last.i2sUnderrunCount &&
now.starvedUpdateCount == last.starvedUpdateCount &&
now.sdNoFreeStreamCount == last.sdNoFreeStreamCount &&
now.sdOpenFailureCount == last.sdOpenFailureCount) {
return;
}
Serial.printf("Audio: +%lu I2S underruns, +%lu starved SD voice updates, +%lu SD triggers "
"without a free stream, +%lu SD open failures; SD %lu KiB/s at %lu Hz, "
"max read %lu us for %lu B\n",
static_cast<unsigned long>(now.i2sUnderrunCount - last.i2sUnderrunCount),
static_cast<unsigned long>(now.starvedUpdateCount - last.starvedUpdateCount),
static_cast<unsigned long>(now.sdNoFreeStreamCount - last.sdNoFreeStreamCount),
static_cast<unsigned long>(now.sdOpenFailureCount - last.sdOpenFailureCount),
static_cast<unsigned long>(sdBytesPerSecond / 1024U),
static_cast<unsigned long>(StorageSD::spiFrequencyHz()),
static_cast<unsigned long>(now.sdMaxReadUs),
static_cast<unsigned long>(now.sdMaxReadBytes));
loggedDiagnostics_ = now;
}
void SamplerApp::uiTaskEntry(void *param) {
auto *self = static_cast<SamplerApp *>(param);
self->runUiTask();
}
void SamplerApp::loaderTaskEntry(void *param) {
auto *self = static_cast<SamplerApp *>(param);
self->runLoaderTask();
}
void SamplerApp::runLoaderTask() {
if (!loaderCommandQueue_ || !uiStatusQueue_) {
vTaskDelete(nullptr);
return;
}
LoaderCommand command;
while (true) {
if (xQueueReceive(loaderCommandQueue_, &command, portMAX_DELAY) == pdTRUE) {
processLoaderCommand(command);
}
}
}
bool SamplerApp::requestLoaderRebuildAndWait(uint32_t timeoutMs) {
if (!loaderCommandQueue_ || !uiStatusQueue_) return false;
LoaderCommand command;
command.type = LoaderCommandType::RebuildPreparedSamples;
if (xQueueSend(loaderCommandQueue_, &command, pdMS_TO_TICKS(200)) != pdTRUE) {
return false;
}
const TickType_t deadline = xTaskGetTickCount() + pdMS_TO_TICKS(timeoutMs);
while (xTaskGetTickCount() < deadline) {
UiStatusEvent event;
if (xQueueReceive(uiStatusQueue_, &event, pdMS_TO_TICKS(20)) != pdTRUE) {
continue;
}
if (event.source == UiStatusSource::SampleLoader &&
event.type == UiStatusType::LoaderRebuildCompleted) {
return event.success;
}
}
return false;
}
void SamplerApp::processLoaderCommand(const LoaderCommand &command) {
if (command.type == LoaderCommandType::PreviewSample) {
if (command.sampleIndex >= 0) {
playbackRouter_.onPreviewSample(static_cast<int>(command.sampleIndex));
}
return;
}
UiStatusEvent status;
status.source = UiStatusSource::SampleLoader;
status.type = UiStatusType::LoaderRebuildCompleted;
status.success = false;
if (command.type == LoaderCommandType::RebuildPreparedSamples) {
runtime_.rebuildPreparedSamples();
status.success = true;
status.assignedSamples = static_cast<uint32_t>(runtime_.assignedSamplesCount());
status.ramSampleCount = static_cast<uint32_t>(runtime_.ramSampleCount());
status.streamSampleCount = static_cast<uint32_t>(runtime_.streamSampleCount());
status.sampleRamUsedBytes = runtime_.sampleRamUsedBytes();
}
(void)xQueueSend(uiStatusQueue_, &status, pdMS_TO_TICKS(20));
}
void SamplerApp::runUiTask() {
uint32_t lastDiagnosticsMs = millis();
while (true) {
if (millis() - lastDiagnosticsMs >= kDiagnosticsIntervalMs) {
lastDiagnosticsMs = millis();
logStreamingDiagnostics();
}
midi_.update();
callbackBinder_.pollInput(input_);
ui_.update();
display_.update();
vTaskDelay(pdMS_TO_TICKS(1));
}
}