diff --git a/BoomCast_Waveshare_Arduino/boomcast_audio.cpp b/BoomCast_Waveshare_Arduino/boomcast_audio.cpp index 0938c2f..14bc03a 100644 --- a/BoomCast_Waveshare_Arduino/boomcast_audio.cpp +++ b/BoomCast_Waveshare_Arduino/boomcast_audio.cpp @@ -32,7 +32,6 @@ bool BoomAudio::begin(BoomQueue* queue) { return false; } - startedMs_ = millis(); xTaskCreatePinnedToCore(taskThunk, "boom_audio", 8192, this, 5, &task_, 0); Serial.println("[Audio] detector task started"); return true; @@ -70,6 +69,7 @@ void BoomAudio::taskLoop() { } ringFilled_ = 0; ringWrite_ = 0; + detector_.reset(); boomState.setAudioDiagnostics(failedReads_, recordingErrors_); if (failedReads_ % 100 == 1) Serial.printf("[Audio] read errors=%lu dropped=%lu recording errors=%lu\n", (unsigned long)failedReads_, (unsigned long)droppedSamples_, (unsigned long)recordingErrors_); @@ -102,51 +102,22 @@ void BoomAudio::updateWaveform(const int16_t* mono, size_t frames) { boomState.setWaveform(points, BOOMCAST_WAVEFORM_POINTS); } -bool BoomAudio::shouldTrigger(float rms, int32_t peak) const { - float t = sensitivity_ / 100.0f; - - // At ~55 this stays close to the Atom Echo prototype's 2.8x RMS / 8x peak - // philosophy, while the slider gives useful room in both directions. - float rmsMult = 4.2f - (2.4f * t); // 4.18 .. 1.80 - float peakMult = 10.0f - (5.0f * t); // 9.95 .. 5.00 - float rmsFloor = 1400.0f - (900.0f * t);// 1391 .. 500 - float peakFloor = 9000.0f - (5000.0f * t);// 8950 .. 4000 - - float rmsThreshold = max(rmsFloor, noiseRms_ * rmsMult); - float peakThreshold = max(peakFloor, noisePeak_ * peakMult); - return rms >= rmsThreshold || peak >= peakThreshold; -} - void BoomAudio::processMonoBlock(const int16_t* mono, size_t frames) { updateWaveform(mono, frames); - - double squareSum = 0.0; - int32_t peak = 0; for (size_t i = 0; i < frames; ++i) { - int32_t v = mono[i]; - int32_t av = abs(v); - if (av > peak) peak = av; - squareSum += double(v) * double(v); - } - float rms = sqrt(squareSum / (frames ? frames : 1)); - - if (capturing_) { - appendCapture(mono, frames); - return; - } - - bool warm = (millis() - startedMs_) >= AUDIO_WARMUP_MS; - bool cooledDown = (millis() - lastTriggerMs_) >= TRIGGER_COOLDOWN_MS; - bool trigger = warm && cooledDown && shouldTrigger(rms, peak); - - if (trigger) { - beginCapture(mono, frames); - } else { - // Learn normal ambient level slowly, but avoid teaching the detector that a - // transient bang is the new normal. - if (rms < noiseRms_ * 2.5f) noiseRms_ += (rms - noiseRms_) * NOISE_EMA_ALPHA; - if (peak < noisePeak_ * 2.5f) noisePeak_ += (peak - noisePeak_) * NOISE_EMA_ALPHA; - pushRing(mono, frames); + const int16_t sample = mono[i]; + // The slider changes the effective input level; the worklet thresholds stay fixed. + float normalised = (float(sample) / 32768.0f) * AUDIO_MIC_NORMALISED_GAIN + * (float(sensitivity_) / AUDIO_SENSITIVITY_REFERENCE); + normalised = constrain(normalised, -1.0f, 1.0f); + if (!capturing_) pushRing(&sample, 1); + const BoomDetector::Result result = detector_.process(normalised); + if (result == BoomDetector::Result::Trigger) beginCapture(); + else if (capturing_ && result != BoomDetector::Result::TailDone) appendCapture(sample); + if (result == BoomDetector::Result::TailDone) { + appendCapture(sample); + finishCapture(); + } } } @@ -158,7 +129,7 @@ void BoomAudio::pushRing(const int16_t* mono, size_t frames) { } } -void BoomAudio::beginCapture(const int16_t* current, size_t frames) { +void BoomAudio::beginCapture() { captureWrite_ = 0; captureTarget_ = min(CAPTURE_SAMPLES, ringFilled_ + size_t(AUDIO_SAMPLE_RATE * POST_EVENT_MS / 1000)); struct timeval now; gettimeofday(&now, nullptr); @@ -166,7 +137,6 @@ void BoomAudio::beginCapture(const int16_t* current, size_t frames) { captureStartedMs_ = triggeredMs_ ? triggeredMs_ - (ringFilled_ * 1000 / AUDIO_SAMPLE_RATE) : 0; capturing_ = true; boomState.setCapturing(true); - lastTriggerMs_ = millis(); // Copy the rolling pre-trigger buffer in chronological order. size_t count = ringFilled_; @@ -174,15 +144,10 @@ void BoomAudio::beginCapture(const int16_t* current, size_t frames) { for (size_t i = 0; i < count && captureWrite_ < captureTarget_; ++i) { capture_[captureWrite_++] = ring_[(start + i) % PRE_EVENT_SAMPLES]; } - appendCapture(current, frames); } -void BoomAudio::appendCapture(const int16_t* mono, size_t frames) { - size_t room = captureTarget_ - captureWrite_; - size_t take = min(room, frames); - memcpy(capture_ + captureWrite_, mono, take * sizeof(int16_t)); - captureWrite_ += take; - if (captureWrite_ >= captureTarget_) finishCapture(); +void BoomAudio::appendCapture(int16_t sample) { + if (captureWrite_ < captureTarget_) capture_[captureWrite_++] = sample; } void BoomAudio::finishCapture() { @@ -197,12 +162,10 @@ void BoomAudio::finishCapture() { Serial.printf("[Audio] queue error; recording errors=%lu\n", (unsigned long)recordingErrors_); } - // Re-seed the rolling buffer with the tail of the just-finished capture so - // the next trigger still has sensible pre-roll context. + // Browser reset clears its rolling audio and starts a fresh 3 s cooldown. ringFilled_ = 0; ringWrite_ = 0; - size_t start = captureWrite_ > PRE_EVENT_SAMPLES ? captureWrite_ - PRE_EVENT_SAMPLES : 0; - pushRing(capture_ + start, captureWrite_ - start); + detector_.reset(); captureWrite_ = 0; capturing_ = false; diff --git a/BoomCast_Waveshare_Arduino/boomcast_audio.h b/BoomCast_Waveshare_Arduino/boomcast_audio.h index 7edb9c9..53d5278 100644 --- a/BoomCast_Waveshare_Arduino/boomcast_audio.h +++ b/BoomCast_Waveshare_Arduino/boomcast_audio.h @@ -1,6 +1,8 @@ #pragma once #include #include "boomcast_queue.h" +#include "boomcast_config.h" +#include "boomcast_detector.h" class BoomAudio { public: @@ -22,20 +24,21 @@ private: uint32_t droppedSamples_ = 0; uint32_t recordingErrors_ = 0; bool capturing_ = false; - uint32_t lastTriggerMs_ = 0; - uint32_t startedMs_ = 0; - float noiseRms_ = 500.0f; - float noisePeak_ = 3000.0f; + BoomDetector detector_{AUDIO_SAMPLE_RATE, + (AUDIO_SAMPLE_RATE + DETECTION_WINDOWS_PER_SECOND / 2) / DETECTION_WINDOWS_PER_SECOND, + AUDIO_SAMPLE_RATE * POST_EVENT_MS / 1000, + DETECTION_WARMUP_SAMPLES, DETECTION_COOLDOWN_SAMPLES, + DETECTION_INITIAL_BASELINE, DETECTION_BASELINE_ALPHA, DETECTION_BASELINE_CLAMP, + DETECTION_RMS_FLOOR, DETECTION_RMS_MULTIPLIER, DETECTION_PEAK_FLOOR}; volatile uint8_t sensitivity_ = 55; static void taskThunk(void* arg); void taskLoop(); void processMonoBlock(const int16_t* mono, size_t frames); void pushRing(const int16_t* mono, size_t frames); - void beginCapture(const int16_t* current, size_t frames); - void appendCapture(const int16_t* mono, size_t frames); + void beginCapture(); + void appendCapture(int16_t sample); void finishCapture(); - bool shouldTrigger(float rms, int32_t peak) const; void updateWaveform(const int16_t* mono, size_t frames); }; diff --git a/BoomCast_Waveshare_Arduino/boomcast_audio_calibration.h b/BoomCast_Waveshare_Arduino/boomcast_audio_calibration.h new file mode 100644 index 0000000..1f3a596 --- /dev/null +++ b/BoomCast_Waveshare_Arduino/boomcast_audio_calibration.h @@ -0,0 +1,6 @@ +#pragma once + +// Hardware-specific settings shared with the C codec board support file. +// The V2 codec input gain comes from Waveshare's example and needs measurement. +#define BOOMCAST_CODEC_INPUT_GAIN_DB 45.0f +#define BOOMCAST_DETECTION_INPUT_GAIN 1.0f diff --git a/BoomCast_Waveshare_Arduino/boomcast_config.h b/BoomCast_Waveshare_Arduino/boomcast_config.h index f5bf1ea..bb01713 100644 --- a/BoomCast_Waveshare_Arduino/boomcast_config.h +++ b/BoomCast_Waveshare_Arduino/boomcast_config.h @@ -1,12 +1,13 @@ #pragma once #include +#include "boomcast_audio_calibration.h" // ----------------------------------------------------------------------------- // BoomCast firmware configuration // Target: Waveshare ESP32-S3-ePaper-1.54 V2 (non-touch) // ----------------------------------------------------------------------------- -#define BOOMCAST_FIRMWARE_VERSION "0.2.0-concept" +#define BOOMCAST_FIRMWARE_VERSION "0.3.0-concept" // Set to 180 when the board is physically mounted upside-down. // Supported by this sketch: 0 or 180. @@ -50,18 +51,27 @@ constexpr uint8_t AUDIO_MIC_SLOT = 0; constexpr uint32_t AUDIO_RAW_BLOCK_BYTES = 1024; constexpr uint32_t AUDIO_FRAMES_PER_BLOCK = AUDIO_RAW_BLOCK_BYTES / 4; // 16-bit stereo -constexpr uint32_t PRE_EVENT_MS = 2500; -constexpr uint32_t POST_EVENT_MS = 2500; +// Browser worklet reference: 10 s rolling clip, 2 s tail, 30 level windows/s. +constexpr uint32_t DETECTION_WINDOWS_PER_SECOND = 30; +constexpr float DETECTION_INITIAL_BASELINE = 0.01f; +constexpr float DETECTION_BASELINE_ALPHA = 0.02f; +constexpr float DETECTION_BASELINE_CLAMP = 2.0f; +constexpr float DETECTION_RMS_FLOOR = 0.075f; +constexpr float DETECTION_RMS_MULTIPLIER = 5.0f; +constexpr float DETECTION_PEAK_FLOOR = 0.16f; +constexpr uint32_t DETECTION_WARMUP_SAMPLES = AUDIO_SAMPLE_RATE; +constexpr uint32_t DETECTION_COOLDOWN_SAMPLES = AUDIO_SAMPLE_RATE * 3; +constexpr uint32_t PRE_EVENT_MS = 8000; +constexpr uint32_t POST_EVENT_MS = 2000; constexpr uint32_t CAPTURE_MS = PRE_EVENT_MS + POST_EVENT_MS; constexpr size_t PRE_EVENT_SAMPLES = (AUDIO_SAMPLE_RATE * PRE_EVENT_MS) / 1000; constexpr size_t CAPTURE_SAMPLES = (AUDIO_SAMPLE_RATE * CAPTURE_MS) / 1000; -// Starting point inherited from the Atom Echo concept. The web slider modifies -// software thresholds around these values rather than changing codec gain. +// Hardware calibration, separate from the browser algorithm. The default +// assumes codec PCM maps linearly to browser Float32 [-1,1]. Calibrate on V2. +constexpr float AUDIO_MIC_NORMALISED_GAIN = BOOMCAST_DETECTION_INPUT_GAIN; +constexpr float AUDIO_SENSITIVITY_REFERENCE = 55.0f; constexpr uint8_t DEFAULT_SENSITIVITY = 55; // 1..100 -constexpr uint32_t TRIGGER_COOLDOWN_MS = 1500; -constexpr uint32_t AUDIO_WARMUP_MS = 3000; -constexpr float NOISE_EMA_ALPHA = 0.008f; // Display behaviour. Dynamic text uses the panel's partial-refresh waveform. // A periodic full refresh reduces accumulated ghosting. diff --git a/BoomCast_Waveshare_Arduino/boomcast_detector.h b/BoomCast_Waveshare_Arduino/boomcast_detector.h new file mode 100644 index 0000000..080fd54 --- /dev/null +++ b/BoomCast_Waveshare_Arduino/boomcast_detector.h @@ -0,0 +1,76 @@ +#pragma once +#include +#include + +// Sample-driven equivalent of Django's burst-listener-worklet.js. +// Input samples must be normalised to the browser Float32 scale [-1, 1]. +class BoomDetector { +public: + enum class Result { None, Trigger, TailDone }; + + BoomDetector(uint32_t sampleRate, uint32_t windowSamples, uint32_t tailSamples, + uint32_t warmupSamples, uint32_t cooldownSamples, + float initialBaseline, float baselineAlpha, float baselineClamp, + float rmsFloor, float rmsMultiplier, float peakFloor) + : sampleRate_(sampleRate), windowSamples_(windowSamples), tailSamples_(tailSamples), + warmupSamples_(warmupSamples), cooldownSamples_(cooldownSamples), + initialBaseline_(initialBaseline), baselineAlpha_(baselineAlpha), + baselineClamp_(baselineClamp), rmsFloor_(rmsFloor), + rmsMultiplier_(rmsMultiplier), peakFloor_(peakFloor), baseline_(initialBaseline) { + lastTrigger_ = -int64_t(sampleRate_) * 10; + } + + Result process(float sample) { + ++frame_; + energy_ += double(sample) * sample; + const float absolute = std::fabs(sample); + if (absolute > peak_) peak_ = absolute; + ++interval_; + + // The worklet decrements its tail counter before checking the level window. + if (tailRemaining_ && --tailRemaining_ == 0) return Result::TailDone; + if (interval_ < windowSamples_) return Result::None; + + const float rms = std::sqrt(energy_ / interval_); + lastRms_ = rms; + if (!tailRemaining_) { + if (frame_ > warmupSamples_ && int64_t(frame_) - lastTrigger_ > cooldownSamples_ + && rms > std::fmax(rmsFloor_, baseline_ * rmsMultiplier_) + && peak_ > peakFloor_) { + tailRemaining_ = tailSamples_; + lastTrigger_ = frame_; + clearWindow(); + return Result::Trigger; + } + baseline_ = baseline_ * (1.0f - baselineAlpha_) + + std::fmin(rms, baseline_ * baselineClamp_) * baselineAlpha_; + } + clearWindow(); + return Result::None; + } + + void reset() { + clearWindow(); + baseline_ = initialBaseline_; + lastTrigger_ = frame_; + tailRemaining_ = 0; + } + + float baseline() const { return baseline_; } + float lastRms() const { return lastRms_; } + uint64_t frame() const { return frame_; } + bool inTail() const { return tailRemaining_ != 0; } + +private: + void clearWindow() { interval_ = 0; energy_ = 0; peak_ = 0; } + uint32_t sampleRate_, windowSamples_, tailSamples_, warmupSamples_, cooldownSamples_; + float initialBaseline_, baselineAlpha_, baselineClamp_, rmsFloor_, rmsMultiplier_, peakFloor_; + uint64_t frame_ = 0; + int64_t lastTrigger_ = 0; + uint32_t interval_ = 0; + uint32_t tailRemaining_ = 0; + double energy_ = 0; + float peak_ = 0; + float baseline_; + float lastRms_ = 0; +}; diff --git a/tests/__pycache__/test_detector_parity.cpython-313.pyc b/tests/__pycache__/test_detector_parity.cpython-313.pyc new file mode 100644 index 0000000..dc56de8 Binary files /dev/null and b/tests/__pycache__/test_detector_parity.cpython-313.pyc differ diff --git a/tests/detector_trace.cpp b/tests/detector_trace.cpp new file mode 100644 index 0000000..683649a --- /dev/null +++ b/tests/detector_trace.cpp @@ -0,0 +1,20 @@ +#include "../BoomCast_Waveshare_Arduino/boomcast_detector.h" +#include +#include + +int main() { + BoomDetector detector(16000, 533, 32000, 16000, 48000, + 0.01f, 0.02f, 2.0f, 0.075f, 5.0f, 0.16f); + int16_t sample; + uint64_t frame = 0; + while (std::fread(&sample, sizeof(sample), 1, stdin) == 1) { + ++frame; + auto result = detector.process(float(sample) / 32768.0f); + if (result == BoomDetector::Result::Trigger) std::printf("T %llu\n", (unsigned long long)frame); + if (result == BoomDetector::Result::TailDone) { + std::printf("E %llu\n", (unsigned long long)frame); + detector.reset(); + } + } + std::printf("B %.8f\n", detector.baseline()); +} diff --git a/tests/test_detector_parity.py b/tests/test_detector_parity.py new file mode 100644 index 0000000..a32ca6a --- /dev/null +++ b/tests/test_detector_parity.py @@ -0,0 +1,70 @@ +"""Compare the firmware's detector with the unchanged browser AudioWorklet.""" +from array import array +from pathlib import Path +import math +import random +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +REFERENCE = ROOT.parent / "django/frontend/src/scripts/burst-listener-worklet.js" +CPP = Path(__file__).with_name("detector_trace.cpp") +JS = Path(__file__).with_name("worklet_trace.cjs") +RATE = 16000 + + +def samples(kind): + rng = random.Random(71) + length = RATE * 13 + values = [0] * length + if kind in {"kitchen", "impulse", "repeated", "overlap"}: + for i in range(length): + values[i] = round(rng.uniform(-0.012, 0.012) * 32767) + def bang(at, duration=.05, amplitude=.8): + start = round(at * RATE) + for j in range(round(duration * RATE)): + if start + j < length: + values[start + j] = round(amplitude * 32767 * math.sin(j * .28)) + if kind == "kitchen": + for at in (2, 4, 7): + bang(at, .15, .12) # below the browser's peak gate + if kind == "impulse": bang(4) + if kind == "repeated": + bang(4) + bang(4.3) + bang(10) + if kind == "overlap": + bang(4, .2) + bang(4.1, .2, .5) + return values + + +class DetectorParity(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.directory = tempfile.TemporaryDirectory() + cls.exe = Path(cls.directory.name) / "detector_trace" + subprocess.run(["g++", "-std=c++17", "-O2", str(CPP), "-o", str(cls.exe)], check=True) + + @classmethod + def tearDownClass(cls): + cls.directory.cleanup() + + def test_reference_traces(self): + for kind in ("silence", "kitchen", "impulse", "repeated", "overlap"): + with self.subTest(kind=kind): + path = Path(self.directory.name) / f"{kind}.pcm" + raw = array('h', samples(kind)) + if sys.byteorder != 'little': raw.byteswap() + path.write_bytes(raw.tobytes()) + firmware = subprocess.check_output([str(self.exe)], input=path.read_bytes(), text=False).decode().splitlines() + browser = subprocess.check_output(["node", str(JS), str(REFERENCE), str(path)], text=True).splitlines() + self.assertEqual(firmware[:-1], browser[:-1]) + self.assertAlmostEqual(float(firmware[-1][2:]), float(browser[-1][2:]), delta=0.00002) + if kind == "silence" or kind == "kitchen": self.assertFalse(firmware[:-1]) + else: self.assertIn("T ", firmware[0]) + + +if __name__ == "__main__": unittest.main() diff --git a/tests/worklet_trace.cjs b/tests/worklet_trace.cjs new file mode 100644 index 0000000..48c9f1f --- /dev/null +++ b/tests/worklet_trace.cjs @@ -0,0 +1,31 @@ +const fs = require('fs'); +const vm = require('vm'); +const source = fs.readFileSync(process.argv[2], 'utf8'); +let Processor; +class AudioWorkletProcessor { + constructor() { this.port = { postMessage(message) { messages.push({ type: message.type, frame: processor.frame }); } }; } +} +let messages = []; +vm.runInNewContext(source, { + sampleRate: 16000, AudioWorkletProcessor, + registerProcessor(name, implementation) { Processor = implementation; }, + Float32Array, Math, +}); +const processor = new Processor(); +const bytes = fs.readFileSync(process.argv[3]); +for (let offset = 0; offset < bytes.length;) { + const length = Math.min(128, (bytes.length - offset) / 2); + const channel = new Float32Array(length); + for (let i = 0; i < length; i++) channel[i] = bytes.readInt16LE(offset + i * 2) / 32768; + messages = []; + processor.process([[channel]]); + for (const message of messages) { + if (message.type === 'trigger') process.stdout.write(`T ${message.frame}\n`); + if (message.type === 'clip') { + process.stdout.write(`E ${message.frame}\n`); + processor.reset(); + } + } + offset = processor.frame * 2; +} +process.stdout.write(`B ${processor.baseline.toFixed(8)}\n`);