-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathaudio.cpp
More file actions
302 lines (227 loc) · 4.97 KB
/
Copy pathaudio.cpp
File metadata and controls
302 lines (227 loc) · 4.97 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
302
#include <Arduino.h>
#include <ESP_I2S.h>
#include <FS.h>
#include <LittleFS.h>
#include <math.h>
#include "audio/wav_stream.h"
#include "pins.h"
#include "display/oled.h"
#include "settings/settings.h"
#include "audio/audio.h"
#include "serial_log.h"
I2SClass I2S;
namespace {
bool audioStorageReady = false;
constexpr const char* kAudioPartition = "spiffs";
void logAudioStorageContents() {
File root = LittleFS.open("/");
if (!root || !root.isDirectory()) {
serialLogPrintln("LittleFS root unavailable");
return;
}
serialLogPrintln("LittleFS files:");
File entry = root.openNextFile();
while (entry) {
serialLogPrint(" ");
serialLogPrintln(entry.name());
entry.close();
entry = root.openNextFile();
}
root.close();
}
bool playWavBlocking(
WavClip clip,
const char* line2
) {
const char* line1 = wavClipOledTitle(clip);
showOledText(line1, line2);
if (!wavStreamStart(clip)) {
showOledText(line1, "Failed");
return false;
}
while (wavStreamUpdate()) {
}
showOledText(line1, "OK");
return true;
}
} // namespace
bool initAudioStorage() {
if (audioStorageReady) {
return true;
}
audioStorageReady = LittleFS.begin(
false,
"/littlefs",
10,
kAudioPartition
);
if (!audioStorageReady) {
serialLogPrintln("LittleFS mount failed (run: pio run -t uploadfs)");
return false;
}
for (uint8_t i = 0; i < kWavClipCount; i++) {
const WavClip clip = static_cast<WavClip>(i);
const char* path = wavClipPath(clip);
if (!LittleFS.exists(path)) {
serialLogPrint(path);
serialLogPrintln(" not on LittleFS");
logAudioStorageContents();
}
}
return true;
}
void playTone(
float frequency,
int durationMs
) {
constexpr int FRAMES = 512;
int16_t buffer[FRAMES * 2];
float phase = 0.0f;
const float phaseStep =
2.0f * PI * frequency / SAMPLE_RATE;
const float amplitude =
32767.0f * settingsVolume() / 100.0f;
const int totalFrames =
SAMPLE_RATE * durationMs / 1000;
int framesPlayed = 0;
while (framesPlayed < totalFrames) {
const int framesThisTime =
min(
FRAMES,
totalFrames - framesPlayed
);
for (int i = 0; i < framesThisTime; i++) {
const int16_t sample =
(int16_t)(
sin(phase) * amplitude
);
phase += phaseStep;
if (phase >= 2.0f * PI) {
phase -= 2.0f * PI;
}
buffer[i * 2] = sample;
buffer[i * 2 + 1] = sample;
}
I2S.write(
(uint8_t*)buffer,
framesThisTime *
2 *
sizeof(int16_t)
);
framesPlayed += framesThisTime;
}
}
void playSilence(int durationMs) {
constexpr int FRAMES = 512;
int16_t buffer[FRAMES * 2] = {0};
const int totalFrames =
SAMPLE_RATE * durationMs / 1000;
int framesPlayed = 0;
while (framesPlayed < totalFrames) {
const int framesThisTime =
min(
FRAMES,
totalFrames - framesPlayed
);
I2S.write(
(uint8_t*)buffer,
framesThisTime *
2 *
sizeof(int16_t)
);
framesPlayed += framesThisTime;
}
}
void stopAllWavPlayback() {
wavStreamStop();
}
void stopBellPlayback() {
wavStreamStop();
}
void stopWelcomePlayback() {
wavStreamStop();
}
void stopAttentionPlayback() {
wavStreamStop();
}
void stopErrorPlayback() {
wavStreamStop();
}
void stopAbortPlayback() {
wavStreamStop();
}
bool startBellPlayback() {
return wavStreamStart(WavClip::Bell);
}
bool startWelcomePlayback() {
return wavStreamStart(WavClip::Welcome);
}
bool startAttentionPlayback() {
return wavStreamStart(WavClip::Attention);
}
bool startErrorPlayback() {
return wavStreamStart(WavClip::Error);
}
bool startAbortPlayback() {
return wavStreamStart(WavClip::Abort);
}
bool updateBellPlayback() {
return wavStreamUpdate();
}
bool updateWelcomePlayback() {
return wavStreamUpdate();
}
bool updateAttentionPlayback() {
return wavStreamUpdate();
}
bool updateErrorPlayback() {
return wavStreamUpdate();
}
bool updateAbortPlayback() {
return wavStreamUpdate();
}
bool playBell() {
return playWavBlocking(WavClip::Bell, "Playing...");
}
bool playWelcome() {
return playWavBlocking(WavClip::Welcome, "Playing...");
}
bool playAttention() {
return playWavBlocking(WavClip::Attention, "Playing...");
}
bool playError() {
return playWavBlocking(WavClip::Error, "Playing...");
}
bool playAbort() {
return playWavBlocking(WavClip::Abort, "Playing...");
}
void runSoundTest() {
serialLogPrintln();
serialLogPrintln("==========================");
serialLogPrintln("SOUND TEST");
serialLogPrintln("==========================");
showOledText(
"SOUND TEST",
"500 Hz"
);
playTone(500, 120);
playSilence(50);
showOledText(
"SOUND TEST",
"700 Hz"
);
playTone(700, 120);
playSilence(50);
showOledText(
"SOUND TEST",
"1000 Hz"
);
playTone(1000, 220);
playSilence(100);
showOledText(
"SOUND TEST",
"OK"
);
serialLogPrintln("Sound OK");
delay(500);
}