Learn the populated microphone slots and DMA packing at runtime

This commit is contained in:
Dejvino
2026-11-12 14:54:47 +01:00
parent 129c670ff4
commit 315cdbd177
3 changed files with 234 additions and 78 deletions
+37 -9
View File
@@ -2,10 +2,12 @@
* @file sound_detector.h
* @brief Dual-microphone sound localizer for the T-Deck Plus (ES7210).
*
* 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.
* The ES7210 emits a 4-slot TDM frame. On the T-Deck/T-Deck Plus only two
* MEMS footprints are populated, and different board revisions populate
* different slots, so the detector learns which two slots actually respond to
* sound instead of assuming a fixed pair. Balance is taken from the level
* difference between those two microphones, so a sound on the left or right
* side moves a radar blip accordingly.
*/
#pragma once
@@ -38,7 +40,11 @@ public:
private:
static void taskEntry(void *arg);
void taskLoop();
void processBlock(const int16_t *frames, size_t frameCount);
void probeLayout(const int16_t *samples, size_t sampleCount);
void finaliseLayout();
void updateSlots(const float *rms);
size_t sampleOffset(int slot) const;
void processBlock(const int16_t *samples, size_t sampleCount);
void publish(float left, float right, float balance, float loudness, bool active);
uint32_t sampleRate_ = 16000;
@@ -55,12 +61,34 @@ private:
float floorL_ = 0.01f;
float floorR_ = 0.01f;
// The codec always delivers four TDM slots, but only two microphones are
// fitted. The pair is (re)discovered from the slots that carry signal.
// The two TDM slots carrying the populated microphones. Seeded with the
// documented T-Deck pair (MIC1 = slot 0, MIC3 = slot 2) and corrected at
// runtime by updateSlots() on the first sounds that arrive.
int leftSlot_ = 0;
int rightSlot_ = 1;
int rightSlot_ = 2;
uint32_t warmupUntil_ = 0;
// Start-up layout probe: the driver may pack the 16-bit TDM slots tightly
// (4 int16 per frame) or pad each sample into one half of a 32-bit word
// (8 int16 per frame). Both the data rate and the energy split between even
// and odd int16 words identify the packing.
uint32_t probeStartMs_ = 0;
uint32_t probeSamples_ = 0;
uint32_t probeIndex_ = 0;
double posSq_[8] = {};
uint32_t posCount_[8] = {};
bool layoutReady_ = false;
size_t frameWords_ = 4;
uint8_t sampleHalf_ = 0; // 0 = low half of a 32-bit word, 1 = high half
bool rawDumped_ = false;
// Per-slot level range used to discover the populated pair. A fitted
// microphone swings between its quiet floor and the sound peaks; a floating
// input stays flat (its DC offset is removed by the high-pass filter), and
// a railed channel stays flat too. Range separates those cases cleanly.
float slotMin_[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
float slotMax_[4] = {};
int candL_ = -1;
int candR_ = -1;
int candCount_ = 0;
uint32_t warmupUntil_ = 0;
};