8#include <driver/i2s_std.h>
9#include <driver/i2s_pdm.h>
20static const UBaseType_t MAX_LISTENERS = 16;
22static const uint32_t READ_DURATION_MS = 16;
24static const size_t TASK_STACK_SIZE = 4096;
25static const ssize_t TASK_PRIORITY = 23;
27static const char *
const TAG =
"i2s_audio.microphone";
41#if SOC_I2S_SUPPORTS_ADC
43 if (this->
parent_->get_port() != I2S_NUM_0) {
44 ESP_LOGE(TAG,
"Internal ADC only works on I2S0");
53 if (this->
parent_->get_port() != I2S_NUM_0) {
54 ESP_LOGE(TAG,
"PDM only works on I2S0");
63 ESP_LOGE(TAG,
"Creating semaphore failed");
70 ESP_LOGE(TAG,
"Creating event group failed");
83 " DC offset correction: %s",
88 uint8_t channel_count = 1;
92 if (this->
channel_ == I2S_CHANNEL_FMT_RIGHT_LEFT) {
96 uint8_t bits_per_sample = 16;
101 if (this->
slot_mode_ == I2S_SLOT_MODE_STEREO) {
106#ifdef USE_ESP32_VARIANT_ESP32
110 if (bits_per_sample < 16) {
111 bits_per_sample = 16;
112 }
else if ((bits_per_sample > 16) && (bits_per_sample <= 32)) {
113 bits_per_sample = 32;
118 bits_per_sample = 16;
131bool I2SAudioMicrophone::start_driver_() {
132 if (!this->
parent_->try_lock()) {
139 i2s_driver_config_t config = {
140 .mode = (i2s_mode_t) (this->
i2s_mode_ | I2S_MODE_RX),
144 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
145 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
149 .tx_desc_auto_clear =
false,
155#if SOC_I2S_SUPPORTS_ADC
157 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_ADC_BUILT_IN);
158 err = i2s_driver_install(this->
parent_->get_port(), &config, 0,
nullptr);
160 ESP_LOGE(TAG,
"Error installing driver: %s", esp_err_to_name(err));
166 ESP_LOGE(TAG,
"Error setting ADC mode: %s", esp_err_to_name(err));
170 err = i2s_adc_enable(this->
parent_->get_port());
172 ESP_LOGE(TAG,
"Error enabling ADC: %s", esp_err_to_name(err));
179 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_PDM);
181 err = i2s_driver_install(this->
parent_->get_port(), &config, 0,
nullptr);
183 ESP_LOGE(TAG,
"Error installing driver: %s", esp_err_to_name(err));
187 i2s_pin_config_t pin_config = this->
parent_->get_pin_config();
188 pin_config.data_in_num = this->
din_pin_;
190 err = i2s_set_pin(this->
parent_->get_port(), &pin_config);
192 ESP_LOGE(TAG,
"Error setting pin: %s", esp_err_to_name(err));
197 i2s_chan_config_t chan_cfg = {
198 .id = this->
parent_->get_port(),
201 .dma_frame_num = 256,
205 err = i2s_new_channel(&chan_cfg, NULL, &this->
rx_handle_);
207 ESP_LOGE(TAG,
"Error creating 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_LOGE(TAG,
"Error initializing channel: %s", esp_err_to_name(err));
286 ESP_LOGE(TAG,
"Enabling failed: %s", esp_err_to_name(err));
303void I2SAudioMicrophone::stop_driver_() {
309#if SOC_I2S_SUPPORTS_ADC
311 err = i2s_adc_disable(this->
parent_->get_port());
313 ESP_LOGW(TAG,
"Error disabling ADC: %s", esp_err_to_name(err));
317 err = i2s_stop(this->
parent_->get_port());
319 ESP_LOGW(TAG,
"Error stopping: %s", esp_err_to_name(err));
321 err = i2s_driver_uninstall(this->
parent_->get_port());
323 ESP_LOGW(TAG,
"Error uninstalling driver: %s", esp_err_to_name(err));
330 ESP_LOGW(TAG,
"Error stopping: %s", esp_err_to_name(err));
335 ESP_LOGW(TAG,
"Error deleting channel: %s", esp_err_to_name(err));
353 std::vector<uint8_t> samples;
354 samples.reserve(bytes_to_read);
360 samples.resize(bytes_to_read);
361 size_t bytes_read = this_microphone->read_(samples.data(), bytes_to_read, 2 * pdMS_TO_TICKS(READ_DURATION_MS));
362 samples.resize(bytes_read);
364 this_microphone->fix_dc_offset_(samples);
368 vTaskDelay(pdMS_TO_TICKS(READ_DURATION_MS));
376 vTaskDelay(pdMS_TO_TICKS(10));
380void I2SAudioMicrophone::fix_dc_offset_(std::vector<uint8_t> &data) {
420 const uint8_t dc_filter_shift = 10;
423 for (uint32_t sample_index = 0; sample_index < total_samples; ++sample_index) {
424 const uint32_t byte_index = sample_index * bytes_per_sample;
434size_t I2SAudioMicrophone::read_(uint8_t *buf,
size_t len, TickType_t ticks_to_wait) {
435 size_t bytes_read = 0;
437 esp_err_t err = i2s_read(this->
parent_->get_port(), buf,
len, &bytes_read, ticks_to_wait);
440 esp_err_t err = i2s_channel_read(this->
rx_handle_, buf,
len, &bytes_read, pdTICKS_TO_MS(ticks_to_wait));
442 if ((err != ESP_OK) && ((err != ESP_ERR_TIMEOUT) || (ticks_to_wait != 0))) {
446 ESP_LOGW(TAG,
"Read error: %s", esp_err_to_name(err));
451 if ((bytes_read == 0) && (ticks_to_wait > 0)) {
456#if defined(USE_ESP32_VARIANT_ESP32) and not defined(USE_I2S_LEGACY)
459 size_t samples_read = bytes_read /
sizeof(int16_t);
460 for (
int i = 0; i < samples_read; i += 2) {
461 int16_t tmp = buf[i];
471 uint32_t event_group_bits = xEventGroupGetBits(this->
event_group_);
474 ESP_LOGV(TAG,
"Task started, attempting to allocate buffer");
475 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_STARTING);
479 ESP_LOGV(TAG,
"Task is running and reading data");
481 xEventGroupClearBits(this->
event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
486 ESP_LOGV(TAG,
"Task finished, freeing resources and uninstalling driver");
490 this->stop_driver_();
515 if (!this->start_driver_()) {
516 ESP_LOGE(TAG,
"Driver failed to start; retrying in 1 second");
518 this->stop_driver_();
527 ESP_LOGE(TAG,
"Task failed to start, retrying in 1 second");
529 this->stop_driver_();
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_error()
void status_momentary_error(const std::string &name, uint32_t length=5000)
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_bits_per_chan_t bits_per_channel_
i2s_channel_fmt_t channel_
i2s_slot_mode_t slot_mode_
i2s_mclk_multiple_t mclk_multiple_
i2s_bits_per_sample_t bits_per_sample_
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_
adc1_channel_t adc_channel_
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.