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: 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:

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.

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 out load-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.

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.

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.

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:

Spell-checking LaTeX documents with aspell – LaTeX is a great way to create beautiful documents. But its embedded markup makes it difficult to check LaTeX documents for spelling errors. This article explains how to setup and use a program called aspell to spell check your LaTeX documents.

Guessing a user’s favorite contacts on iOS – Many iOS applications contain an “invite your friends” feature. Ideally this feature should suggest people that the user is likely to invite. This article explains an App Store-legal method of guessing a user’s favorite contacts from their address book on iOS. The method is described in detail and a demo application is available for download.

🐍🐍 Peer-to-Peer Learning for Pythonistas PythonistaCafe is an invite-only, online community of Python and software development enthusiasts helping each other succeed and grow: » Learn more at pythonistacafe.com

Working With File I/O in Python – Learn the basics of working with files in Python. How to read from files, how to write data to them, what file seeks are, and why files should be closed.

How to Reverse a String in Python – An overview of the three main ways to reverse a Python string: “slicing”, reverse iteration, and the classic in-place reversal algorithm. Also includes performance benchmarks.