Make a Raspberry Pi audio player with Mopidy Music

By PJ Evans. Posted

Recently we built a standalone music server using a DAC (digital-to-analogue converter) HAT, Mopidy, Iris, and a Raspberry Pi touchscreen. All these things, together with a pair of decent speakers, guarantee audio bliss for your home or workplace. Now we’re going to extend the capabilities of Mopidy using its plug-in architecture. With a few commands, Mopidy can be extended to stream audio from a wide range of online services, from giants like Spotify to the gargantuan collections of the Internet Archive. This month we’re looking at the best Mopidy extensions and how to install them.

Expand your local library collection

Let’s start by looking again at our local collection. Storing music locally allows you reliable playback and complete control over how your music is organised. The downside is storage costs, so before embarking on ripping all your CDs, have a think about how you want to store them. Last month we discussed using abcde (A Better CD Encoder) to rip to FLAC, which produces large files with no quality loss. You can also consider using Ogg Vorbis. This open-source codec does involve quality loss, but produces much smaller files, and there’s every chance you won’t be able to tell the difference. To rip a CD to Ogg Vorbis:

abcde -o ogg

Warning! Where you live may or may not allow the copying of data from a CD. Check here for more info.

Tag, you’re it

Many CD rippers, abcde included, will use online services to find information about your CD and automatically name files after the artist, album, and track name. They will also record that information inside the file itself so servers like Mopidy can display details about what’s playing. This is called tagging. Unfortunately, the online services don’t always get it right, so if you find a tag is misnamed or contains mistakes, you can use Kid3 to set the tags yourself. To install from the command line:

sudo apt install kid3

This desktop app provides easy bulk retagging of files and can even import artwork. Figuring configuring

In the following steps, we’ll be setting up a number of online services to interface directly with your Mopidy installation. In many cases, you’ll need to make changes to the Mopidy config file, so it’s worth explaining how it’s formatted. Each extension is named ‘Mopidy-Extension’. In the config file located at /etc/mopidy/mopidy.conf, Extension becomes [extension] and any relevant options go underneath that line in the form ‘key = value’. For example:

[local]
media_dir = /home/pi/Music

We’ve provided an example config file to help make sure things are properly set up.

Add Spotify to your Raspberry Pi audio streaming system

Now your local collection is set up, let’s move on to the first of our extensions. Spotify is an amazing collection of music. Wouldn’t it be great to be able to access not only Spotify’s archive but also your playlists and tagged artists? If you’ve got a Spotify Premium account, you can do just that. Start by installing the extension from the command line:

sudo apt install mopidy-spotify

We now need to authorise Mopidy to use your account by visiting magpi.cc/mpdyspotify. Once you’re authorised, a config file will be displayed. Cut and paste this into /etc/mopidy/mopidy.conf and add your Spotify username and password. See the code listing overleaf for an example.

Now restart Mopidy:

sudo systemctl restart mopidy

Go back to Iris and the world of Spotify is literally at your fingertips.

Add audio services, like Spotify, SomaFM, and YouTube for a fully-fledged modern music system

Radio waves with TuneIn and RadioNet

How about adding radio stations to your Mopidy setup? There is easy and free access to thousands of radio stations streaming across the planet, catering for all interests. We have the option of two extensions for playing radio: TuneIn and RadioNet. The TuneIn extension provides access not only to live stations, but also historical recordings and is more suited to the curious browser rather that just getting to your favourite station. RadioNet is a simpler but easier-to-navigate service that allows you to create a list of favourite stations. To install:

sudo apt install mopidy-tunein
sudo pip3 install Mopidy-RadioNet
sudo systemctl restart mopidy

Playing podcasts

If you prefer your radio programmes ‘on demand’, you’re probably a fan of podcasts. You’ve got two options for listening to your favourite shows on Mopidy. If you like to subscribe direct to podcast RSS feeds, then it’s as easy as this:

sudo pip3 install Mopidy-Podcast
sudo systemctl restart mopidy

To use it, you need to point to an OPML file (a list of podcast locations), as shown in the listing. Most podcast clients can generate this for you. If you prefer to browse a huge catalogue of programming, then additionally install the iTunes plug-in:

sudo pip3 install Mopidy-Podcast-iTunes
sudo systemctl restart mopidy

Now you can use Browse to find your favourite show and listen on demand.

Listen to all your favourite podcasts directly from Mopidy and discover new shows with the iTunes Podcast catalogue

SomaFM

SomaFM was one of the early pioneers of internet audio streaming. Working through a donations model, it has expanded to over 30 channels, all with the theme of ambience but covering different genres from electronica to jazz. Often SomaFM makes for perfect background music. To add SomaFM to Mopidy (you’ve probably spotted a pattern emerging by now):

sudo pip3 install Mopidy-SomaFM
sudo systemctl restart mopidy

SoundCloud

SoundCloud is an excellent online showcase of music and other forms of audio. As well as providing a way for bands to sell direct to their fans, it’s a platform for new and upcoming material from unsigned musicians. You can browse SoundCloud’s vast collection from Mopidy, but as with Spotify, you need to have an account (it’s free to join) and authorise your installation.

First, visit mopidy.com/ext/soundcloud and follow the instructions to authorise Mopidy. Copy and paste the resulting config file, and add it to /etc/mopidy/mopidy.conf. Now install the SoundCloud extension:

sudo pip3 install Mopidy-SoundCloud sudo systemctl restart mopidy

YouTube

The ubiquitous YouTube contains countless hours of audio-specific content. Either listen to one of the many audio recordings uploaded or just the soundtrack of a favourite video. Again, the standard installation pattern applies:

sudo pip3 install Mopidy-YouTube
sudo systemctl restart mopidy

One difference with this extension is the need to specifically enable it in /etc/mopidy/mopidy.conf. Make sure this is in the file or YouTube files will not appear in search results:

[youtube]
enabled = true

Digging through the archives

The Internet Archive (archive.org) is one of the crowning achievements of the internet. This vast resource aims to preserve anything that can be expressed in digital form. Unsurprisingly, its audio collection is vast, containing thousands of hours or public domain recordings, from vintage music to public speeches, documentaries, and a massive catalogue of Grateful Dead recordings! You can access all of these from your Mopidy system:

sudo pip3 install Mopidy-InternetArchive
sudo systemctl restart mopidy

We recommend using the ‘Browse’ feature of Iris to navigate this resource. Search results can be overwhelming!

Party time, excellent!

You now have access to a mind-boggling array of music and audio. Time to celebrate. We’ve been using Iris as our interface to Mopidy, but in fact you can install different interfaces and even run them concurrently. If you’re having a few friends around, Mopidy-Party allows you to safely give everyone access to your music collection from their phones and add their picks to a playback queue. Now, who put Showaddywaddy on?

Make your own

Something missing from your collection? Is there no access to the latest and greatest streaming service? If you’re handy with a bit of Python coding, then you’re free to make your own Mopidy extensions and share them with users all over the world. Not only have the Mopidy developers provided detailed documentation, they have also provided a ‘cookiecutter’ project at magpi.cc/mpdycookie which will get you up and running in no time. Contributions to improve existing extensions are also welcomed.

Click here to download the full code

[http]
# Make sure the web interface can be accessed by the local network
hostname = 0.0.0.0

[audio]
# Direct audio to the DAC, not HDMI
output = alsasink

# The File extension isn't of much use, so we can disable it
[file]
enabled = false

# If enabled, this adds support for M3U playlists
[m3u]
enabled = false

# Location of local music
[local]
media_dir = /home/pi/Music

# Replace with the information given when you authorise
# then add in you Spotify username and password
[spotify]
username = <Spotify username>
password = <Spotify password>
client_id = <Client ID>
client_secret = <Client secret>

# Getting to many results in TuneIn? Add this so you only get stations.
[tunein]
filter = station

# Once authorised, add the provided token here
[soundcloud]
auth_token = <Auth token>

# Most extensions are enabled by default. You must explicitly enable YouTube.
[youtube]
enabled = true

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.