Make microphone slot discovery self-healing

The ES7210 power-up transient is loud enough to look like the first
sound. It placed one centred blip right after init and then pinned the
mic pair to whichever slots were loudest at that instant. If that was
the wrong pair, later claps were ignored forever.

Ignore the first 800 ms after starting the codec, and re-evaluate the
mic pair on every clear sound instead of locking once. The pair only
changes after repeating for a few blocks, or immediately when the
current pair is silent while another one is loud. Also enable the
per-second rms/pair debug line so the slot mapping can be verified on
hardware.
This commit is contained in:
Dejvino
2026-11-12 14:10:17 +01:00
parent b8de153e5a
commit 72f80bfdd8
2 changed files with 73 additions and 40 deletions
+68 -36
View File
@@ -26,7 +26,7 @@
// Set to 1 to print detected levels while tuning the microphones on hardware.
#ifndef SOUND_DETECTOR_DEBUG
#define SOUND_DETECTOR_DEBUG 0
#define SOUND_DETECTOR_DEBUG 1
#endif
// Set to 1 if a sound on the left moves the blip to the right (mirrored pair).
@@ -47,11 +47,15 @@ static constexpr float LOUD_MIN_DB = -60.0f;
static constexpr float LOUD_MAX_DB = -20.0f;
static constexpr uint32_t HOLD_MS = 260;
// Slot discovery: keep a peak-hold score per TDM slot and lock the two loudest
// slots once a sound clearly stands above the noise on both of them.
static constexpr float SLOT_DECAY = 0.995f;
static constexpr float SLOT_LOCK_LEVEL = 0.002f;
static constexpr float SLOT_PAIR_RATIO = 0.20f;
// Slot discovery: the codec emits four TDM slots but only two microphones are
// fitted. Whenever a clear sound appears, take the two loudest slots as the mic
// pair. The current pair is only replaced after the new pair repeats, or
// immediately if the current pair is silent while the new one is loud.
static constexpr float SLOT_SELECT_LEVEL = 0.0020f;
static constexpr float SLOT_PAIR_RATIO = 0.10f;
static constexpr float SLOT_DEAD_RATIO = 0.25f;
static constexpr int SLOT_SWITCH_BLOCKS = 3;
static constexpr uint32_t WARMUP_MS = 800;
// ES7210 SDP interface 2 / high-pass registers. The LilyGO driver leaves the
// codec in 2-channel I2S mode and the HPF disabled; enable both here.
@@ -146,6 +150,10 @@ bool SoundDetector::begin(uint32_t sampleRate)
i2s_set_pin(MIC_I2S_PORT, &pins);
i2s_zero_dma_buffer(MIC_I2S_PORT);
// The ADC power-up transient is very loud; ignore it so it neither places a
// blip nor influences which slots are treated as microphones.
warmupUntil_ = millis() + WARMUP_MS;
running_ = true;
if (xTaskCreatePinnedToCore(taskEntry, "sound-detector", 8192, this, 4,
&task_, 0) != pdPASS) {
@@ -183,10 +191,11 @@ void SoundDetector::taskLoop()
size_t frames = bytesRead / (MIC_SLOTS * sizeof(int16_t));
if (frames == 0) continue;
if ((int32_t)(millis() - suppressUntil_) < 0) {
if ((int32_t)(millis() - warmupUntil_) < 0 ||
(int32_t)(millis() - suppressUntil_) < 0) {
// Still drain I2S so the DMA doesn't overflow while muted. Drop the
// envelopes too, otherwise the device's own ping would be the first
// thing the detector reports once the mute expires.
// envelopes too, otherwise the device's own ping or the ADC power-up
// transient would be the first thing the detector reports.
envL_ = 0.0f;
envR_ = 0.0f;
publish(0.0f, 0.0f, 0.0f, 0.0f, false);
@@ -215,31 +224,54 @@ void SoundDetector::processBlock(const int16_t *frames, size_t frameCount)
rms[s] = sqrtf(var) / 32768.0f;
}
// Learn which two slots carry the populated microphones: the loudest slots
// on a clear sound are the microphones, the others stay at noise level.
for (int s = 0; s < MIC_SLOTS; s++) {
slotScore_[s] = fmaxf(slotScore_[s] * SLOT_DECAY, rms[s]);
// Find the two loudest slots. With only two microphones fitted, a real
// sound shows up in exactly those slots; the other two stay near the noise
// floor. Re-evaluate on every clear sound so a wrong initial guess (for
// example caused by the codec's power-up transient) cannot stick forever.
int best = 0, second = 1;
if (rms[second] > rms[best]) {
int t = best; best = second; second = t;
}
if (!pairLocked_) {
int best = 0, second = 1;
if (slotScore_[second] > slotScore_[best]) {
int t = best; best = second; second = t;
for (int s = 2; s < MIC_SLOTS; s++) {
if (rms[s] > rms[best]) {
second = best;
best = s;
} else if (rms[s] > rms[second]) {
second = s;
}
for (int s = 2; s < MIC_SLOTS; s++) {
if (slotScore_[s] > slotScore_[best]) {
second = best;
best = s;
} else if (slotScore_[s] > slotScore_[second]) {
second = s;
}
if (rms[best] > SLOT_SELECT_LEVEL &&
rms[second] > rms[best] * SLOT_PAIR_RATIO) {
int lo = best < second ? best : second;
int hi = best < second ? second : best;
if (lo != leftSlot_ || hi != rightSlot_) {
float currentLevel = fmaxf(rms[leftSlot_], rms[rightSlot_]);
if (lo == candL_ && hi == candR_) {
candCount_++;
} else {
candL_ = lo;
candR_ = hi;
candCount_ = 1;
}
}
if (slotScore_[best] > SLOT_LOCK_LEVEL &&
slotScore_[second] > slotScore_[best] * SLOT_PAIR_RATIO) {
leftSlot_ = best < second ? best : second;
rightSlot_ = best < second ? second : best;
pairLocked_ = true;
Serial.printf("Sound detector locked mic slots L=%d R=%d\n",
leftSlot_, rightSlot_);
// Switch immediately when the current pair is effectively dead,
// otherwise require the new pair to repeat for a few blocks.
if (currentLevel < rms[best] * SLOT_DEAD_RATIO ||
candCount_ >= SLOT_SWITCH_BLOCKS) {
leftSlot_ = lo;
rightSlot_ = hi;
candCount_ = 0;
envL_ = 0.0f;
envR_ = 0.0f;
floorL_ = 0.01f;
floorR_ = 0.01f;
Serial.printf("Sound detector using mic slots L=%d R=%d "
"(rms %.4f %.4f %.4f %.4f)\n",
leftSlot_, rightSlot_,
rms[0], rms[1], rms[2], rms[3]);
}
} else {
candCount_ = 0;
}
}
@@ -297,13 +329,13 @@ void SoundDetector::processBlock(const int16_t *frames, size_t frameCount)
#if SOUND_DETECTOR_DEBUG
static uint32_t lastPrintMs = 0;
if (active && (uint32_t)(millis() - lastPrintMs) > 250) {
if ((uint32_t)(millis() - lastPrintMs) > 1000) {
lastPrintMs = millis();
Serial.printf("mic slots %.4f/%.4f/%.4f/%.4f L%d=%.4f R%d=%.4f "
"bal=%+.2f loud=%.2f\n",
Serial.printf("mic rms %.4f %.4f %.4f %.4f | pair L%d R%d | "
"bal %+.2f loud %.2f%s\n",
rms[0], rms[1], rms[2], rms[3],
leftSlot_, sigL, rightSlot_, sigR,
balance, loudness);
leftSlot_, rightSlot_, balance, loudness,
active ? "" : " (quiet)");
}
#endif
+5 -4
View File
@@ -56,10 +56,11 @@ private:
float floorR_ = 0.01f;
// The codec always delivers four TDM slots, but only two microphones are
// fitted. These are discovered at runtime from whichever slots carry the
// most signal.
// fitted. The pair is (re)discovered from the slots that carry signal.
int leftSlot_ = 0;
int rightSlot_ = 1;
bool pairLocked_ = false;
float slotScore_[4] = {0.0f, 0.0f, 0.0f, 0.0f};
int candL_ = -1;
int candR_ = -1;
int candCount_ = 0;
uint32_t warmupUntil_ = 0;
};