ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
i2s_audio_microphone.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include "../i2s_audio.h"
6#include "dc_offset_filter.h"
7
10
11#include <freertos/FreeRTOS.h>
12#include <freertos/event_groups.h>
13#include <freertos/semphr.h>
14#include <freertos/task.h>
15
16#include <atomic>
17
18namespace esphome::i2s_audio {
19
20class I2SAudioMicrophone final : public I2SAudioIn, public microphone::Microphone, public Component {
21 public:
22 void setup() override;
23 void dump_config() override;
24 void start() override;
25 void stop() override;
26
27 void loop() override;
28
29 void set_correct_dc_offset(bool correct_dc_offset) { this->correct_dc_offset_ = correct_dc_offset; }
30
31 void set_din_pin(int8_t pin) { this->din_pin_ = (gpio_num_t) pin; }
32
33 void set_pdm(bool pdm) { this->pdm_ = pdm; }
34
35#if SOC_I2S_SUPPORTS_PDM_RX
36 void set_pdm_dsr(i2s_pdm_dsr_t pdm_dsr) { this->pdm_dsr_ = pdm_dsr; }
37#endif
38
39#ifdef USE_I2S_AUDIO_FULL_DUPLEX
40 void build_full_duplex_config(i2s_std_config_t &std_cfg) override;
41 void full_duplex_rx_enabling(i2s_chan_handle_t rx_handle, uint32_t dma_desc_num) override;
42#endif
43
44 protected:
46 void build_std_config_(i2s_std_config_t &std_cfg) const;
47
50 bool start_driver_();
51
53 void stop_driver_();
54
57 bool start_capture_count_(i2s_chan_handle_t rx_handle, uint32_t dma_desc_num);
58
61 bool read_dma_buffer_size_();
62
63 size_t read_(uint8_t *buf, size_t len, uint32_t timeout_ms);
64
67
68 static void mic_task(void *params);
69
71 static bool i2s_on_recv_cb(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx);
73 static bool i2s_on_recv_q_ovf_cb(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx);
74
75 SemaphoreHandle_t active_listeners_semaphore_{nullptr};
76 EventGroupHandle_t event_group_{nullptr};
77
78 TaskHandle_t task_handle_{nullptr};
79
80 // Written by the DMA interrupt and read by the mic task, under dma_lock_. Since the channel was enabled: the bytes
81 // received (wrapping), and the time the newest buffer was received, or 0 if none has been.
82 portMUX_TYPE dma_lock_ = portMUX_INITIALIZER_UNLOCKED;
85 // Buffers dropped from the driver's full queue since the channel was enabled, wrapping. The interrupt is its only
86 // writer, so it needs no lock.
87 std::atomic<uint32_t> dma_buffers_dropped_{0};
88
89 // Bytes the mic task has read since the channel was enabled, wrapping. A full duplex channel can stay enabled
90 // across microphone sessions, so this outlives the task.
92
93 // DMA layout of the RX channel, set when the driver starts
96
97 gpio_num_t din_pin_{I2S_GPIO_UNUSED};
98 i2s_chan_handle_t rx_handle_{nullptr};
99#if SOC_I2S_SUPPORTS_PDM_RX
100 i2s_pdm_dsr_t pdm_dsr_{I2S_PDM_DSR_8S};
101#endif
102
103 bool pdm_{false};
105 bool locked_driver_{false};
107};
108
109} // namespace esphome::i2s_audio
110
111#endif // USE_ESP32
Removes DC from mono or stereo PCM, retaining separate channel histories between buffers.
static bool i2s_on_recv_q_ovf_cb(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
Counts a buffer the driver dropped because the task fell behind; it will never be read.
void build_full_duplex_config(i2s_std_config_t &std_cfg) override
static bool i2s_on_recv_cb(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
Records the byte count and time of each DMA buffer the driver queues for reading.
void set_correct_dc_offset(bool correct_dc_offset)
void configure_stream_settings_()
Starts the I2S driver.
void build_std_config_(i2s_std_config_t &std_cfg) const
Builds the standard (non-PDM) mode configuration from the configured settings.
void full_duplex_rx_enabling(i2s_chan_handle_t rx_handle, uint32_t dma_desc_num) override
const void size_t len
Definition hal.h:68
static void uint32_t
spi_device_handle_t handle