I finally got my multi-room setup to work !
My goals were:
- be able to play Spotify in all rooms
- Be able to adjust the volume in some rooms (blast music in the bathroom while keeping other rooms low, and in general be able to control all the multiplexing in HomeAssistant)
- Be able to fade out the music, play an announcement or a notification sound and fade in the music back
- Be able to control spotify through my phone like a regular speaker (so MusicAssistant was not an option because the delay is way too high)
- Have everything in docker so I can restart everything if my NUC dies.
It’s the draft of the draft (I’m clearly not able to make full proof tutorial at this stage) but more of a general direction for the most tech savvy who want to give it a try.
I’ll try to formalize it a bit more as I learn, maybe it’s possible to make it reusable in some way.
Tech stack
Spotify connects to Librespot, which sends the audio to pulseaudio, pulseaudio then does the multiplexing with announcements and sends the result to snapcast which plays it. HomeAssistant controls pulseaudio through pactl command line tool. All of that is in docker
librespot
Librespot exposes a Spotify Connect device “Appartement” which works without being on the same network (This is important so homeassistant can start music without needing my phone).
It then sends the audio to the pulseaudio backend.
so far the docker-comfile file look like this:
services:
[...]
librespot:
image: giof71/librespot:latest
depends_on:
- pulseaudio
entrypoint: /usr/bin/librespot --backend pulseaudio --bitrate 320 --initial-volume 100 --name 'Appartement' --device snapcast --cache /data/cache --zeroconf-backend libmdns
environment:
- PULSE_SERVER=/var/run/dbus/pulseaudio # pulseaudio socket
volumes:
- "/vars/librespot/cache:/data/cache"
- "/vars/dbus:/var/run/dbus" # volume where pulseaudio write its socket
pulseaudio
Pulseaudio is a linux audio server which can multiplex audio streams, control volume, etc..
This is needed to be able to play announcements will still having the music in the background.
services:
[...]
pulseaudio:
image: woahbase/alpine-pulseaudio:x86_64
entrypoint: ""
depends_on:
- dbus
# I didn't succeed in making pulseaudio run without dbus so the first part is needed
command: bash -c "export DBUS_SESSION_BUS_ADDRESS=$(cat /var/run/dbus/address.txt) && pulseaudio --daemonize=false -F /pulseaudio.pa --exit-idle-time=-1 -v"
configs:
- source: pulseaudio.conf
target: /pulseaudio.pa
volumes:
- "/vars/dbus:/var/run/dbus" # A volume where the pulseaudio socket will go
- "snapcast-sources:/sources" # A volume where the snapcast socket is
[...]
configs:
# line 2 - disable pulseaudio suspend when no audio is played
# line 3 set snapcast fifo file as a "sink" (where the audio goes)
pulseaudio.conf:
content: |
load-module module-dbus-protocol
unload-module module-suspend-on-idle
load-module module-pipe-sink file=/sources/snapfifo sink_name=snapcast
pulseaudio requires dbus to run but it’s not actively used.
I’m not sure that everything in the config is needed but it works like that for now.
services:
[...]
dbus:
build:
dockerfile_inline: |
FROM alpine
RUN apk update && \
apk add dbus strace
CMD dbus-daemon --config-file=/dbus.conf --nofork --print-address > /var/run/dbus/address.txt
configs:
- source: dbus-conf
target: /dbus.conf
user: "0:0"
volumes:
- "/vars/dbus:/var/run/dbus"
environment:
- DBUS_VERBOSE=1
[...]
configs:
[...]
dbus-conf:
content: |
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<listen>unix:path=/var/run/dbus/system_bus_socket</listen>
<policy context="default">
<allow own="*"/>
<allow send_destination="*"/>
<allow receive_sender="*"/>
<allow send_interface="*"/>
<allow send_type="*"/>
<allow receive_type="*"/>
</policy>
<auth>ANONYMOUS</auth>
<allow_anonymous/>
</busconfig>
Snapcast
Snapcast is used to play the audio to clients in sync
services:
[...]
snapcast:
build:
dockerfile_inline: |
FROM ubuntu:latest
RUN apt update
RUN apt install -y snapserver
CMD snapserver -c /conf
volumes:
- "/vars/snapcast/librespot/cache:/librespot/cache"
- "snapcast-sources:/sources"
restart: always
profiles:
- nuc
configs:
- source: snapserver.conf
target: /conf
network_mode: host
configs:
[...]
# tell snapcast to take the audio from /sources/snapfifo
snapserver.conf:
content: |
[stream]
source = pipe:///sources/snapfifo?name=Radio&sampleformat=48000:16:2&codec=flac
HomeAssistant
I did somemodification to my HomeAssistant service to have what is need to play announcements
- Install pulseaudio-utils (for pactl)
- go for Gosling TTS
- mpg123 to convert mp3 into wav
The relevant parts look like so:
services:
homeassistant:
[...]
build:
dockerfile_inline: |
FROM homeassistant/home-assistant:latest
RUN apk add pulseaudio-utils go mpg123
RUN go install github.com/Samyak2/gosling@latest
volumes:
[...]
- "/vars/dbus:/dbus-sockets"
environment:
TZ: Europe/Paris
PULSE_SERVER: /dbus-sockets/pulseaudio
Scripts
play text-to-speech
I used this script which I added to homeassistant as a shell_command with the text to say as argument.
It’s a crud version just to check if it works reliably at this stage
#!/bin/bash
# see Gosling documentation
export GOOGLE_APPLICATION_CREDENTIALS=/config/gcp_credentials.json
# Convert argument as a mp3 file
echo $1 | /root/go/bin/gosling - --pitch=0 -l fr-fr -v fr-FR-Chirp3-HD-Zephyr /tmp/tts.mp3
# Convert this mp3 file to wav
mpg123 -w /tmp/tts.wav /tmp/tts.mp3
# for each "sink input" (aka streams producing audio => Spotify for now),
# lower the volume of 5% and do it 10 times with a small delay
count=10
for i in $(seq $count); do
pactl -f json list sink-inputs | jq ".[] | .index" | while read p; do
pactl set-sink-input-volume $p '-5%'
done
sleep 0.001
done
sleep 0.1
# Play the message
paplay --volume=60536 /tmp/tts.wav
#Do the same but increase the volume by 5%
count=10
for i in $(seq $count); do
pactl -f json list sink-inputs | jq ".[] | .index" | while read p; do
pactl set-sink-input-volume $p '+5%'
done
sleep 0.01
done
# Make sureeverything is back to 100%
pactl -f json list sink-inputs | jq ".[] | .index" | while read p; do
pactl set-sink-input-volume $p '100%'
done
Next step
I bough a bunch of Rasbperry PI with speakers and amplifiers. Once I’ve setup everything I’ll try to make a real tutorial if it get some interest.