MRA2: Installing Snapcast

Introduction

This post goes about installing Snapcast. This is the transportation system for the audio. From a player, discussed in part 3 & 4, it receives audio and sends it to a client. The full installation instructions can be found here.

First, we need to install the Snapcast system. Snapcast exists of a server and a client. The server creates different pipe’s. There are multiple players that can put their audio in a pipe. Examples are Mopidy and Librespot. A Snapclient can play the audio from one pipe. It is possible to switch pipe. In the server settings, we can manage wich client need to play what pipe’s audio.

Snapserver

Download and install

  • Download the latest version of Snapserver from this link: https://github.com/badaix/snapcast/releases/tag/v0.14.0. In the terminal command line, you can use wget to get the .zip file. (With a right-click, you can get the full download link)
  • Install with dpkg and install missing dependencies
$ cd Downloads
$ sudo wget https://github.com/badaix/snapcast/releases/download/v0.14.0/snapserver_0.14.0_amd64.deb
$ sudo dpkg -i snapserver_0.14.0_amd64.deb
$ sudo apt-get -f install

Sometimes, installing with dpkg fails. By running the -f install script it should run just fine.

I had also the weard behavior that the clients could not discover the server. Installing the avahi packages solved it.

$ sudo apt-get install avahi-daemon avahi-discover avahi-utils libnss-mdns mdns-scan

The installer makes a service script, and enables automatically. For restarting or status of the service, use: systemctl and journalctl -u. Restarting is always neede when a change is made in the configuration file, seen in the following section.

Edit the configuration file

A file snapserver is created by the installer in /etc/default:

# defaults file for snapserver

# start snapserver automatically?
START_SNAPSERVER=true

# Allowed options:
#   -h, --help                          Produce help message
#   -v, --version                       Show version number
#   -p, --port arg (=1704)              Server port
#   --controlPort arg (=1705)           Remote control port
#   -s, --stream arg (=pipe:///tmp/snapfifo?name=default)
#                                       URI of the PCM input stream.
#                                       Format: TYPE://host/path?name=NAME
#                                       [&codec=CODEC]
#                                       [&sampleformat=SAMPLEFORMAT]
#   --sampleformat arg (=48000:16:2)    Default sample format
#   -c, --codec arg (=flac)             Default transport codec
#                                       (flac|ogg|pcm)[:options]
#                                       Type codec:? to get codec specific options
#   --streamBuffer arg (=20)            Default stream read buffer [ms]
#   -b, --buffer arg (=1000)            Buffer [ms]
#   --sendToMuted                       Send audio to muted clients
#   -d, --daemon [=arg(=0)]             Daemonize
#                                       optional process priority [-20..19]
#   --user arg                          the user[:group] to run snapserver as when daemonized

USER_OPTS="--user snapserver:snapserver"

#SNAPSERVER_OPTS="-d -s pipe:///tmp/snapfifo1?name=Sibrecht&mode=create -s pipe:///tmp/snapfifo2?name=Jasna&mode=create"
SNAPSERVER_OPTS="-d -s spotify:///librespot?name=Sibrecht&devicename=Sibrecht&bitrate=320 -s spotify:///librespot?name=Jasna&devicename=Jasna&bitrate=320"

 

Here are some things to remark:

    • Everything starting with a ‘#’ is comment. It Snapcast will not read it.
    • You can see I have two lines with SNAPSERVER_OPTS. The first is commented out because I use now the Librespot plugin. The first line is used to work with Mopidy.
    • It is possible to make multiple pipes. simply add -s pipe:///tmp/snapfifo1?name=PipeName&mode=create for every pipe you want to make. Just make sure you choose a different name for the FIFO (eg: snapfifo1, snapfifo2,…) and a name for the pipe itself. In Home Assistant, the pipe name will be visible. I had issues with the mode ‘read’. When Mopidy creates the file, it is not really a FIFO. Snapcast will loop about all the streamed audio when music is paused. So, it is better to choose the ‘create’ mode. When Snapcast creates the file, it is a really FIFO. It deletes the information already played.

Snapclient

Download and install

  • Download the latest version of Snapclient from this link: https://github.com/badaix/snapcast/releases/tag/v0.14.0. In the terminal command line, you can use wget to get the .zip file. (With a right-click, you can get the full download link)
  • Install with dpkg and install missing dependencies
$ cd Downloads
$ sudo wget https://github.com/badaix/snapcast/releases/download/v0.14.0/snapclient_0.14.0_amd64.deb
$ sudo dpkg -i snapclient_0.14.0_amd64.deb
$ sudo apt-get -f install

Sometimes, the installation failes because avahi is not installed. To install this, run the following command:

$ sudo apt-get install avahi-daemon avahi-discover avahi-utils libnss-mdns mdns-scan

This will also create a file snapclient in /etc/defaultsbut no modifications need te be done in this file.

The installer makes a service script and enables automatically. For restarting or status of the service, use: systemctl and journalctl -u.

Start options

All the functions can be seen with: snapclient --help.

Audio card output

It is possible to choose the audio card output for the Snapclient. With the command snapclient -l, it is possible to see all the audio cards connected. Every card has a number. Starting the Snapclient with eg: snapclient -s 4, the output will be redirected to that specific audio card. My USB audio card was listed multiple times, but some ‘cards’ were playing a bit too fast and stammered the whole time. Going over all the audio cards, resulted in a good audio quality.

Different instances on one device

In combination with the above option, there is also an instance option. When using eg: snapclient -s 4 -i 2, there is a new instance made of snapclient. Now it is possible to have multiple audio cards on one device and multiple instances. Every instance reacts as a new independent speaker.

Names of the connected clients

The name of the client can be seen in the file ~/.config/snapcast/server.json. These names will also be found in Home Assistant.

{
    "ConfigVersion": 2,
    "Groups": [
        {
            "clients": [
                {
                    "config": {
                        "instance": 1,
                        "latency": 0,
                        "name": "",
                        "volume": {
                            "muted": false,
                            "percent": 100
                        }
                    },
                    "connected": true,
                    "host": {
                        "arch": "x86_64",
                        "ip": "::ffff:192.168.1.101",
                        "mac": "00:1b:b1:8b:8d:a9",
                        "name": "Memien",
                        "os": "Ubuntu 17.10"
                    },
                    "id": "00:1b:b1:8b:8d:a9",
                    "lastSeen": {
                        "sec": 1526640137,
                        "usec": 741645
                    },
                    "snapclient": {
                        "name": "Snapclient",
                        "protocolVersion": 2,
                        "version": "0.14.0"
                    }
                }
            ],
            "id": "a0c8f68c-a0ba-3b70-51eb-d328fb310e72",
            "muted": false,
            "name": "",
            "stream_id": "default"
        }
    ]
}

Features of Snapcast

The Snapclient has an autodiscovery feature. The server will automatically find all the clients in the network. A view of all the connected clients can be seen in the json file seen above. Also, when a new client is found by the server, it adds it to the first pipe automatically. If you only have one pipe, it will just work out-of-the-box.

The Snapserver has a build-in JSON RPC API. This means that with JSON commands to the telnet port 1705, we can control all the audio of the network. Here we can set the name of the groups and also what client needs to listen to what pipe. This API is completely implemented in Home Assistant. Finding the component and installing should be very easy.

1 thought on “MRA2: Installing Snapcast”

Leave a Reply

Your email address will not be published. Required fields are marked *