adding various improvements to the experience

This commit is contained in:
Joren de Graaf
2026-10-10 00:34:14 +02:00
parent 9ec360d736
commit 7f5ef0b61b
9 changed files with 250 additions and 71 deletions
+19 -56
View File
@@ -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;