1// Sound lab S4 Recorder. uv run pio run -d labs/sound-lab-s4 -e sound-lab-s4 -t upload2// A voice-activated recorder. Armed, it waits for sound louder than the trigger, then records,3// starting half a second *before* the trigger (the pre-roll: the sampler's ring still has it),4// until 1.2 s of quiet. Clips go to PSRAM (12 clips, 6 MB, gone on reset). Listen to them on your5// phone at http://bench.local (the Wi-Fi W1 saved, plus the admin token), or pull a WAV file over6// USB with tools/sound_pull.py.7//8// And with the MAX98357A + speaker wired, `play <n>` plays a clip on the board itself, at any speed9// (0.5 = slower and deeper, 2 = the chipmunk).10//11// Which microphone matters here more than anywhere. The KY-037 has no amplifier, so a voice fills12// a small part of the ADC's range: clips are made louder ("normalised") on the way out and sound13// thin and hissy: a loudness sensor first, a microphone second. The INMP441 has its own amplifier14// and a 24-bit converter inside: its clips are actually pleasant to listen to. `mic auto` (the15// default) picks the INMP441 whenever it answers.16//17// Echo guard: while the speaker plays (and 200 ms after), the shell gives the lab no frames, so the18// voice trigger can't record the board's own playback. `play` refuses while recording, and `rec`19// stops a playback first.20//21// Wiring: a microphone (KY-037: + -> 3V3, G -> GND, AO -> GPIO 6, DO -> GPIO 21; or INMP441: VDD ->22// 3V3, GND, L/R -> GND, SCK -> 40, WS -> 41, SD -> 42), the screen from lesson 03, optionally the23// MAX98357A (VIN -> 5V, GND, BCLK -> 39, LRC -> 38, DIN -> 47). Push button on GPIO 5 optional.24//25// Views: recorder · clips · web (click = next, double-click = the action in the footer,26// long press = rec / stop; on the clips view: double-click = play, long press = next clip)27// Serial: help lists the commands. Shared code: kit/lib/sound-kit, kit/lib/wifi-kit (Wi-Fi, token).28#include "api_token.h"29#include "connection.h"30#include "recorder.h"31#include "recorder_api.h"32#include "s4_views.h"33#include "sound_lab.h"34 35namespace {36 37rec::Recorder recorder;38ApiToken token;39Connection connection;40RecorderApi api(recorder.store(), token);41int picked = 0; // the clips view's picked row42uint32_t lastStatus = 0;43 44// Playback on the board's speaker. The clip is leased from the store while it plays, so `del`,45// `clear` or a full store can take it off the list but can't free its samples under the speaker.46// The speaker task reads the samples on its own core, up to a block (16 ms) after it was told to47// stop, so a stopped clip is "retiring" for 100 ms before its lease is given back.48struct Playback {49 rec::Clip *clip = nullptr;50 uint32_t number = 0;51 float speed = 1;52 uint32_t sinceMs = 0;53} playback;54rec::Clip *retiring = nullptr;55uint32_t retireAt = 0;56 57void endPlayback() {58 rec::ClipStore::release(retiring); // an older one still retiring: long done by now59 retiring = playback.clip;60 retireAt = millis();61 playback = Playback{};62}63 64// ---- Views ------------------------------------------------------------------------------------65 66void drawRecorder(SoundLab &, FrameBuffer &fb, float t) { s4v::recorder(fb, recorder, recorder.config(), recorder.store(), t); }67void drawClips(SoundLab &lab, FrameBuffer &fb, float) {68 s4v::clips(fb, recorder.store(), picked, playback.number, playback.clip ? lab.speaker().progress() : 0);69}70void drawWeb(SoundLab &, FrameBuffer &fb, float) { s4v::web(fb, connection, api.started(), api.clients()); }71 72const SoundView kViews[] = {73 {"recorder", drawRecorder, "arm"},74 {"clips", drawClips, "play"},75 {"web", drawWeb, "wifi"},76};77 78// ---- Commands ---------------------------------------------------------------------------------79 80void cmdArm(SoundLab &lab, const String &, const String &lower) {81 if (!lower.isEmpty() && lower != "on" && lower != "off") return lab.warn("arm | arm on | arm off");82 recorder.arm(lab, lower.isEmpty() ? !recorder.armed() : lower == "on");83}84 85void stopPlayback(SoundLab &lab) {86 if (!playback.clip) return;87 lab.speaker().stop();88 lab.note(sv::Note::Info, "playback of clip %lu stopped", static_cast<unsigned long>(playback.number));89 endPlayback();90}91 92void cmdRec(SoundLab &lab, const String &, const String &) {93 if (recorder.recording()) return recorder.stop(lab); // rec again (or a long press) stops94 stopPlayback(lab); // never record the board's own speaker95 recorder.start(lab);96}97 98void cmdStop(SoundLab &lab, const String &, const String &) {99 if (playback.clip && !recorder.recording()) return stopPlayback(lab);100 recorder.stop(lab);101}102 103// Play clip n (0 = the picked one) at `speed` on the board's speaker.104void startPlayback(SoundLab &lab, uint32_t n, float speed) {105 if (recorder.recording()) return lab.warn("recording: stop first (the microphone would hear the speaker)");106 if (n == 0) {107 rec::Clip c;108 if (!recorder.store().facts(picked, c)) return lab.warn("no clips yet");109 n = c.number;110 }111 rec::Clip *c = recorder.store().lease(n);112 if (!c) return lab.warn("no finished clip %lu", static_cast<unsigned long>(n));113 if (playback.clip) {114 lab.speaker().stop();115 endPlayback();116 }117 // Normalised like the download, but at most x16: a whisper on the KY-037 would otherwise come118 // back as mostly hiss, at full volume.119 const float gain = std::min(16.0f, snd::gainFor(c->peak));120 playback = Playback{c, n, speed, millis()};121 lab.speaker().play(c->data, c->samples, speed * float(c->rate) / float(snd::Speaker::kRate), gain);122 lab.note(sv::Note::Good, "playing clip %lu (%.1f s) at x%.2f", static_cast<unsigned long>(n), c->durationMs() / 1000.0f / speed, speed);123}124 125void cmdPlay(SoundLab &lab, const String &arg, const String &) {126 const int sp = arg.indexOf(' ');127 long n = 0;128 float speed = 1.0f;129 if (!arg.isEmpty() && !SoundLab::number(sp < 0 ? arg : arg.substring(0, sp), 1, 1000000, n))130 return lab.warn("play [n] [speed 0.5-2.0]: clip n (default: the picked one) on the board's speaker");131 if (sp > 0 && !SoundLab::decimal(arg.substring(sp + 1), 0.5f, 2.0f, speed)) return lab.warn("speed: 0.5 (slow, deep) to 2.0 (fast, chipmunk)");132 startPlayback(lab, uint32_t(n), speed);133}134 135void cmdTrigger(SoundLab &lab, const String &arg, const String &) {136 float db = 0;137 if (!SoundLab::decimal(arg, 3, 60, db)) return lab.warn("trigger <dB above the quiet>, 3-60 (now %.0f)", recorder.config().startDb);138 recorder.config().startDb = db;139 lab.note(sv::Note::Info, "voice starts above %.0f dB", db);140}141 142void cmdHold(SoundLab &lab, const String &arg, const String &) {143 long ms = 0;144 if (!SoundLab::number(arg, 200, 10000, ms)) return lab.warn("hold <ms>, 200-10000: quiet this long ends a clip (now %lu)", static_cast<unsigned long>(recorder.config().holdMs));145 recorder.config().holdMs = uint32_t(ms);146 lab.note(sv::Note::Info, "a clip ends after %.1f s of quiet", ms / 1000.0f);147}148 149void cmdMax(SoundLab &lab, const String &arg, const String &) {150 long s = 0;151 if (!SoundLab::number(arg, 1, 120, s)) return lab.warn("max <s>, 1-120: the longest clip (now %lu)", static_cast<unsigned long>(recorder.config().maxMs / 1000));152 recorder.config().maxMs = uint32_t(s) * 1000;153 lab.note(sv::Note::Info, "clips stop at %ld s (from the next one)", s);154}155 156void cmdClips(SoundLab &lab, const String &, const String &) {157 rec::ClipStore &store = recorder.store();158 Serial.printf("%d clips, %.2f of %.0f MB of PSRAM (gone on reset):\n", store.count(), store.used() / 1048576.0, store.budget / 1048576.0);159 Serial.println(" # length kind loudest sent x started");160 store.each([](const rec::Clip &c) {161 char when[24] = "-";162 if (c.wall > 0) {163 const time_t w = time_t(c.wall);164 struct tm tm;165 localtime_r(&w, &tm);166 strftime(when, sizeof(when), "%Y-%m-%d %H:%M:%S", &tm);167 }168 Serial.printf(" %3lu %5.1fs %-5s %5.1f dBFS x%-5.1f %s%s\n", static_cast<unsigned long>(c.number), c.durationMs() / 1000.0f,169 c.manual ? "hand" : "voice", snd::toDb(float(c.peak)), snd::gainFor(c.peak), when, c.open ? " (recording)" : "");170 });171 lab.showView("clips");172}173 174void cmdNext(SoundLab &lab, const String &, const String &) {175 const int n = recorder.store().count();176 picked = n ? (picked + 1) % n : 0;177 lab.showView("clips");178}179 180void cmdDel(SoundLab &lab, const String &arg, const String &) {181 long n = 0;182 if (!SoundLab::number(arg, 1, 1000000, n)) return lab.warn("del <n>: the clip's number (clips lists them)");183 if (recorder.recording() && uint32_t(n) == recorder.clipNumber()) return lab.warn("clip %ld is recording: stop first", n);184 if (!recorder.store().remove(uint32_t(n))) return lab.warn("no clip %ld", n);185 lab.note(sv::Note::Info, "clip %ld deleted", n);186}187 188void cmdClear(SoundLab &lab, const String &, const String &) {189 const int n = recorder.store().clear();190 picked = 0;191 lab.note(sv::Note::Info, "%d clip%s deleted", n, n == 1 ? "" : "s");192}193 194// The serial transfer: a header line, the WAV file as base64 lines, a CRC-32 line. Nothing else may195// be printed in between, so this runs to the end before loop() goes on (~20 s for 5 s of sound196// at 115200 baud: 32 KB of WAV a second of sound, 4/3 more as base64, 11.5 KB/s down the wire).197void cmdSend(SoundLab &lab, const String &arg, const String &) {198 long n = 0;199 if (!SoundLab::number(arg, 1, 1000000, n)) return lab.warn("send <n>: for tools/sound_pull.py (close the serial monitor first)");200 if (recorder.recording()) return lab.warn("recording: stop first, the transfer pauses everything else");201 rec::Clip *c = recorder.store().lease(uint32_t(n));202 if (!c) return lab.warn("no finished clip %ld", n);203 const float gain = snd::gainFor(c->peak);204 char name[48];205 rec::clipName(*c, name, sizeof(name));206 const size_t total = c->wavBytes();207 Serial.printf("WAV-BEGIN %lu %u %s\n", static_cast<unsigned long>(c->number), unsigned(total), name);208 uint8_t bytes[57]; // 57 bytes = one 76-character base64 line209 char line[80];210 uint32_t crc = 0;211 for (size_t at = 0; at < total;) {212 const size_t got = rec::wavRead(*c, gain, bytes, sizeof(bytes), at);213 crc = rec::crc32(crc, bytes, got);214 rec::base64(bytes, got, line);215 Serial.println(line);216 at += got;217 }218 Serial.printf("WAV-END %08lx\n", static_cast<unsigned long>(crc));219 rec::ClipStore::release(c);220 lab.note(sv::Note::Good, "sent clip %ld (%u bytes)", n, unsigned(total));221}222 223void cmdToken(SoundLab &lab, const String &, const String &lower) {224 if (lower == "new") {225 token.regenerate();226 lab.note(sv::Note::Warn, "new admin token: open pages must unlock again");227 } else if (!lower.isEmpty()) {228 return lab.warn("token | token new");229 }230 Serial.printf("Admin token: %s\nPaste it into the page at http://%s.local (it is shared with R7/R8; keep it out of chats and git)\n",231 token.value().c_str(), Connection::kHostname);232}233 234void cmdWifi(SoundLab &lab, const String &, const String &) {235 if (connection.ssid().isEmpty()) return lab.warn("no Wi-Fi saved: flash wifi-lab-w1 once and join, or use tools/sound_pull.py over USB");236 if (connection.state() != Connection::State::Connected)237 return lab.note(sv::Note::Warn, "joining '%s'... (last reason: %s)", connection.ssid().c_str(), Connection::reasonHint(connection.lastReason()));238 Serial.printf("Wi-Fi '%s' · IP %s · %ld dBm · page http://%s.local · %u page(s) open\n", connection.ssid().c_str(),239 WiFi.localIP().toString().c_str(), static_cast<long>(WiFi.RSSI()), Connection::kHostname, unsigned(api.clients()));240 lab.showView("web");241}242 243const SoundCommand kCommands[] = {244⋯ 14 lines of data folded away. Show them258};259 260// ---- Hooks ------------------------------------------------------------------------------------261 262void setup(SoundLab &lab) {263 token.begin();264 connection.begin();265 Serial.println("S4: clips are kept in PSRAM only, a reset forgets them. Recording always shows: REC on screen, red LED.");266 if (!lab.mic().digital())267 Serial.println("S4: recording with the KY-037: expect thin, hissy clips. An INMP441 (mic auto) sounds far better.");268}269 270void frame(SoundLab &lab, const snd::Frame &f, uint32_t ms) { recorder.frame(lab, f, ms); }271 272void publishStatus(SoundLab &lab) {273 const snd::LevelReading &r = lab.level();274 const rec::ClipStore &store = recorder.store();275 JsonDocument d;276 d["state"] = recorder.recording() ? "recording" : recorder.armed() ? "armed" : "off";277 d["armed"] = recorder.armed();278 d["manual"] = recorder.recording() && recorder.manual();279 d["clip"] = recorder.clipNumber();280 d["recMs"] = recorder.recordedMs();281 d["above"] = roundf(r.above() * 10) / 10;282 d["floorDb"] = roundf(r.floorDb * 10) / 10;283 d["trigger"] = recorder.config().startDb;284 d["holdMs"] = recorder.config().holdMs;285 d["maxS"] = recorder.config().maxMs / 1000;286 d["prerollMs"] = recorder.config().prerollMs;287 d["clips"] = store.count();288 d["usedBytes"] = store.used();289 d["budgetBytes"] = store.budget;290 d["rate"] = lab.mic().rate();291 d["mic"] = snd::sourceShort(lab.mic().source());292 d["playing"] = playback.number; // 0 = the board's speaker is quiet293 d["playProgress"] = playback.clip ? roundf(lab.speaker().progress() * 1000) / 1000 : 0;294 String out;295 serializeJson(d, out);296 api.publishStatus(out);297}298 299void tick(SoundLab &lab) {300 // Playback ends when the speaker says so; then the clip goes back to the store.301 // (Give the speaker task 150 ms to pick up the play command before trusting "not playing".)302 if (playback.clip && !lab.speaker().playing() && millis() - playback.sinceMs > 150) {303 lab.note(sv::Note::Info, "clip %lu played", static_cast<unsigned long>(playback.number));304 endPlayback();305 }306 if (retiring && millis() - retireAt > 100) {307 rec::ClipStore::release(retiring);308 retiring = nullptr;309 }310 recorder.tick(lab);311 connection.loop();312 if (!api.started() && connection.state() == Connection::State::Connected) {313 api.begin();314 lab.note(sv::Note::Good, "page: http://%s.local", Connection::kHostname);315 }316 if (!api.started()) return;317 WebCommand c;318 while (api.take(c)) {319 switch (c.type) {320 case WebCommand::Type::Arm: recorder.arm(lab, c.value != 0); break;321 case WebCommand::Type::Rec:322 stopPlayback(lab);323 recorder.start(lab);324 break;325 case WebCommand::Type::Stop:326 if (recorder.recording()) recorder.stop(lab);327 stopPlayback(lab);328 break;329 case WebCommand::Type::Play: startPlayback(lab, c.value, c.speed); break;330 case WebCommand::Type::Delete:331 if (recorder.recording() && c.value == recorder.clipNumber()) lab.note(sv::Note::Warn, "web: clip %lu is recording", static_cast<unsigned long>(c.value));332 else if (recorder.store().remove(c.value)) lab.note(sv::Note::Info, "web: clip %lu deleted", static_cast<unsigned long>(c.value));333 break;334 }335 }336 api.loop();337 if (millis() - lastStatus >= 100) {338 lastStatus = millis();339 publishStatus(lab);340 }341}342 343// On the clips view, a long press picks the next clip (double-click plays it). Elsewhere: rec.344bool gesture(SoundLab &lab, Gesture g, bool) {345 if (g != Gesture::LongPress || strcmp(lab.spec().views[lab.view()].name, "clips") != 0) return false;346 Serial.println("> next (long press)");347 lab.runLine("next");348 return true;349}350 351const SoundLabSpec kSpec{352 "sound-lab-s4", "S4", "Recorder", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,353 kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, frame, tick, gesture, "rec",354};355 356SoundLab lab(kSpec);357 358} // namespace359 360void setup() { lab.begin(); }361 362void loop() { lab.loop(); }
// Sound lab S4 Recorder. uv run pio run -d labs/sound-lab-s4 -e sound-lab-s4 -t upload
// A voice-activated recorder. Armed, it waits for sound louder than the trigger, then records,
// starting half a second *before* the trigger (the pre-roll: the sampler's ring still has it),
// until 1.2 s of quiet. Clips go to PSRAM (12 clips, 6 MB, gone on reset). Listen to them on your
// phone at http://bench.local (the Wi-Fi W1 saved, plus the admin token), or pull a WAV file over
// USB with tools/sound_pull.py.
//
// And with the MAX98357A + speaker wired, `play <n>` plays a clip on the board itself, at any speed
// (0.5 = slower and deeper, 2 = the chipmunk).
// Which microphone matters here more than anywhere. The KY-037 has no amplifier, so a voice fills
// a small part of the ADC's range: clips are made louder ("normalised") on the way out and sound
// thin and hissy: a loudness sensor first, a microphone second. The INMP441 has its own amplifier
// and a 24-bit converter inside: its clips are actually pleasant to listen to. `mic auto` (the
// default) picks the INMP441 whenever it answers.
// Echo guard: while the speaker plays (and 200 ms after), the shell gives the lab no frames, so the
// voice trigger can't record the board's own playback. `play` refuses while recording, and `rec`
// stops a playback first.
// Wiring: a microphone (KY-037: + -> 3V3, G -> GND, AO -> GPIO 6, DO -> GPIO 21; or INMP441: VDD ->
// 3V3, GND, L/R -> GND, SCK -> 40, WS -> 41, SD -> 42), the screen from lesson 03, optionally the
// MAX98357A (VIN -> 5V, GND, BCLK -> 39, LRC -> 38, DIN -> 47). Push button on GPIO 5 optional.
// Views: recorder · clips · web (click = next, double-click = the action in the footer,
// long press = rec / stop; on the clips view: double-click = play, long press = next clip)
// Serial: help lists the commands. Shared code: kit/lib/sound-kit, kit/lib/wifi-kit (Wi-Fi, token).
#include "api_token.h"
#include "connection.h"
#include "recorder.h"
#include "recorder_api.h"
#include "s4_views.h"
#include "sound_lab.h"
namespace {
rec::Recorder recorder;
ApiToken token;
Connection connection;
RecorderApi api(recorder.store(), token);
int picked = 0; // the clips view's picked row
uint32_t lastStatus = 0;
// Playback on the board's speaker. The clip is leased from the store while it plays, so `del`,
// `clear` or a full store can take it off the list but can't free its samples under the speaker.
// The speaker task reads the samples on its own core, up to a block (16 ms) after it was told to
// stop, so a stopped clip is "retiring" for 100 ms before its lease is given back.
struct Playback {
rec::Clip *clip = nullptr;
uint32_t number = 0;
float speed = 1;
uint32_t sinceMs = 0;
} playback;
rec::Clip *retiring = nullptr;
uint32_t retireAt = 0;
void endPlayback() {
rec::ClipStore::release(retiring); // an older one still retiring: long done by now
retiring = playback.clip;
retireAt = millis();
playback = Playback{};
}
// ---- Views ------------------------------------------------------------------------------------
void drawRecorder(SoundLab &, FrameBuffer &fb, float t) { s4v::recorder(fb, recorder, recorder.config(), recorder.store(), t); }
void drawClips(SoundLab &lab, FrameBuffer &fb, float) {
s4v::clips(fb, recorder.store(), picked, playback.number, playback.clip ? lab.speaker().progress() : 0);
void drawWeb(SoundLab &, FrameBuffer &fb, float) { s4v::web(fb, connection, api.started(), api.clients()); }
const SoundView kViews[] = {
{"recorder", drawRecorder, "arm"},
{"clips", drawClips, "play"},
{"web", drawWeb, "wifi"},
};
// ---- Commands ---------------------------------------------------------------------------------
void cmdArm(SoundLab &lab, const String &, const String &lower) {
if (!lower.isEmpty() && lower != "on" && lower != "off") return lab.warn("arm | arm on | arm off");
recorder.arm(lab, lower.isEmpty() ? !recorder.armed() : lower == "on");
void stopPlayback(SoundLab &lab) {
if (!playback.clip) return;
lab.speaker().stop();
lab.note(sv::Note::Info, "playback of clip %lu stopped", static_cast<unsigned long>(playback.number));
endPlayback();
void cmdRec(SoundLab &lab, const String &, const String &) {
if (recorder.recording()) return recorder.stop(lab); // rec again (or a long press) stops
stopPlayback(lab); // never record the board's own speaker
recorder.start(lab);
void cmdStop(SoundLab &lab, const String &, const String &) {
if (playback.clip && !recorder.recording()) return stopPlayback(lab);
recorder.stop(lab);
// Play clip n (0 = the picked one) at `speed` on the board's speaker.
void startPlayback(SoundLab &lab, uint32_t n, float speed) {
if (recorder.recording()) return lab.warn("recording: stop first (the microphone would hear the speaker)");
if (n == 0) {
rec::Clip c;
if (!recorder.store().facts(picked, c)) return lab.warn("no clips yet");
n = c.number;
rec::Clip *c = recorder.store().lease(n);
if (!c) return lab.warn("no finished clip %lu", static_cast<unsigned long>(n));
if (playback.clip) {
// Normalised like the download, but at most x16: a whisper on the KY-037 would otherwise come
// back as mostly hiss, at full volume.
const float gain = std::min(16.0f, snd::gainFor(c->peak));
playback = Playback{c, n, speed, millis()};
lab.speaker().play(c->data, c->samples, speed * float(c->rate) / float(snd::Speaker::kRate), gain);
lab.note(sv::Note::Good, "playing clip %lu (%.1f s) at x%.2f", static_cast<unsigned long>(n), c->durationMs() / 1000.0f / speed, speed);
void cmdPlay(SoundLab &lab, const String &arg, const String &) {
const int sp = arg.indexOf(' ');
long n = 0;
float speed = 1.0f;
if (!arg.isEmpty() && !SoundLab::number(sp < 0 ? arg : arg.substring(0, sp), 1, 1000000, n))
return lab.warn("play [n] [speed 0.5-2.0]: clip n (default: the picked one) on the board's speaker");
if (sp > 0 && !SoundLab::decimal(arg.substring(sp + 1), 0.5f, 2.0f, speed)) return lab.warn("speed: 0.5 (slow, deep) to 2.0 (fast, chipmunk)");
startPlayback(lab, uint32_t(n), speed);
void cmdTrigger(SoundLab &lab, const String &arg, const String &) {
float db = 0;
if (!SoundLab::decimal(arg, 3, 60, db)) return lab.warn("trigger <dB above the quiet>, 3-60 (now %.0f)", recorder.config().startDb);
recorder.config().startDb = db;
lab.note(sv::Note::Info, "voice starts above %.0f dB", db);
void cmdHold(SoundLab &lab, const String &arg, const String &) {
long ms = 0;
if (!SoundLab::number(arg, 200, 10000, ms)) return lab.warn("hold <ms>, 200-10000: quiet this long ends a clip (now %lu)", static_cast<unsigned long>(recorder.config().holdMs));
recorder.config().holdMs = uint32_t(ms);
lab.note(sv::Note::Info, "a clip ends after %.1f s of quiet", ms / 1000.0f);
void cmdMax(SoundLab &lab, const String &arg, const String &) {
long s = 0;
if (!SoundLab::number(arg, 1, 120, s)) return lab.warn("max <s>, 1-120: the longest clip (now %lu)", static_cast<unsigned long>(recorder.config().maxMs / 1000));
recorder.config().maxMs = uint32_t(s) * 1000;
lab.note(sv::Note::Info, "clips stop at %ld s (from the next one)", s);
void cmdClips(SoundLab &lab, const String &, const String &) {
rec::ClipStore &store = recorder.store();
Serial.printf("%d clips, %.2f of %.0f MB of PSRAM (gone on reset):\n", store.count(), store.used() / 1048576.0, store.budget / 1048576.0);
Serial.println(" # length kind loudest sent x started");
store.each([](const rec::Clip &c) {
char when[24] = "-";
if (c.wall > 0) {
const time_t w = time_t(c.wall);
struct tm tm;
localtime_r(&w, &tm);
strftime(when, sizeof(when), "%Y-%m-%d %H:%M:%S", &tm);
Serial.printf(" %3lu %5.1fs %-5s %5.1f dBFS x%-5.1f %s%s\n", static_cast<unsigned long>(c.number), c.durationMs() / 1000.0f,
c.manual ? "hand" : "voice", snd::toDb(float(c.peak)), snd::gainFor(c.peak), when, c.open ? " (recording)" : "");
});
lab.showView("clips");
void cmdNext(SoundLab &lab, const String &, const String &) {
const int n = recorder.store().count();
picked = n ? (picked + 1) % n : 0;
void cmdDel(SoundLab &lab, const String &arg, const String &) {
if (!SoundLab::number(arg, 1, 1000000, n)) return lab.warn("del <n>: the clip's number (clips lists them)");
if (recorder.recording() && uint32_t(n) == recorder.clipNumber()) return lab.warn("clip %ld is recording: stop first", n);
if (!recorder.store().remove(uint32_t(n))) return lab.warn("no clip %ld", n);
lab.note(sv::Note::Info, "clip %ld deleted", n);
void cmdClear(SoundLab &lab, const String &, const String &) {
const int n = recorder.store().clear();
picked = 0;
lab.note(sv::Note::Info, "%d clip%s deleted", n, n == 1 ? "" : "s");
// The serial transfer: a header line, the WAV file as base64 lines, a CRC-32 line. Nothing else may
// be printed in between, so this runs to the end before loop() goes on (~20 s for 5 s of sound
// at 115200 baud: 32 KB of WAV a second of sound, 4/3 more as base64, 11.5 KB/s down the wire).
void cmdSend(SoundLab &lab, const String &arg, const String &) {
if (!SoundLab::number(arg, 1, 1000000, n)) return lab.warn("send <n>: for tools/sound_pull.py (close the serial monitor first)");
if (recorder.recording()) return lab.warn("recording: stop first, the transfer pauses everything else");
rec::Clip *c = recorder.store().lease(uint32_t(n));
if (!c) return lab.warn("no finished clip %ld", n);
const float gain = snd::gainFor(c->peak);
char name[48];
rec::clipName(*c, name, sizeof(name));
const size_t total = c->wavBytes();
Serial.printf("WAV-BEGIN %lu %u %s\n", static_cast<unsigned long>(c->number), unsigned(total), name);
uint8_t bytes[57]; // 57 bytes = one 76-character base64 line
char line[80];
uint32_t crc = 0;
for (size_t at = 0; at < total;) {
const size_t got = rec::wavRead(*c, gain, bytes, sizeof(bytes), at);
crc = rec::crc32(crc, bytes, got);
rec::base64(bytes, got, line);
Serial.println(line);
at += got;
Serial.printf("WAV-END %08lx\n", static_cast<unsigned long>(crc));
rec::ClipStore::release(c);
lab.note(sv::Note::Good, "sent clip %ld (%u bytes)", n, unsigned(total));
void cmdToken(SoundLab &lab, const String &, const String &lower) {
if (lower == "new") {
token.regenerate();
lab.note(sv::Note::Warn, "new admin token: open pages must unlock again");
} else if (!lower.isEmpty()) {
return lab.warn("token | token new");
Serial.printf("Admin token: %s\nPaste it into the page at http://%s.local (it is shared with R7/R8; keep it out of chats and git)\n",
token.value().c_str(), Connection::kHostname);
void cmdWifi(SoundLab &lab, const String &, const String &) {
if (connection.ssid().isEmpty()) return lab.warn("no Wi-Fi saved: flash wifi-lab-w1 once and join, or use tools/sound_pull.py over USB");
if (connection.state() != Connection::State::Connected)
return lab.note(sv::Note::Warn, "joining '%s'... (last reason: %s)", connection.ssid().c_str(), Connection::reasonHint(connection.lastReason()));
Serial.printf("Wi-Fi '%s' · IP %s · %ld dBm · page http://%s.local · %u page(s) open\n", connection.ssid().c_str(),
WiFi.localIP().toString().c_str(), static_cast<long>(WiFi.RSSI()), Connection::kHostname, unsigned(api.clients()));
lab.showView("web");
const SoundCommand kCommands[] = {
// ---- Hooks ------------------------------------------------------------------------------------
void setup(SoundLab &lab) {
token.begin();
connection.begin();
Serial.println("S4: clips are kept in PSRAM only, a reset forgets them. Recording always shows: REC on screen, red LED.");
if (!lab.mic().digital())
Serial.println("S4: recording with the KY-037: expect thin, hissy clips. An INMP441 (mic auto) sounds far better.");
void frame(SoundLab &lab, const snd::Frame &f, uint32_t ms) { recorder.frame(lab, f, ms); }
void publishStatus(SoundLab &lab) {
const snd::LevelReading &r = lab.level();
const rec::ClipStore &store = recorder.store();
JsonDocument d;
d["state"] = recorder.recording() ? "recording" : recorder.armed() ? "armed" : "off";
d["armed"] = recorder.armed();
d["manual"] = recorder.recording() && recorder.manual();
d["clip"] = recorder.clipNumber();
d["recMs"] = recorder.recordedMs();
d["above"] = roundf(r.above() * 10) / 10;
d["floorDb"] = roundf(r.floorDb * 10) / 10;
d["trigger"] = recorder.config().startDb;
d["holdMs"] = recorder.config().holdMs;
d["maxS"] = recorder.config().maxMs / 1000;
d["prerollMs"] = recorder.config().prerollMs;
d["clips"] = store.count();
d["usedBytes"] = store.used();
d["budgetBytes"] = store.budget;
d["rate"] = lab.mic().rate();
d["mic"] = snd::sourceShort(lab.mic().source());
d["playing"] = playback.number; // 0 = the board's speaker is quiet
d["playProgress"] = playback.clip ? roundf(lab.speaker().progress() * 1000) / 1000 : 0;
String out;
serializeJson(d, out);
api.publishStatus(out);
void tick(SoundLab &lab) {
// Playback ends when the speaker says so; then the clip goes back to the store.
// (Give the speaker task 150 ms to pick up the play command before trusting "not playing".)
if (playback.clip && !lab.speaker().playing() && millis() - playback.sinceMs > 150) {
lab.note(sv::Note::Info, "clip %lu played", static_cast<unsigned long>(playback.number));
if (retiring && millis() - retireAt > 100) {
rec::ClipStore::release(retiring);
retiring = nullptr;
recorder.tick(lab);
connection.loop();
if (!api.started() && connection.state() == Connection::State::Connected) {
api.begin();
lab.note(sv::Note::Good, "page: http://%s.local", Connection::kHostname);
if (!api.started()) return;
WebCommand c;
while (api.take(c)) {
switch (c.type) {
case WebCommand::Type::Arm: recorder.arm(lab, c.value != 0); break;
case WebCommand::Type::Rec:
stopPlayback(lab);
break;
case WebCommand::Type::Stop:
if (recorder.recording()) recorder.stop(lab);
case WebCommand::Type::Play: startPlayback(lab, c.value, c.speed); break;
case WebCommand::Type::Delete:
if (recorder.recording() && c.value == recorder.clipNumber()) lab.note(sv::Note::Warn, "web: clip %lu is recording", static_cast<unsigned long>(c.value));
else if (recorder.store().remove(c.value)) lab.note(sv::Note::Info, "web: clip %lu deleted", static_cast<unsigned long>(c.value));
api.loop();
if (millis() - lastStatus >= 100) {
lastStatus = millis();
publishStatus(lab);
// On the clips view, a long press picks the next clip (double-click plays it). Elsewhere: rec.
bool gesture(SoundLab &lab, Gesture g, bool) {
if (g != Gesture::LongPress || strcmp(lab.spec().views[lab.view()].name, "clips") != 0) return false;
Serial.println("> next (long press)");
lab.runLine("next");
return true;
const SoundLabSpec kSpec{
"sound-lab-s4", "S4", "Recorder", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,
kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, frame, tick, gesture, "rec",
SoundLab lab(kSpec);
} // namespace
void setup() { lab.begin(); }
void loop() { lab.loop(); }
New word #include
#include
Pastes another file in here, so this file can use what it declares.
PlatformIO environment sound-lab-s4 includes C++ source files covering pre-roll from a ring, trigger + hold, PSRAM clip store, WAV format, streaming responses, base64 + CRC-32, playback speed = pitch, tokens & visible recording.