Files

94 lines
3.6 KiB
C++

/**
* @file sound_detector.h
* @brief Dual-microphone sound localizer for the T-Deck Plus (ES7210).
*
* 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
#include <Arduino.h>
#include <driver/i2s.h>
struct SoundReading {
float left; // smoothed level above the noise floor (0..1)
float right; // smoothed level above the noise floor (0..1)
float slots[4]; // per-slot smoothed level above the noise floor (0..1)
float balance; // -1 = far left, 0 = centre, +1 = far right
float loudness; // 0 = quiet, 1 = loud (dB-mapped)
bool active; // true while a sound is present (with a short hold)
};
class SoundDetector {
public:
// Starts the ES7210 codec and the I2S receive task. Returns false if the
// codec or I2S driver could not be initialised.
bool begin(uint32_t sampleRate = 16000);
// Latest measurement. Safe to call from the main loop.
SoundReading read();
bool available() const { return running_; }
// Ignore microphone input for a while (used so the device's own radar
// ping does not register as a target).
void suppress(uint32_t ms);
private:
static void taskEntry(void *arg);
void taskLoop();
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, const float *slots, float balance, float loudness, bool active);
uint32_t sampleRate_ = 16000;
TaskHandle_t task_ = nullptr;
volatile bool running_ = false;
volatile uint32_t suppressUntil_ = 0;
portMUX_TYPE mux_ = portMUX_INITIALIZER_UNLOCKED;
SoundReading reading_ = {};
uint32_t lastActiveMs_ = 0;
float env_[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
float floor_[4] = { 0.01f, 0.01f, 0.01f, 0.01f };
// 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_ = 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;
};