Distorted start of sound from I2S output

clydeps
Posts: 8
Joined: Tue Jun 14, 2022 12:17 am

Distorted start of sound from I2S output

Postby clydeps » 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:
scope_4.png
scope_4.png (35.26 KiB) Viewed 1986 times
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();
        }
    }

ESP-Kevin
Posts: 9
Joined: Fri Jul 01, 2022 6:10 am

Re: Distorted start of sound from I2S output

Postby ESP-Kevin » Wed Nov 16, 2022 3:38 am

May I ask how many bytes are wrote in every loop? Could you take a try to increase the sending buffer size.

As you know, the i2s dma buffers are linked as a ring, and it'll keep looping once it start. Therefore, if the sending buffer size is short, there might be some interval before transmitting the next buffer.

clydeps
Posts: 8
Joined: Tue Jun 14, 2022 12:17 am

Re: Distorted start of sound from I2S output

Postby clydeps » Wed Nov 16, 2022 4:24 am

As you know, the i2s dma buffers are linked as a ring, and it'll keep looping once it start.
I did not know that for sure since the documentation is silent on that aspect, though I determined that was the case by experimentation (if the buffers are not kept full it outputs noise). But in any case I am keeping the buffer full, and have tried various buffer sizes without any change in behaviour.

I however believe that I now have isolated the problem to hardware, not software. I was powering the amplifier chip from the 3.3V supply from the ESP32 board and although I could not see significant noise on the 3.3V supply line associated with the problem, if I power it instead from the 5V (USB) supply the problem goes away. There are two possible explanations that I have thought of for this:

1. The 3.3V supply may have marginal current capacity for the spike in current when the tone starts - but I did put a scope on the 3.3V line and could not see any significant noise or voltage sag;

2. It's possible that the 1M resistor pulling up the SD input to the MAX98357A is marginal for a 3.3V supply - but I changed that to a small resistor (to force it into L channel only mode) and the behaviour did not change.

So while I'm still not sure of the exact nature of the problem, it appears to be not software related. I'm just breadboarding right now so the final product will have a better power supply for the amplifier.

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 183 guests