5#include <driver/i2s_std.h>
6#include <driver/i2s_pdm.h>
15static const UBaseType_t MAX_LISTENERS = 16;
17static const uint32_t READ_DURATION_MS = 16;
19static const size_t TASK_STACK_SIZE = 4096;
20static const ssize_t TASK_PRIORITY = 23;
22static const char *
const TAG =
"i2s_audio.microphone";
37 ESP_LOGE(TAG,
"Creating semaphore failed");
44 ESP_LOGE(TAG,
"Creating event group failed");
57 " DC offset correction: %s",
62 uint8_t channel_count = 1;
63 uint8_t bits_per_sample = 16;
68 if (this->
slot_mode_ == I2S_SLOT_MODE_STEREO) {
72#ifdef USE_ESP32_VARIANT_ESP32
76 if (bits_per_sample < 16) {
78 }
else if ((bits_per_sample > 16) && (bits_per_sample <= 32)) {
97bool I2SAudioMicrophone::start_driver_() {
98 if (!this->
parent_->try_lock()) {
104 i2s_chan_config_t chan_cfg = {
105 .id = this->
parent_->get_port(),
108 .dma_frame_num = 256,
112 err = i2s_new_channel(&chan_cfg, NULL, &this->
rx_handle_);
114 ESP_LOGE(TAG,
"Error creating channel: %s", esp_err_to_name(err));
118 i2s_clock_src_t clk_src = I2S_CLK_SRC_DEFAULT;
119#ifdef I2S_CLK_SRC_APLL
121 clk_src = I2S_CLK_SRC_APLL;
124 i2s_std_gpio_config_t pin_config = this->
parent_->get_pin_config();
125#if SOC_I2S_SUPPORTS_PDM_RX
127 i2s_pdm_rx_clk_config_t clk_cfg = {
131 .dn_sample_mode = I2S_PDM_DSR_8S,
134 i2s_pdm_rx_slot_config_t slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, this->
slot_mode_);
136 case I2S_STD_SLOT_LEFT:
137 slot_cfg.slot_mask = I2S_PDM_SLOT_LEFT;
139 case I2S_STD_SLOT_RIGHT:
140 slot_cfg.slot_mask = I2S_PDM_SLOT_RIGHT;
142 case I2S_STD_SLOT_BOTH:
143 slot_cfg.slot_mask = I2S_PDM_SLOT_BOTH;
148 i2s_pdm_rx_config_t pdm_rx_cfg = {
150 .slot_cfg = slot_cfg,
153 .clk = pin_config.ws,
157 .clk_inv = pin_config.invert_flags.ws_inv,
161 err = i2s_channel_init_pdm_rx_mode(this->
rx_handle_, &pdm_rx_cfg);
165 i2s_std_clk_config_t clk_cfg = {
170 i2s_std_slot_config_t std_slot_cfg =
177 i2s_std_config_t std_cfg = {
179 .slot_cfg = std_slot_cfg,
180 .gpio_cfg = pin_config,
183 err = i2s_channel_init_std_mode(this->
rx_handle_, &std_cfg);
186 ESP_LOGE(TAG,
"Error initializing channel: %s", esp_err_to_name(err));
193 ESP_LOGE(TAG,
"Enabling failed: %s", esp_err_to_name(err));
209void I2SAudioMicrophone::stop_driver_() {
218 ESP_LOGW(TAG,
"Error stopping: %s", esp_err_to_name(err));
223 ESP_LOGW(TAG,
"Error deleting channel: %s", esp_err_to_name(err));
235 xEventGroupSetBits(this_microphone->
event_group_, MicrophoneEventGroupBits::TASK_STARTING);
240 std::vector<uint8_t> samples;
241 samples.reserve(bytes_to_read);
243 xEventGroupSetBits(this_microphone->
event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
245 while (!(xEventGroupGetBits(this_microphone->
event_group_) & MicrophoneEventGroupBits::COMMAND_STOP)) {
247 samples.resize(bytes_to_read);
248 size_t bytes_read = this_microphone->read_(samples.data(), bytes_to_read, 2 * pdMS_TO_TICKS(READ_DURATION_MS));
249 samples.resize(bytes_read);
251 this_microphone->fix_dc_offset_(samples);
255 vTaskDelay(pdMS_TO_TICKS(READ_DURATION_MS));
260 xEventGroupSetBits(this_microphone->
event_group_, MicrophoneEventGroupBits::TASK_STOPPED);
263 vTaskDelay(pdMS_TO_TICKS(10));
267void I2SAudioMicrophone::fix_dc_offset_(std::vector<uint8_t> &data) {
307 const uint8_t dc_filter_shift = 10;
310 for (
uint32_t sample_index = 0; sample_index < total_samples; ++sample_index) {
311 const uint32_t byte_index = sample_index * bytes_per_sample;
321size_t I2SAudioMicrophone::read_(uint8_t *buf,
size_t len, TickType_t ticks_to_wait) {
322 size_t bytes_read = 0;
324 esp_err_t err = i2s_channel_read(this->
rx_handle_, buf,
len, &bytes_read, pdTICKS_TO_MS(ticks_to_wait));
325 if ((err != ESP_OK) && ((err != ESP_ERR_TIMEOUT) || (ticks_to_wait != 0))) {
329 ESP_LOGW(TAG,
"Read error: %s", esp_err_to_name(err));
334 if ((bytes_read == 0) && (ticks_to_wait > 0)) {
339#ifdef USE_ESP32_VARIANT_ESP32
342 int16_t *samples =
reinterpret_cast<int16_t *
>(buf);
343 size_t sample_count = bytes_read /
sizeof(int16_t);
344 for (
size_t i = 0; i + 1 < sample_count; i += 2) {
345 int16_t tmp = samples[i];
346 samples[i] = samples[i + 1];
347 samples[i + 1] = tmp;
357 if (event_group_bits & MicrophoneEventGroupBits::TASK_STARTING) {
358 ESP_LOGV(TAG,
"Task started, attempting to allocate buffer");
359 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_STARTING);
362 if (event_group_bits & MicrophoneEventGroupBits::TASK_RUNNING) {
363 ESP_LOGV(TAG,
"Task is running and reading data");
365 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
369 if ((event_group_bits & MicrophoneEventGroupBits::TASK_STOPPED)) {
370 ESP_LOGV(TAG,
"Task finished, freeing resources and uninstalling driver");
374 this->stop_driver_();
399 if (!this->start_driver_()) {
400 ESP_LOGE(TAG,
"Driver failed to start; retrying in 1 second");
402 this->stop_driver_();
411 ESP_LOGE(TAG,
"Task failed to start, retrying in 1 second");
413 this->stop_driver_();
421 xEventGroupSetBits(this->
event_group_, MicrophoneEventGroupBits::COMMAND_STOP);
void mark_failed()
Mark this component as failed.
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
void status_set_warning()
void status_clear_error()
bool status_has_warning() const
bool status_has_error() const
void status_clear_warning()
I2SAudioComponent * parent_
size_t ms_to_bytes(uint32_t ms) const
Converts duration to bytes.
size_t samples_to_bytes(uint32_t samples) const
Converts samples to bytes.
uint32_t bytes_to_samples(size_t bytes) const
Convert bytes to samples.
i2s_std_slot_mask_t std_slot_mask_
i2s_slot_bit_width_t slot_bit_width_
i2s_slot_mode_t slot_mode_
i2s_mclk_multiple_t mclk_multiple_
SemaphoreHandle_t active_listeners_semaphore_
TaskHandle_t task_handle_
int32_t dc_offset_prev_output_
EventGroupHandle_t event_group_
void configure_stream_settings_()
Starts the I2S driver.
void dump_config() override
int32_t dc_offset_prev_input_
static void mic_task(void *params)
i2s_chan_handle_t rx_handle_
audio::AudioStreamInfo audio_stream_info_
CallbackManager< void(const std::vector< uint8_t > &)> data_callbacks_
int32_t unpack_audio_sample_to_q31(const uint8_t *data, size_t bytes_per_sample)
Unpacks a quantized audio sample into a Q31 fixed-point number.
void pack_q31_as_audio_sample(int32_t sample, uint8_t *data, size_t bytes_per_sample)
Packs a Q31 fixed-point number as an audio sample with the specified number of bytes per sample.