Revisit: Config of I2S interface as PCM output? #1698
-
I’ve been working on interfacing esp32 to a module accepting PCM mono input. I’ve asked ChatGPT and while it provided a positive answer that the i2s interface could do PCM out, I’m unsure if the audio tools can be configured to do it? Answer here: To configure Arduino Audio Tools to output PCM audio via the I2S interface on an ESP32, you'll need to use the I2S library along with the Arduino Audio library for managing PCM data. Here's a step-by-step guide: 1. Install Required LibrariesMake sure you have the necessary libraries installed:
You can install the Arduino Audio library via the Library Manager in the Arduino IDE. 2. Configure I2S for PCM OutputFirst, set up the I2S configuration for PCM output. Here’s an example setup for the ESP32: #include <driver/i2s.h>
#define I2S_NUM (0) // I2S port number
#define I2S_BCK_IO (26) // I2S BCK pin
#define I2S_WS_IO (25) // I2S WS pin
#define I2S_DATA_OUT_IO (22) // I2S Data Out pin
void setup() {
// Configure I2S
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Master mode and transmit
.sample_rate = 16000, // Sample rate (e.g., 16 kHz)
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // 16-bit PCM data
.channel_format = I2S_CHANNEL_FMT_RIGHT_ONLY, // Mono PCM data (right channel only)
.communication_format = I2S_COMM_FORMAT_I2S_MSB, // I2S format
.intr_alloc_flags = 0, // Default interrupt priority
.dma_buf_count = 8, // Number of DMA buffers
.dma_buf_len = 64, // Length of each DMA buffer
.use_apll = false, // Use APLL if required
};
i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCK_IO, // I2S BCLK pin
.ws_io_num = I2S_WS_IO, // I2S WS pin
.data_out_num = I2S_DATA_OUT_IO, // I2S data out pin
.data_in_num = I2S_PIN_NO_CHANGE, // Not used for output
};
// Install and configure I2S driver
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM, &pin_config);
}
void loop() {
// Your code to generate or read PCM data and send to I2S
} 3. Generate or Read PCM DataYou need to generate or read PCM data that will be output via I2S. Here's an example of how you might write PCM data to the I2S interface: void loop() {
static uint8_t pcm_data[1024]; // Example buffer for PCM data
// Fill pcm_data with your PCM data here
size_t bytes_written;
// Write data to I2S
i2s_write(I2S_NUM, pcm_data, sizeof(pcm_data), &bytes_written, portMAX_DELAY);
} 4. Integrate with Arduino Audio LibraryIf you are using the Arduino Audio library for more advanced features, you can integrate it with your I2S setup by creating an Here’s a simple example that integrates an audio file: #include <driver/i2s.h>
#include <Arduino.h>
#define I2S_NUM (0)
#define I2S_BCK_IO (26)
#define I2S_WS_IO (25)
#define I2S_DATA_OUT_IO (22)
void setup() {
Serial.begin(115200);
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
.sample_rate = 16000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_ONLY,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false,
};
i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCK_IO,
.ws_io_num = I2S_WS_IO,
.data_out_num = I2S_DATA_OUT_IO,
.data_in_num = I2S_PIN_NO_CHANGE,
};
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM, &pin_config);
// Your PCM data processing and sending code here
}
void loop() {
static uint8_t pcm_data[1024];
// Fill pcm_data with your PCM data here
size_t bytes_written;
i2s_write(I2S_NUM, pcm_data, sizeof(pcm_data), &bytes_written, portMAX_DELAY);
} SummaryTo output PCM audio via I2S on an ESP32 using Arduino, configure the I2S interface with the appropriate settings for PCM data and write the PCM data to the I2S bus. Ensure that the sample rate, bit depth, and channel format match your audio data requirements. Integrating with the Arduino Audio library can provide additional audio handling capabilities if needed. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This answer is nonsense: You should rather rely on the actual original documentation provided by the platforms you want to use! The Espressif IDF provides a low level C I2S API. Your example above uses the API which is obsolete and is not supported any more! If you are using Arduino you better use either the high level I2S API that is provided by Arduino or the one provided by this project. The advantage of the audio-tools is, that it provides a consistent API across platforms and versions ps. I2S is by definition a protocol using PCM data and any of the APIs above supports mono |
Beta Was this translation helpful? Give feedback.
-
Wow, so much makes sense now. 😂 |
Beta Was this translation helpful? Give feedback.
This answer is nonsense: You should rather rely on the actual original documentation provided by the platforms you want to use!
The Espressif IDF provides a low level C I2S API. Your example above uses the API which is obsolete and is not supported any more!
If you are using Arduino you better use either the high level I2S API that is provided by Arduino or the one provided by this project.
For further information on the latter, read the Wiki!
The advantage of the audio-tools is, that it provides a consistent API across platforms and versions
ps. I2S is by definition a protocol using PCM data and any of the APIs above supports mono