Tuesday 24 April 2018

Multiroom RPi Music Server Configuration

Introduction

My previous post described the steps to set-up an RPi to use a bluetooth speaker and initial setup of an MPD music server to play music from the RPi through the bluetooth speaker.
This post deals with configuration to make the setup more useable.

Multi-room capability

Pure Jongos have a capability called caskeid which allows a set of them to play the same music.  The Pure Connect app, which runs on IoS or Android allows you in settings to specify which Jongo devices will use caskeid to work together.  They use wifi to pass music to each other so they need to be on the same wifi network.  When you attach to one Jongo using bluetooth the sound is streamed to the others using wifi and they play in sync.  Pure connect is also used to set the relative volume of each device.  In my setup JON is the Jongo near the RPi and distributes music to the other devices.

Bluetooth Connection

A bluetooth connection from PI3 to a Jongo device is key to making the system work.  Unfortunately in the past bluetooth has been a real pain on RPi.  I used the PI3 which has integral bluetooth and made sure that software and drivers were up to date.  As noted in the previous blog I used the RPi GUI for initial testing.  I have tried to list the key points for a happy bluetooth experience.

Initial device connection

Make sure the device you are using is paired and trusted - this only needs to be done once.
You can check device status with info <device addr>.
Other bluetooth tech can steal/hog a device so make sure that phones/ipads/headphones have wifi switched off when near the jongo to be setup.
The jongo should be showing solid orange meaning it is ready to connect.
Make sure that pulseaudio is started before connecting.
bluetoothctl connect command should now work and after flashing green the jongo should show a solid green light.
You can use mpg123 or mpd/mpc to play some music.  If no sound comes out the jongo connect a speaker to RPi audio jack and see if the sounds are coming out there instead.  If so it is a pulseaudio problem, use pactl commands to check whether pulseaudio can see the bluetooth sink.
If mpd appears not to be working, use mpc playlist command to see whether anything is waiting to play.

Device Reconnection

After a few minutes without playing bluetooth automatically disconnects and the jongo light reverts to orange.  Running the bluetooth connect command again should reinstate the connection.  If playing a readio-station the bluetooth connection can stay active for many hours.

MultiRoom sound

Other jongos don't always sync as you would hope.  Sometimes they are bit slow so it is worth waiting a couple of minutes before investigating.
Make sure that they show an amber or green-flashing light before running the connection command.  If the device is showing solid green it is connected to something else.
If the device is showing red it has an error.  In either of these two cases power off / on the jongo.

RPi Startup

When RPi is started up bluetooth, pulseaudio and mpd should all be working.
You need to mount the rpip share with /home/pi/scripts/rpipmount
You need to start the ympd web server with /home/pi/scripts/ympd.sh

MPD Clients

MPC is very useful for checking what is happening on the server and is the best trouble-shooting tool but it is not convenient for managing playlists.  I want to control the server from a variety of devices so all of the clients below are useful. MPD communicates with clients on port 6600 so it is only necessary to configure a client for IP:port to establish communication.

ncmpcpp (linux command)

This is an ncurses program which does a good job of using text screens to control mpd.  It is much more convenient for browsing folders and controlling playlists.

Auremo (Windows)

A very nice, easy to use client which allows streams to be setup.

MPDluxe (iPad)

The app costs £2.99 and is the only MPD client for the iPad since the demise of mPod.
It works well and includes the ability to switch repeat/consume etc on/off

M.A.L.P and MPDroid (Android)

M.A.L.P. only allows selection of songs or albums so is a bit limited.
MPDroid is better and allows playlists or streams to be selected.

ympd (Chrome on Windows, iPad, Android)

ympd is a very nice little python application which runs on the RPi with a small webserver on port 6800 (my choice) and its own connection to MPD on port 6600.  It shows what is playing and allows MPD to be controlled well.
Clearly it helps avoid the need to install other apps.

Streaming

mpd can also stream music directly to a device although it doesn't really fit in with the multi-room sound.  MPD allows you to send output to multiple devices so you can enable streaming on port 8000 in /etc/mpd.conf.  You can then go to a browser and enter //PI3:8000 which will play the music for you.  Note that it is not synchronised with bluetooth outputs so it cannot be played at the same time as others.

Music

NAS (samba/cifs)

As mentioned in the previous post most music is stored on my home NAS, usually known as RPIP.  In general music is stored as //rpip/rpipmusic/<artist>/<album>/<song>.mp3 or //rpip/rpipmusic/<artist+album>/<song>.mp3.
The NAS has a mount point /var/lib/mpd/music/rpipmusic so all albums are easily found with the mpd structure.

Web (http)

MPD accepts urls as valid filenames so anything which is stored on a webserver can be played.  For example rpip webserver is at /var/www/html/music/<artist>..... and music can be played as http://rpip.helliwell.org:8024/music/<artist>......

Playlists

MPD only allows playlists with suffix .m3u but it isn't very choosy what goes in them. Terminology is somewhat confusing.  The playlist is the list of songs that mpc is currently playing or waiting to play. A set of songs can be saved in a playlist file which can then be loaded and played together.  It is easy to use Auremo or MPDluxe to put an the tracks in an album on the playlist and then save them as a playlist file with the album name.  The names of tracks can be local files, remote NAS files or web urls.

Radio

Most radio stations are now available for internet streaming.  A url for a radio stream can be downloaded to Windows from radiofeeds.co.uk.  This can then be stored as a single entry in a playlist file and played by mpd.  For example capital_radio.m3u contains the line 
http://media-sov.musicradio.com:80/CapitalMP3
When this playlist is loaded or run from a client the station starts playing.  In most cases clients are able to display what track is playing and update it as the song changes.

Rplay

rpip hosts my Rplay application which saves music streams and cuts out the adverts.
As files are stored in //rpip/var/www/html/music/Radiostream they are available to MPD.
However I will change this.




Sunday 22 April 2018

Multiroom RPi Music server using Pure Jongos

Requirement

There are only a million RPi music server projects so I am sure there is room for 1,000,001.  I already have speakers in a number of rooms for the radio, hifi and sound bar and they work well so I don't want to duplicate or replace them.  Current systems are all a few years old so don't have built in bluetooth connectivity. However I would like to be able to use bluetooth and listen to the same music throughout the house and to have the entire music collection in one place.
Sonos is the current vogue for multi-room music.  Music is stored on a NAS or PC and each device stores an index of available tracks and their location.  It is controlled via a phone app which does not need to be active unless you want to change something.  It is not suitable for my requirement due to the need to buy lots of hardware but the music server approach seems good.
I am very fond of the Pure Jongo A2 bluetooth hifi adapter.  It attaches to any music device with phono inputs and allows you to play music from your phone or tablet using bluetooth.  It can also route music from NAS or internet radio channels to a sound system.  It also has a wonderful feature called "caskeid" which shares music between Jongo devices so that you can listen to the same sounds in multiple rooms.  There are Jongo speakers which can add sounds in extra rooms or outside.  These features make Jongo an excellent basis for a multi-room system but there are a couple of catches.  Firstly, your phone/tablet needs to stream the music to Jongo.  You can't move it to a different room or turn it off.  Secondly Pure appear to have lost the multi-room battle to Sonos and are discontinuing their Jongo range.  In fact this means prices for that  A2s (£20) and speakers (£50-£150) are reasonable on Amazon or Ebay so it makes for an affordable system.

Design

Hardware

I currently have a RPi which is used as a NAS so it makes sense to use it as a music server as well.  The current device is B+ so I need to plug in a USB bluetooth adapter which will communicate with one of the Jongos.
I already have three Jongo A2 attached to my study HiFi, TV sound bar and kitchen mini-system so these will form the core of the playback system.  I will add a Jongo speaker and another A2 to allow two more rooms to be linked in.  I may buy another outdoor specification Jongo for use in a bathroom.  All devices will be setp via the Pure Connect app with caskeid so that they synchronise playback.

Software

RPi software setup will comprise the bulk of the project.  The obvious music server to use is MPD which is a long-term component of linux.  It plays music from multiple sources, namely local disks or network URLs which may represent web music (http), NAS (smb) or streams (pls).  Files can be added into playlists (m3u).  Mopidy was investigated as a popular alternative to MPD but doesn't seem to have important extra features.
An advantage of using MPD is that it is a standard product so there are many clients available to control it (play/pause, create/load playlists, etc).  MPDroid or M.A.L.P can be used on Android, MPDluxe (£2.99) on IoS and Auremo was chosen for the Windows as I have previously found it useful.  In addition to these platform specific apps there is a small package called ympd which runs as a small RPi python web server to allow MPD to be controlled from a browser window.  There are other web-based clients but they have more complex/specific requiremetns for web-server, database etc.  As a backstop the standard linux music client MPC can be used via an ssh session (mobaxterm/putty on Windows, reflection on iPad) or at the RPi console / GUI.
The RPi will continue to be used "headless" so generally the GUI will not be present, although it is useful during testing.
I have found bluetooth and audio routing problematical on RPi previously.  Bluetoothctl and pulseaudio for sound routing are pretty standard so I will need to make them reliable.

Media

Existing music collection stored on large USB stick will be available, I would also like to setup a number of radio streams and access playlists in my Rplay no-advert radio system.

Installation

This is intended to be a reasonably high-level description of the installation in April 2018 on PI3, a RPi B+ running Raspbian Stretch.

Step 1 Software installation

Bluetooth software on RPi has changed a lot of the past two or three years with new versions of raspbian so it is best not to use old tutorials, this one seems quite recent.

Update linux: # apt-get update;apt-get upgrade; apt-get dist-upgrade; rpi-update
Install bluetooth/pulse audio: # apt-get install blueman pulseaudio pulseaudio-module-bluetooth
Install music software: # apt-get install mpd mpc mpg123

Step 2 Sound testing

I don't have speakers connected to PI3 by default so initially I connected some and made sure that playing an mp3 file using the command line player mpg123 resulted in sounds.  Note that not all mp3 players support .mp3 files and many don't recognize pulseaudio, so mpg123 is a good choice.
I also used the GUI initially.  There are bluetooth icons on the menu bar on the top right of the screen.
I made sure that my Jongo A2 was visible by running bluetooth setup from the icon and ensuring it was paired and trusted but not connected.  Then, right-clicking on the speaker icon and selecting the bluetooth speaker caused the bluetooth speaker to burst into life.  There was some trial and error involved, in particular pulseaudio didn't always seem to be running which prevented bluetooth being used with mpg123.  Once this step is complete we know that we can play music on PI3 through a bluetooth speaker using the GUI.

Step 3 MPD Music Server

MPD (Music Player Daemon) is a venerable but widely used music server for Linux.  It contains a database of song titles and plays to a music device.  MPC (Music Player Client) is the corresponding  linux command line client software which allows music to be started, stopped, re-indexed and playlists to be managed. As installed MPD doesn't support pulseaudio properly because pulseaudio is running as pi and cant communicate with the mpd user.  The wiki explains how to setup MPD so it streams to localhost (127.0.0.1) which pulseaudio can read and send to bluetooth.
Once mpd is setup music can be stored in /var/lib/mpd/music. CIFS/SMB shares on other devices can be accessed by specifying mount points in /var/lib/mpd/music.  For example I setup a mount point for my RPi NAS at /var/lib/mpd/music/rpipmusic/ with:
 sudo mount -t cifs /<nas ip>/<nas share>/ /var/lib/mpd/music/rpipmusic / -o user=x, pass=x
In addition MPD accepts urls as file locations.  These may be http://server/folder/song.mp3 or streaming radio URLs.

Step 4 Play music with MPC

Once some music has been placed in /var/lib/mpd/music use the command mpc update to index it and make the music available. It is fiddly to do this entirely using mpc but it is still the fastest way of getting started. mpc ls will show you what folders and playlists are available.  You should see your music there.    mpc listall <foldername> will show you what is available in the folder (e.g. an album).  mpc add <foldername>  will add files in <foldername> and any sub-directories to your playlist which you can then show with mpc playlist.  Finally mpc play tells mpd to start playing the music and mpc pause or mpc toggle stops it.

Conclusion

At this stage we have a music server playing through a bluetooth speaker so we have met our key technical requirements.  There is a lot more we can do to improve the system which I will summarise in the next posting.