adding various improvements to the experience
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#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.
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
// 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;
|
||||
};
|
||||
Reference in New Issue
Block a user