RPI 4 & Ubuntu MATE - Audio configuration

Updated: July 3, 2020

A few weeks ago, I got meself a 4GB Raspberry Pi 4 Model B, and set about a-tweakin' the living daylights out of it. I tried two operating systems - the official Raspberry Pi OS and Ubuntu MATE, and spent quite some time polishing both. Now, Ubuntu MATE has official, well-sorted images available for the older Pi models, but not just yet for the lasted board. This meant a fair deal of manual changes.

So far, I've shown you how to get rid of black bars and funny screen resolution, how to get HW acceleration, and now we will talk about audio setup - so you can have nice sound either via HDMI or 3.5mm headphones or external speakers. Like the other tutorials, the tweaks we need to introduce are based on how you'd normally do that using raspi-config, plus some extras. After me.

Boot configuration

There are two steps to our work here. First, we need to edit the /boot/firmware/usercfg.txt file, as I've shown you in a number of related tutorials already. Here, we need the following lines:

hdmi_drive=2
dtparam=audio=on

The first line forces HDMI mode (rather than VGA or DVI) for displays. The second line enables audio, and this will also load the snd_bcm2835 audio driver on system startup.

Make sure your user has access to the audio group

There are various ways you can do this. You can check what permissions and groups you have using the Users and Groups panel in Control Center in MATE. You can manually edit the /etc/group file, or you can run a command in a terminal window:

usermod -a -G audio "username"

You will need to restart your user session for the change to take place.

ALSA configuration

The second step is to configure your audio devices using amixer. In essence, what we're doing here is what the raspi-config tool can do, but if you do it manually, you actually understand what's happening behind the scenes, and you can make changes and tweaks with confidence.

sudo amixer cset numid=3 X

You need sudo, otherwise, you will see the following error:

amixer
amixer: Mixer attach default error: No such file or directory

The command will set the device (number 3) to use one of the three options:

sudo amixer cset numid=3 2
numid=3,iface=MIXER,name='PCM Playback Route'
  ; type=INTEGER,access=rw------,values=1,min=0,max=3,step=0
  : values=2

Now, the reason you can "blindly" assume that it's numid=3 you need is because Raspberry Pi 4 comes with only one hardware configuration. You can actually check what hardware you have with aplay:

aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 7/7
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 IEC958/HDMI [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: ALSA [bcm2835 ALSA], device 2: bcm2835 IEC958/HDMI1 [bcm2835 IEC958/HDMI1]
Subdevices: 1/1
Subdevice #0: subdevice #0

The changes are immediate, and you do not need to restart or login again. If you put a headset on, you will see the joy of this right away. Lastly, if you want your configuration to be committed and re-used every time you log into your session, you can create a configuration file for the ALSA sounds system. This is done using a hidden file .asoundrc in your home directory. Again, replace X with the choice above.

pcm.!default {
  type asym
  playback.pcm {
    type plug
    slave.pcm "output"
  }
  capture.pcm {
    type plug
    slave.pcm "input"
  }
}

pcm.output {
  type hw
  card X
}

ctl.!default {
  type hw
  card X
}

What about PulseAudio?

Now, this is a complicated topic, and sometimes, having the two - Pulse and ALSA - side by side, can cause problems. But then, it is also possible to make them coexist and work together fine. Which is why I may write an additional tutorial that will elaborate on this aspect of the audio subsystem as well. For now, ALSA it is.

Conclusion

If there was a problem, yo I solve it. We just did. We have audio, and that means our Pi 4 board is now becoming a proper computer in its own right. After all, I set upon this ambitious journey to transform my Raspberry into a full-experience mini desktop, and we're getting there. When I introduced my project in the first article, I promised you a bunch of guides, and I hope you're happy with the results.

We're not done. We still have a few more tasks ahead of us. I'm also going to show how to tweak the Network Manager, and we will also have a generic MATE desktop tutorial. Y'know, all the fine bits and pieces that will steer us toward a seamless, perhaps even perfect experience. Applications, themes, icons, desktop settings, the whole deal. So stay tuned for another slice of Pi. Word to your Tux.

Cheers.

You may also like: