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.
66 lines
2.2 KiB
C++
66 lines
2.2 KiB
C++
/**
|
|
* @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.
|
|
*/
|
|
#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 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 processBlock(const int16_t *frames, size_t frameCount);
|
|
void publish(float left, float right, 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 envL_ = 0.0f;
|
|
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};
|
|
};
|