Raspberry Pi Whole Home Audio: Playing Music

It’s very rare that I tweet teasers to my blog post, but
last weekend I was so excited to be making progress on my Raspberry Pi Whole
Home Audio
project that I told the world I’d be publishing this one on
Thursday.

Here we are on Friday morning. There’s doubtless a lesson in
here for me about making promises I can’t keep, but I’m hoping you’ll forgive
me when you read about what I’ve done.

If you’ve been following my #RPiWHA Project
hashtag on this blog then you’ll know that when we left off last time I had
three Raspberry Pis networked together with synchronized clocks, and one of
them had access to the internet and the music library I keep on my home server.
That’s important, but not especially exciting. Today we’re going to move in a
more exciting direction, though.

Today we’re going to put Pis #2 and #3 aside, but we’re
going to get Pi #1 playing music!

Specifying and Testing the Pi’s Audio Output

The Raspberry Pi has two options for audio output. It has a
3.5mm analog output, and digital output through its HDMI port. It decides which
output to use automatically – if you have a HDMI monitor plugged in then the Pi
will detect this and assume you want to use HDMI for audio too.

That’s not the situation for me (my Pi is “headless” and
doesn’t have a display connected at all), and audio comes out of the 3.5mm
output automatically – which is what I want. If you need to specify, you can
run:

sudo amixer cset numid=3 1

The digit 1 at the
end means the Pi should use the analog audio output. Putting 0 there instead would mean auto-detect
(the default) and putting 2 would
mean digital output over HDMI.

Plug in some headphones or speakers and run:

aplay /usr/share/sounds/alsa/Front_Center.wav

You should hear a voice saying “front centre.”

Install Mopidy

We’re going to install music playing software called mopidy.
It provides a web-based interface so you can control your music, or there are
controller apps available in your app-marketplace of choice. Mopidy’s website
takes you through the
install process in detail
, but I’m going to summarize the commands here:

wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/mopidy.list
sudo apt-get update
sudo apt-get install mopidy python-setuptools

Done! We now have a basic install of mopidy, and the especially
astute amongst you will have noticed that we’ve also installed a package called
python-setuptools. Mopidy is written
in the python programming language, and the latter package is going to help us
with installing and setting up some mopidy extensions in an automated way. The
first thing we’re going to do is install something called pip, which is a tool for installing python packages:

sudo easy_install pip

Now that we have that, we’re going to in turn use pip to
install some mopidy extensions:

sudo pip install Mopidy-MusicBox-Webclient
sudo pip install Mopidy-WebSettings

With those commands we’ve added a web-based interface to our
player so we can control it from a browser on another computer or mobile
device.

Setting Up Mopidy

We’re almost ready to start mopidy and try things out, but
there is a little bit of initial setup to be done first. Edit the file ~/.config/mopidy/mopidy.conf. In the http section, change the following line
so that the web interface becomes accessible to any device on your network:

hostname = ::

In the local
section, tell mopidy where it should find your music library. In my case:

media_dir = /mnt/music

Save the file and exit. The next step is to tell mopidy to
scan your music folder:

mopidy local scan

If, like me, you have a large music collection stored on a
network share, be prepared for this to take a very long time. I started it
running before I went to bed, and it eventually completed the process about 45
minutes after I got up the following morning.

When it’s done though, we’re ready! Run mopidy:

mopidy

It’ll take a little while to fully start up, but when you
see the line

INFO     HTTP server running at [::]:6680

that means we’re good to go. Open the browser on your
computer or mobile device and point it to
http://192.168.1.71:6680/musicbox_webclient (replace 192.168.1.71 with the IP
address of your Pi as appropriate). Go to Browse, Local Media, and select a
song!

If all is well the music will be playing through the
headphones or speakers attached to the Pi, and we’re all done until the next
installment – rerouting the audio from Pi #1 and streaming it, synchronized, to
Pis #2 and #3 instead.

Enjoy!

Leave a Reply