1// Sound lab S8 Voice FX. uv run pio run -d labs/sound-lab-s8 -e sound-lab-s8 -t upload2// Microphone in, speaker out. Say something and the parrot says it back: as a chipmunk, a giant, a3// robot, with an echo, or backwards. Then the live effects: an echo you talk into, a robot voice.4//5// parrot voice-activated like S4 (snd::Trigger, with 0.3 s of pre-roll from the mic's ring),6// up to 6 s, then played straight back through the chosen effect. While it talks the7// parrot doesn't listen: the shell's echo guard holds back frames while the speaker is8// loud, and the trigger is only re-armed 300 ms after the speaker went quiet. Otherwise9// it would hear itself and repeat itself forever.10// echo live: mic -> delay line -> speaker. Latency = how long the sound waits in buffers: our11// processing step (~one screen frame) + the speaker's queue + its 16 ms DMA block.12// robot live: mic -> ring modulator + bitcrush -> speaker.13// scope what goes in and what comes out, and the chain between them.14//15// The live modes read the microphone's ring by sample number in tick(), not through frame(), so the16// echo guard (which would mute them, the speaker being loud) doesn't get in the way and the spec17// keeps hearSpeaker = false: the parrot stays deaf to its own voice.18//19// Feedback: the mic hears the speaker. If the sound comes back louder than it went out (loop gain20// above 1), it goes round and round: the howl. The guard (fx.h HowlDetector) cuts the live effect21// when the level keeps climbing or stays very loud. Keep the speaker away from the microphone,22// pointing elsewhere, and the volume low (`vol 25`).23//24// Wiring: an INMP441 (much clearer than the KY-037 here), the MAX98357A + speaker, the screen.25// INMP441: VDD -> 3V3, GND, L/R -> GND, SCK -> 40, WS -> 41, SD -> 4226// MAX98357A: VIN -> 5V, GND, BCLK -> 39, LRC -> 38, DIN -> 47, speaker on its terminal27//28// Views: parrot · echo · robot · scope (click = next view, double-click = the action in the footer,29// long press = record by hand / stop)30#include <esp_heap_caps.h>31 32#include "fx.h"33#include "sound_lab.h"34#include "trigger.h"35#include "wav.h"36 37namespace {38 39constexpr uint32_t kRate = 16000; // the speaker's rate; the parrot adapts if the mic's differs40constexpr uint32_t kMaxRec = kRate * 6;41constexpr uint32_t kTail = kRate * 3 / 2; // room for the echo effect's tail42constexpr uint32_t kMaxDelay = kRate / 2; // 500 ms43constexpr float kLiveGain = 4; // voices are quiet: +12 dB before the speaker's volume44 45// ---- State ------------------------------------------------------------------------------------46 47enum class Effect : uint8_t { Normal, Chipmunk, Giant, Robot, Echo, Reverse };48const char *const kEffectNames[] = {"normal", "chipmunk", "giant", "robot", "echo", "reverse"};49const char *const kEffectWhat[] = {"just as you said it", "1.5x faster = higher", "0.7x slower = deeper", "ring modulator + bitcrush",50 "220 ms echo, 45 % feedback", "backwards"};51constexpr int kEffects = 6;52 53enum class Parrot : uint8_t { Listening, Recording, Talking, Resting, Off };54enum class Live : uint8_t { None, Echo, Robot };55 56struct State {57 Effect effect = Effect::Chipmunk;58 Parrot parrot = Parrot::Listening;59 Live live = Live::None;60 bool parrotOn = true;61 // The recording.62 int16_t *rec = nullptr, *out = nullptr;63 uint32_t recLen = 0, outLen = 0, copied = 0, recFrom = 0;64 uint32_t restUntil = 0, talks = 0;65 // Live.66 uint32_t liveFrom = 0, lastTick = 0;67 float tickMs = 40;68 uint16_t delayMs = 250;69 uint8_t feedbackPct = 40;70 float carrierHz = 50;71 float inDb = -96, outDb = -96;72 uint32_t howlUntil = 0;73 // The scope's last blocks.74 int16_t scopeIn[320] = {}, scopeOut[320] = {};75} st;76 77snd::Trigger trig(kRate);78fx::DelayLine delayLine;79fx::RingMod ringMod;80fx::Bitcrush crush;81fx::HowlDetector howl;82int16_t *delayBuf = nullptr;83 84// ---- The parrot -------------------------------------------------------------------------------85 86void listen(SoundLab &lab) {87 st.parrot = st.parrotOn && st.live == Live::None ? Parrot::Listening : Parrot::Off;88 trig.arm(st.parrot == Parrot::Listening);89 lab.setRecording(false);90}91 92// Copy what the mic has heard so far into the recording.93void copyUpTo(SoundLab &lab, uint32_t to) {94 while (st.copied < to && st.recLen < kMaxRec) {95 const uint32_t n = std::min<uint32_t>({to - st.copied, kMaxRec - st.recLen, 1024});96 if (!lab.mic().copy(st.copied, st.rec + st.recLen, n)) {97 st.copied = to; // fell more than 4 s behind: keep what we have98 return;99 }100 st.copied += n, st.recLen += n;101 }102}103 104// Turn the recording into the effect's sound and play it.105void talk(SoundLab &lab) {106 if (st.recLen < kRate / 10) return listen(lab);107 const float gain = std::min(snd::gainFor(fx::peakOf(st.rec, st.recLen)), 24.0f);108 // A mic running at another rate plays back at the speaker's 16 kHz faster or slower: correct for it.109 const float rateFix = float(lab.mic().rate()) / kRate;110 float step = rateFix;111 const int16_t *src = st.rec;112 uint32_t n = st.recLen;113 switch (st.effect) {114 case Effect::Chipmunk: step *= 1.5f; break;115 case Effect::Giant: step *= 0.7f; break;116 case Effect::Robot:117 ringMod.set(st.carrierHz, kRate);118 crush.set(6, 2);119 for (uint32_t i = 0; i < n; i++) st.out[i] = crush.process(ringMod.process(st.rec[i]));120 src = st.out;121 break;122 case Effect::Echo: {123 fx::DelayLine d;124 d.begin(delayBuf, kMaxDelay);125 d.set(kRate * 220 / 1000, 0.45f);126 const uint32_t total = std::min(n + kTail, kMaxRec + kTail);127 for (uint32_t i = 0; i < total; i++) {128 const int16_t x = i < n ? st.rec[i] : 0;129 st.out[i] = fx::clamp16(x + 0.7f * d.process(x));130 }131 src = st.out, n = total;132 delayLine.clear(); // the live echo shares the buffer: start it clean133 break;134 }135 case Effect::Reverse:136 std::copy(st.rec, st.rec + n, st.out);137 fx::reverse(st.out, n);138 src = st.out;139 break;140 case Effect::Normal: break;141 }142 st.outLen = n;143 trig.arm(false); // no listening while talking144 lab.speaker().play(src, n, step, gain);145 st.parrot = Parrot::Talking;146 st.talks++;147 lab.note(sv::Note::Good, "%s, %.1f s, x%.0f louder", kEffectNames[int(st.effect)], st.recLen / float(lab.mic().rate()), gain);148}149 150void handle(SoundLab &lab, const snd::Trigger::Result &r) {151 switch (r.action) {152 case snd::Trigger::Action::Start:153 st.parrot = Parrot::Recording;154 st.recLen = 0;155 st.copied = st.recFrom = r.from;156 lab.setRecording(true); // the REC dot and red LED: nothing records without them157 break;158 case snd::Trigger::Action::Save:159 copyUpTo(lab, r.to);160 lab.setRecording(false);161 talk(lab);162 break;163 case snd::Trigger::Action::Drop:164 lab.note(sv::Note::Info, "too short: a bang, not a word");165 listen(lab);166 break;167 case snd::Trigger::Action::None: break;168 }169}170 171// ---- Live effects -----------------------------------------------------------------------------172 173void setLive(SoundLab &lab, Live mode) {174 if (mode != Live::None && lab.mic().rate() != kRate) return lab.warn("live effects need the mic at 16000 Hz (rate 16000)");175 st.live = mode;176 lab.speaker().stop();177 if (mode == Live::None) {178 st.restUntil = millis() + 300;179 st.parrot = Parrot::Resting;180 return lab.note(sv::Note::Info, "live off");181 }182 trig.arm(false);183 lab.setRecording(false);184 st.parrot = Parrot::Off;185 delayLine.clear();186 delayLine.set(kRate * st.delayMs / 1000, st.feedbackPct / 100.0f);187 ringMod.set(st.carrierHz, kRate);188 crush.set(8, 1);189 howl.reset();190 st.liveFrom = lab.mic().written();191 lab.note(sv::Note::Warn, "live %s: speaker away from the mic, volume low", mode == Live::Echo ? "echo" : "robot");192}193 194float blockDb(const int16_t *s, uint32_t n) {195 float sum = 0;196 for (uint32_t i = 0; i < n; i++) sum += float(s[i]) * s[i];197 return snd::toDb(sqrtf(sum / std::max<uint32_t>(n, 1)));198}199 200void runLive(SoundLab &lab) {201 snd::Mic &m = lab.mic();202 const uint32_t w = m.written();203 // More than 1/4 s behind (a slow frame)? Skip ahead: low latency matters more than every sample.204 if (w - st.liveFrom > kRate / 4) st.liveFrom = w - 256;205 static int16_t in[512], out[512];206 while (st.liveFrom < w) {207 const uint32_t n = std::min<uint32_t>(512, w - st.liveFrom);208 if (!m.copy(st.liveFrom, in, n)) {209 st.liveFrom = w;210 break;211 }212 st.liveFrom += n;213 for (uint32_t i = 0; i < n; i++) {214 const int16_t x = fx::clamp16(in[i] * kLiveGain);215 out[i] = st.live == Live::Echo ? delayLine.process(x) : crush.process(ringMod.process(x));216 }217 lab.speaker().stream(out, n);218 st.inDb = blockDb(in, n), st.outDb = blockDb(out, n);219 std::copy(in + n - std::min<uint32_t>(n, 320), in + n, st.scopeIn);220 std::copy(out + n - std::min<uint32_t>(n, 320), out + n, st.scopeOut);221 if (howl.push(st.inDb, n * 1000 / kRate)) {222 st.howlUntil = millis() + 5000;223 lab.note(sv::Note::Bad, "Howl! Move the speaker away or turn it down");224 lab.chime(Chime::No);225 setLive(lab, Live::None);226 return;227 }228 }229}230 231// ---- Drawing ----------------------------------------------------------------------------------232 233constexpr uint16_t kGreen = rgb(60, 200, 110), kGreenDark = rgb(30, 130, 70), kRed = rgb(235, 70, 60), kBeak = rgb(255, 196, 60);234 235// The parrot: body, wing, head, eye, beak, crest, on a perch. `open` 0-1 opens the beak.236void drawParrot(FrameBuffer &fb, int cx, int cy, float t, float open, Parrot mood) {237 const int bob = mood == Parrot::Listening ? int(2 * sinf(t * 2.5f)) : mood == Parrot::Talking ? int(2 * sinf(t * 14)) : 0;238 fb.fillRoundRect(cx - 50, cy + 52, 100, 6, 3, rgb(120, 80, 50)); // perch239 fb.fillTriangle(cx - 6, cy + 40, cx + 6, cy + 40, cx - 2, cy + 70, kRed); // tail240 fb.fillCircle(cx, cy + 22 + bob, 24, kGreen); // body241 fb.fillTriangle(cx - 22, cy + 10 + bob, cx - 4, cy + 18 + bob, cx - 18, cy + 46 + bob, kGreenDark); // wing242 fb.fillCircle(cx + 8, cy - 8 + bob, 18, kGreen); // head243 for (int i = 0; i < 3; i++) fb.fillTriangle(cx + 2 + i * 6, cy - 24 + bob, cx + 8 + i * 6, cy - 24 + bob, cx + i * 7, cy - 38 + bob - i * 2, kRed); // crest244 // The eye: wide while recording, blinking now and then while listening.245 const bool blink = mood == Parrot::Listening && fmodf(t, 3.7f) < 0.12f;246 const int eyeR = mood == Parrot::Recording ? 7 : 5;247 if (blink) fb.drawFastHLine(cx + 9, cy - 12 + bob, 10, pal::night);248 else {249 fb.fillCircle(cx + 14, cy - 12 + bob, eyeR, pal::ink);250 fb.fillCircle(cx + 15 + (mood == Parrot::Listening ? int(2 * sinf(t)) : 0), cy - 12 + bob, eyeR / 2, pal::night);251 }252 // The beak: upper half fixed, lower half drops with `open`.253 const int bx = cx + 24, by = cy - 4 + bob, drop = int(open * 12);254 fb.fillTriangle(bx, by - 8, bx, by, bx + 16, by + 2, kBeak);255 fb.fillTriangle(bx, by + 1 + drop / 2, bx, by + 6 + drop, bx + 11, by + 3 + drop, mix(kBeak, pal::night, 60));256 fb.fillCircle(cx - 6, cy + 54, 3, kBeak), fb.fillCircle(cx + 6, cy + 54, 3, kBeak); // feet257}258 259void drawParrotView(SoundLab &lab, FrameBuffer &fb, float t) {260 const Parrot mood = st.parrot;261 const float open = mood == Parrot::Talking ? std::min(1.0f, lab.speaker().outputLevel() * 2.5f) : 0;262 drawParrot(fb, 70, sv::kTop + 40, t, open, mood);263 // Sound arriving while it listens: arcs from the right.264 if (mood == Parrot::Listening)265 for (int i = 0; i < 3; i++) {266 const float k = fmodf(t * 0.8f + i / 3.0f, 1.0f);267 fb.drawCircleHelper(150, sv::kTop + 30, 8 + int((1 - k) * 30), 0x8 | 0x1, mix(sv::kSound, pal::night, uint8_t(k * 220)));268 }269 // The speech bubble.270 const int bx = 150, by = sv::kTop + 6, bw = 162, bh = 58;271 fb.fillRoundRect(bx, by, bw, bh, 10, sv::kPanel);272 fb.fillTriangle(bx, by + 30, bx, by + 44, bx - 14, by + 42, sv::kPanel);273 const char *line1 = mood == Parrot::Listening ? "Say something!"274 : mood == Parrot::Recording ? "I'm listening..."275 : mood == Parrot::Talking ? "Squawk!"276 : mood == Parrot::Off ? (st.live == Live::None ? "Resting (parrot on)" : "Busy: live effect on")277 : "...";278 sv::bold(fb, bx + 8, by + 22, mood == Parrot::Recording ? sv::kRec : pal::ink, line1);279 char text[48];280 snprintf(text, sizeof(text), "voice: %s", kEffectNames[int(st.effect)]);281 sv::small(fb, bx + 8, by + 34, sv::kSound, text);282 sv::small(fb, bx + 8, by + 45, pal::muted, kEffectWhat[int(st.effect)]);283 if (mood == Parrot::Recording) {284 const float secs = (lab.mic().written() - st.recFrom) / float(lab.mic().rate());285 snprintf(text, sizeof(text), "REC %.1f s", secs);286 sv::bold(fb, 150, sv::kTop + 88, sv::kRec, text);287 } else if (mood == Parrot::Talking) {288 fb.drawRect(150, sv::kTop + 80, 160, 8, pal::dim);289 fb.fillRect(151, sv::kTop + 81, int(lab.speaker().progress() * 158), 6, sv::kSound);290 }291 // The level against the trigger line.292 const float above = lab.level().above();293 sv::meter(fb, 150, sv::kTop + 100, 160, 10, lab.level(), 50, trig.config.startDb);294 snprintf(text, sizeof(text), "%.0f dB, starts at %.0f", above, trig.config.startDb);295 sv::small(fb, 150, sv::kTop + 114, pal::muted, text);296}297 298void drawEcho(SoundLab &lab, FrameBuffer &fb, float t) {299 const bool on = st.live == Live::Echo;300 char text[64];301 sv::bold(fb, 10, sv::kTop + 20, on ? pal::teal : pal::muted, on ? "Echo ON" : "Echo off");302 snprintf(text, sizeof(text), "delay %u ms - feedback %u %%", st.delayMs, st.feedbackPct);303 sv::small(fb, 110, sv::kTop + 10, pal::ink, text);304 // The taps: what one clap turns into. Each repeat feedback x quieter, delay apart.305 const int x0 = 14, y0 = sv::kTop + 34, h = 54, w = 292;306 fb.drawFastHLine(x0, y0 + h, w, pal::dim);307 fb.fillRect(x0, y0, 3, h, pal::ink); // the clap itself (in the room)308 float a = 1;309 for (int k = 1; k < 12; k++) {310 const int x = x0 + k * st.delayMs * w / 2000;311 if (x > x0 + w) break;312 const int hh = int(h * a);313 if (hh > 0) fb.fillRect(x, y0 + h - hh, 3, hh, sv::kSound);314 a *= st.feedbackPct / 100.0f;315 }316 sv::small(fb, x0, y0 + h + 3, pal::muted, "one clap -> its echoes over 2 s");317 // Meters and latency.318 const float latency = st.tickMs + lab.speaker().streamQueued() * 1000.0f / kRate + 16;319 snprintf(text, sizeof(text), "in %.0f dBFS out %.0f dBFS latency ~%.0f ms", st.inDb, st.outDb, latency);320 sv::small(fb, 10, sv::kTop + 104, on ? pal::ink : pal::muted, text);321 if (millis() < st.howlUntil && fmodf(t, 0.5f) < 0.35f) sv::bold(fb, 10, sv::kTop + 126, pal::coral, "Howl! Move the speaker away");322 else sv::small(fb, 10, sv::kTop + 120, pal::muted, "speaker away from the mic, vol low");323}324 325void drawRobot(SoundLab &lab, FrameBuffer &fb, float t) {326 const bool on = st.live == Live::Robot;327 // A robot head; its mouth lights with what comes out of the speaker.328 const int cx = 80, cy = sv::kTop + 62;329 fb.fillRoundRect(cx - 44, cy - 44, 88, 84, 10, rgb(110, 130, 150));330 fb.drawFastVLine(cx, cy - 58, 14, pal::muted);331 fb.fillCircle(cx, cy - 60, 4, on && fmodf(t, 1.0f) < 0.5f ? pal::coral : pal::dim);332 fb.fillCircle(cx - 18, cy - 14, 9, pal::night), fb.fillCircle(cx + 18, cy - 14, 9, pal::night);333 fb.fillCircle(cx - 18, cy - 14, 4, on ? pal::teal : pal::dim), fb.fillCircle(cx + 18, cy - 14, 4, on ? pal::teal : pal::dim);334 const float lvl = on ? lab.speaker().outputLevel() : 0;335 for (int i = 0; i < 7; i++) {336 const int hh = 4 + int(lvl * 18 * (0.6f + 0.4f * sinf(i * 1.7f + t * 20)));337 fb.fillRect(cx - 30 + i * 9, cy + 18 - hh / 2, 6, std::max(2, hh), on ? sv::kSound : pal::dim);338 }339 char text[48];340 sv::bold(fb, 150, sv::kTop + 20, on ? pal::teal : pal::muted, on ? "Robot ON" : "Robot off");341 snprintf(text, sizeof(text), "carrier %.0f Hz", st.carrierHz);342 sv::small(fb, 150, sv::kTop + 36, pal::ink, text);343 sv::small(fb, 150, sv::kTop + 52, pal::muted, "voice x a slow sine:");344 sv::small(fb, 150, sv::kTop + 63, pal::muted, "every f becomes f-c, f+c");345 sv::small(fb, 150, sv::kTop + 74, pal::muted, "+ 8-bit crunch");346 if (millis() < st.howlUntil) sv::bold(fb, 150, sv::kTop + 100, pal::coral, "Howl! Stopped");347 else sv::small(fb, 150, sv::kTop + 100, pal::muted, "speaker away from the mic");348}349 350void drawScope(SoundLab &lab, FrameBuffer &fb, float) {351 // Input on top, output below, the chain on the left.352 static int16_t in[320], out[320];353 if (st.live == Live::None) {354 lab.mic().latest(in, 320);355 // Talking: the part of the effect's sound the speaker is on now.356 if (st.parrot == Parrot::Talking && st.outLen) {357 const int16_t *src = (st.effect == Effect::Robot || st.effect == Effect::Echo || st.effect == Effect::Reverse) ? st.out : st.rec;358 const uint32_t n = (st.effect == Effect::Robot || st.effect == Effect::Echo || st.effect == Effect::Reverse) ? st.outLen : st.recLen;359 const uint32_t at = std::min<uint32_t>(uint32_t(lab.speaker().progress() * n), n > 320 ? n - 320 : 0);360 std::copy(src + at, src + std::min(n, at + 320), out);361 } else std::fill_n(out, 320, int16_t(0));362 } else {363 std::copy(st.scopeIn, st.scopeIn + 320, in);364 std::copy(st.scopeOut, st.scopeOut + 320, out);365 }366 const bool active = st.live != Live::None || st.parrot == Parrot::Talking || st.parrot == Parrot::Recording;367 const char *mid = st.live == Live::Echo ? "delay" : st.live == Live::Robot ? "ring mod" : kEffectNames[int(st.effect)];368 const char *boxes[3] = {"mic", mid, "speaker"};369 for (int i = 0; i < 3; i++) {370 const int y = sv::kTop + 8 + i * 42;371 fb.fillRoundRect(6, y, 76, 26, 6, active ? mix(sv::kPanel, sv::kSound, 70) : sv::kPanel);372 sv::small(fb, 12, y + 10, active ? pal::ink : pal::muted, boxes[i]);373 if (i < 2) fb.fillTriangle(40, y + 30, 48, y + 30, 44, y + 38, active ? sv::kSound : pal::dim);374 }375 sv::small(fb, 92, sv::kTop + 4, pal::muted, "in");376 sv::wave(fb, 92, sv::kTop + 14, 222, 50, in, 320, sv::autoScale(in, 320, 512), pal::teal);377 sv::small(fb, 92, sv::kTop + 68, pal::muted, "out");378 sv::wave(fb, 92, sv::kTop + 78, 222, 50, out, 320, sv::autoScale(out, 320, 512), sv::kSound);379}380 381const SoundView kViews[] = {382 {"parrot", drawParrotView, "fx next"},383 {"echo", drawEcho, "echo"},384 {"robot", drawRobot, "robot"},385 {"scope", drawScope, "again"},386};387 388// ---- Commands ---------------------------------------------------------------------------------389 390void cmdFx(SoundLab &lab, const String &, const String &lower) {391 if (lower == "next") st.effect = Effect((int(st.effect) + 1) % kEffects);392 else {393 int found = -1;394 for (int i = 0; i < kEffects; i++)395 if (lower == kEffectNames[i]) found = i;396 if (found < 0) return lab.warn("fx normal | chipmunk | giant | robot | echo | reverse | next");397 st.effect = Effect(found);398 }399 lab.note(sv::Note::Info, "voice: %s (%s)", kEffectNames[int(st.effect)], kEffectWhat[int(st.effect)]);400}401 402void cmdAgain(SoundLab &lab, const String &, const String &) {403 if (st.recLen == 0) return lab.warn("nothing recorded yet: say something first");404 if (st.parrot == Parrot::Recording || st.live != Live::None) return lab.warn("busy");405 talk(lab);406}407 408void cmdEcho(SoundLab &lab, const String &, const String &lower) {409 const bool on = lower.isEmpty() ? st.live != Live::Echo : lower == "on";410 setLive(lab, on ? Live::Echo : Live::None);411 lab.showView("echo");412}413 414void cmdRobot(SoundLab &lab, const String &, const String &lower) {415 const bool on = lower.isEmpty() ? st.live != Live::Robot : lower == "on";416 setLive(lab, on ? Live::Robot : Live::None);417 lab.showView("robot");418}419 420void cmdDelay(SoundLab &lab, const String &arg, const String &) {421 long ms = 0;422 if (!SoundLab::number(arg, 20, 500, ms)) return lab.warn("delay <20-500 ms> (now %u)", st.delayMs);423 st.delayMs = uint16_t(ms);424 delayLine.set(kRate * st.delayMs / 1000, st.feedbackPct / 100.0f);425}426 427void cmdFeedback(SoundLab &lab, const String &arg, const String &) {428 long pct = 0;429 if (!SoundLab::number(arg, 0, 70, pct)) return lab.warn("feedback <0-70 %%> (now %u): how much of each echo echoes again", st.feedbackPct);430 st.feedbackPct = uint8_t(pct);431 delayLine.set(kRate * st.delayMs / 1000, st.feedbackPct / 100.0f);432}433 434void cmdCarrier(SoundLab &lab, const String &arg, const String &) {435 float hz = 0;436 if (!SoundLab::decimal(arg, 10, 300, hz)) return lab.warn("carrier <10-300 Hz> (now %.0f): 30-80 = robot, 150+ = bell-like", st.carrierHz);437 st.carrierHz = hz;438 ringMod.set(hz, kRate);439}440 441void cmdTrigger(SoundLab &lab, const String &arg, const String &) {442 float db = 0;443 if (!SoundLab::decimal(arg, 4, 40, db)) return lab.warn("trigger <dB above the quiet> 4-40 (now %.0f)", trig.config.startDb);444 trig.config.startDb = db;445}446 447void cmdParrot(SoundLab &lab, const String &, const String &lower) {448 st.parrotOn = lower.isEmpty() ? !st.parrotOn : lower == "on";449 if (st.parrot != Parrot::Talking) listen(lab);450 lab.note(sv::Note::Info, st.parrotOn ? "the parrot listens" : "the parrot rests");451}452 453// Long press: start or stop a recording by hand.454void cmdRec(SoundLab &lab, const String &, const String &) {455 if (st.live != Live::None || st.parrot == Parrot::Talking) return lab.warn("busy");456 if (st.parrot == Parrot::Recording) return handle(lab, trig.stop(lab.mic().written()));457 handle(lab, trig.start(lab.mic().written()));458}459 460const SoundCommand kCommands[] = {461⋯ 10 lines of data folded away. Show them471};472 473// ---- Hooks ------------------------------------------------------------------------------------474 475void setup(SoundLab &lab) {476 st.rec = static_cast<int16_t *>(heap_caps_malloc(kMaxRec * sizeof(int16_t), MALLOC_CAP_SPIRAM));477 st.out = static_cast<int16_t *>(heap_caps_malloc((kMaxRec + kTail) * sizeof(int16_t), MALLOC_CAP_SPIRAM));478 delayBuf = static_cast<int16_t *>(heap_caps_calloc(kMaxDelay, sizeof(int16_t), MALLOC_CAP_SPIRAM));479 if (!st.rec || !st.out || !delayBuf) {480 log_e("S8: no PSRAM for the buffers");481 st.parrotOn = false;482 }483 delayLine.begin(delayBuf, kMaxDelay);484 trig.setRate(lab.mic().rate());485 trig.config.startDb = 12;486 trig.config.holdMs = 700;487 trig.config.prerollMs = 300;488 trig.config.maxMs = 6000;489 trig.config.minMs = 300;490 listen(lab);491 if (!lab.mic().digital()) lab.note(sv::Note::Warn, "KY-037: it works, but an INMP441 sounds much clearer");492}493 494void frame(SoundLab &lab, const snd::Frame &f, uint32_t) {495 if (st.parrot != Parrot::Listening && st.parrot != Parrot::Recording) return;496 if (lab.meter().learning()) return;497 handle(lab, trig.push(lab.level().above(), f.end));498}499 500void tick(SoundLab &lab) {501 const uint32_t now = millis();502 if (st.lastTick) st.tickMs += (float(now - st.lastTick) - st.tickMs) * 0.1f;503 st.lastTick = now;504 if (st.live != Live::None) return runLive(lab);505 switch (st.parrot) {506 case Parrot::Recording:507 copyUpTo(lab, lab.mic().written());508 if (st.recLen >= kMaxRec) handle(lab, trig.stop(lab.mic().written()));509 break;510 case Parrot::Talking:511 if (!lab.speaker().busy()) st.parrot = Parrot::Resting, st.restUntil = now + 300;512 break;513 case Parrot::Resting:514 // 300 ms after the speaker went quiet: the room has stopped ringing.515 if (now >= st.restUntil && !lab.speaker().recentlyLoud(300)) listen(lab);516 break;517 default: break;518 }519}520 521const SoundLabSpec kSpec{522 "sound-lab-s8", "S8", "Voice FX", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,523 kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, frame, tick, nullptr, "rec",524};525 526SoundLab lab(kSpec);527 528} // namespace529 530void setup() { lab.begin(); }531 532void loop() { lab.loop(); }
// Sound lab S8 Voice FX. uv run pio run -d labs/sound-lab-s8 -e sound-lab-s8 -t upload
// Microphone in, speaker out. Say something and the parrot says it back: as a chipmunk, a giant, a
// robot, with an echo, or backwards. Then the live effects: an echo you talk into, a robot voice.
//
// parrot voice-activated like S4 (snd::Trigger, with 0.3 s of pre-roll from the mic's ring),
// up to 6 s, then played straight back through the chosen effect. While it talks the
// parrot doesn't listen: the shell's echo guard holds back frames while the speaker is
// loud, and the trigger is only re-armed 300 ms after the speaker went quiet. Otherwise
// it would hear itself and repeat itself forever.
// echo live: mic -> delay line -> speaker. Latency = how long the sound waits in buffers: our
// processing step (~one screen frame) + the speaker's queue + its 16 ms DMA block.
// robot live: mic -> ring modulator + bitcrush -> speaker.
// scope what goes in and what comes out, and the chain between them.
// The live modes read the microphone's ring by sample number in tick(), not through frame(), so the
// echo guard (which would mute them, the speaker being loud) doesn't get in the way and the spec
// keeps hearSpeaker = false: the parrot stays deaf to its own voice.
// Feedback: the mic hears the speaker. If the sound comes back louder than it went out (loop gain
// above 1), it goes round and round: the howl. The guard (fx.h HowlDetector) cuts the live effect
// when the level keeps climbing or stays very loud. Keep the speaker away from the microphone,
// pointing elsewhere, and the volume low (`vol 25`).
// Wiring: an INMP441 (much clearer than the KY-037 here), the MAX98357A + speaker, the screen.
// INMP441: VDD -> 3V3, GND, L/R -> GND, SCK -> 40, WS -> 41, SD -> 42
// MAX98357A: VIN -> 5V, GND, BCLK -> 39, LRC -> 38, DIN -> 47, speaker on its terminal
// Views: parrot · echo · robot · scope (click = next view, double-click = the action in the footer,
// long press = record by hand / stop)
#include <esp_heap_caps.h>
#include "fx.h"
#include "sound_lab.h"
#include "trigger.h"
#include "wav.h"
namespace {
constexpr uint32_t kRate = 16000; // the speaker's rate; the parrot adapts if the mic's differs
constexpr uint32_t kMaxRec = kRate * 6;
constexpr uint32_t kTail = kRate * 3 / 2; // room for the echo effect's tail
constexpr uint32_t kMaxDelay = kRate / 2; // 500 ms
constexpr float kLiveGain = 4; // voices are quiet: +12 dB before the speaker's volume
// ---- State ------------------------------------------------------------------------------------
enum class Effect : uint8_t { Normal, Chipmunk, Giant, Robot, Echo, Reverse };
const char *const kEffectNames[] = {"normal", "chipmunk", "giant", "robot", "echo", "reverse"};
const char *const kEffectWhat[] = {"just as you said it", "1.5x faster = higher", "0.7x slower = deeper", "ring modulator + bitcrush",
"220 ms echo, 45 % feedback", "backwards"};
constexpr int kEffects = 6;
enum class Parrot : uint8_t { Listening, Recording, Talking, Resting, Off };
enum class Live : uint8_t { None, Echo, Robot };
struct State {
Effect effect = Effect::Chipmunk;
Parrot parrot = Parrot::Listening;
Live live = Live::None;
bool parrotOn = true;
// The recording.
int16_t *rec = nullptr, *out = nullptr;
uint32_t recLen = 0, outLen = 0, copied = 0, recFrom = 0;
uint32_t restUntil = 0, talks = 0;
// Live.
uint32_t liveFrom = 0, lastTick = 0;
float tickMs = 40;
uint16_t delayMs = 250;
uint8_t feedbackPct = 40;
float carrierHz = 50;
float inDb = -96, outDb = -96;
uint32_t howlUntil = 0;
// The scope's last blocks.
int16_t scopeIn[320] = {}, scopeOut[320] = {};
} st;
snd::Trigger trig(kRate);
fx::DelayLine delayLine;
fx::RingMod ringMod;
fx::Bitcrush crush;
fx::HowlDetector howl;
int16_t *delayBuf = nullptr;
// ---- The parrot -------------------------------------------------------------------------------
void listen(SoundLab &lab) {
st.parrot = st.parrotOn && st.live == Live::None ? Parrot::Listening : Parrot::Off;
trig.arm(st.parrot == Parrot::Listening);
lab.setRecording(false);
}
// Copy what the mic has heard so far into the recording.
void copyUpTo(SoundLab &lab, uint32_t to) {
while (st.copied < to && st.recLen < kMaxRec) {
const uint32_t n = std::min<uint32_t>({to - st.copied, kMaxRec - st.recLen, 1024});
if (!lab.mic().copy(st.copied, st.rec + st.recLen, n)) {
st.copied = to; // fell more than 4 s behind: keep what we have
return;
st.copied += n, st.recLen += n;
// Turn the recording into the effect's sound and play it.
void talk(SoundLab &lab) {
if (st.recLen < kRate / 10) return listen(lab);
const float gain = std::min(snd::gainFor(fx::peakOf(st.rec, st.recLen)), 24.0f);
// A mic running at another rate plays back at the speaker's 16 kHz faster or slower: correct for it.
const float rateFix = float(lab.mic().rate()) / kRate;
float step = rateFix;
const int16_t *src = st.rec;
uint32_t n = st.recLen;
switch (st.effect) {
case Effect::Chipmunk: step *= 1.5f; break;
case Effect::Giant: step *= 0.7f; break;
case Effect::Robot:
ringMod.set(st.carrierHz, kRate);
crush.set(6, 2);
for (uint32_t i = 0; i < n; i++) st.out[i] = crush.process(ringMod.process(st.rec[i]));
src = st.out;
break;
case Effect::Echo: {
fx::DelayLine d;
d.begin(delayBuf, kMaxDelay);
d.set(kRate * 220 / 1000, 0.45f);
const uint32_t total = std::min(n + kTail, kMaxRec + kTail);
for (uint32_t i = 0; i < total; i++) {
const int16_t x = i < n ? st.rec[i] : 0;
st.out[i] = fx::clamp16(x + 0.7f * d.process(x));
src = st.out, n = total;
delayLine.clear(); // the live echo shares the buffer: start it clean
case Effect::Reverse:
std::copy(st.rec, st.rec + n, st.out);
fx::reverse(st.out, n);
case Effect::Normal: break;
st.outLen = n;
trig.arm(false); // no listening while talking
lab.speaker().play(src, n, step, gain);
st.parrot = Parrot::Talking;
st.talks++;
lab.note(sv::Note::Good, "%s, %.1f s, x%.0f louder", kEffectNames[int(st.effect)], st.recLen / float(lab.mic().rate()), gain);
void handle(SoundLab &lab, const snd::Trigger::Result &r) {
switch (r.action) {
case snd::Trigger::Action::Start:
st.parrot = Parrot::Recording;
st.recLen = 0;
st.copied = st.recFrom = r.from;
lab.setRecording(true); // the REC dot and red LED: nothing records without them
case snd::Trigger::Action::Save:
copyUpTo(lab, r.to);
talk(lab);
case snd::Trigger::Action::Drop:
lab.note(sv::Note::Info, "too short: a bang, not a word");
listen(lab);
case snd::Trigger::Action::None: break;
// ---- Live effects -----------------------------------------------------------------------------
void setLive(SoundLab &lab, Live mode) {
if (mode != Live::None && lab.mic().rate() != kRate) return lab.warn("live effects need the mic at 16000 Hz (rate 16000)");
st.live = mode;
lab.speaker().stop();
if (mode == Live::None) {
st.restUntil = millis() + 300;
st.parrot = Parrot::Resting;
return lab.note(sv::Note::Info, "live off");
trig.arm(false);
st.parrot = Parrot::Off;
delayLine.clear();
delayLine.set(kRate * st.delayMs / 1000, st.feedbackPct / 100.0f);
crush.set(8, 1);
howl.reset();
st.liveFrom = lab.mic().written();
lab.note(sv::Note::Warn, "live %s: speaker away from the mic, volume low", mode == Live::Echo ? "echo" : "robot");
float blockDb(const int16_t *s, uint32_t n) {
float sum = 0;
for (uint32_t i = 0; i < n; i++) sum += float(s[i]) * s[i];
return snd::toDb(sqrtf(sum / std::max<uint32_t>(n, 1)));
void runLive(SoundLab &lab) {
snd::Mic &m = lab.mic();
const uint32_t w = m.written();
// More than 1/4 s behind (a slow frame)? Skip ahead: low latency matters more than every sample.
if (w - st.liveFrom > kRate / 4) st.liveFrom = w - 256;
static int16_t in[512], out[512];
while (st.liveFrom < w) {
const uint32_t n = std::min<uint32_t>(512, w - st.liveFrom);
if (!m.copy(st.liveFrom, in, n)) {
st.liveFrom = w;
st.liveFrom += n;
for (uint32_t i = 0; i < n; i++) {
const int16_t x = fx::clamp16(in[i] * kLiveGain);
out[i] = st.live == Live::Echo ? delayLine.process(x) : crush.process(ringMod.process(x));
lab.speaker().stream(out, n);
st.inDb = blockDb(in, n), st.outDb = blockDb(out, n);
std::copy(in + n - std::min<uint32_t>(n, 320), in + n, st.scopeIn);
std::copy(out + n - std::min<uint32_t>(n, 320), out + n, st.scopeOut);
if (howl.push(st.inDb, n * 1000 / kRate)) {
st.howlUntil = millis() + 5000;
lab.note(sv::Note::Bad, "Howl! Move the speaker away or turn it down");
lab.chime(Chime::No);
setLive(lab, Live::None);
// ---- Drawing ----------------------------------------------------------------------------------
constexpr uint16_t kGreen = rgb(60, 200, 110), kGreenDark = rgb(30, 130, 70), kRed = rgb(235, 70, 60), kBeak = rgb(255, 196, 60);
// The parrot: body, wing, head, eye, beak, crest, on a perch. `open` 0-1 opens the beak.
void drawParrot(FrameBuffer &fb, int cx, int cy, float t, float open, Parrot mood) {
const int bob = mood == Parrot::Listening ? int(2 * sinf(t * 2.5f)) : mood == Parrot::Talking ? int(2 * sinf(t * 14)) : 0;
fb.fillRoundRect(cx - 50, cy + 52, 100, 6, 3, rgb(120, 80, 50)); // perch
fb.fillTriangle(cx - 6, cy + 40, cx + 6, cy + 40, cx - 2, cy + 70, kRed); // tail
fb.fillCircle(cx, cy + 22 + bob, 24, kGreen); // body
fb.fillTriangle(cx - 22, cy + 10 + bob, cx - 4, cy + 18 + bob, cx - 18, cy + 46 + bob, kGreenDark); // wing
fb.fillCircle(cx + 8, cy - 8 + bob, 18, kGreen); // head
for (int i = 0; i < 3; i++) fb.fillTriangle(cx + 2 + i * 6, cy - 24 + bob, cx + 8 + i * 6, cy - 24 + bob, cx + i * 7, cy - 38 + bob - i * 2, kRed); // crest
// The eye: wide while recording, blinking now and then while listening.
const bool blink = mood == Parrot::Listening && fmodf(t, 3.7f) < 0.12f;
const int eyeR = mood == Parrot::Recording ? 7 : 5;
if (blink) fb.drawFastHLine(cx + 9, cy - 12 + bob, 10, pal::night);
else {
fb.fillCircle(cx + 14, cy - 12 + bob, eyeR, pal::ink);
fb.fillCircle(cx + 15 + (mood == Parrot::Listening ? int(2 * sinf(t)) : 0), cy - 12 + bob, eyeR / 2, pal::night);
// The beak: upper half fixed, lower half drops with `open`.
const int bx = cx + 24, by = cy - 4 + bob, drop = int(open * 12);
fb.fillTriangle(bx, by - 8, bx, by, bx + 16, by + 2, kBeak);
fb.fillTriangle(bx, by + 1 + drop / 2, bx, by + 6 + drop, bx + 11, by + 3 + drop, mix(kBeak, pal::night, 60));
fb.fillCircle(cx - 6, cy + 54, 3, kBeak), fb.fillCircle(cx + 6, cy + 54, 3, kBeak); // feet
void drawParrotView(SoundLab &lab, FrameBuffer &fb, float t) {
const Parrot mood = st.parrot;
const float open = mood == Parrot::Talking ? std::min(1.0f, lab.speaker().outputLevel() * 2.5f) : 0;
drawParrot(fb, 70, sv::kTop + 40, t, open, mood);
// Sound arriving while it listens: arcs from the right.
if (mood == Parrot::Listening)
for (int i = 0; i < 3; i++) {
const float k = fmodf(t * 0.8f + i / 3.0f, 1.0f);
fb.drawCircleHelper(150, sv::kTop + 30, 8 + int((1 - k) * 30), 0x8 | 0x1, mix(sv::kSound, pal::night, uint8_t(k * 220)));
// The speech bubble.
const int bx = 150, by = sv::kTop + 6, bw = 162, bh = 58;
fb.fillRoundRect(bx, by, bw, bh, 10, sv::kPanel);
fb.fillTriangle(bx, by + 30, bx, by + 44, bx - 14, by + 42, sv::kPanel);
const char *line1 = mood == Parrot::Listening ? "Say something!"
: mood == Parrot::Recording ? "I'm listening..."
: mood == Parrot::Talking ? "Squawk!"
: mood == Parrot::Off ? (st.live == Live::None ? "Resting (parrot on)" : "Busy: live effect on")
: "...";
sv::bold(fb, bx + 8, by + 22, mood == Parrot::Recording ? sv::kRec : pal::ink, line1);
char text[48];
snprintf(text, sizeof(text), "voice: %s", kEffectNames[int(st.effect)]);
sv::small(fb, bx + 8, by + 34, sv::kSound, text);
sv::small(fb, bx + 8, by + 45, pal::muted, kEffectWhat[int(st.effect)]);
if (mood == Parrot::Recording) {
const float secs = (lab.mic().written() - st.recFrom) / float(lab.mic().rate());
snprintf(text, sizeof(text), "REC %.1f s", secs);
sv::bold(fb, 150, sv::kTop + 88, sv::kRec, text);
} else if (mood == Parrot::Talking) {
fb.drawRect(150, sv::kTop + 80, 160, 8, pal::dim);
fb.fillRect(151, sv::kTop + 81, int(lab.speaker().progress() * 158), 6, sv::kSound);
// The level against the trigger line.
const float above = lab.level().above();
sv::meter(fb, 150, sv::kTop + 100, 160, 10, lab.level(), 50, trig.config.startDb);
snprintf(text, sizeof(text), "%.0f dB, starts at %.0f", above, trig.config.startDb);
sv::small(fb, 150, sv::kTop + 114, pal::muted, text);
void drawEcho(SoundLab &lab, FrameBuffer &fb, float t) {
const bool on = st.live == Live::Echo;
char text[64];
sv::bold(fb, 10, sv::kTop + 20, on ? pal::teal : pal::muted, on ? "Echo ON" : "Echo off");
snprintf(text, sizeof(text), "delay %u ms - feedback %u %%", st.delayMs, st.feedbackPct);
sv::small(fb, 110, sv::kTop + 10, pal::ink, text);
// The taps: what one clap turns into. Each repeat feedback x quieter, delay apart.
const int x0 = 14, y0 = sv::kTop + 34, h = 54, w = 292;
fb.drawFastHLine(x0, y0 + h, w, pal::dim);
fb.fillRect(x0, y0, 3, h, pal::ink); // the clap itself (in the room)
float a = 1;
for (int k = 1; k < 12; k++) {
const int x = x0 + k * st.delayMs * w / 2000;
if (x > x0 + w) break;
const int hh = int(h * a);
if (hh > 0) fb.fillRect(x, y0 + h - hh, 3, hh, sv::kSound);
a *= st.feedbackPct / 100.0f;
sv::small(fb, x0, y0 + h + 3, pal::muted, "one clap -> its echoes over 2 s");
// Meters and latency.
const float latency = st.tickMs + lab.speaker().streamQueued() * 1000.0f / kRate + 16;
snprintf(text, sizeof(text), "in %.0f dBFS out %.0f dBFS latency ~%.0f ms", st.inDb, st.outDb, latency);
sv::small(fb, 10, sv::kTop + 104, on ? pal::ink : pal::muted, text);
if (millis() < st.howlUntil && fmodf(t, 0.5f) < 0.35f) sv::bold(fb, 10, sv::kTop + 126, pal::coral, "Howl! Move the speaker away");
else sv::small(fb, 10, sv::kTop + 120, pal::muted, "speaker away from the mic, vol low");
void drawRobot(SoundLab &lab, FrameBuffer &fb, float t) {
const bool on = st.live == Live::Robot;
// A robot head; its mouth lights with what comes out of the speaker.
const int cx = 80, cy = sv::kTop + 62;
fb.fillRoundRect(cx - 44, cy - 44, 88, 84, 10, rgb(110, 130, 150));
fb.drawFastVLine(cx, cy - 58, 14, pal::muted);
fb.fillCircle(cx, cy - 60, 4, on && fmodf(t, 1.0f) < 0.5f ? pal::coral : pal::dim);
fb.fillCircle(cx - 18, cy - 14, 9, pal::night), fb.fillCircle(cx + 18, cy - 14, 9, pal::night);
fb.fillCircle(cx - 18, cy - 14, 4, on ? pal::teal : pal::dim), fb.fillCircle(cx + 18, cy - 14, 4, on ? pal::teal : pal::dim);
const float lvl = on ? lab.speaker().outputLevel() : 0;
for (int i = 0; i < 7; i++) {
const int hh = 4 + int(lvl * 18 * (0.6f + 0.4f * sinf(i * 1.7f + t * 20)));
fb.fillRect(cx - 30 + i * 9, cy + 18 - hh / 2, 6, std::max(2, hh), on ? sv::kSound : pal::dim);
sv::bold(fb, 150, sv::kTop + 20, on ? pal::teal : pal::muted, on ? "Robot ON" : "Robot off");
snprintf(text, sizeof(text), "carrier %.0f Hz", st.carrierHz);
sv::small(fb, 150, sv::kTop + 36, pal::ink, text);
sv::small(fb, 150, sv::kTop + 52, pal::muted, "voice x a slow sine:");
sv::small(fb, 150, sv::kTop + 63, pal::muted, "every f becomes f-c, f+c");
sv::small(fb, 150, sv::kTop + 74, pal::muted, "+ 8-bit crunch");
if (millis() < st.howlUntil) sv::bold(fb, 150, sv::kTop + 100, pal::coral, "Howl! Stopped");
else sv::small(fb, 150, sv::kTop + 100, pal::muted, "speaker away from the mic");
void drawScope(SoundLab &lab, FrameBuffer &fb, float) {
// Input on top, output below, the chain on the left.
static int16_t in[320], out[320];
if (st.live == Live::None) {
lab.mic().latest(in, 320);
// Talking: the part of the effect's sound the speaker is on now.
if (st.parrot == Parrot::Talking && st.outLen) {
const int16_t *src = (st.effect == Effect::Robot || st.effect == Effect::Echo || st.effect == Effect::Reverse) ? st.out : st.rec;
const uint32_t n = (st.effect == Effect::Robot || st.effect == Effect::Echo || st.effect == Effect::Reverse) ? st.outLen : st.recLen;
const uint32_t at = std::min<uint32_t>(uint32_t(lab.speaker().progress() * n), n > 320 ? n - 320 : 0);
std::copy(src + at, src + std::min(n, at + 320), out);
} else std::fill_n(out, 320, int16_t(0));
} else {
std::copy(st.scopeIn, st.scopeIn + 320, in);
std::copy(st.scopeOut, st.scopeOut + 320, out);
const bool active = st.live != Live::None || st.parrot == Parrot::Talking || st.parrot == Parrot::Recording;
const char *mid = st.live == Live::Echo ? "delay" : st.live == Live::Robot ? "ring mod" : kEffectNames[int(st.effect)];
const char *boxes[3] = {"mic", mid, "speaker"};
const int y = sv::kTop + 8 + i * 42;
fb.fillRoundRect(6, y, 76, 26, 6, active ? mix(sv::kPanel, sv::kSound, 70) : sv::kPanel);
sv::small(fb, 12, y + 10, active ? pal::ink : pal::muted, boxes[i]);
if (i < 2) fb.fillTriangle(40, y + 30, 48, y + 30, 44, y + 38, active ? sv::kSound : pal::dim);
sv::small(fb, 92, sv::kTop + 4, pal::muted, "in");
sv::wave(fb, 92, sv::kTop + 14, 222, 50, in, 320, sv::autoScale(in, 320, 512), pal::teal);
sv::small(fb, 92, sv::kTop + 68, pal::muted, "out");
sv::wave(fb, 92, sv::kTop + 78, 222, 50, out, 320, sv::autoScale(out, 320, 512), sv::kSound);
const SoundView kViews[] = {
{"parrot", drawParrotView, "fx next"},
{"echo", drawEcho, "echo"},
{"robot", drawRobot, "robot"},
{"scope", drawScope, "again"},
};
// ---- Commands ---------------------------------------------------------------------------------
void cmdFx(SoundLab &lab, const String &, const String &lower) {
if (lower == "next") st.effect = Effect((int(st.effect) + 1) % kEffects);
int found = -1;
for (int i = 0; i < kEffects; i++)
if (lower == kEffectNames[i]) found = i;
if (found < 0) return lab.warn("fx normal | chipmunk | giant | robot | echo | reverse | next");
st.effect = Effect(found);
lab.note(sv::Note::Info, "voice: %s (%s)", kEffectNames[int(st.effect)], kEffectWhat[int(st.effect)]);
void cmdAgain(SoundLab &lab, const String &, const String &) {
if (st.recLen == 0) return lab.warn("nothing recorded yet: say something first");
if (st.parrot == Parrot::Recording || st.live != Live::None) return lab.warn("busy");
void cmdEcho(SoundLab &lab, const String &, const String &lower) {
const bool on = lower.isEmpty() ? st.live != Live::Echo : lower == "on";
setLive(lab, on ? Live::Echo : Live::None);
lab.showView("echo");
void cmdRobot(SoundLab &lab, const String &, const String &lower) {
const bool on = lower.isEmpty() ? st.live != Live::Robot : lower == "on";
setLive(lab, on ? Live::Robot : Live::None);
lab.showView("robot");
void cmdDelay(SoundLab &lab, const String &arg, const String &) {
long ms = 0;
if (!SoundLab::number(arg, 20, 500, ms)) return lab.warn("delay <20-500 ms> (now %u)", st.delayMs);
st.delayMs = uint16_t(ms);
void cmdFeedback(SoundLab &lab, const String &arg, const String &) {
long pct = 0;
if (!SoundLab::number(arg, 0, 70, pct)) return lab.warn("feedback <0-70 %%> (now %u): how much of each echo echoes again", st.feedbackPct);
st.feedbackPct = uint8_t(pct);
void cmdCarrier(SoundLab &lab, const String &arg, const String &) {
float hz = 0;
if (!SoundLab::decimal(arg, 10, 300, hz)) return lab.warn("carrier <10-300 Hz> (now %.0f): 30-80 = robot, 150+ = bell-like", st.carrierHz);
st.carrierHz = hz;
ringMod.set(hz, kRate);
void cmdTrigger(SoundLab &lab, const String &arg, const String &) {
float db = 0;
if (!SoundLab::decimal(arg, 4, 40, db)) return lab.warn("trigger <dB above the quiet> 4-40 (now %.0f)", trig.config.startDb);
trig.config.startDb = db;
void cmdParrot(SoundLab &lab, const String &, const String &lower) {
st.parrotOn = lower.isEmpty() ? !st.parrotOn : lower == "on";
if (st.parrot != Parrot::Talking) listen(lab);
lab.note(sv::Note::Info, st.parrotOn ? "the parrot listens" : "the parrot rests");
// Long press: start or stop a recording by hand.
void cmdRec(SoundLab &lab, const String &, const String &) {
if (st.live != Live::None || st.parrot == Parrot::Talking) return lab.warn("busy");
if (st.parrot == Parrot::Recording) return handle(lab, trig.stop(lab.mic().written()));
handle(lab, trig.start(lab.mic().written()));
const SoundCommand kCommands[] = {
// ---- Hooks ------------------------------------------------------------------------------------
void setup(SoundLab &lab) {
st.rec = static_cast<int16_t *>(heap_caps_malloc(kMaxRec * sizeof(int16_t), MALLOC_CAP_SPIRAM));
st.out = static_cast<int16_t *>(heap_caps_malloc((kMaxRec + kTail) * sizeof(int16_t), MALLOC_CAP_SPIRAM));
delayBuf = static_cast<int16_t *>(heap_caps_calloc(kMaxDelay, sizeof(int16_t), MALLOC_CAP_SPIRAM));
if (!st.rec || !st.out || !delayBuf) {
log_e("S8: no PSRAM for the buffers");
st.parrotOn = false;
delayLine.begin(delayBuf, kMaxDelay);
trig.setRate(lab.mic().rate());
trig.config.startDb = 12;
trig.config.holdMs = 700;
trig.config.prerollMs = 300;
trig.config.maxMs = 6000;
trig.config.minMs = 300;
if (!lab.mic().digital()) lab.note(sv::Note::Warn, "KY-037: it works, but an INMP441 sounds much clearer");
void frame(SoundLab &lab, const snd::Frame &f, uint32_t) {
if (st.parrot != Parrot::Listening && st.parrot != Parrot::Recording) return;
if (lab.meter().learning()) return;
handle(lab, trig.push(lab.level().above(), f.end));
void tick(SoundLab &lab) {
const uint32_t now = millis();
if (st.lastTick) st.tickMs += (float(now - st.lastTick) - st.tickMs) * 0.1f;
st.lastTick = now;
if (st.live != Live::None) return runLive(lab);
switch (st.parrot) {
case Parrot::Recording:
copyUpTo(lab, lab.mic().written());
if (st.recLen >= kMaxRec) handle(lab, trig.stop(lab.mic().written()));
case Parrot::Talking:
if (!lab.speaker().busy()) st.parrot = Parrot::Resting, st.restUntil = now + 300;
case Parrot::Resting:
// 300 ms after the speaker went quiet: the room has stopped ringing.
if (now >= st.restUntil && !lab.speaker().recentlyLoud(300)) listen(lab);
default: break;
const SoundLabSpec kSpec{
"sound-lab-s8", "S8", "Voice FX", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,
kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, frame, tick, nullptr, "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-s8 includes C++ source files covering resampling = pitch + speed, ring modulation f±c, bitcrush, delay + feedback, loop gain < 1, howl detection, latency budget, echo guard.