I2S audio on Raspberry Pi OS “trixie”

I have an audio project (streaming radio) running on a Raspberry Pi. When I tested it on the latest version of the OS (“trixie”) I couldn’t get the audio to work. This post explains what worked for me.

The amp I am using is the Adafruit I2S 3W Class D Amplifier, based on the Analog Devices MAX98357A chip. Adafruit provide lots of support info on their website, including a script that is supposed to do all the setup necessary. I have used it in the past, with previous versions of the OS. However, with trixie, it didn’t work at all. I either got no output at all or a very distorted noise.

After a bit of research and testing, I found that there were only a couple of changes required. The following assumes you are starting from a clean installation of the OS.

Before doing anything else, make sure all the software is up to date by running the commands:

sudo apt update
sudo apt full-upgrade -y

Note: all my projects run “headless”, i.e. without an attached monitor and therefore without a graphical UI. So my explanations assume the use of the command line.

There are two changes required:

  1. Modify /boot/firmware/config.txt
  2. Add/modify the file /etc/asound.conf

config.txt

Edit the file in your favourite editor (or vi) and add the line:

dtoverlay=googlevoicehat-soundcard

Or:

dtoverlay=max98357a

I think I have tried both of those and they both worked.

Then comment out the following line (this may not be required so you could try without deleting it first):

#dtparam=audio=on

asound.conf

Create or modify the file with the following content:

pcm.sndrpigooglevoi {
   type hw card 0
}

pcm.dmixer {
   type dmix
   ipc_key 1024
   ipc_perm 0666
   slave {
     pcm "sndrpigooglevoi"
     period_time 0
     period_size 1024
     buffer_size 8192
     rate 44100
     channels 2
   }
}

ctl.dmixer {
    type hw card 0
}

pcm.softvol {
    type softvol
    slave.pcm "dmixer"
    control.name "PCM"
    control.card 0
}

ctl.softvol {
    type hw card 0
}

pcm.!default {
    type             plug
    slave.pcm       "softvol"
}

Note: In older versions of the OS, sndrpigooglevoi used to be speakerbonnet

Reboot and test

Now reboot (sudo reboot) and then test the audio output with:

speaker-test -c2 --test=wav -w /usr/share/sounds/alsa/Front_Center.wav

If you get an error like:

ALSA lib pcm_direct.c:1258:(snd1_pcm_direct_initialize_slave) requested or auto-format is not available
ALSA lib pcm_dmix.c:1011:(snd_pcm_dmix_open) unable to initialize slave

Try running raspi-config and select: System settings > Audio > MAX98357A

Avoiding clicks

To prevent clicks at the start and end of playback, you can add a service in the fileĀ /etc/systemd/system/aplay.service. This plays “nothing” as a background task, so the audio system is always active.

[Unit]
Description=Invoke aplay from /dev/zero at system start.

[Service]
ExecStart=/usr/bin/aplay -D default -t raw -r 44100 -c 2 -f S16_LE /dev/zero

[Install]
WantedBy=multi-user.target

Save the file and start the service with:

sudo systemctl enable --now aplay

That’s it. I hope it works for you.

Leave a Reply

Your email address will not be published. Required fields are marked *