refactor: move the v2 controller to the repository root
The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
71bd320661
commit
1c222771a5
66 files changed
+30
-31
No files matched your search
+179
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* INMP441 I2S microphone: level meter plus a 16-bit PCM feed for streaming.
|
||||
* Reads both I2S slots and uses whichever one the mic is driving (L/R pin to
|
||||
* GND = left, to 3V3 = right), keeping a running RMS/peak for the last block.
|
||||
* Every block also goes to the beat detector.
|
||||
*/
|
||||
#include <math.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/stream_buffer.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_check.h"
|
||||
#include "driver/i2s_std.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "mic.h"
|
||||
#include "beat.h"
|
||||
|
||||
#define MIC_BLOCK_SAMPLES (MIC_SAMPLE_RATE / 50) // 20 ms per read, keeps stream latency low
|
||||
#define LEVEL_BLOCKS 5 // 100 ms per level reading
|
||||
#define FULL_SCALE_24BIT 8388608.0f
|
||||
#define DBFS_FLOOR -120.0f
|
||||
#define INMP441_SPL_OFFSET 120.0f // 94 dB SPL - (-26 dBFS)
|
||||
|
||||
static const char *TAG = "mic";
|
||||
|
||||
static i2s_chan_handle_t s_rx_chan;
|
||||
static portMUX_TYPE s_level_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
// 16-bit PCM for the HTTP stream; only fed while a listener is attached
|
||||
#define STREAM_BUF_BYTES MIC_SAMPLE_RATE // 0.5 s of 16-bit mono
|
||||
static StreamBufferHandle_t s_stream_buf;
|
||||
static volatile bool s_stream_active;
|
||||
|
||||
static mic_level_t s_level = { DBFS_FLOOR, DBFS_FLOOR, DBFS_FLOOR + INMP441_SPL_OFFSET };
|
||||
|
||||
static float to_dbfs(float x)
|
||||
{
|
||||
return x > 0.0f ? fmaxf(20.0f * log10f(x), DBFS_FLOOR) : DBFS_FLOOR;
|
||||
}
|
||||
|
||||
static void mic_task(void *arg)
|
||||
{
|
||||
static int32_t buf[MIC_BLOCK_SAMPLES * 2]; // interleaved L/R
|
||||
static int16_t pcm[MIC_BLOCK_SAMPLES];
|
||||
static float filtered[MIC_BLOCK_SAMPLES];
|
||||
// One-pole high-pass to strip the INMP441's DC offset
|
||||
float hp_prev_in = 0.0f, hp_prev_out = 0.0f;
|
||||
// Level is accumulated over several short blocks so the meter stays steady
|
||||
double level_sum_sq = 0.0;
|
||||
float level_peak = 0.0f;
|
||||
size_t level_frames = 0, level_blocks = 0;
|
||||
int64_t log_count = 0;
|
||||
|
||||
while (true) {
|
||||
size_t bytes_read = 0;
|
||||
if (i2s_channel_read(s_rx_chan, buf, sizeof(buf), &bytes_read, portMAX_DELAY) != ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
size_t frames = bytes_read / (2 * sizeof(int32_t));
|
||||
if (frames == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The unused slot is tri-stated and reads as 0; pick the busier one
|
||||
int32_t raw_min = INT32_MAX, raw_max = INT32_MIN;
|
||||
size_t nonzero[2] = {0, 0};
|
||||
for (size_t i = 0; i < frames; i++) {
|
||||
for (int ch = 0; ch < 2; ch++) {
|
||||
int32_t v = buf[i * 2 + ch];
|
||||
nonzero[ch] += (v != 0);
|
||||
raw_min = v < raw_min ? v : raw_min;
|
||||
raw_max = v > raw_max ? v : raw_max;
|
||||
}
|
||||
}
|
||||
int ch = nonzero[1] > nonzero[0] ? 1 : 0;
|
||||
|
||||
for (size_t i = 0; i < frames; i++) {
|
||||
// 24-bit sample sits in the top bits of the 32-bit slot
|
||||
float x = (float)(buf[i * 2 + ch] >> 8) / FULL_SCALE_24BIT;
|
||||
float y = x - hp_prev_in + 0.995f * hp_prev_out;
|
||||
hp_prev_in = x;
|
||||
hp_prev_out = y;
|
||||
filtered[i] = y;
|
||||
level_sum_sq += (double)y * y;
|
||||
level_peak = fmaxf(level_peak, fabsf(y));
|
||||
pcm[i] = (int16_t)fmaxf(fminf(y * CONFIG_MIC_STREAM_GAIN * 32767.0f, 32767.0f), -32768.0f);
|
||||
}
|
||||
level_frames += frames;
|
||||
beat_feed(filtered, frames);
|
||||
|
||||
if (s_stream_active) {
|
||||
// Drop audio rather than block capture if the listener falls behind
|
||||
xStreamBufferSend(s_stream_buf, pcm, frames * sizeof(int16_t), 0);
|
||||
}
|
||||
|
||||
if (++level_blocks < LEVEL_BLOCKS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
mic_level_t level;
|
||||
level.rms_dbfs = to_dbfs(sqrtf((float)(level_sum_sq / level_frames)));
|
||||
level.peak_dbfs = to_dbfs(level_peak);
|
||||
level.spl_db = level.rms_dbfs + INMP441_SPL_OFFSET;
|
||||
level_sum_sq = 0.0;
|
||||
level_peak = 0.0f;
|
||||
level_frames = level_blocks = 0;
|
||||
|
||||
portENTER_CRITICAL(&s_level_lock);
|
||||
s_level = level;
|
||||
portEXIT_CRITICAL(&s_level_lock);
|
||||
|
||||
if (++log_count % 50 == 0) { // every 5 s
|
||||
ESP_LOGI(TAG, "%s slot: RMS %.1f dBFS, peak %.1f dBFS, ~%.0f dB SPL",
|
||||
ch ? "right" : "left", level.rms_dbfs, level.peak_dbfs, level.spl_db);
|
||||
ESP_LOGI(TAG, "raw min %ld max %ld, nonzero L %u R %u of %u",
|
||||
(long)raw_min, (long)raw_max, (unsigned)nonzero[0], (unsigned)nonzero[1], (unsigned)frames);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t mic_start(void)
|
||||
{
|
||||
s_stream_buf = xStreamBufferCreate(STREAM_BUF_BYTES, 1);
|
||||
ESP_RETURN_ON_FALSE(s_stream_buf, ESP_ERR_NO_MEM, TAG, "Stream buffer alloc failed");
|
||||
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
|
||||
ESP_RETURN_ON_ERROR(i2s_new_channel(&chan_cfg, NULL, &s_rx_chan), TAG, "I2S channel create failed");
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(MIC_SAMPLE_RATE),
|
||||
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
.bclk = CONFIG_MIC_SCK_GPIO,
|
||||
.ws = CONFIG_MIC_WS_GPIO,
|
||||
.dout = I2S_GPIO_UNUSED,
|
||||
.din = CONFIG_MIC_SD_GPIO,
|
||||
},
|
||||
};
|
||||
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_init_std_mode(s_rx_chan, &std_cfg), TAG, "I2S std mode init failed");
|
||||
// SD tri-states outside the mic's slot; the datasheet asks for a pull-down
|
||||
gpio_pullup_dis(CONFIG_MIC_SD_GPIO);
|
||||
gpio_pulldown_en(CONFIG_MIC_SD_GPIO);
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_rx_chan), TAG, "I2S enable failed");
|
||||
|
||||
ESP_RETURN_ON_ERROR(beat_start(MIC_SAMPLE_RATE), TAG, "Beat detector start failed");
|
||||
if (xTaskCreate(mic_task, "mic", 6144, NULL, 5, NULL) != pdPASS) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
ESP_LOGI(TAG, "INMP441 started at %d Hz", MIC_SAMPLE_RATE);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void mic_get_level(mic_level_t *out)
|
||||
{
|
||||
portENTER_CRITICAL(&s_level_lock);
|
||||
*out = s_level;
|
||||
portEXIT_CRITICAL(&s_level_lock);
|
||||
}
|
||||
|
||||
esp_err_t mic_stream_open(void)
|
||||
{
|
||||
if (s_stream_active) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
xStreamBufferReset(s_stream_buf);
|
||||
s_stream_active = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
size_t mic_stream_read(void *buf, size_t len, TickType_t timeout)
|
||||
{
|
||||
return xStreamBufferReceive(s_stream_buf, buf, len, timeout);
|
||||
}
|
||||
|
||||
void mic_stream_close(void)
|
||||
{
|
||||
s_stream_active = false;
|
||||
}
|
||||
Reference in new issue
Block a user