ESP32 i2s PDM Audio transmit problem
Posted: Mon Dec 16, 2024 11:16 am
I am trying to output an .wav file with the help of i2s interface to a D-Class amplifier (MAX98357A)
It will then be converted to an analog signal with the help of speaker coils. I have a hard time achieving good results. Speaker outputs so much noise and only the beginning of the signal is recognizable, the rest just sounds like static or sounds of human screaming.
Here is the transmit function block
Is it because i am trying to send all of the data at the same time ? Should i transmit the data in chunks of smaller size, for example 2048 or 4096 bytes ? Here is the I2S initialization section :
Any help is good. Thanks in advance
It will then be converted to an analog signal with the help of speaker coils. I have a hard time achieving good results. Speaker outputs so much noise and only the beginning of the signal is recognizable, the rest just sounds like static or sounds of human screaming.
Here is the transmit function block
Code: Select all
void i2s_transmit_wav_task(void *pvParameters){
// Parameter acquisition
i2s_task_params_t *params = (i2s_task_params_t *)pvParameters;
i2s_chan_handle_t *tx_chan = params->tx_chan;
wav_header_t *wav_header = ¶ms->wav_file.wav_header;
int16_t *data = params->wav_file.data;
TaskHandle_t *task_handle = params->task_handle;
size_t written_bytes = 0;
size_t data_size = wav_header->data_chunk.subchunk_size;
int16_t *buf = calloc(data_size, sizeof(int16_t));
memcpy(buf, data, data_size * sizeof(int16_t));
i2s_channel_enable(*tx_chan);
ESP_ERROR_CHECK(i2s_channel_write(*tx_chan, buf, data_size, &written_bytes, 1000));
i2s_channel_disable(*tx_chan);
free(buf);
*task_handle = NULL;
vTaskDelete(NULL);
}Code: Select all
static esp_err_t i2s_init_std_mode(i2s_chan_handle_t *handler)
{
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
tx_chan_cfg.auto_clear = true;
ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, handler, NULL));
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(44100),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = GPIO_NUM_4,
.ws = GPIO_NUM_5,
.dout = GPIO_NUM_2 ,
.din = I2S_GPIO_UNUSED,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
return i2s_channel_init_std_mode(*handler, &std_cfg);
};