5#include <driver/i2s_std.h>
6#include <driver/i2s_pdm.h>
16static const UBaseType_t MAX_LISTENERS = 16;
18static const uint32_t READ_DURATION_MS = 16;
20static const size_t TASK_STACK_SIZE = 4096;
21static const ssize_t TASK_PRIORITY = 23;
23static const char *
const TAG =
"i2s_audio.microphone";
38 ESP_LOGE(TAG,
"Creating semaphore failed");
45 ESP_LOGE(TAG,
"Creating event group failed");
58 " DC offset correction: %s",
63 uint8_t channel_count = 1;
64 uint8_t bits_per_sample = 16;
69 if (this->
slot_mode_ == I2S_SLOT_MODE_STEREO) {
73#ifdef USE_ESP32_VARIANT_ESP32
77 if (bits_per_sample < 16) {
79 }
else if ((bits_per_sample > 16) && (bits_per_sample <= 32)) {
98bool I2SAudioMicrophone::start_driver_() {
99 if (!this->
parent_->try_lock()) {
105 i2s_chan_config_t chan_cfg = {
106 .id = this->
parent_->get_port(),
109 .dma_frame_num = 256,
113 err = i2s_new_channel(&chan_cfg, NULL, &this->
rx_handle_);
115 ESP_LOGE(TAG,
"Error creating channel: %s", esp_err_to_name(err));
119 i2s_clock_src_t clk_src = I2S_CLK_SRC_DEFAULT;
120#ifdef I2S_CLK_SRC_APLL
122 clk_src = I2S_CLK_SRC_APLL;
125 i2s_std_gpio_config_t pin_config = this->
parent_->get_pin_config();
126#if SOC_I2S_SUPPORTS_PDM_RX
128 i2s_pdm_rx_clk_config_t clk_cfg = {
132 .dn_sample_mode = I2S_PDM_DSR_8S,
135 i2s_pdm_rx_slot_config_t slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, this->
slot_mode_);
137 case I2S_STD_SLOT_LEFT:
138 slot_cfg.slot_mask = I2S_PDM_SLOT_LEFT;
140 case I2S_STD_SLOT_RIGHT:
141 slot_cfg.slot_mask = I2S_PDM_SLOT_RIGHT;
143 case I2S_STD_SLOT_BOTH:
144 slot_cfg.slot_mask = I2S_PDM_SLOT_BOTH;
149 i2s_pdm_rx_config_t pdm_rx_cfg = {
151 .slot_cfg = slot_cfg,
154 .clk = pin_config.ws,
158 .clk_inv = pin_config.invert_flags.ws_inv,
162 err = i2s_channel_init_pdm_rx_mode(this->
rx_handle_, &pdm_rx_cfg);
166 i2s_std_clk_config_t clk_cfg = {
171 i2s_std_slot_config_t std_slot_cfg =
178 i2s_std_config_t std_cfg = {
180 .slot_cfg = std_slot_cfg,
181 .gpio_cfg = pin_config,
184 err = i2s_channel_init_std_mode(this->
rx_handle_, &std_cfg);
187 ESP_LOGE(TAG,
"Error initializing channel: %s", esp_err_to_name(err));
194 ESP_LOGE(TAG,
"Enabling failed: %s", esp_err_to_name(err));
210void I2SAudioMicrophone::stop_driver_() {
219 ESP_LOGW(TAG,
"Error stopping: %s", esp_err_to_name(err));
224 ESP_LOGW(TAG,
"Error deleting channel: %s", esp_err_to_name(err));
236 xEventGroupSetBits(this_microphone->
event_group_, MicrophoneEventGroupBits::TASK_STARTING);
241 std::vector<uint8_t> samples;
242 samples.reserve(bytes_to_read);
244 xEventGroupSetBits(this_microphone->
event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
246 while (!(xEventGroupGetBits(this_microphone->
event_group_) & MicrophoneEventGroupBits::COMMAND_STOP)) {
248 samples.resize(bytes_to_read);
249 size_t bytes_read = this_microphone->read_(samples.data(), bytes_to_read, 2 * pdMS_TO_TICKS(READ_DURATION_MS));
250 samples.resize(bytes_read);
252 this_microphone->fix_dc_offset_(samples);
256 vTaskDelay(pdMS_TO_TICKS(READ_DURATION_MS));
261 xEventGroupSetBits(this_microphone->
event_group_, MicrophoneEventGroupBits::TASK_STOPPED);
264 vTaskDelay(pdMS_TO_TICKS(10));
268void I2SAudioMicrophone::fix_dc_offset_(std::vector<uint8_t> &data) {
308 const uint8_t dc_filter_shift = 10;
311 for (
uint32_t sample_index = 0; sample_index < total_samples; ++sample_index) {
312 const uint32_t byte_index = sample_index * bytes_per_sample;
322size_t I2SAudioMicrophone::read_(uint8_t *buf,
size_t len, TickType_t ticks_to_wait) {
323 size_t bytes_read = 0;
325 esp_err_t err = i2s_channel_read(this->
rx_handle_, buf,
len, &bytes_read, pdTICKS_TO_MS(ticks_to_wait));
326 if ((err != ESP_OK) && ((err != ESP_ERR_TIMEOUT) || (ticks_to_wait != 0))) {
330 ESP_LOGW(TAG,
"Read error: %s", esp_err_to_name(err));
335 if ((bytes_read == 0) && (ticks_to_wait > 0)) {
340#ifdef USE_ESP32_VARIANT_ESP32
343 int16_t *samples =
reinterpret_cast<int16_t *
>(buf);
344 size_t sample_count = bytes_read /
sizeof(int16_t);
345 for (
size_t i = 0; i + 1 < sample_count; i += 2) {
346 int16_t tmp = samples[i];
347 samples[i] = samples[i + 1];
348 samples[i + 1] = tmp;
358 if (event_group_bits & MicrophoneEventGroupBits::TASK_STARTING) {
359 ESP_LOGV(TAG,
"Task started, attempting to allocate buffer");
360 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_STARTING);
363 if (event_group_bits & MicrophoneEventGroupBits::TASK_RUNNING) {
364 ESP_LOGV(TAG,
"Task is running and reading data");
366 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
370 if ((event_group_bits & MicrophoneEventGroupBits::TASK_STOPPED)) {
371 ESP_LOGV(TAG,
"Task finished, freeing resources and uninstalling driver");
375 this->stop_driver_();
400 if (!this->start_driver_()) {
401 ESP_LOGE(TAG,
"Driver failed to start; retrying in 1 second");
403 this->stop_driver_();
412 ESP_LOGE(TAG,
"Task failed to start, retrying in 1 second");
414 this->stop_driver_();
422 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.
Providing packet encoding functions for exchanging data with a remote host.