With the holidays coming up, many of us can expect to spend time with our friends and families, often listening to seasonal music, or indeed escaping the seasonal music! Whether you’re joining in or getting away from the crowd, this simple project can help.
This is part one of a series of posts where I build up my own ‘smart’ speaker using a Raspberry Pi, some Open Source software, and a few other inexpensive components. In this part I’ve built a simple music player which shows up as a Spotify Connect device on the network. That means it can be controlled via the official Spotify app on any computer or mobile device.
Once setup, the device can have headphones, speakers, TV or a full sound system attached to stream music and podcasts from my favourite Spotify playlists. All I needed for this project was a network-connected Raspberry Pi 2 and some headphones or speakers. The Spotifyd software used requires a premium Spotify account. Many thanks to Simon Persson who created Spotifyd and published it in the Snap Store.
I used a Raspberry Pi 2 which is more than capable, but this project will also work on newer, faster, Raspberry Pi 3. The software I’m using is Spotifyd which registers as a ‘Spotify Connect’ device and can play audio on demand, when instructed by other devices running the official Spotify app.
Get The Party Started
In a previous blog post – Secure VPN Connection with Ubuntu Core I selected Ubuntu Core for the OS on the Pi. I’m following the same path with this project, using the latest download of Ubuntu Core to get started.
Ubuntu Core is a lean, secure, automatically updated OS designed for devices like the Raspberry Pi. It’s perfect for an appliance you want to setup and leave somewhere, safe in the knowledge it’s kept up to date
The steps for grabbing the Ubuntu Core image and flashing to a microSD card are quick and straightforward. Much like installing any other OS on a Pi. There is a setup process on first boot which walks you through connecting to wired or WiFi network (if available).
If you already have a Raspberry Pi 2 or better running a recent install Raspbian, you can use that instead of Ubuntu Core. You just need to
sudo apt install snapd before continuing with the rest of these steps.
Refractions in the Plastic Pulse
Next up I needed to install PulseAudio which the Spotifyd daemon will connect to later. It’s a good idea to also install PulseMixer, which enables us to control the volume and check everything is working. Installing both is simple as they’re both available as snaps.
snap install pulseaudio
snap install pulsemixer
PulseAudio will run as root in the background and listen out for incoming connections. We can check the audio levels by running
sudo pulsemixer on the Pi in an SSH session. Here’s what that looks like. Note the
bcm2835 device which is the sound ‘card’ in the Raspberry Pi. We can adjust the volume with the arrow keys.
Red Chip Installation
Now we’re ready to install Spotifyd. This is as simple as:
snap install spotifyd
Once installed, Spotifyd will automatically start without any configuration. However, it defaults to using ALSA, but we’d like to use PulseAudio as the backend. Spotifyd is built with the PulseAudio backend, so we need to make sure it’s used. That’s pretty easily done with a configuration file. On the Pi, issue the following commands:
$ sudo mkdir -p /root/snap/spotifyd/current/.config/spotifyd
Once created, we can add a configuration file:
$ sudo vi /root/snap/spotifyd/current/.config/spotifyd/spotifyd.conf
Add the following lines to the file, and save it.
backend = pulseaudio
device_name = pi
Note I’ve also added a
device_name which is what you’ll see in the official Spotify client under ‘Connect to device’. If you setup more than one device, you could name each differently, perhaps based on what room they’re in. There are some limits such as name cannot contain spaces. For more details on the options in the configuration file, check out the upstream Spotifyd project on GitHub.
For the changes to take effect, restart Spotifyd.
sudo snap restart spotifyd
That’s it! Plug in some headphones, open up the Spotify client on a mobile device or PC and start playing on the Pi!
If things aren’t playing nicely, there’s a couple of things we can do to debug. On the Pi we can use
sudo pulsemixer to see what the volume is set to, adjust it, and confirm whether Spotifyd is connected to PulseAudio.
Sometimes the daemon needs restarting, which can be done with a simple
sudo snap restart spotifyd. We can also get to the logs of Spotifyd with
snap logs spotifyd. If you uncover interesting bugs, I’d recommend filing them on the upstream Spotifyd issue tracker.
There’s a few things I can imagine doing to improve this. It might be cool to have some audio visualization output on the HDMI port while music is playing. That would be great if used as a jukebox as a party, with a big screen on. It could also potentially display track information on the display too.
The audio circuitry in the Raspberry Pi isn’t what you might call ‘Studio’ quality. So another option might be to add a 3rd party USB sound card with a higher quality Digital to Analog Converter.
In a further update I will add the ability to voice control the device via Alexa, but that can wait until after the Christmas holidays. If you setup an Ubuntu Core Smart Speaker, do let us know over on the snapcraft forums!
Happy holidays everyone!