Philip P. Ide

Author, programmer, science enthusiast, half-wit.
Life is sweet. Have you tasted it lately?

User Tools

Site Tools


blog:articles:raspberry:mpd_multiroom

Whole-home Music

If you read my article MPD Network, you know that I built a home music system using a Raspberry Pi model 4b, which is a credit-card sized computer, pulling a mere 15 watts of power, so is ideal for 'always on' usage. I also described using a NAS (essentially disk-drives on the network and so available to all devices) on which to store all my music. This made it easy to upload music from any machine and perform automatic backups whilst leaving the 'music centre' to just deliver music. I also use a VPN and configured MPD (the music player software) to also stream the music it was currently playing - allowing me to listen to my music when I'm out of the house.

It was time to rethink what was happening in the house.

Whole House Music

FLAC compression is similar to MP3 compression, but MP3 gets additional compaction by discarding data. For example, a CD records music in frames - 44,100 frames per second. Each frame is 256 bits of data plus a 16 bit value representing volume control. A track recorded in FLAC format will have every bit of data as it appears on the CD, and this equates to ~4.5Mb of data per minute of recording. An MP3 is usually recorded at 128 bits of data (plus 16 bit volume), and also discards many of the frames, which results in ~1Mb of data per minute of recording.

The human ear is happy enough with this degradation - it is noticeable, but for the vast majority of people only barely so. Even audiophiles like me are happy to listen to MP3, but prefer the higher quality of CD or better still HFPA.

I mention all this because you can link speaker to your music centre by BlueTooth, but BlueTooth has a lower bandwidth than the average MP3. If you want a whole-house music system, you probably don't want to compromise on audio quality, and besides, there is a limit to how many speaker you can link to you music centre by BT unless you fork out a fortune on BlueTooth repeater and splitter transmitters.

You have a network though, and I've already mentioned MPD can output to both a pair of speakers (or 2.1) and stream the same music via http at the same time. This means any computer in the house (or device with a web browser) can play the music simply by browsing to the stream. However, there is a much better solution: Snapcast.

Snapcast

I leave the http stream running because it allows me to access the music outside the house using MAFA, which is a very nice tool (on my Android phone) that gives me a vast amount of control to search my music and build playlists, whilst also controlling MPD. However, I'm turning off the MPD output to the speakers. Instead I setup MPD to output the music to a FIFO file (first-in, first-out).

audio_output {
        type "fifo"
        enabled "yes"
        name "snapcast"
        path "/tmp/mpd.fifo"
        format "48000:16:2"
}

The 'format' option details how to output the data it reads - if the data is different, it will convert it to this format.

Then I install Snapcast server, and in its configuration file, tell it to read that same file as its input. Additionally, I set the output stream to flac and set the chunk size to 26:

source = pipe:///tmp/mpd.fifo?name=myStreamName
sampleformat = 48000:16:2
code = flac
chunk_ms 26
buffer = 1000

The buffer value is in milliseconds, so 1000 = 1 second. This is the latency for the stream. It allows all clients to receive the data, decode it and send the data to the speakers.

At this point, you can't hear the music from the music centre's speakers. The Snapcast server doesn't send anything to the speakers, so now you need a client program to accept data from the the Snapcast server. Installing the Snapcast client is sufficient: the client automatically detects the server and the server tells it which streams it has available (there is only one - but it's possible to make more). The client connects to the first stream available and starts relaying that to the speakers.

Another Room

So, now you get another Raspberry Pi (preferably with a decent sound card attached) and connect some speakers to the it. Place it in another room and install Snapcast client. That's all there is to it. Repeat this for each additional room you want music in.

Snapcast Features

You put some music on, and now your whole house is blaring the music out, and you may only want certain rooms playing music at any particular time. Snapcast provides a web page on port 1708 of your music centre e.g.

http://music:1708

.. which allows you to turn individual rooms on/off as you like.

Before you turn anything off though, you may have noticed something else: all speakers in all rooms are synchronised, with none of them lagging behind any others. This is because Snapcast streams the music data in packets which have a timestamp set in the future - the receiving client waits until that moment to play the music. If any of the speakers are out of synch, adjust the latency value in the server configuration, the restart the server with the command

sudo systemctl restart snapserver

You'll also notice that the Snapcast web page also allows you to stream to your web browser. This means that you can stream to a device via Snapcast or via the MPD http stream.

Turn into an Alarm Clock

I've written some scripts for both Linux and Windows to turn the music system into an alarm clock. If you are running an Apple system, I'm sure you can make these scripts work for you. The scripts are written in PHP and are therefore cross-platform.

It is necessary to create a playlist on MPD called “Alarm”. You can put anything you like in the playlist, and you can change the contents (so you don't have to change the scripts!). My advice is don't use Beethoven's 5th, unless you're evil.

The first script is run as a cron job (or a Windows Scheduler task), and turns all the speakers off in the home except the device you designate as your bedroom unit. It then loads the Alarm playlist and starts it running.

The second script I run on boot of my desktop, and it turns the bedroom speakers off, and enables a list of designated speakers, so I don't have to manually turn them on myself - because why do it yourself when you can automate it?

The scripts are on GitHub at mpd_snap_alarm (note the imaginative name for the repository). There is a full explanation of how it all works, how to configure it and everything else you need to know except how to paint your toenails.

I have other repositories there too at stroggprog. You can only see the public ones, but others will made public when the projects reach a certain level of maturity.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies

Discussion

Enter your comment:
P B G O S
 
blog/articles/raspberry/mpd_multiroom.txt · Last modified: 2024/06/21 13:09 by Phil Ide

Except where otherwise noted, content on this wiki is licensed under the following license: Copyright © Phil Ide
Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki