Crackle-free audio on the Raspberry Pi with mpd and PulseAudio
The Raspberry Pi single-board computer is a wonderful little machine. Sadly, its audio output is a bit riddled with software problems and produces crackling and popping noises. In this article I’ll explain how to configure your Pi to get crackle-free audio playback on Raspbian Linux with mpd and PulseAudio.
Update: The problem seems to be fixed, please read below.
Update: If you’re still having problems with the latest firmware, please read below.
The problem
While the Raspberry Pi is a wonderful little machine for experimentation and buying it is good for your karma1 it has – at least with the current 346337 firmware and Raspbian Linux 3.2.272 – an audio playback problem:
Starting and stopping playback creates strange (and loud) crackling and popping noises on the analog audio output.
This makes the Pi almost unusable for certain audio playback tasks. For example, I’m using the Raspberry Pi to build an internet radio and I can’t stand the noise that appears everytime I switch stations. Luckily, a relatively simple fix for this problem exists that I’m going to explain to you now.
The fix
To fix the audio problems we use the mpd audio player for playback and configure it to use PulseAudio as its audio backend. Additionally, we’ll tell PulseAudio to never let the audio hardware go to sleep. This gives us crackling and popping-free audio playback on Raspbian 3.2.27 and firmware 346337. This guide assumes that you’ve already setup mpd correctly4. Before I explain the steps to apply the fix I’ll give a brief introduction to mpd and PulseAudio.
Music Player Daemon (mpd) is a minimalistic audio player that runs well even on weak hardware. A plain installation of mpd comes without a graphical user interface. Instead, you control mpd with simple text-based commands such as play
, pause
, or setvol 85
. Mpd runs as a background service that handles audio playback, remote streaming, and playlists. It’s quite resource efficient and therefore runs nicely on the Rasbperry Pi.
PulseAudio is a sound system for Linux – this means that it works as a proxy between your audio hardware and programs that want to play sounds. It also supports advanced functionality, such as routing audio from one machine to another. But how does using PulseAudio fix the Pi’s buggy audio output? PulseAudio can be configured to never suspend the audio device it is writing to – even if no audio is playing. Normally, this is a disadvantage as the suspend mode sends the audio hardware to sleep to save power. On the Raspberry Pi the crackling and popping audio problems seem to appear when the audio hardware goes to sleep or wakes up again. Disallowing the audio hardware to sleep therefore fixes the audio problems.
To apply the fix follow these steps:
- Install PulseAudio by running
sudo apt-get install pulseaudio
- Configure mpd to use PulseAudio as output plugin by editing
/etc/mpd.conf
. You need to remove or comment out the lines that refer to the Alsa output plugin and add these lines instead:
audio_output { type "pulse" name "MPD PulseAudio Output" }
- Edit
/etc/pulse/default.pa
and remove or comment outload-module module-suspend-on-idle
. This is the important part that prevents PulseAudio from sending the audio hardware to sleep. I’ve read that simply using PulseAudio lessened the problem for some people. But on my Pi only disabling idle suspend really helped. - Restart PulseAudio and mpd to let the configuration changes take effect. You do this by running the following two commands:
sudo /etc/init.d/pulseaudio restart sudo /etc/init.d/mpd restart
These steps completely fix the audio crackles and pops with mpd on my 512 MB Model B Raspberry Pi. I can issue mpc pause
and mpc play
all day long without hearing any crackling noise.
More information
Here are other web sites and forums that also discuss the Raspberry Pi “crackling noise” audio problem:
- Issue #128 of raspberrypi/linux on GitHub This is the official bug report where software solutions and interim fixes are discussed. It’s likely that this problem will be fixed in a future firmware or driver update.
- Corresponding thread in the official Raspberry Pi forums Several fixes for the audio problem are discussed here. This is where I got the initial idea to use PulseAudio from.
- Issue #175 of xbianonpi/xbian on GitHub Xbian is a Linux distribution for the Pi that is aimed at media center use. It exhibits the same audio problems as Raspbian. This bug report also discusses various ideas for fixing the problem.
- Eben Upton’s message to the ALSA development mailing list The founder of the Raspberry Pi Foundation, Eben Upton, seems to be aware of the Pi’s audio problem. He says that the Foundation wants to hire ALSA developers to solve the problem. This is really good news.
Some mpd users also suggested that the mpd pipe
output plugin should be used in combination with afplay
. For me this didn’t work as afplay also disables the audio device when it is idle, thus leading to the same crackling noises. The only thing that helped in my case was to use mpd in together with PulseAudio, as described here.
Update: The problem seems to be fixed
According to this comment on the GitHub issue the problem seems to have been fixed or at least mitigated in a recent firmware update. I’m now using firmware 380831 that I installed via rpi-update. Firmware commit 779f0fb specifically targets the crackling noise problem. Updating the firmware prevents any noticeable crackling on my Pi and I’ve switched back to using the Alsa output plugin for mpd. This means that with a properly updated firmware you very likely do not have to use the PulseAudio workaround anymore.
I’ve followed these steps to update my Pi’s firmware:
# Update all Raspbian packages. # Do this *before* the firmware update. sudo apt-get update sudo apt-get upgrade # Install rpi-update as described at # https://github.com/Hexxeh/rpi-update sudo apt-get install git-core sudo wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update sudo chmod +x /usr/bin/rpi-update # Backup the existing firmware. sudo cp /boot/start.elf /boot/start.elf.knowngood # Update to the latest firmware and activate it. sudo rpi-update sudo reboot
Update: Alternative solution
Ralf Goecke sent me an email where he mentioned that the crackling came back with newer firmwares and modules 3.18.6. However he’s found another workaround that involves disabling “timer-based audio scheduling” in PulseAudio.
You can give this a shot by making a change to /etc/pulse/default.pa
and adding the tsched=0
parameter:
### Automatically load driver modules depending on the hardware available .ifexists module-udev-detect.so load-module module-udev-detect tsched=0
Thanks for sharing, Ralf!
-
The Raspberry Pi Foundation is a charity project that wants to turn kids into hardware and software hackers. This is a good thing. ↩
-
You can find out the installed firmware version on your Pi by running
vcgencmd version
. The Linux kernel version can be determined by runninguname -a
. ↩ -
See this issue on GitHub for further information on the problem. ↩
-
If you haven’t installed and set up mpd please look at this guide. ↩