I describe how I build the Zynthian Raspberry Pi OS synthesizer from a kit in a previous blog post. It sits mostly idle connected to my sound card, so I decided to make it a bit more useful while it is idling.
The first goal was to make it play Radio Helsinki, one of my most played online radio stations. That's simple enough; I just created a systemd service which connects to the stream using mplayer.
[Unit] | |
Description=Radio helsinki | |
After=jack2.service network-online.target | |
Wants=network-online.target | |
Requires=jack2.service | |
[Service] | |
Restart=always | |
Type=simple | |
ExecStart=/usr/bin/mplayer -volume 31 -nogui -ao jack -cache 1032 -cache-min 10 "http://stream.radiohelsinki.fi:8006/;" | |
[Install] | |
WantedBy=multi-user.target |
Note that I'm using jackaudio as an audio "sink." The synthesizer is thus perfectly playable, even if I am listening to something else meanwhile.
I get soon bored listening to only one channel. The next step is then to add Spotify. There is a pre-made debian package called raspotify, but alas, the bundled librespot Spotify client does not work with the jackaudio backend. I decided that the most comfortable choice is to compile librespot in the Raspberry Pi and then create a systemd service (after all, that is pretty much all raspotify does).
The Spotify client service is quite simple to run.
[Unit] | |
Description=Spotify player | |
After=radiohelsinki.service | |
Wants=network-online.target | |
[Service] | |
Restart=always | |
Type=simple | |
ExecStart=/root/librespot/target/release/librespot --name hima --backend jackaudio --username <REDACTED> --password <REDACTED> --bitrate 320 --disable-audio-cache --enable-volume-normalisation --mixer-linear-volume --initial-volume=100 --onevent /root/connect-spotify-jack.sh | |
[Install] | |
WantedBy=multi-user.target |
I added a script that listens to the client's events to start/stop Radio Helsinki service accordingly. The music never stops!
#!/bin/bash | |
if [[ "$PLAYER_EVENT" == "start" || "$PLAYER_EVENT" == "playing" ]]; then | |
service radiohelsinki stop | |
n=0 | |
until [ "$n" -ge 3 ] | |
do | |
jack_connect librespot:out_0 system:playback_1 &>/dev/null | |
jack_connect librespot:out_1 system:playback_2 &>/dev/null | |
n=$((n+1)) | |
sleep 3 | |
done | |
fi | |
if [[ "$PLAYER_EVENT" == "stop" || "$PLAYER_EVENT" == "paused" ]]; then | |
service radiohelsinki start | |
fi | |
if [[ "$PLAYER_EVENT" == "volume_set" && "$VOLUME" == "0" ]]; then | |
service radiohelsinki stop | |
fi |
Comments
Post a Comment