ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
i2s_audio_microphone.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
5#ifdef USE_I2S_LEGACY
6#include <driver/i2s.h>
7#else
8#include <driver/i2s_std.h>
9#include <driver/i2s_pdm.h>
10#endif
11
12#include "esphome/core/hal.h"
13#include "esphome/core/log.h"
14
16
17namespace esphome {
18namespace i2s_audio {
19
20static const UBaseType_t MAX_LISTENERS = 16;
21
22static const uint32_t READ_DURATION_MS = 16;
23
24static const size_t TASK_STACK_SIZE = 4096;
25static const ssize_t TASK_PRIORITY = 23;
26
27static const char *const TAG = "i2s_audio.microphone";
28
29enum MicrophoneEventGroupBits : uint32_t {
30 COMMAND_STOP = (1 << 0), // stops the microphone task, set and cleared by ``loop``
31
32 TASK_STARTING = (1 << 10), // set by mic task, cleared by ``loop``
33 TASK_RUNNING = (1 << 11), // set by mic task, cleared by ``loop``
34 TASK_STOPPED = (1 << 13), // set by mic task, cleared by ``loop``
35
36 ALL_BITS = 0x00FFFFFF, // All valid FreeRTOS event group bits
37};
38
40#ifdef USE_I2S_LEGACY
41#if SOC_I2S_SUPPORTS_ADC
42 if (this->adc_) {
43 if (this->parent_->get_port() != I2S_NUM_0) {
44 ESP_LOGE(TAG, "Internal ADC only works on I2S0");
45 this->mark_failed();
46 return;
47 }
48 } else
49#endif
50#endif
51 {
52 if (this->pdm_) {
53 if (this->parent_->get_port() != I2S_NUM_0) {
54 ESP_LOGE(TAG, "PDM only works on I2S0");
55 this->mark_failed();
56 return;
57 }
58 }
59 }
60
61 this->active_listeners_semaphore_ = xSemaphoreCreateCounting(MAX_LISTENERS, MAX_LISTENERS);
62 if (this->active_listeners_semaphore_ == nullptr) {
63 ESP_LOGE(TAG, "Creating semaphore failed");
64 this->mark_failed();
65 return;
66 }
67
68 this->event_group_ = xEventGroupCreate();
69 if (this->event_group_ == nullptr) {
70 ESP_LOGE(TAG, "Creating event group failed");
71 this->mark_failed();
72 return;
73 }
74
76}
77
79 ESP_LOGCONFIG(TAG,
80 "Microphone:\n"
81 " Pin: %d\n"
82 " PDM: %s\n"
83 " DC offset correction: %s",
84 static_cast<int8_t>(this->din_pin_), YESNO(this->pdm_), YESNO(this->correct_dc_offset_));
85}
86
88 uint8_t channel_count = 1;
89#ifdef USE_I2S_LEGACY
90 uint8_t bits_per_sample = this->bits_per_sample_;
91
92 if (this->channel_ == I2S_CHANNEL_FMT_RIGHT_LEFT) {
93 channel_count = 2;
94 }
95#else
96 uint8_t bits_per_sample = 16;
97 if (this->slot_bit_width_ != I2S_SLOT_BIT_WIDTH_AUTO) {
98 bits_per_sample = this->slot_bit_width_;
99 }
100
101 if (this->slot_mode_ == I2S_SLOT_MODE_STEREO) {
102 channel_count = 2;
103 }
104#endif
105
106#ifdef USE_ESP32_VARIANT_ESP32
107 // ESP32 reads audio aligned to a multiple of 2 bytes. For example, if configured for 24 bits per sample, then it will
108 // produce 32 bits per sample, where the actual data is in the most significant bits. Other ESP32 variants produce 24
109 // bits per sample in this situation.
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;
114 }
115#endif
116
117 if (this->pdm_) {
118 bits_per_sample = 16; // PDM mics are always 16 bits per sample
119 }
120
121 this->audio_stream_info_ = audio::AudioStreamInfo(bits_per_sample, channel_count, this->sample_rate_);
122}
123
125 if (this->is_failed())
126 return;
127
128 xSemaphoreTake(this->active_listeners_semaphore_, 0);
129}
130
131bool I2SAudioMicrophone::start_driver_() {
132 if (!this->parent_->try_lock()) {
133 return false; // Waiting for another i2s to return lock
134 }
135 this->locked_driver_ = true;
136 esp_err_t err;
137
138#ifdef USE_I2S_LEGACY
139 i2s_driver_config_t config = {
140 .mode = (i2s_mode_t) (this->i2s_mode_ | I2S_MODE_RX),
141 .sample_rate = this->sample_rate_,
142 .bits_per_sample = this->bits_per_sample_,
143 .channel_format = this->channel_,
144 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
145 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
146 .dma_buf_count = 4,
147 .dma_buf_len = 240, // Must be divisible by 3 to support 24 bits per sample on old driver and newer variants
148 .use_apll = this->use_apll_,
149 .tx_desc_auto_clear = false,
150 .fixed_mclk = 0,
151 .mclk_multiple = this->mclk_multiple_,
152 .bits_per_chan = this->bits_per_channel_,
153 };
154
155#if SOC_I2S_SUPPORTS_ADC
156 if (this->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);
159 if (err != ESP_OK) {
160 ESP_LOGE(TAG, "Error installing driver: %s", esp_err_to_name(err));
161 return false;
162 }
163
164 err = i2s_set_adc_mode(ADC_UNIT_1, this->adc_channel_);
165 if (err != ESP_OK) {
166 ESP_LOGE(TAG, "Error setting ADC mode: %s", esp_err_to_name(err));
167 return false;
168 }
169
170 err = i2s_adc_enable(this->parent_->get_port());
171 if (err != ESP_OK) {
172 ESP_LOGE(TAG, "Error enabling ADC: %s", esp_err_to_name(err));
173 return false;
174 }
175 } else
176#endif
177 {
178 if (this->pdm_)
179 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_PDM);
180
181 err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
182 if (err != ESP_OK) {
183 ESP_LOGE(TAG, "Error installing driver: %s", esp_err_to_name(err));
184 return false;
185 }
186
187 i2s_pin_config_t pin_config = this->parent_->get_pin_config();
188 pin_config.data_in_num = this->din_pin_;
189
190 err = i2s_set_pin(this->parent_->get_port(), &pin_config);
191 if (err != ESP_OK) {
192 ESP_LOGE(TAG, "Error setting pin: %s", esp_err_to_name(err));
193 return false;
194 }
195 }
196#else
197 i2s_chan_config_t chan_cfg = {
198 .id = this->parent_->get_port(),
199 .role = this->i2s_role_,
200 .dma_desc_num = 4,
201 .dma_frame_num = 256,
202 .auto_clear = false,
203 };
204 /* Allocate a new RX channel and get the handle of this channel */
205 err = i2s_new_channel(&chan_cfg, NULL, &this->rx_handle_);
206 if (err != ESP_OK) {
207 ESP_LOGE(TAG, "Error creating channel: %s", esp_err_to_name(err));
208 return false;
209 }
210
211 i2s_clock_src_t clk_src = I2S_CLK_SRC_DEFAULT;
212#ifdef I2S_CLK_SRC_APLL
213 if (this->use_apll_) {
214 clk_src = I2S_CLK_SRC_APLL;
215 }
216#endif
217 i2s_std_gpio_config_t pin_config = this->parent_->get_pin_config();
218#if SOC_I2S_SUPPORTS_PDM_RX
219 if (this->pdm_) {
220 i2s_pdm_rx_clk_config_t clk_cfg = {
221 .sample_rate_hz = this->sample_rate_,
222 .clk_src = clk_src,
223 .mclk_multiple = this->mclk_multiple_,
224 .dn_sample_mode = I2S_PDM_DSR_8S,
225 };
226
227 i2s_pdm_rx_slot_config_t slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, this->slot_mode_);
228 switch (this->std_slot_mask_) {
229 case I2S_STD_SLOT_LEFT:
230 slot_cfg.slot_mask = I2S_PDM_SLOT_LEFT;
231 break;
232 case I2S_STD_SLOT_RIGHT:
233 slot_cfg.slot_mask = I2S_PDM_SLOT_RIGHT;
234 break;
235 case I2S_STD_SLOT_BOTH:
236 slot_cfg.slot_mask = I2S_PDM_SLOT_BOTH;
237 break;
238 }
239
240 /* Init the channel into PDM RX mode */
241 i2s_pdm_rx_config_t pdm_rx_cfg = {
242 .clk_cfg = clk_cfg,
243 .slot_cfg = slot_cfg,
244 .gpio_cfg =
245 {
246 .clk = pin_config.ws,
247 .din = this->din_pin_,
248 .invert_flags =
249 {
250 .clk_inv = pin_config.invert_flags.ws_inv,
251 },
252 },
253 };
254 err = i2s_channel_init_pdm_rx_mode(this->rx_handle_, &pdm_rx_cfg);
255 } else
256#endif
257 {
258 i2s_std_clk_config_t clk_cfg = {
259 .sample_rate_hz = this->sample_rate_,
260 .clk_src = clk_src,
261 .mclk_multiple = this->mclk_multiple_,
262 };
263 i2s_std_slot_config_t std_slot_cfg =
264 I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG((i2s_data_bit_width_t) this->slot_bit_width_, this->slot_mode_);
265 std_slot_cfg.slot_bit_width = this->slot_bit_width_;
266 std_slot_cfg.slot_mask = this->std_slot_mask_;
267
268 pin_config.din = this->din_pin_;
269
270 i2s_std_config_t std_cfg = {
271 .clk_cfg = clk_cfg,
272 .slot_cfg = std_slot_cfg,
273 .gpio_cfg = pin_config,
274 };
275 /* Initialize the channel */
276 err = i2s_channel_init_std_mode(this->rx_handle_, &std_cfg);
277 }
278 if (err != ESP_OK) {
279 ESP_LOGE(TAG, "Error initializing channel: %s", esp_err_to_name(err));
280 return false;
281 }
282
283 /* Before reading data, start the RX channel first */
284 i2s_channel_enable(this->rx_handle_);
285 if (err != ESP_OK) {
286 ESP_LOGE(TAG, "Enabling failed: %s", esp_err_to_name(err));
287 return false;
288 }
289#endif
290
291 this->configure_stream_settings_(); // redetermine the settings in case some settings were changed after compilation
292
293 return true;
294}
295
297 if (this->state_ == microphone::STATE_STOPPED || this->is_failed())
298 return;
299
300 xSemaphoreGive(this->active_listeners_semaphore_);
301}
302
303void I2SAudioMicrophone::stop_driver_() {
304 // There is no harm continuing to unload the driver if an error is ever returned by the various functions. This
305 // ensures that we stop/unload the driver when it only partially starts.
306
307 esp_err_t err;
308#ifdef USE_I2S_LEGACY
309#if SOC_I2S_SUPPORTS_ADC
310 if (this->adc_) {
311 err = i2s_adc_disable(this->parent_->get_port());
312 if (err != ESP_OK) {
313 ESP_LOGW(TAG, "Error disabling ADC: %s", esp_err_to_name(err));
314 }
315 }
316#endif
317 err = i2s_stop(this->parent_->get_port());
318 if (err != ESP_OK) {
319 ESP_LOGW(TAG, "Error stopping: %s", esp_err_to_name(err));
320 }
321 err = i2s_driver_uninstall(this->parent_->get_port());
322 if (err != ESP_OK) {
323 ESP_LOGW(TAG, "Error uninstalling driver: %s", esp_err_to_name(err));
324 }
325#else
326 if (this->rx_handle_ != nullptr) {
327 /* Have to stop the channel before deleting it */
328 err = i2s_channel_disable(this->rx_handle_);
329 if (err != ESP_OK) {
330 ESP_LOGW(TAG, "Error stopping: %s", esp_err_to_name(err));
331 }
332 /* If the handle is not needed any more, delete it to release the channel resources */
333 err = i2s_del_channel(this->rx_handle_);
334 if (err != ESP_OK) {
335 ESP_LOGW(TAG, "Error deleting channel: %s", esp_err_to_name(err));
336 }
337 this->rx_handle_ = nullptr;
338 }
339#endif
340 if (this->locked_driver_) {
341 this->parent_->unlock();
342 this->locked_driver_ = false;
343 }
344}
345
347 I2SAudioMicrophone *this_microphone = (I2SAudioMicrophone *) params;
348 xEventGroupSetBits(this_microphone->event_group_, MicrophoneEventGroupBits::TASK_STARTING);
349
350 { // Ensures the samples vector is freed when the task stops
351
352 const size_t bytes_to_read = this_microphone->audio_stream_info_.ms_to_bytes(READ_DURATION_MS);
353 std::vector<uint8_t> samples;
354 samples.reserve(bytes_to_read);
355
356 xEventGroupSetBits(this_microphone->event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
357
358 while (!(xEventGroupGetBits(this_microphone->event_group_) & MicrophoneEventGroupBits::COMMAND_STOP)) {
359 if (this_microphone->data_callbacks_.size() > 0) {
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);
363 if (this_microphone->correct_dc_offset_) {
364 this_microphone->fix_dc_offset_(samples);
365 }
366 this_microphone->data_callbacks_.call(samples);
367 } else {
368 vTaskDelay(pdMS_TO_TICKS(READ_DURATION_MS));
369 }
370 }
371 }
372
373 xEventGroupSetBits(this_microphone->event_group_, MicrophoneEventGroupBits::TASK_STOPPED);
374 while (true) {
375 // Continuously delay until the loop method deletes the task
376 vTaskDelay(pdMS_TO_TICKS(10));
377 }
378}
379
380void I2SAudioMicrophone::fix_dc_offset_(std::vector<uint8_t> &data) {
420 const uint8_t dc_filter_shift = 10;
421 const size_t bytes_per_sample = this->audio_stream_info_.samples_to_bytes(1);
422 const uint32_t total_samples = this->audio_stream_info_.bytes_to_samples(data.size());
423 for (uint32_t sample_index = 0; sample_index < total_samples; ++sample_index) {
424 const uint32_t byte_index = sample_index * bytes_per_sample;
425 int32_t input = audio::unpack_audio_sample_to_q31(&data[byte_index], bytes_per_sample);
426 int32_t output = input - this->dc_offset_prev_input_ +
427 (this->dc_offset_prev_output_ - (this->dc_offset_prev_output_ >> dc_filter_shift));
428 this->dc_offset_prev_input_ = input;
429 this->dc_offset_prev_output_ = output;
430 audio::pack_q31_as_audio_sample(output, &data[byte_index], bytes_per_sample);
431 }
432}
433
434size_t I2SAudioMicrophone::read_(uint8_t *buf, size_t len, TickType_t ticks_to_wait) {
435 size_t bytes_read = 0;
436#ifdef USE_I2S_LEGACY
437 esp_err_t err = i2s_read(this->parent_->get_port(), buf, len, &bytes_read, ticks_to_wait);
438#else
439 // i2s_channel_read expects the timeout value in ms, not ticks
440 esp_err_t err = i2s_channel_read(this->rx_handle_, buf, len, &bytes_read, pdTICKS_TO_MS(ticks_to_wait));
441#endif
442 if ((err != ESP_OK) && ((err != ESP_ERR_TIMEOUT) || (ticks_to_wait != 0))) {
443 // Ignore ESP_ERR_TIMEOUT if ticks_to_wait = 0, as it will read the data on the next call
444 if (!this->status_has_warning()) {
445 // Avoid spamming the logs with the error message if its repeated
446 ESP_LOGW(TAG, "Read error: %s", esp_err_to_name(err));
447 }
448 this->status_set_warning();
449 return 0;
450 }
451 if ((bytes_read == 0) && (ticks_to_wait > 0)) {
452 this->status_set_warning();
453 return 0;
454 }
455 this->status_clear_warning();
456#if defined(USE_ESP32_VARIANT_ESP32) and not defined(USE_I2S_LEGACY)
457 // For ESP32 8/16 bit standard mono mode samples need to be switched.
458 if (this->slot_mode_ == I2S_SLOT_MODE_MONO && this->slot_bit_width_ <= 16 && !this->pdm_) {
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];
462 buf[i] = buf[i + 1];
463 buf[i + 1] = tmp;
464 }
465 }
466#endif
467 return bytes_read;
468}
469
471 uint32_t event_group_bits = xEventGroupGetBits(this->event_group_);
472
473 if (event_group_bits & MicrophoneEventGroupBits::TASK_STARTING) {
474 ESP_LOGV(TAG, "Task started, attempting to allocate buffer");
475 xEventGroupClearBits(this->event_group_, MicrophoneEventGroupBits::TASK_STARTING);
476 }
477
478 if (event_group_bits & MicrophoneEventGroupBits::TASK_RUNNING) {
479 ESP_LOGV(TAG, "Task is running and reading data");
480
481 xEventGroupClearBits(this->event_group_, MicrophoneEventGroupBits::TASK_RUNNING);
483 }
484
485 if ((event_group_bits & MicrophoneEventGroupBits::TASK_STOPPED)) {
486 ESP_LOGV(TAG, "Task finished, freeing resources and uninstalling driver");
487
488 vTaskDelete(this->task_handle_);
489 this->task_handle_ = nullptr;
490 this->stop_driver_();
491 xEventGroupClearBits(this->event_group_, ALL_BITS);
492 this->status_clear_error();
493
495 }
496
497 // Start the microphone if any semaphores are taken
498 if ((uxSemaphoreGetCount(this->active_listeners_semaphore_) < MAX_LISTENERS) &&
501 }
502
503 // Stop the microphone if all semaphores are returned
504 if ((uxSemaphoreGetCount(this->active_listeners_semaphore_) == MAX_LISTENERS) &&
507 }
508
509 switch (this->state_) {
511 if (this->status_has_error()) {
512 break;
513 }
514
515 if (!this->start_driver_()) {
516 ESP_LOGE(TAG, "Driver failed to start; retrying in 1 second");
517 this->status_momentary_error("driver_fail", 1000);
518 this->stop_driver_(); // Stop/frees whatever possibly started
519 break;
520 }
521
522 if (this->task_handle_ == nullptr) {
523 xTaskCreate(I2SAudioMicrophone::mic_task, "mic_task", TASK_STACK_SIZE, (void *) this, TASK_PRIORITY,
524 &this->task_handle_);
525
526 if (this->task_handle_ == nullptr) {
527 ESP_LOGE(TAG, "Task failed to start, retrying in 1 second");
528 this->status_momentary_error("task_fail", 1000);
529 this->stop_driver_(); // Stops the driver to return the lock; will be reloaded in next attempt
530 }
531 }
532
533 break;
535 break;
537 xEventGroupSetBits(this->event_group_, MicrophoneEventGroupBits::COMMAND_STOP);
538 break;
540 break;
541 }
542}
543
544} // namespace i2s_audio
545} // namespace esphome
546
547#endif // USE_ESP32
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
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()
size_t ms_to_bytes(uint32_t ms) const
Converts duration to bytes.
Definition audio.h:73
size_t samples_to_bytes(uint32_t samples) const
Converts samples to bytes.
Definition audio.h:58
uint32_t bytes_to_samples(size_t bytes) const
Convert bytes to samples.
Definition audio.h:48
i2s_std_slot_mask_t std_slot_mask_
Definition i2s_audio.h:45
i2s_slot_bit_width_t slot_bit_width_
Definition i2s_audio.h:46
i2s_bits_per_chan_t bits_per_channel_
Definition i2s_audio.h:41
i2s_mclk_multiple_t mclk_multiple_
Definition i2s_audio.h:50
i2s_bits_per_sample_t bits_per_sample_
Definition i2s_audio.h:40
void configure_stream_settings_()
Starts the I2S driver.
audio::AudioStreamInfo audio_stream_info_
Definition microphone.h:39
CallbackManager< void(const std::vector< uint8_t > &)> data_callbacks_
Definition microphone.h:41
__int64 ssize_t
Definition httplib.h:175
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.
Definition audio.h:142
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.
Definition audio.h:168
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279