Page 1 of 1

I2S audio distortion issue

Posted: Thu Mar 20, 2025 9:07 pm
by Sophi9999
hello!
I have an ESP32-S3-WROOM on a custom PCB.
I'm retrieving a wav file (a song) from an SD card via SPI and playing it through DAC PCM5102A using I2S.

I am able to retrieve any wav file from the SD card and write the name of the file to the shell.
I am also able to play a generated tone with I2C through the DAC.

The problem is that after the wav file plays for a split second, it gets distorted.
I have an oscilloscope here, and have test points on BCLK, LRCK, and DOUT lines.

I've seen this issue reported in a few places, Reddit, Arduino forum and others, but no solution.
I'm working in microPython but if microPython is missing support for a needed parameter I can switch.

Setup

Code: Select all

i2s = I2S(
    0,  # I2S bus ID (use 0 or 1)
    sck=Pin(35),   # BCLK (Bit Clock)
    ws=Pin(36),    # LRCK (Word Select / Left-Right Clock)
    sd=Pin(37),    # DOUT (Data Out)
    mode=I2S.TX,   # Transmit mode
    bits=16,       # 16-bit audio
    #bits=32,       # 32-bit audio
    #format=I2S.STEREO,
    format=I2S.MONO,
    rate=44100,    # Standard audio rate (adjust as needed)
    #rate=22500,
    ibuf=100000     # Buffer size
)

Code: Select all

def play_specific_sound(): 
    file_path = "/sd/final_pump_clip.wav"
    
    #if file_path not in os.listdir("/sd"):
    if "final_pump_clip.wav" not in os.listdir("/sd"):
        print("File not found:", file_path)
        return
    print("Playing:", file_path)

    with open(file_path, "rb") as f:
        f.read(44)  # Skip WAV header

        while True:
            audio_data = f.read(128)  # Read in chunks
            if not audio_data:
                break       
            i2s.write(audio_data)  # Send to DAC
            # Convert 16-bit PCM to properly aligned I2S 16-bit format

    print("Playback finished")
Thank you!

Re: I2S audio distortion issue

Posted: Fri Mar 21, 2025 10:40 am
by ahsrabrifat
You are able to retrieve wav files from the SD card and write the name of the file to the shell. You are also able to play a generated tone with I2C through the DAC. That means your PCB is working. It's not a matter of manufacturing error. The problems that you're facing may occur due to buffer handling, I2S format mismatches, or timing problems. You can check the answers here:
https://esp32.com/viewtopic.php?t=30561

Re: I2S audio distortion issue

Posted: Fri Mar 21, 2025 1:10 pm
by MicroController
I'd try increasing the 'chunk' size first, like audio_data = f.read(1024) or f.read(2048) or even more.

Re: I2S audio distortion issue

Posted: Fri Mar 21, 2025 3:26 pm
by xiqwertyui
Hard to tell where exactly the problem is. But speaking with the parameter, It's not clear exactly which i2s format micropython calls the underlying i2s controller. Maybe check the format configuration (Pin16 of PCM5102A) in your PCB first.

Re: I2S audio distortion issue

Posted: Fri Mar 21, 2025 4:12 pm
by Sophi9999
Thanks all!

Pin 16 of PCM5102A is tied to GND: Audio format selection : I2S (Low) / Left-justified (High)
I am powering the PCM5102A DAC from a regulator on the board, not from ESP32 3.3V.
Supply looks stable.

Increasing f.read(2048) gives a timeout error. f.read(1024) does not help.
I downloaded more wavs to test and similar error... speed and sound distortion does all point to rate/ buffer errors.
However, short of trial and error, I don't know how to change these.

using ffmpeg to bring wavs to the same level does seems to change the wav but does not seem to match when I play the sound.

Code: Select all

ffmpeg -i scuse_me.wav -acodec pcm_s16le -ar 44100 -ac 2 scuse_me2.wav

Re: I2S audio distortion issue

Posted: Sun Mar 23, 2025 6:16 am
by xiqwertyui
Assuming your PCB is ok, and assuming their library is ok, then the problem may be with your code. Is your wav file single channel, if not you can't write it to i2s directly like the example code you show does.