Distorted start of sound from I2S output
Posted: Mon Nov 14, 2022 3:30 am
I have a small program driving a MAX98357A I2S amplifier from I2S0 on an ESP32S3. The problem is that when I switch from one sound (silence) to another (1000Hz sine wave) the first few cycles of the sine wave are distorted:
If I continue to write the same data the signal is fine, but after sending a burst of silence and restarting the tone, the same distortion appears. It can last for a few cycles, or the entire duration of the sound. I see similar issues when playing converted WAV files, such that the sound varies from one play to the next.
Here is my code writing the data - any ideas on what is the problem? I am going to try the internal I2S DAC and see if it suffers from the same problem. I have tried different bit rates, dma buffer sizes and counts, all with no change.
If I continue to write the same data the signal is fine, but after sending a burst of silence and restarting the tone, the same distortion appears. It can last for a few cycles, or the entire duration of the sound. I see similar issues when playing converted WAV files, such that the sound varies from one play to the next.
Here is my code writing the data - any ideas on what is the problem? I am going to try the internal I2S DAC and see if it suffers from the same problem. I have tried different bit rates, dma buffer sizes and counts, all with no change.
Code: Select all
for (;;) {
int64_t start = esp_timer_get_time();
int64_t end = esp_timer_get_time();
while ((end - start)/1000 < SAMPLE_DURATION * 1) {
ESP_ERROR_CHECK(i2s_write(I2S_NUM_0, samples_data, sizeof(samples_data), &i2s_bytes_write, portMAX_DELAY));
end = esp_timer_get_time();
}
int32_t msElapsed = (int32_t)(end - start) / 1000;
//ESP_LOGI(TAG, "i2s_write wrote %d bytes, took %d mS", i2s_bytes_write, msElapsed);
cnt++;
while ((end - start)/1000 < SAMPLE_DURATION * 2) {
ESP_ERROR_CHECK(i2s_write(I2S_NUM_0, silence, sizeof(silence), &i2s_bytes_write, portMAX_DELAY));
end = esp_timer_get_time();
}
}