M314-style radar: dual-mic blips, sweep reveal, fade lifecycle
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
#define MIC_I2S_PORT I2S_NUM_1
|
||||
#define MIC_BLOCK_FRAMES 256 // 16 ms at 16 kHz
|
||||
#define MIC_SLOTS 4 // 4-slot TDM
|
||||
#define MIC_GAIN GAIN_6DB
|
||||
#define MIC_GAIN GAIN_30DB
|
||||
|
||||
// Set to 1 to print detected levels while tuning the microphones on hardware.
|
||||
#ifndef SOUND_DETECTOR_DEBUG
|
||||
@@ -43,10 +43,10 @@ static constexpr float ENV_ATTACK = 0.55f;
|
||||
static constexpr float ENV_RELEASE = 0.18f;
|
||||
static constexpr float FLOOR_FALL = 0.12f; // fast follow when quieter
|
||||
static constexpr float FLOOR_RISE = 0.0008f; // very slow drift upward
|
||||
static constexpr float FLOOR_MARGIN = 1.25f;
|
||||
static constexpr float FLOOR_MARGIN = 1.0f;
|
||||
static constexpr float ABS_GATE = 0.0008f;
|
||||
static constexpr float BALANCE_DEADBAND = 0.02f;
|
||||
static constexpr float BALANCE_GAIN = 1.8f; // widen the usable stereo range
|
||||
static constexpr float BALANCE_DEADBAND = 0.005f;
|
||||
static constexpr float BALANCE_GAIN = 4.0f; // widen the usable stereo range
|
||||
static constexpr float LOUD_MIN_DB = -55.0f;
|
||||
static constexpr float LOUD_MAX_DB = -6.0f;
|
||||
static constexpr uint32_t HOLD_MS = 260;
|
||||
@@ -63,7 +63,7 @@ static constexpr float SLOT_MIN_RISE = 0.002f; // slow, so gaps keep the flo
|
||||
|
||||
// A channel parked at the rails (overdriven analogue gain) carries no usable
|
||||
// audio; ignore it rather than reporting a permanent maximum.
|
||||
static constexpr float RAIL_LEVEL = 0.50f;
|
||||
static constexpr float RAIL_LEVEL = 0.95f;
|
||||
|
||||
// The codec always delivers four TDM slots, but only two are fitted. The
|
||||
// power-up transient is very loud and can be mistaken for signal.
|
||||
@@ -213,9 +213,9 @@ void SoundDetector::taskLoop()
|
||||
// Still drain I2S so the DMA doesn't overflow while muted. Drop the
|
||||
// envelopes too, otherwise the device's own ping or the ADC
|
||||
// power-up transient would be the first thing we report.
|
||||
envL_ = 0.0f;
|
||||
envR_ = 0.0f;
|
||||
publish(0.0f, 0.0f, 0.0f, 0.0f, false);
|
||||
for (int s = 0; s < MIC_SLOTS; s++) { env_[s] = 0.0f; floor_[s] = 0.01f; }
|
||||
float z[4] = {0,0,0,0};
|
||||
publish(0.0f, 0.0f, z, 0.0f, 0.0f, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -338,10 +338,7 @@ void SoundDetector::updateSlots(const float *rms)
|
||||
leftSlot_ = lo;
|
||||
rightSlot_ = hi;
|
||||
candCount_ = 0;
|
||||
envL_ = 0.0f;
|
||||
envR_ = 0.0f;
|
||||
floorL_ = 0.01f;
|
||||
floorR_ = 0.01f;
|
||||
for (int s = 0; s < MIC_SLOTS; s++) { env_[s] = 0.0f; floor_[s] = 0.01f; }
|
||||
Serial.printf("Sound detector mic slots L=%d R=%d "
|
||||
"(swing %.4f %.4f %.4f %.4f)\n",
|
||||
leftSlot_, rightSlot_,
|
||||
@@ -389,46 +386,38 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount)
|
||||
}
|
||||
#endif
|
||||
|
||||
float rmsL = rms[MIC_SWAP_LR ? rightSlot_ : leftSlot_];
|
||||
float rmsR = rms[MIC_SWAP_LR ? leftSlot_ : rightSlot_];
|
||||
if (rmsL > RAIL_LEVEL) rmsL = 0.0f;
|
||||
if (rmsR > RAIL_LEVEL) rmsR = 0.0f;
|
||||
|
||||
// Normalise to full scale, then smooth with a fast attack / slow release.
|
||||
envL_ += (rmsL - envL_) * (rmsL > envL_ ? ENV_ATTACK : ENV_RELEASE);
|
||||
envR_ += (rmsR - envR_) * (rmsR > envR_ ? ENV_ATTACK : ENV_RELEASE);
|
||||
|
||||
// Per-channel noise floor: fall quickly when it gets quieter, and only
|
||||
// creep up while the input is actually close to the floor. Without that
|
||||
// guard a sustained loud sound (or a clipped channel) drags the floor up to
|
||||
// its own level and the detector goes permanently deaf.
|
||||
if (envL_ < floorL_) {
|
||||
floorL_ += (envL_ - floorL_) * FLOOR_FALL;
|
||||
} else if (envL_ < floorL_ * 1.5f) {
|
||||
floorL_ += (envL_ - floorL_) * FLOOR_RISE;
|
||||
// Monitor all four TDM slots independently. A railed slot is zeroed.
|
||||
float sig[MIC_SLOTS];
|
||||
float peak = 0.0f;
|
||||
bool present = false;
|
||||
for (int s = 0; s < MIC_SLOTS; s++) {
|
||||
float r = rms[s];
|
||||
if (r > RAIL_LEVEL) r = 0.0f;
|
||||
env_[s] += (r - env_[s]) * (r > env_[s] ? ENV_ATTACK : ENV_RELEASE);
|
||||
if (env_[s] < floor_[s]) {
|
||||
floor_[s] += (env_[s] - floor_[s]) * FLOOR_FALL;
|
||||
} else if (env_[s] < floor_[s] * 1.5f) {
|
||||
floor_[s] += (env_[s] - floor_[s]) * FLOOR_RISE;
|
||||
} else {
|
||||
floor_[s] += (env_[s] - floor_[s]) * 0.05f;
|
||||
}
|
||||
if (floor_[s] < 1e-5f) floor_[s] = 1e-5f;
|
||||
float v = env_[s] - floor_[s] * FLOOR_MARGIN;
|
||||
if (v < 0.0f) v = 0.0f;
|
||||
sig[s] = v;
|
||||
if (v > peak) peak = v;
|
||||
if (v > ABS_GATE) present = true;
|
||||
}
|
||||
if (envR_ < floorR_) {
|
||||
floorR_ += (envR_ - floorR_) * FLOOR_FALL;
|
||||
} else if (envR_ < floorR_ * 1.5f) {
|
||||
floorR_ += (envR_ - floorR_) * FLOOR_RISE;
|
||||
}
|
||||
if (floorL_ < 1e-5f) floorL_ = 1e-5f;
|
||||
if (floorR_ < 1e-5f) floorR_ = 1e-5f;
|
||||
|
||||
float sigL = envL_ - floorL_ * FLOOR_MARGIN;
|
||||
float sigR = envR_ - floorR_ * FLOOR_MARGIN;
|
||||
if (sigL < 0.0f) sigL = 0.0f;
|
||||
if (sigR < 0.0f) sigR = 0.0f;
|
||||
|
||||
// Adaptive gate: a sound must stand clearly above its channel's floor.
|
||||
float gateL = fmaxf(floorL_ * 1.5f, ABS_GATE);
|
||||
float gateR = fmaxf(floorR_ * 1.5f, ABS_GATE);
|
||||
bool present = (sigL > gateL) || (sigR > gateR);
|
||||
|
||||
uint32_t now = millis();
|
||||
if (present) lastActiveMs_ = now;
|
||||
bool active = present || (uint32_t)(now - lastActiveMs_) < HOLD_MS;
|
||||
|
||||
// Keep a left/right summary for compatibility (max of the two left-ish /
|
||||
// right-ish slots).
|
||||
float sigL = fmaxf(sig[0], sig[2]);
|
||||
float sigR = fmaxf(sig[1], sig[3]);
|
||||
|
||||
float total = sigL + sigR;
|
||||
float balance = 0.0f;
|
||||
if (total > 1e-6f) {
|
||||
@@ -436,9 +425,6 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount)
|
||||
if (fabsf(balance) < BALANCE_DEADBAND) {
|
||||
balance = 0.0f;
|
||||
} else {
|
||||
// Remove the deadband, rescale the remaining range to +/-1 and then
|
||||
// widen it: the two microphones are close together, so a sound at
|
||||
// the side only produces a few dB of level difference.
|
||||
float sign = balance < 0.0f ? -1.0f : 1.0f;
|
||||
balance = (balance - sign * BALANCE_DEADBAND) / (1.0f - BALANCE_DEADBAND);
|
||||
balance *= BALANCE_GAIN;
|
||||
@@ -447,7 +433,6 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount)
|
||||
}
|
||||
}
|
||||
|
||||
float peak = fmaxf(sigL, sigR);
|
||||
float db = 20.0f * log10f(peak + 1e-9f);
|
||||
float loudness = (db - LOUD_MIN_DB) / (LOUD_MAX_DB - LOUD_MIN_DB);
|
||||
if (loudness < 0.0f) loudness = 0.0f;
|
||||
@@ -457,25 +442,25 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount)
|
||||
static uint32_t lastPrintMs = 0;
|
||||
if ((uint32_t)(millis() - lastPrintMs) > 300) {
|
||||
lastPrintMs = millis();
|
||||
Serial.printf("mic rms %.4f %.4f %.4f %.4f | range %.4f %.4f %.4f %.4f\n",
|
||||
Serial.printf("mic rms %.4f %.4f %.4f %.4f | sig %.3f %.3f %.3f %.3f%s\n",
|
||||
rms[0], rms[1], rms[2], rms[3],
|
||||
slotMax_[0] - slotMin_[0], slotMax_[1] - slotMin_[1],
|
||||
slotMax_[2] - slotMin_[2], slotMax_[3] - slotMin_[3]);
|
||||
Serial.printf(" pair L%d R%d | sig %.3f %.3f | bal %+.2f loud %.2f%s\n",
|
||||
leftSlot_, rightSlot_, sigL, sigR, balance, loudness,
|
||||
sig[0], sig[1], sig[2], sig[3],
|
||||
active ? "" : " (quiet)");
|
||||
}
|
||||
#endif
|
||||
|
||||
publish(sigL, sigR, balance, loudness, active);
|
||||
publish(sigL, sigR, sig, balance, loudness, active);
|
||||
}
|
||||
|
||||
void SoundDetector::publish(float left, float right, float balance, float loudness,
|
||||
bool active)
|
||||
void SoundDetector::publish(float left, float right, const float *slots, float balance,
|
||||
float loudness, bool active)
|
||||
{
|
||||
portENTER_CRITICAL(&mux_);
|
||||
reading_.left = left;
|
||||
reading_.right = right;
|
||||
if (slots) {
|
||||
for (int s = 0; s < 4; s++) reading_.slots[s] = slots[s];
|
||||
}
|
||||
reading_.balance = balance;
|
||||
reading_.loudness = loudness;
|
||||
reading_.active = active;
|
||||
|
||||
Reference in New Issue
Block a user