What you'll need to build this home music system
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.
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.
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:
Find the line that reads:
sudo nano /boot/config.txt
Remove the line (or add a # to the front to comment it out) then add these lines:
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:
dtparam=audio=off dtoverlay=justboom-dac lcd_rotate=2
You should hear a calm voice say ‘front centre’.
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:
Now install Mopidy and set its user’s permissions:
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
Finally, set Mopidy to run as a background service on boot:
sudo apt install mopidy sudo adduser mopidy video
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.
The file will be empty except for some comments. After the comment block, add this:
sudo nano /etc/mopidy/mopidy.conf
Save the file with CTRL+X and then start Mopidy:
[http] hostname = 0.0.0.0 [audio] output = alsasink
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.
sudo systemctl enable mopidy
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:
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.
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
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:
Add append the following:
sudo nano /etc/mopidy/mopidy.conf
Restart Mopidy to pick up the configuration changes and then initiate the first music scan:
[local] media_dir = /home/pi/Music
Now in Iris, you should be able to see your albums appear. Try playing one!
sudo mopidyctl local scan
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.
Now we’ll create a script to launch Chromium with all the correct settings:
sudo apt install unclutter
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:
Now try it out by running ~/kiosk.sh. If you’re entering commands remotely via SSH, use this command instead:
chmod +x ~/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:
Add this line, including the ‘@‘ sign:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
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’.
Hook up a CD drive to the USB port, insert a CD, and enter this:
sudo apt install abcde
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.
abcde -o flac