Fix microphone stereo detection on the T-Deck Plus
I2S_CHANNEL_FMT_ALL_LEFT copies a single channel into every slot, so both microphones read the same signal and the balance stayed at zero. Use I2S_CHANNEL_FMT_MULTIPLE (the real TDM mode) instead. The board only fits two of the four ES7210 mic footprints, but the schematic carries all four. Rather than hardcode MIC1/MIC3, the detector now measures the TDM slots and locks onto the two that carry signal, treating the lower slot as the left microphone. Equal maximum gain is applied to all inputs so the pair amplifies consistently. Also widen the balance range (claps produce only a few dB of difference between the closely spaced mics) and recalibrate loudness to the dBFS range seen with 37.5 dB analog gain.
This commit is contained in:
@@ -1079,7 +1079,7 @@ void setup()
|
||||
if (!soundDetector.begin(16000)) {
|
||||
Serial.println("Sound detector init failed (ES7210 mics)");
|
||||
} else {
|
||||
Serial.println("Sound detector ready (MIC1 left / MIC3 right)");
|
||||
Serial.println("Sound detector ready (dual mics, slots auto-detected)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
*
|
||||
* The T-Deck Plus has two populated MEMS microphones on the ES7210. The codec
|
||||
* outputs all four ADC channels as a 4-slot TDM stream on SDOUT1:
|
||||
* slot 0 = MIC1 (left), slot 2 = MIC3 (right)
|
||||
* We only need slots 0 and 2; slot 1/3 are ignored.
|
||||
* slot 0 = MIC1, slot 1 = MIC2, slot 2 = MIC3, slot 3 = MIC4
|
||||
* The board only fits two of the four footprints, and the schematic has all
|
||||
* four, so the detector finds the live pair at runtime (the first loud sound
|
||||
* identifies the two slots with signal) and treats the lower slot as left.
|
||||
*
|
||||
* The detector runs in its own FreeRTOS task so the radar loop never blocks on
|
||||
* I2S. It publishes smoothed, noise-floor-relative levels that the sketch turns
|
||||
@@ -19,16 +21,19 @@
|
||||
|
||||
#define MIC_I2S_PORT I2S_NUM_1
|
||||
#define MIC_BLOCK_FRAMES 256 // 16 ms at 16 kHz
|
||||
#define MIC_CHANNELS 4 // 4-slot TDM
|
||||
#define MIC_SLOT_LEFT 0 // MIC1
|
||||
#define MIC_SLOT_RIGHT 2 // MIC3
|
||||
#define MIC_GAIN GAIN_30DB
|
||||
#define MIC_SLOTS 4 // 4-slot TDM
|
||||
#define MIC_GAIN GAIN_37_5DB
|
||||
|
||||
// Set to 1 to print detected levels while tuning the microphones on hardware.
|
||||
#ifndef SOUND_DETECTOR_DEBUG
|
||||
#define SOUND_DETECTOR_DEBUG 0
|
||||
#endif
|
||||
|
||||
// Set to 1 if a sound on the left moves the blip to the right (mirrored pair).
|
||||
#ifndef MIC_SWAP_LR
|
||||
#define MIC_SWAP_LR 0
|
||||
#endif
|
||||
|
||||
// Envelope / detector tuning (per 16 ms block).
|
||||
static constexpr float ENV_ATTACK = 0.55f;
|
||||
static constexpr float ENV_RELEASE = 0.18f;
|
||||
@@ -36,11 +41,18 @@ 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 ABS_GATE = 0.0008f;
|
||||
static constexpr float BALANCE_DEADBAND = 0.06f;
|
||||
static constexpr float LOUD_MIN_DB = -48.0f;
|
||||
static constexpr float LOUD_MAX_DB = -6.0f;
|
||||
static constexpr float BALANCE_DEADBAND = 0.02f;
|
||||
static constexpr float BALANCE_GAIN = 1.8f; // widen the usable stereo range
|
||||
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;
|
||||
|
||||
// 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.
|
||||
static const uint8_t ES7210_SDP_INTERFACE2 = 0x12;
|
||||
@@ -74,7 +86,8 @@ bool SoundDetector::begin(uint32_t sampleRate)
|
||||
err |= es7210_adc_config_i2s(cfg.codec_mode, &cfg.i2s_iface);
|
||||
|
||||
// Put all four ADCs on SDOUT1 as TDM slots. Without 0x02 on this register
|
||||
// only MIC1/MIC2 reach SDOUT1 and the right microphone is missing.
|
||||
// the codec stays in 2-channel mode and MIC3/MIC4 are only sent on SDOUT2,
|
||||
// which is not wired to the ESP32.
|
||||
if (!es7210WriteReg(ES7210_SDP_INTERFACE2, 0x02)) {
|
||||
Serial.println("ES7210 TDM config failed");
|
||||
return false;
|
||||
@@ -86,10 +99,13 @@ bool SoundDetector::begin(uint32_t sampleRate)
|
||||
es7210WriteReg(ES7210_ADC34_HPF2, 0x0a);
|
||||
es7210WriteReg(ES7210_ADC34_HPF1, 0x2a);
|
||||
|
||||
// Use the same analog gain on both populated mics so the amplitude
|
||||
// difference between them is meaningful.
|
||||
// Use the same analog gain on all four inputs. Only two microphones are
|
||||
// fitted, but the populated pair is discovered at runtime, and equal gain
|
||||
// is required for a meaningful left/right amplitude comparison.
|
||||
err |= es7210_adc_set_gain(ES7210_INPUT_MIC1, MIC_GAIN);
|
||||
err |= es7210_adc_set_gain(ES7210_INPUT_MIC2, MIC_GAIN);
|
||||
err |= es7210_adc_set_gain(ES7210_INPUT_MIC3, MIC_GAIN);
|
||||
err |= es7210_adc_set_gain(ES7210_INPUT_MIC4, MIC_GAIN);
|
||||
err |= es7210_adc_ctrl_state(cfg.codec_mode, AUDIO_HAL_CTRL_START);
|
||||
if (err != ESP_OK) {
|
||||
Serial.println("ES7210 init failed");
|
||||
@@ -100,7 +116,9 @@ bool SoundDetector::begin(uint32_t sampleRate)
|
||||
i2s.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX);
|
||||
i2s.sample_rate = sampleRate_;
|
||||
i2s.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
|
||||
i2s.channel_format = I2S_CHANNEL_FMT_ALL_LEFT;
|
||||
// MULTIPLE is the TDM mode: ALL_LEFT/ALL_RIGHT copy a single channel into
|
||||
// every slot, which made both microphones read the same signal.
|
||||
i2s.channel_format = I2S_CHANNEL_FMT_MULTIPLE;
|
||||
i2s.communication_format = I2S_COMM_FORMAT_STAND_I2S;
|
||||
i2s.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
|
||||
i2s.dma_buf_count = 8;
|
||||
@@ -112,7 +130,7 @@ bool SoundDetector::begin(uint32_t sampleRate)
|
||||
i2s.bits_per_chan = I2S_BITS_PER_CHAN_16BIT;
|
||||
i2s.chan_mask = (i2s_channel_t)(I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 |
|
||||
I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3);
|
||||
i2s.total_chan = 4;
|
||||
i2s.total_chan = MIC_SLOTS;
|
||||
|
||||
if (i2s_driver_install(MIC_I2S_PORT, &i2s, 0, nullptr) != ESP_OK) {
|
||||
Serial.println("Mic I2S install failed");
|
||||
@@ -151,7 +169,7 @@ void SoundDetector::suppress(uint32_t ms)
|
||||
|
||||
void SoundDetector::taskLoop()
|
||||
{
|
||||
static int16_t block[MIC_BLOCK_FRAMES * MIC_CHANNELS];
|
||||
static int16_t block[MIC_BLOCK_FRAMES * MIC_SLOTS];
|
||||
|
||||
for (;;) {
|
||||
size_t bytesRead = 0;
|
||||
@@ -162,7 +180,7 @@ void SoundDetector::taskLoop()
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t frames = bytesRead / (MIC_CHANNELS * sizeof(int16_t));
|
||||
size_t frames = bytesRead / (MIC_SLOTS * sizeof(int16_t));
|
||||
if (frames == 0) continue;
|
||||
|
||||
if ((int32_t)(millis() - suppressUntil_) < 0) {
|
||||
@@ -181,28 +199,54 @@ void SoundDetector::taskLoop()
|
||||
|
||||
void SoundDetector::processBlock(const int16_t *frames, size_t frameCount)
|
||||
{
|
||||
int64_t sumL = 0, sumR = 0;
|
||||
int64_t sqL = 0, sqR = 0;
|
||||
|
||||
for (size_t i = 0; i < frameCount; i++) {
|
||||
int32_t l = frames[i * MIC_CHANNELS + MIC_SLOT_LEFT];
|
||||
int32_t r = frames[i * MIC_CHANNELS + MIC_SLOT_RIGHT];
|
||||
sumL += l;
|
||||
sumR += r;
|
||||
sqL += (int64_t)l * l;
|
||||
sqR += (int64_t)r * r;
|
||||
// RMS of every TDM slot. Two of the four slots are silent because the
|
||||
// board only fits two microphones.
|
||||
float rms[MIC_SLOTS] = {};
|
||||
for (int s = 0; s < MIC_SLOTS; s++) {
|
||||
int64_t sum = 0, sq = 0;
|
||||
for (size_t i = 0; i < frameCount; i++) {
|
||||
int32_t v = frames[i * MIC_SLOTS + s];
|
||||
sum += v;
|
||||
sq += (int64_t)v * v;
|
||||
}
|
||||
float mean = (float)sum / (float)frameCount;
|
||||
float var = (float)sq / (float)frameCount - mean * mean;
|
||||
if (var < 0.0f) var = 0.0f;
|
||||
rms[s] = sqrtf(var) / 32768.0f;
|
||||
}
|
||||
|
||||
float meanL = (float)sumL / (float)frameCount;
|
||||
float meanR = (float)sumR / (float)frameCount;
|
||||
float varL = (float)sqL / (float)frameCount - meanL * meanL;
|
||||
float varR = (float)sqR / (float)frameCount - meanR * meanR;
|
||||
if (varL < 0.0f) varL = 0.0f;
|
||||
if (varR < 0.0f) varR = 0.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]);
|
||||
}
|
||||
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 (slotScore_[s] > slotScore_[best]) {
|
||||
second = best;
|
||||
best = s;
|
||||
} else if (slotScore_[s] > slotScore_[second]) {
|
||||
second = s;
|
||||
}
|
||||
}
|
||||
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_);
|
||||
}
|
||||
}
|
||||
|
||||
float rmsL = rms[MIC_SWAP_LR ? rightSlot_ : leftSlot_];
|
||||
float rmsR = rms[MIC_SWAP_LR ? leftSlot_ : rightSlot_];
|
||||
|
||||
// Normalise to full scale, then smooth with a fast attack / slow release.
|
||||
float rmsL = sqrtf(varL) / 32768.0f;
|
||||
float rmsR = sqrtf(varR) / 32768.0f;
|
||||
envL_ += (rmsL - envL_) * (rmsL > envL_ ? ENV_ATTACK : ENV_RELEASE);
|
||||
envR_ += (rmsR - envR_) * (rmsR > envR_ ? ENV_ATTACK : ENV_RELEASE);
|
||||
|
||||
@@ -234,9 +278,14 @@ void SoundDetector::processBlock(const int16_t *frames, size_t frameCount)
|
||||
if (fabsf(balance) < BALANCE_DEADBAND) {
|
||||
balance = 0.0f;
|
||||
} else {
|
||||
// Remove the deadband and rescale the remaining range to +/-1.
|
||||
// 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;
|
||||
if (balance < -1.0f) balance = -1.0f;
|
||||
if (balance > 1.0f) balance = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,8 +299,11 @@ void SoundDetector::processBlock(const int16_t *frames, size_t frameCount)
|
||||
static uint32_t lastPrintMs = 0;
|
||||
if (active && (uint32_t)(millis() - lastPrintMs) > 250) {
|
||||
lastPrintMs = millis();
|
||||
Serial.printf("mic L=%.4f R=%.4f bal=%+.2f loud=%.2f\n",
|
||||
sigL, sigR, balance, loudness);
|
||||
Serial.printf("mic slots %.4f/%.4f/%.4f/%.4f L%d=%.4f R%d=%.4f "
|
||||
"bal=%+.2f loud=%.2f\n",
|
||||
rms[0], rms[1], rms[2], rms[3],
|
||||
leftSlot_, sigL, rightSlot_, sigR,
|
||||
balance, loudness);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
* @file sound_detector.h
|
||||
* @brief Dual-microphone sound localizer for the T-Deck Plus (ES7210).
|
||||
*
|
||||
* Reads the two populated MEMS microphones as a 4-slot TDM stream:
|
||||
* slot 0 = MIC1 (left), slot 2 = MIC3 (right)
|
||||
* and exposes a smoothed left/right level, a stereo balance and a loudness
|
||||
* value suitable for placing a radar blip.
|
||||
* Reads the ES7210's four TDM slots and uses the two slots that carry a
|
||||
* populated MEMS microphone (the board only fits two of the four footprints).
|
||||
* Balance is taken from the level difference between the pair, so a sound on
|
||||
* the left or right side moves a radar blip accordingly.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
@@ -54,4 +54,12 @@ private:
|
||||
float envR_ = 0.0f;
|
||||
float floorL_ = 0.01f;
|
||||
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.
|
||||
int leftSlot_ = 0;
|
||||
int rightSlot_ = 1;
|
||||
bool pairLocked_ = false;
|
||||
float slotScore_[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user