Setting up Raspberry Pi and mpd (music player daemon) with Raspbian

Published: 2014-06-28
Tagged: raspberry-pi linux guide

Raspberry Pi + MPD setup (working title)

Today we're going to set up the Raspberry Pi as a home sound system. This assumes that the Pi is already set up - it has ssh access and some sort of network connection. I'm using a slimmed down version of Raspbian, but this should work with minor alterations for any other distro out there.

First, what do I mean by sound system? We'll allow the Pi to make our ears happy in two ways:

  1. Output sound through the built-in 3.5mm jack.
  2. Enable HTTP streaming.

I've been mostly using the first option. I hooked up the Pi to my awe-inspiring 2.1 sound system via the 3.5mm output. I can control it through one of three ways - the very bare bones mpc client, a more feature rich, curses based pms, and finally through an android application called Droid MPD Client, which is free to download. Using the bare bones mpc client, it is very easy to mess around with many other *nix utilities such as cron or at to get your sound system to play a specific track at a specific time. It's just plain awesome.

As far as the second option goes, I use it mainly on my laptop, since it's easier to store all my music in one location and use Clementine to receive the HTTP stream. This avoids having a duplicate library on my laptop, on the Pi, on my desktop, etc. This option would also work if you wanted to stream your music outside of your home, but this is out of the scope of this article. For those interested, I'll only mention that you need to secure your Pi (iptables, fail2ban), set up port forwarding on your router and dynamic dns and bam, you have music everywhere that you have wifi/4G. We truly live in blessed times.

The Setup

First, we're going to install everything we need to get things working:

sudo apt-get install mpd mpc alsa-utils

The alsa-utils package should give us some much needed functionality as well as some troubleshooting tools. The other two packages are what we're really interested in though - mpd or music player daemon is the server that either controls audio or HTTP streaming output, and 'mpc' or music player client is a handy command line tool to tell the server what to do. It's useful to have this both on the Pi as well as your laptop since it allows you to control mpd from any one of those machines.

Before we can get any sound out of our speakers, we have to load up the module responsible for sound output to the 3.5mm jack:

sudo modprobe snd_bcm2835

In order to persistently load this module at every boot, we have to add a single line - snd_bcm2835 to /etc/modules. This simple text file tells the system what modules to load at boot and one only has to add the modules name (one name per line) for it to work.

One more thing to consider is to increase the broadcom chip's volume:

alsamixer
# press F6 and choose bcm2835 ALSA
# use the up arrow to increase the volume to some higher value

Next up we're going to take care of configuring mpd itself. First off, we have add the user that mpd created to the audio group:

sudo usermod -G audio -a mpd

Now we're going to tweak the mpd configuration file found in /etc/mpd.conf. One of the changes is the location of the music directory and before we get into that, I'll note that mpd has to have read access to the music directory and all the files. This caused me to lose about 40 minutes trying to troubleshoot why mpd wasn't detecting any of my music. My solution involved adding my own user to the audio group and giving the music folder and all files in it rwx permission by using chmod -R 775 /mnt/music followed by chown -R mpd:audio /mnt/music.

Back at the mpd.conf file, starting from the top, we gotta change these lines:

# note: you can make this a softlink
music_directory = /var/lib/mpd/music

# ... scroll scroll scroll...

# be default, the audio_output for ALSA is uncommented and ready to go and doesn't need any changes
# however I also uncommented the HTTP streaming output, which is a few lines down:

audio_output {
        type            "httpd"
        name            "My HTTP Stream"
        encoder         "vorbis"                # optional, vorbis or lame
        port            "8001"
        quality         "8.0"                   # do not define if bitrate is defined
#       bitrate         "256"                   # do not define if quality is defined
        format          "44100:16:1"
}

# ...scroll scroll scroll...
# around line 328, uncomment the software mixer_type

mixer_type                      "software"

And that should be it! Restart mpd using sudo service mpd restart and check the log file under /var/log/mpd/mpd.log to see if there are any errors. Online resources such as the ArchWiki mpd page are pretty comprehensive when it comes to setting up and troubleshooting mpd so if you have any problems - check out that place first.

Using the Client

Everything is all well and good, but how do get the music going?

Mpd keeps track of music files in a database. It plays music off of one or more playlists. First thing we gotta do is to tell mpd to scan the music_directory and update the database. After that we have to add the tracks to a playlist. I'm not going to go into how to manage playlists to keep things short - I'll just show to add everything in the music directory to the default playlist:

mpc update && mpc ls | mpc add

Note that if you were using mpc on your laptop you have to change the target host because mpc uses localhost by default. Keeping that in mind, the above series of commands would look like this:

# 192.168.0.10 is your Pi's IP on your local network
mpc -h 192.168.0.10 && mpc -h 192.168.0.10 ls | mpc -h 192.168.0.10 add

To print the all of the tracks in the current playlist, just issue a:

mpc playlist -f "[%position%) - %file%]"

To play a specific track just enter:

mpc play 64 # 64 is the position number in this case

To get a better grasp of mpc check out the mpc man page.

Graphical Clients and HTTP Streaming

There's quite a few graphical clients that make managing playlists and actually playing music much easier than using mpc. A full list can be found here (ArchWiki). I personally stuck with pms. These clients operate in a similar fashion as does mpc, but are much more comfortable for everyday use. I like to control mpd with a free android client that I mentioned earlier since it provides a simple and intuitive GUI. Plus it's plain cool to touch a button on your phone and have music start playing : ).

Finally, if you uncommented out the HTTP configuration, you'll also be able to use an audio stream client to listen to your music. Just point it at the Pi's IP address and port 8000 (default) and you're all good. I use Clementine, but I think most players have built-in stream-listening functionality nowadays.

Bonus image: The wifi adapter RT2571WF chipset which runs so hot, it appears to shutdown from time to time - on a usb extension cord to keep the heat away from the Pi itself.

Update July 27th, 2014

I recently had to re-image the sdcard due to corruption (it lasted ~8 months) and while installing mpd again, I've noticed a few other settings have to be tweaked:

# this will make mpd reachable from outside localhost
bind_to_address "0.0.0.0"
# uncomment the next line:
group "audio"
# and this one too:
port "6600"

Oh and if the snd_bcm2835 modules isn't loading at boot, just add it to /etc/modules/:

# might need root
echo "snd_bcm2835" >> /etc/modules

Comments

There aren't any comments here.

Add new comment