Build a home music system with Raspberry Pi

By PJ Evans. Posted

The ability to play music throughout your home, in perfect sync and controllable from your phone, has been around for a few years now. Many solutions are available, from simple Bluetooth speakers to high-end systems like Sonos. Often the price can be off-putting and there has been recent controversy over the closed nature of these systems. Can we build a whole-house audio system using nothing but open-source technology and our favourite little computer? This month, we’ll start by building a dedicated music-playback station that can produce audiophile-level sound and web-based control.

What you'll need to build this home music system

Hardware assembly


The JustBoom DAC (digital-to-analogue converter) HAT provides high-quality audio output for our project. It also blocks the GPIO header, which we need to power the touchscreen. Luckily, the HAT has a GPIO breakout, so we soldered a two-pin header to the 5V and GND for the touchscreen so the whole system can be powered by a single connector. You can also use two power supplies if you prefer. Mount the DAC HAT on the GPIO pins and secure. We used 20mm 2.5M screws to secure the HAT to the computer and screen controller standoffs. Connect the 5V and GND pins for the touchscreen to their partners on the DAC HAT.

The DAC HAT provides high-resolution output at line level. You can swap this with an amplifier HAT and use passive speakers instead

Prepare Raspberry Pi OS

The case prevents access to the microSD card, so make sure you’ve got a basic setup of Raspberry Pi OS before assembling everything. For this project we’re going to be using Chromium, so we need the ‘standard’ OS image with a Desktop (but we don’t need the Recommended Software version). Burn this image with your preferred method and make sure you’re connected to the network with SSH enabled or hook up a mouse and keyboard if you prefer. Finally, as ever, make sure you’re running the latest version of everything with sudo apt update && sudo apt upgrade.

Mount into the case


The Raspberry Pi 4B case used here will not accept the assembly with the DAC HAT. If you’re not using a HAT, you can skip this step. Otherwise, carefully remove the two wall mounting screw holes and then line up the HAT with the top ventilation spacers and remove any that are blocking the two phono sockets and the 3.5mm socket from sitting flush in the case. Measure twice and cut once! The plastic is soft, so it’s fairly easy to do. Once the assembly is sitting comfortably in the case, you can screw in the touchscreen and it should now sit up securely.

We’ve modified the case and removed the back so everything fits. The screen takes its 5V feed from the GPIO breakout on the DAC HAT.

Enable DAC output

No sound will come from the DAC HAT by default. Luckily, the required drivers are already installed and waiting to be enabled. From the command line, run the following:

sudo nano /boot/config.txt
Find the line that reads:
dtparam=audio=on
Remove the line (or add a # to the front to comment it out) then add these lines:
dtparam=audio=off
dtoverlay=justboom-dac
lcd_rotate=2
That last line flips the screen if you’ve found, as we did, that the touchscreen display is now upside down. Reboot your Raspberry Pi computer and from the command line, test audio output:
aplay /usr/share/sounds/alsa/Front_Center.wav
You should hear a calm voice say ‘front centre’.

Install Mopidy


Mopidy is a headless music server. This means it has no user interface of its own, so we’ll add one later. To install Mopidy, we need to add its repository to APT. Run the following commands one-by-one:

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/buster.list
sudo apt update
Now install Mopidy and set its user’s permissions:
sudo apt install mopidy
sudo adduser mopidy video
Finally, set Mopidy to run as a background service on boot:
sudo systemctl enable mopidy

Configure and test Mopidy

Before Mopidy can be run, we need to configure a few things. The default settings are sensible but if you want to access the web interface (once installed) from another device, we’ll need to allow that.

sudo nano /etc/mopidy/mopidy.conf
The file will be empty except for some comments. After the comment block, add this:
[http]
hostname = 0.0.0.0

[audio]
output = alsasink 
Save the file with CTRL+X and then start Mopidy:
sudo systemctl enable mopidy
In a browser, go to http://<hostname>:6680/ (replace <hostname> with the name of your Raspberry Pi – it’s ‘raspberrypi.local’ by default). If you get a rather boring welcome page displayed, all is working well.

Install the UI & file support


Mopidy is made up of modules. Out of the box it can’t do very much, but with some add-ons it becomes powerful. Next we’re going to install Iris, a user interface, and support for playing music stored on the local system. Run these commands:

sudo pip3 install Mopidy-Iris
sudo pip3 install Mopidy-Local
sudo sh -c 'echo "mopidy ALL=NOPASSWD: /usr/local/lib/python3.7/dist-packages/mopidy_iris/system.sh" >> /etc/sudoers’
sudo systemctl restart mopidy
This installs the Iris and Local modules then gives Iris permission to run Mopidy commands as the superuser. Finally, we restart Mopidy to pick up the changes. In a web browser, go to http://<hostname>:6680/iris/ to see our shiny new interface.

The Iris interface works exceptionally well with the touchscreen set up in kiosk mode

Just add music

Our installation is now ready to play music, so we should probably get some! Mopidy supports a wide range of formats including MP3 and FLAC. Whatever you decide to use, create a space for your music and upload some files there. You’ll find a Music folder already exists in your home directory. We need to tell Mopidy where to look for your files. Edit the Mopidy configuration file:

sudo nano /etc/mopidy/mopidy.conf
Add append the following:
[local]
media_dir = /home/pi/Music
Restart Mopidy to pick up the configuration changes and then initiate the first music scan:
sudo mopidyctl local scan
Now in Iris, you should be able to see your albums appear. Try playing one!

Kiosk mode

To get Iris running on the touchscreen, we can make Chromium run in ‘kiosk’ mode so no other elements of Raspberry Pi OS Desktop can be seen. The first step is to install ‘unclutter’, a handy app for hiding the mouse pointer when not in use.

sudo apt install unclutter
Now we’ll create a script to launch Chromium with all the correct settings:
nano ~/kiosk.sh
Add the code shown in the listing. When run this will enable cursor hiding, prevent some Chromium error messages from being displayed and then start Chromium in kiosk mode, going directly to the Iris website in full-screen.

Implement the kiosk script

Before we can use the script, it needs to be flagged as an executable:

chmod +x ~/kiosk.sh
Now try it out by running ~/kiosk.sh. If you’re entering commands remotely via SSH, use this command instead:
DISPLAY=:0 ~/kiosk.sh
All being well, Iris should start full-screen and be controllable by touch. We recommend going into settings and enabling ‘Wide scrollbars’ to make it easier to control with fingers. You can also choose between light and dark themes, the darker theme being a little kinder on the eyes when used with the touchscreen.

Run on boot


The final step for our music system is to make it run on boot and go straight into kiosk mode. We’ll ask the Desktop itself to do this. Enter the following in the command line:

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
Add this line, including the ‘@‘ sign:
@/home/pi/kiosk.sh
Save the file and reboot your system. The desktop will appear and then Iris will boot full-screen. Your touchscreen music system is now ready for use. You can control everything from either the touchscreen, a web browser on any device, or your smartphone.

#!/bin/bash
unclutter -idle 0.5 -root &
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences
/usr/bin/chromium-browser --noerrdialogs --disable-infobars --kiosk http://127.0.0.1:6680/iris &

Load up the music

If you want to add some audiophile-grade music to your collection, you can use your Raspberry Pi computer to rip CDs to FLAC, a lossless compression system. The files are are large, but no music detail is lost. A very easy-to-use ripper is ‘A Better CD Encoder’ aka ‘abcde’.

sudo apt install abcde
Hook up a CD drive to the USB port, insert a CD, and enter this:
abcde -o flac
Abcde will guide you through the whole process, including automatically naming the tracks. Place the resulting directory in your Music directory and go to Settings in Iris to initiate a rescan.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.