Pi I2S Tweaks

Reducing popping

For people who followed our original installation instructions with the simple alsa config, they may find that the I2S audio pops when playing new audio.

The workaround is to use a software mixer to output a fixed sample rate to the I2S device so the bit clock does not change. I use ALSA so I configured dmixer and I no longer have any pops or clicks. Note that the RaspPi I2S driver does not support dmixer by default and you must follow these instructions provided to add it. Continue on for step-by-step on how to enable it!

Step 1

Start by modify /boot/config.txt to adddtoverlay=i2s-mmap

Run sudo nano /boot/config.txt and add the text to the bottom like so:

Save and exit.

Then change /etc/asound.conf to:

pcm.speakerbonnet {
   type hw card 0 
}

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

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

ctl.dmixer { 
  type hw card 0 
}

By running sudo nano /etc/asound.conf

This creates a PCM device called speakerbonnet which is connected to the hardware I2S device. Then we make a new 'dmix' device (type dmix) called pcm.dmixer. We give it a unique Inter Process Communication key (ipc_key 1024) and permissions that are world-read-writeable (ipc_perm 0666) The mixer will control the hardware pcm device speakerbonnet (pcm "speakerbonnet") and has a buffer set up so its nice and fast. The communication buffer is set up so there's no delays (period_time 0, period_size 1024 and buffer_size 8192 work well). The default mixed rate is 44.1khz stereo (rate 44100 channels 2)

Finally we set up a control interface but it ended up working best to just put in the hardware device here -ctl.dmixer { type hw card 0 }

Save and exit. Then reboot the Pi to enable the mixer. Also, while it will greatly reduce popping, you still may get one once in a while - especially when first playing audio!

Add software volume control

The basic I2S chipset used here does not have software control built in. So we have to 'trick' the Pi into creating a software volume control. Luckily, its not hard once you know how to do it.

Create a new audio config file in ~/.asoundrc with nano ~/.asoundrc and inside put the following text:

pcm.speakerbonnet {
   type hw card 0
}

pcm.dmixer {
   type dmix
   ipc_key 1024
   ipc_perm 0666
   slave {
     pcm "speakerbonnet"
     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"
}
This assumes you set up the dmixer for no-popping above!

Save and exit

Now, here's the trick, you have to reboot, then play some audio through alsa, then reboot to get the alsamixer to sync up right:

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

Then you can type alsamixer to control the volume with the 'classic' alsa mixing interface

Just press the up and down arrows to set the volume, and ESC to quit

Last updated on 2017-11-21 at 03.54.20 PM Published on 2017-11-21 at 11.46.30 PM