3 #include "AudioConfig.h"
4 #if defined(ESP32) && defined(USE_I2S) && \
5 ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) || \
8 #include "AudioI2S/I2SConfig.h"
9 #include "driver/i2s.h"
10 #include "esp_system.h"
12 #ifndef I2S_MCLK_MULTIPLE_DEFAULT
13 #define I2S_MCLK_MULTIPLE_DEFAULT ((i2s_mclk_multiple_t)0)
26 friend class AnalogAudio;
40 if (info.
equals(cfg))
return true;
44 return i2s_set_sample_rates((i2s_port_t)cfg.port_no, cfg.
sample_rate) == ESP_OK;
65 return begin(cfg, cfg.pin_data, I2S_PIN_NO_CHANGE);
70 cfg, I2S_PIN_NO_CHANGE,
71 cfg.pin_data != I2S_PIN_NO_CHANGE ? cfg.pin_data : cfg.pin_data_rx);
73 return begin(cfg, cfg.pin_data, cfg.pin_data_rx);
75 LOGE(
"Did not expect go get here");
79 int available() {
return I2S_BUFFER_COUNT * I2S_BUFFER_SIZE; }
87 i2s_driver_uninstall(i2s_num);
99 if (isNoChannelConversion(cfg)) {
100 if (i2s_write(i2s_num, src, size_bytes, &result, ticks_to_wait_write) !=
104 LOGD(
"i2s_write %d -> %d bytes", size_bytes, result);
112 size_t readBytes(
void *dest,
size_t size_bytes) {
114 if (isNoChannelConversion(cfg)) {
115 if (i2s_read(i2s_num, dest, size_bytes, &result, ticks_to_wait_read) !=
121 uint8_t temp[size_bytes * 2];
122 if (i2s_read(i2s_num, temp, size_bytes * 2, &result,
123 ticks_to_wait_read) != ESP_OK) {
129 ChannelReducerT<int16_t> reducer16(1, 2);
130 result = reducer16.convert((uint8_t *)dest, temp, result);
133 ChannelReducerT<int24_t> reducer24(1, 2);
134 result = reducer24.convert((uint8_t *)dest, temp, result);
137 ChannelReducerT<int32_t> reducer32(1, 2);
138 result = reducer32.convert((uint8_t *)dest, temp, result);
145 LOGE(
"Invalid channels: %d", cfg.
channels);
150 void setWaitTimeReadMs(TickType_t ms) {
151 ticks_to_wait_read = pdMS_TO_TICKS(ms);
153 void setWaitTimeWriteMs(TickType_t ms) {
154 ticks_to_wait_write = pdMS_TO_TICKS(ms);
160 i2s_config_t i2s_config;
161 bool is_started =
false;
162 TickType_t ticks_to_wait_read = portMAX_DELAY;
163 TickType_t ticks_to_wait_write = portMAX_DELAY;
165 bool isNoChannelConversion(I2SConfigESP32 cfg) {
166 if (cfg.channels == 2)
return true;
167 if (cfg.channels == 1 && cfg.channel_format == I2S_CHANNEL_FMT_ALL_RIGHT)
169 if (cfg.channels == 1 && cfg.channel_format == I2S_CHANNEL_FMT_ALL_LEFT)
171 if (cfg.channels == 1 && cfg.channel_format == I2S_CHANNEL_FMT_ONLY_RIGHT)
173 if (cfg.channels == 1 && cfg.channel_format == I2S_CHANNEL_FMT_ONLY_LEFT)
183 this->i2s_num = (i2s_port_t)cfg.port_no;
186 i2s_config_t i2s_config_new = {
188 .sample_rate = (eps32_i2s_sample_rate_type)cfg.
sample_rate,
190 .channel_format = (i2s_channel_fmt_t)cfg.channel_format,
191 .communication_format = toCommFormat(cfg.i2s_format),
192 .intr_alloc_flags = 0,
193 .dma_buf_count = cfg.buffer_count,
194 .dma_buf_len = cfg.buffer_size,
195 .use_apll = (bool)cfg.use_apll,
196 .tx_desc_auto_clear = cfg.auto_clear,
197 #
if ESP_IDF_VERSION_MAJOR >= 4
198 .fixed_mclk = (
int)(cfg.fixed_mclk > 0 ? cfg.fixed_mclk : 0),
199 .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT,
200 .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT,
203 i2s_config = i2s_config_new;
208 LOGD(
"%s",
"I2S restarting");
212 LOGD(
"i2s_driver_install");
213 if (i2s_driver_install(i2s_num, &i2s_config, 0, NULL) != ESP_OK) {
214 LOGE(
"%s - %s", __func__,
"i2s_driver_install");
218 if (this->cfg.signal_type == Digital || this->cfg.signal_type == PDM) {
219 i2s_pin_config_t pin_config = {
220 #if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 4, 0)
221 .mck_io_num = cfg.pin_mck,
223 .bck_io_num = cfg.pin_bck,
224 .ws_io_num = cfg.pin_ws,
225 .data_out_num = txPin,
226 .data_in_num = rxPin};
229 if (i2s_set_pin(i2s_num, &pin_config) != ESP_OK) {
230 LOGE(
"%s - %s", __func__,
"i2s_set_pin");
233 LOGD(
"Using built in DAC");
235 i2s_set_pin(i2s_num, NULL);
239 LOGD(
"i2s_zero_dma_buffer");
240 i2s_zero_dma_buffer(i2s_num);
243 LOGD(
"%s - %s", __func__,
"started");
248 void setChannels(
int channels) { cfg.channels = channels; }
252 const void *src,
size_t size_bytes) {
255 switch (bits_per_sample) {
257 for (j = 0; j < size_bytes; j++) {
259 int8_t *data = (int8_t *)src;
262 size_t result_call = 0;
263 if (i2s_write(i2s_num, frame,
sizeof(int8_t) * 2, &result_call,
264 ticks_to_wait_write) != ESP_OK) {
267 result += result_call;
273 for (j = 0; j < size_bytes / 2; j++) {
275 int16_t *data = (int16_t *)src;
278 size_t result_call = 0;
279 if (i2s_write(i2s_num, frame,
sizeof(int16_t) * 2, &result_call,
280 ticks_to_wait_write) != ESP_OK) {
283 result += result_call;
289 for (j = 0; j < size_bytes / 4; j++) {
294 size_t result_call = 0;
295 if (i2s_write(i2s_num, frame,
sizeof(
int24_t) * 2, &result_call,
296 ticks_to_wait_write) != ESP_OK) {
299 result += result_call;
305 for (j = 0; j < size_bytes / 4; j++) {
307 int32_t *data = (int32_t *)src;
310 size_t result_call = 0;
311 if (i2s_write(i2s_num, frame,
sizeof(int32_t) * 2, &result_call,
312 ticks_to_wait_write) != ESP_OK) {
315 result += result_call;
323 #pragma GCC diagnostic push
324 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
328 i2s_comm_format_t toCommFormat(
I2SFormat mode) {
330 case I2S_PHILIPS_FORMAT:
332 return (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_I2S;
333 case I2S_LEFT_JUSTIFIED_FORMAT:
335 return (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S |
336 I2S_COMM_FORMAT_I2S_MSB);
337 case I2S_RIGHT_JUSTIFIED_FORMAT:
339 return (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S |
340 I2S_COMM_FORMAT_I2S_LSB);
342 return (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_PCM_SHORT;
345 LOGE(
"unsupported mode");
346 return (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_I2S;
349 #pragma GCC diagnostic pop
351 int getModeDigital(I2SConfigESP32 &cfg) {
352 int i2s_format = cfg.is_master ? I2S_MODE_MASTER : I2S_MODE_SLAVE;
354 switch (cfg.rx_tx_mode) {
356 i2s_rx_tx = I2S_MODE_TX;
359 i2s_rx_tx = I2S_MODE_RX;
362 i2s_rx_tx = I2S_MODE_RX | I2S_MODE_TX;
365 LOGE(
"Undefined rx_tx_mode: %d", cfg.rx_tx_mode);
367 return (i2s_format | i2s_rx_tx);
371 i2s_mode_t toMode(I2SConfigESP32 &cfg) {
372 i2s_mode_t mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX);
373 switch (cfg.signal_type) {
375 mode = (i2s_mode_t)getModeDigital(cfg);
379 mode = (i2s_mode_t)(getModeDigital(cfg) | I2S_MODE_PDM);
383 #if defined(USE_ANALOG)
384 mode = (i2s_mode_t)(cfg.rx_tx_mode ? I2S_MODE_DAC_BUILT_IN
385 : I2S_MODE_ADC_BUILT_IN);
387 LOGE(
"mode not supported");
392 LOGW(
"signal_type undefined");
393 mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
400 using I2SDriver = I2SDriverESP32;
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:26