Dan Bader

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.

My Raspberry Pi

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:

  1. Install PulseAudio by running sudo apt-get install pulseaudio
  2. 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"
  1. 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.
  2. 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:

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!

  1. The Raspberry Pi Foundation is a charity project that wants to turn kids into hardware and software hackers. This is a good thing. 

  2. You can find out the installed firmware version on your Pi by running vcgencmd version. The Linux kernel version can be determined by running uname -a

  3. See this issue on GitHub for further information on the problem. 

  4. If you haven’t installed and set up mpd please look at this guide

📘 Write Cleaner & More Pythonic Code: Discover Python’s best practices with simple examples and start writing even more beautiful + Pythonic code. My book Python Tricks will show you how: » Click here to get a free sample chapter

Improve Your Python with a fresh 🐍 Python Trick 💌 every couple of days

🔒 No spam ever. Unsubscribe any time.

Related Articles:
  • Monochrome font rendering with FreeType and Python – For my Raspberry Pi internet radio project I needed a way to render text suitable for a low resolution monochrome LCD. This article describes how to render 1-bit text using FreeType and Python.
  • A Framework for Remote Usability Evaluation on Mobile Devices – PDF and supporting materials for my computer science bachelor’s thesis at TU München in 2011.
  • Co-Activity Detection with Mobile Sensors – PDF and supporting materials for my computer science master’s thesis at TU München in 2012.
  • 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.
Latest Articles:

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

← Browse All Articles