Setting up a Hifiberry AMP+ with Raspbian, Mopidy and Spotify



My brother decided he wanted a multi-room audio set-up in his new house, but also wanted to be able to play separate streams in each room. Things had to be done on a budget, so a Sonos set-up or something similar with a beefy receiver with multiple zone support was out of the question. The Raspberry Pi 3 was one of the first things we thought of, along with Google's Chromecast. The Chromecast still requires an external amplifier, though, and with one Chromecast for each room, and an amplifier for each of those Chromecasts to drive its stereo speaker set, things would get clunky soon. The software on the Chromecast is also Googley, so that means you have little say in what goes on and if things break, you can't fix them yourself either. Like with smartphones, the big question with this kind of integrated stuff is always how long the manufacturer will support it; and with Android being catastrophical in updates, that was another box ticked for the Raspberry Pi. The latter is an insanely popular platform, and its add-on boards have proliferated with every generation gaining more tracioin. For our use case, an add-on board like the HiFiBerry AMP+. HiFiBerry also offers a universal case so you can tuck it all away neatly. It's not a looker, but it will do nicely; so you end up with a compact device, just a little bigger than a regular Raspberry Pi box.

Picking your distribution

It goes without saying you run some Linux on these babies. Raspbian is the Raspberry Pi Foundation's official Debian flavour, so that means plenty of support and a great knowledge base to fall back on; however, despite Debian Stretch being released recently, there don't seem to be any Stretch images available and you're 'stuck' with Debian 8 (Jessie). This is not necessarily a bad thing; Jessie will be getting updates for some time still in the future, and the stuff we need (like Mopidy) offers only Jessie binaries in its own repos (stuff in Debian Jessie's own repos, with all due respect, being rather old by now), and no Stretch binaries yet. There is a stock Debian Stretch image available for those who are adventurous enough; however, since Mopidy does not provide any ARM64 binaries (yet) you're on your own there; I got stuck compiling libspotify, and at that point the time I spent on it meant I wouldn't be able to keep the setup up to date without spending too much time tinkering and turning nuts and bolts manually.

I considered other, appliance-like solutions like RuneAudio, ArchPhile and Pi MusicBox. They all offer an integrated web interface, which is great, but the first two are based on Arch, which pretty much guarantees you'll need manual intervention at some point during maintenance, and the last one had its last RCs in March, whereas previous releases date back to September 2015. That doesn't instill much confidence concerning updates. Mopidy, it turned out, offers a web interface as well with some extra configuration; so the web interface was not a unique selling point. While most of the solutions listed allow you to use an MPD client (!) to interface with them, keep in mind Windows MPD clients are few and not well-maintained either, so a web interface is the logical solution if you have Windows clients to serve. RuneAudio also enables ZeroConf by default, so your 'player' can be found by other devices on the network; you can replicate this no doubt with a regular Debian or Arch Linux install, but in our case the client devices will be limited, and I'll be setting them up myself, so that's not really an issue.

Setting up Raspbian

Setting up Raspbian is pretty straightforward; dd it to an SD card, and do make sure that you have a file named SSH on your boot partition so the OpenSSH server gets enabled at boot and you can set-up remotely. Assign a static IP to your RPi as well.

Installing Mopidy

Run the following in your favourite terminal:

$ 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/jessie.list
$ sudo apt-get update 
$ sudo apt-get install mopidy mopidy-spotify mopidy-spotify-web

Mopidy needs access to the sound card(s), so add it to the appropriate group:

$ sudo gpasswd -a mopidy audio

Configuring Debian audio settings

The Raspberry Pi has onboard audio; we'll leave that enabled so we don't break anything. Low-level configuration is done in /boot/config.txt. To ensure the snd_bcm2835 module gets loaded, grep it for dtparam:

$ grep dtparam=audio /boot/config.txt 
dtparam=audio=on

If that looks good, add the Hifiberry AMP+ overlay:

dtoverlay=hifiberry-amp

Reboot, and check with aplay if the AMP+ is set up correctly:

$ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: sndrpihifiberry [snd_rpi_hifiberry_amp], device 0: HifiBerry AMP HiFi tas5713-hifi-0 []
  Subdevices: 0/1
  Subdevice #0: subdevice #0

Because there's two sound devices present now, we want audio software to pick the AMP+ (listed as card 1) by default. For this, we create a system-wide /etc/asoundrc with the following contents:

pcm.!default {
  type hw card 1
}
ctl.!default {
  type hw card 1
}

Setting up Mopidy

Basic configuration

You bootstrap the Mopidy service by running a few commands. The two last commands assume you use systemd as your init.

$ sudo mopidyctl config
$ sudo mopidyctl local scan
$ sudo systemctl enable mopidy
$ sudo systemctl start mopidy

System-wide Mopidy configuration is done through /etc/mopidy/mopidy.conf. Be sure to set the IP in the MPD section, by default it only listens on localhost. If there is no such section, just create one.

[mpd]
hostname = 192.168.1.175

To allow Mopidy access to your Spotify account, you'll need to register it through the Mopidy website. Enter your login credentials to generate a client ID and secret, which you can then all add to the configuration file:

[spotify] 
username = ...
password = ...
client_id = ...
client_secret = ...

Adding a web interface

What's great about Mopidy is it will work with most MPD clients; however, there are few actively maintained MPD clients for Windows. In fact, I haven't found any... For a good user experience, you're better off using a Mopidy web interface. Since Mopidy by default run its own web server, this is pretty easy (I do find the fact it automatically starts a web server a bit annoying though).

First, configure the mopidy-spotify-web extension. You do this through the system-wide Mopidy config file once more. The section can be pretty basic, just mirroring the Spotify one, except this one only needs the client ID and secret and not the actual login credentials:

[spotify_web]
client_id = ...
client_secret = ...

Make sure the HTTP section looks like this. If it doesn't exist, just add it like you did with the MPD one.

[http]
enabled = true
hostname = 192.168.1.175
port = 6680

Now you're all set to install a web interface like e.g. Moped. The easiest way is through the Python PIP tool:

$ sudo pip install mopidy-moped

Restart Mopidy, browse to http://192.168.1.175:6680/moped/, and you're good to go. Another web interface you might be interested in is Iris, which looks a bit more polished.

Listening to online radio

Install the Mopidy TuneIn extension for this and restart Mopidy:

$ sudo apt-get install mopidy-tunein
$ sudo systemctl restart mopidy