5 #include <driver/i2s.h> 13 static const size_t BUFFER_SIZE = 512;
15 static const char *
const TAG =
"i2s_audio.microphone";
18 ESP_LOGCONFIG(TAG,
"Setting up I2S Audio Microphone...");
19 #if SOC_I2S_SUPPORTS_ADC 21 if (this->
parent_->get_port() != I2S_NUM_0) {
22 ESP_LOGE(TAG,
"Internal ADC only works on I2S0!");
29 if (this->
parent_->get_port() != I2S_NUM_0) {
30 ESP_LOGE(TAG,
"PDM only works on I2S0!");
45 if (!this->
parent_->try_lock()) {
48 i2s_driver_config_t config = {
49 .mode = (i2s_mode_t) (this->
i2s_mode_ | I2S_MODE_RX),
53 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
54 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
58 .tx_desc_auto_clear =
false,
60 .mclk_multiple = I2S_MCLK_MULTIPLE_256,
66 #if SOC_I2S_SUPPORTS_ADC 68 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_ADC_BUILT_IN);
69 err = i2s_driver_install(this->
parent_->get_port(), &config, 0,
nullptr);
71 ESP_LOGW(TAG,
"Error installing I2S driver: %s", esp_err_to_name(err));
78 ESP_LOGW(TAG,
"Error setting ADC mode: %s", esp_err_to_name(err));
82 err = i2s_adc_enable(this->
parent_->get_port());
84 ESP_LOGW(TAG,
"Error enabling ADC: %s", esp_err_to_name(err));
93 config.mode = (i2s_mode_t) (config.mode | I2S_MODE_PDM);
95 err = i2s_driver_install(this->
parent_->get_port(), &config, 0,
nullptr);
97 ESP_LOGW(TAG,
"Error installing I2S driver: %s", esp_err_to_name(err));
102 i2s_pin_config_t pin_config = this->
parent_->get_pin_config();
103 pin_config.data_in_num = this->
din_pin_;
105 err = i2s_set_pin(this->
parent_->get_port(), &pin_config);
107 ESP_LOGW(TAG,
"Error setting I2S pin: %s", esp_err_to_name(err));
129 #if SOC_I2S_SUPPORTS_ADC 131 err = i2s_adc_disable(this->
parent_->get_port());
133 ESP_LOGW(TAG,
"Error disabling ADC: %s", esp_err_to_name(err));
139 err = i2s_stop(this->
parent_->get_port());
141 ESP_LOGW(TAG,
"Error stopping I2S microphone: %s", esp_err_to_name(err));
145 err = i2s_driver_uninstall(this->
parent_->get_port());
147 ESP_LOGW(TAG,
"Error uninstalling I2S driver: %s", esp_err_to_name(err));
158 size_t bytes_read = 0;
159 esp_err_t err = i2s_read(this->
parent_->get_port(), buf,
len, &bytes_read, (100 / portTICK_PERIOD_MS));
161 ESP_LOGW(TAG,
"Error reading from I2S microphone: %s", esp_err_to_name(err));
165 if (bytes_read == 0) {
173 case I2S_BITS_PER_SAMPLE_8BIT:
174 case I2S_BITS_PER_SAMPLE_16BIT:
176 case I2S_BITS_PER_SAMPLE_24BIT:
177 case I2S_BITS_PER_SAMPLE_32BIT: {
178 size_t samples_read = bytes_read /
sizeof(int32_t);
179 for (
size_t i = 0; i < samples_read; i++) {
180 int32_t temp =
reinterpret_cast<int32_t *
>(buf)[i] >> 14;
181 buf[i] = clamp<int16_t>(temp, INT16_MIN, INT16_MAX);
183 return samples_read *
sizeof(int16_t);
192 std::vector<int16_t> samples;
193 samples.resize(BUFFER_SIZE);
194 size_t bytes_read = this->
read(samples.data(), BUFFER_SIZE /
sizeof(int16_t));
195 samples.resize(bytes_read /
sizeof(int16_t));
void status_set_warning(const char *message="unspecified")
CallbackManager< void(const std::vector< int16_t > &)> data_callbacks_
void status_set_error(const char *message="unspecified")
HighFrequencyLoopRequester high_freq_
I2SAudioComponent * parent_
i2s_channel_fmt_t channel_
void start()
Start running the loop continuously.
void status_clear_warning()
void stop()
Stop running the loop continuously.
size_t read(int16_t *buf, size_t len) override
void status_clear_error()
i2s_bits_per_sample_t bits_per_sample_
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
i2s_bits_per_chan_t bits_per_channel_
adc1_channel_t adc_channel_