ESPHome  2024.4.2
i2s_audio_microphone.cpp
Go to the documentation of this file.
1 #include "i2s_audio_microphone.h"
2 
3 #ifdef USE_ESP32
4 
5 #include <driver/i2s.h>
6 
7 #include "esphome/core/hal.h"
8 #include "esphome/core/log.h"
9 
10 namespace esphome {
11 namespace i2s_audio {
12 
13 static const size_t BUFFER_SIZE = 512;
14 
15 static const char *const TAG = "i2s_audio.microphone";
16 
18  ESP_LOGCONFIG(TAG, "Setting up I2S Audio Microphone...");
19 #if SOC_I2S_SUPPORTS_ADC
20  if (this->adc_) {
21  if (this->parent_->get_port() != I2S_NUM_0) {
22  ESP_LOGE(TAG, "Internal ADC only works on I2S0!");
23  this->mark_failed();
24  return;
25  }
26  } else
27 #endif
28  if (this->pdm_) {
29  if (this->parent_->get_port() != I2S_NUM_0) {
30  ESP_LOGE(TAG, "PDM only works on I2S0!");
31  this->mark_failed();
32  return;
33  }
34  }
35 }
36 
38  if (this->is_failed())
39  return;
40  if (this->state_ == microphone::STATE_RUNNING)
41  return; // Already running
43 }
45  if (!this->parent_->try_lock()) {
46  return; // Waiting for another i2s to return lock
47  }
48  i2s_driver_config_t config = {
49  .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX),
50  .sample_rate = this->sample_rate_,
51  .bits_per_sample = this->bits_per_sample_,
52  .channel_format = this->channel_,
53  .communication_format = I2S_COMM_FORMAT_STAND_I2S,
54  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
55  .dma_buf_count = 4,
56  .dma_buf_len = 256,
57  .use_apll = this->use_apll_,
58  .tx_desc_auto_clear = false,
59  .fixed_mclk = 0,
60  .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT,
61  .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT,
62  };
63 
64  esp_err_t err;
65 
66 #if SOC_I2S_SUPPORTS_ADC
67  if (this->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);
70  if (err != ESP_OK) {
71  ESP_LOGW(TAG, "Error installing I2S driver: %s", esp_err_to_name(err));
72  this->status_set_error();
73  return;
74  }
75 
76  err = i2s_set_adc_mode(ADC_UNIT_1, this->adc_channel_);
77  if (err != ESP_OK) {
78  ESP_LOGW(TAG, "Error setting ADC mode: %s", esp_err_to_name(err));
79  this->status_set_error();
80  return;
81  }
82  err = i2s_adc_enable(this->parent_->get_port());
83  if (err != ESP_OK) {
84  ESP_LOGW(TAG, "Error enabling ADC: %s", esp_err_to_name(err));
85  this->status_set_error();
86  return;
87  }
88 
89  } else
90 #endif
91  {
92  if (this->pdm_)
93  config.mode = (i2s_mode_t) (config.mode | I2S_MODE_PDM);
94 
95  err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
96  if (err != ESP_OK) {
97  ESP_LOGW(TAG, "Error installing I2S driver: %s", esp_err_to_name(err));
98  this->status_set_error();
99  return;
100  }
101 
102  i2s_pin_config_t pin_config = this->parent_->get_pin_config();
103  pin_config.data_in_num = this->din_pin_;
104 
105  err = i2s_set_pin(this->parent_->get_port(), &pin_config);
106  if (err != ESP_OK) {
107  ESP_LOGW(TAG, "Error setting I2S pin: %s", esp_err_to_name(err));
108  this->status_set_error();
109  return;
110  }
111  }
113  this->high_freq_.start();
114  this->status_clear_error();
115 }
116 
118  if (this->state_ == microphone::STATE_STOPPED || this->is_failed())
119  return;
120  if (this->state_ == microphone::STATE_STARTING) {
122  return;
123  }
125 }
126 
128  esp_err_t err;
129 #if SOC_I2S_SUPPORTS_ADC
130  if (this->adc_) {
131  err = i2s_adc_disable(this->parent_->get_port());
132  if (err != ESP_OK) {
133  ESP_LOGW(TAG, "Error disabling ADC: %s", esp_err_to_name(err));
134  this->status_set_error();
135  return;
136  }
137  }
138 #endif
139  err = i2s_stop(this->parent_->get_port());
140  if (err != ESP_OK) {
141  ESP_LOGW(TAG, "Error stopping I2S microphone: %s", esp_err_to_name(err));
142  this->status_set_error();
143  return;
144  }
145  err = i2s_driver_uninstall(this->parent_->get_port());
146  if (err != ESP_OK) {
147  ESP_LOGW(TAG, "Error uninstalling I2S driver: %s", esp_err_to_name(err));
148  this->status_set_error();
149  return;
150  }
151  this->parent_->unlock();
153  this->high_freq_.stop();
154  this->status_clear_error();
155 }
156 
157 size_t I2SAudioMicrophone::read(int16_t *buf, size_t len) {
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));
160  if (err != ESP_OK) {
161  ESP_LOGW(TAG, "Error reading from I2S microphone: %s", esp_err_to_name(err));
162  this->status_set_warning();
163  return 0;
164  }
165  if (bytes_read == 0) {
166  this->status_set_warning();
167  return 0;
168  }
169  this->status_clear_warning();
170  if (this->bits_per_sample_ == I2S_BITS_PER_SAMPLE_16BIT) {
171  return bytes_read;
172  } else if (this->bits_per_sample_ == I2S_BITS_PER_SAMPLE_32BIT) {
173  std::vector<int16_t> samples;
174  size_t samples_read = bytes_read / sizeof(int32_t);
175  samples.resize(samples_read);
176  for (size_t i = 0; i < samples_read; i++) {
177  int32_t temp = reinterpret_cast<int32_t *>(buf)[i] >> 14;
178  samples[i] = clamp<int16_t>(temp, INT16_MIN, INT16_MAX);
179  }
180  memcpy(buf, samples.data(), samples_read * sizeof(int16_t));
181  return samples_read * sizeof(int16_t);
182  } else {
183  ESP_LOGE(TAG, "Unsupported bits per sample: %d", this->bits_per_sample_);
184  return 0;
185  }
186 }
187 
189  std::vector<int16_t> samples;
190  samples.resize(BUFFER_SIZE);
191  size_t bytes_read = this->read(samples.data(), BUFFER_SIZE / sizeof(int16_t));
192  samples.resize(bytes_read / sizeof(int16_t));
193  this->data_callbacks_.call(samples);
194 }
195 
197  switch (this->state_) {
199  break;
201  this->start_();
202  break;
204  if (this->data_callbacks_.size() > 0) {
205  this->read_();
206  }
207  break;
209  this->stop_();
210  break;
211  }
212 }
213 
214 } // namespace i2s_audio
215 } // namespace esphome
216 
217 #endif // USE_ESP32
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
CallbackManager< void(const std::vector< int16_t > &)> data_callbacks_
Definition: microphone.h:31
void status_set_error(const char *message="unspecified")
Definition: component.cpp:159
void start()
Start running the loop continuously.
Definition: helpers.cpp:547
void status_clear_warning()
Definition: component.cpp:166
void stop()
Stop running the loop continuously.
Definition: helpers.cpp:553
size_t read(int16_t *buf, size_t len) override
void status_clear_error()
Definition: component.cpp:172
std::string size_t len
Definition: helpers.h:292
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7