83 uint8_t channel_count = 1;
87 if (this->
channel_ == I2S_CHANNEL_FMT_RIGHT_LEFT) {
91 uint8_t bits_per_sample = 16;
96 if (this->
slot_mode_ == I2S_SLOT_MODE_STEREO) {
101#ifdef USE_ESP32_VARIANT_ESP32
105 if (bits_per_sample < 16) {
106 bits_per_sample = 16;
107 }
else if ((bits_per_sample > 16) && (bits_per_sample <= 32)) {
108 bits_per_sample = 32;
113 bits_per_sample = 16;
127 if (!this->
parent_->try_lock()) {
133 i2s_driver_config_t config = {
134 .mode = (i2s_mode_t) (this->
i2s_mode_ | I2S_MODE_RX),
138 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
139 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
143 .tx_desc_auto_clear =
false,
149#if SOC_I2S_SUPPORTS_ADC
151 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_ADC_BUILT_IN);
152 err = i2s_driver_install(this->
parent_->get_port(), &config, 0,
nullptr);
154 ESP_LOGW(TAG,
"Error installing I2S driver: %s", esp_err_to_name(err));
161 ESP_LOGW(TAG,
"Error setting ADC mode: %s", esp_err_to_name(err));
165 err = i2s_adc_enable(this->
parent_->get_port());
167 ESP_LOGW(TAG,
"Error enabling ADC: %s", esp_err_to_name(err));
176 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_PDM);
178 err = i2s_driver_install(this->
parent_->get_port(), &config, 0,
nullptr);
180 ESP_LOGW(TAG,
"Error installing I2S driver: %s", esp_err_to_name(err));
185 i2s_pin_config_t pin_config = this->
parent_->get_pin_config();
186 pin_config.data_in_num = this->
din_pin_;
188 err = i2s_set_pin(this->
parent_->get_port(), &pin_config);
190 ESP_LOGW(TAG,
"Error setting I2S pin: %s", esp_err_to_name(err));
196 i2s_chan_config_t chan_cfg = {
197 .id = this->
parent_->get_port(),
200 .dma_frame_num = 256,
204 err = i2s_new_channel(&chan_cfg, NULL, &this->
rx_handle_);
206 ESP_LOGW(TAG,
"Error creating new I2S channel: %s", esp_err_to_name(err));
211 i2s_clock_src_t clk_src = I2S_CLK_SRC_DEFAULT;
212#ifdef I2S_CLK_SRC_APLL
214 clk_src = I2S_CLK_SRC_APLL;
217 i2s_std_gpio_config_t pin_config = this->
parent_->get_pin_config();
218#if SOC_I2S_SUPPORTS_PDM_RX
220 i2s_pdm_rx_clk_config_t clk_cfg = {
224 .dn_sample_mode = I2S_PDM_DSR_8S,
227 i2s_pdm_rx_slot_config_t slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, this->
slot_mode_);
229 case I2S_STD_SLOT_LEFT:
230 slot_cfg.slot_mask = I2S_PDM_SLOT_LEFT;
232 case I2S_STD_SLOT_RIGHT:
233 slot_cfg.slot_mask = I2S_PDM_SLOT_RIGHT;
235 case I2S_STD_SLOT_BOTH:
236 slot_cfg.slot_mask = I2S_PDM_SLOT_BOTH;
241 i2s_pdm_rx_config_t pdm_rx_cfg = {
243 .slot_cfg = slot_cfg,
246 .clk = pin_config.ws,
250 .clk_inv = pin_config.invert_flags.ws_inv,
254 err = i2s_channel_init_pdm_rx_mode(this->
rx_handle_, &pdm_rx_cfg);
258 i2s_std_clk_config_t clk_cfg = {
263 i2s_std_slot_config_t std_slot_cfg =
270 i2s_std_config_t std_cfg = {
272 .slot_cfg = std_slot_cfg,
273 .gpio_cfg = pin_config,
276 err = i2s_channel_init_std_mode(this->
rx_handle_, &std_cfg);
279 ESP_LOGW(TAG,
"Error initializing I2S channel: %s", esp_err_to_name(err));
287 ESP_LOGW(TAG,
"Error enabling I2S Microphone: %s", esp_err_to_name(err));
355 uint8_t start_counter = 0;
357 while (!started && start_counter < 10) {
359 vTaskDelay(pdMS_TO_TICKS(100));
367 std::vector<uint8_t> samples;
368 samples.reserve(bytes_to_read);
372 samples.resize(bytes_to_read);
373 size_t bytes_read = this_microphone->
read_(samples.data(), bytes_to_read, 2 * pdMS_TO_TICKS(READ_DURATION_MS));
374 samples.resize(bytes_read);
380 vTaskDelay(pdMS_TO_TICKS(READ_DURATION_MS));
391 vTaskDelay(pdMS_TO_TICKS(10));
399 if (total_samples == 0) {
403 int64_t offset_accumulator = 0;
404 for (uint32_t sample_index = 0; sample_index < total_samples; ++sample_index) {
405 const uint32_t byte_index = sample_index * bytes_per_sample;
407 offset_accumulator += sample;
412 const int32_t new_offset = offset_accumulator / total_samples;
413 this->
dc_offset_ = new_offset / DC_OFFSET_MOVING_AVERAGE_COEFFICIENT_DENOMINATOR +
414 (DC_OFFSET_MOVING_AVERAGE_COEFFICIENT_DENOMINATOR - 1) * this->
dc_offset_ /
415 DC_OFFSET_MOVING_AVERAGE_COEFFICIENT_DENOMINATOR;
419 size_t bytes_read = 0;
421 esp_err_t err = i2s_read(this->
parent_->get_port(), buf,
len, &bytes_read, ticks_to_wait);
424 esp_err_t err = i2s_channel_read(this->
rx_handle_, buf,
len, &bytes_read, pdTICKS_TO_MS(ticks_to_wait));
426 if ((err != ESP_OK) && ((err != ESP_ERR_TIMEOUT) || (ticks_to_wait != 0))) {
428 ESP_LOGW(TAG,
"Error reading from I2S microphone: %s", esp_err_to_name(err));
432 if ((bytes_read == 0) && (ticks_to_wait > 0)) {
437#if defined(USE_ESP32_VARIANT_ESP32) and not defined(USE_I2S_LEGACY)
440 size_t samples_read = bytes_read /
sizeof(int16_t);
441 for (
int i = 0; i < samples_read; i += 2) {
442 int16_t tmp = buf[i];
452 uint32_t event_group_bits = xEventGroupGetBits(this->
event_group_);
455 ESP_LOGD(TAG,
"Task has started, attempting to setup I2S audio driver");
456 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_STARTING);
460 ESP_LOGD(TAG,
"Task is running and reading data");
462 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
467 ESP_LOGD(TAG,
"Task is stopping, attempting to unload the I2S audio driver");
468 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_STOPPING);
472 ESP_LOGD(TAG,
"Task is finished, freeing resources");