Skip to content

Instantly share code, notes, and snippets.

@alepee
Last active March 10, 2018 22:45
Show Gist options
  • Save alepee/fb2e5d4756ec95ceca95a32359632278 to your computer and use it in GitHub Desktop.
Save alepee/fb2e5d4756ec95ceca95a32359632278 to your computer and use it in GitHub Desktop.
Setup a Spotify Connect client
#!/bin/bash
# Setup a Spotify Connect client
#
# Hardware:
# - RaspberryPi3
# - Creative Sound Blaster Play2
#
# Resources:
# https://github.com/plietar/librespot
# install dependencies
sudo apt-get install build-essential portaudio19-dev git
curl https://sh.rustup.rs -sSf | sh
git clone git@github.com:plietar/librespot.git
# /!\ use a fan or any other way to cool down CPU as program is compiling
cd librespot && cargo build --release
ln -s target/release/librespot /usr/local/bin/librespot
cd -
# you need to plugin your USB sound card from here
# list your audio devices and get your card and device identifier
aplay -l
# **** List of PLAYBACK Hardware Devices ****
# card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
# Subdevices: 1/1
# Subdevice #0: subdevice #0
# card 1: S2 [Sound Blaster Play! 2], device 0: USB Audio [USB Audio]
# Subdevices: 1/1
# Subdevice #0: subdevice #0
# in this example the identifier is "hw:1,0" for card 1, device 0
# note that this address can change and depends on your system setup
# set usb interface as default
# https://github.com/plietar/librespot/issues/69#issuecomment-201951418
sudo cat > /etc/asound.conf <<HEREDOC
pcm.!default {
type plug
slave.pcm "hw:1,0"
}
HEREDOC
alsactl --force restore
# test soundcard (dual channel and 48kHz)
speaker-test -c 2 -r 48000
# start librespot client for the first time !
librespot --cache /var/cache/librespot \
--name HiFi \
--username <SPOTIFY_USERNAME> \
--password <SPOTIFY_PASSWORD> \
--bitrate 320
# add librespot to init.d (autostart on boot)
sudo cat > /etc/init.d/librespot <<HEREDOC
/usr/local/bin/librespot --cache /var/cache/librespot \
--name HiFi \
--username <SPOTIFY_USERNAME> \
--password <SPOTIFY_PASSWORD> \
--bitrate 320
HEREDOC
# restart and test
sudo reboot now
@alepee
Copy link
Author

alepee commented Jun 18, 2017

add unit file for systemd

[Unit]
Description=Spotify Connect Client
After=network.target alsa-utils.service

[Service]
Type=simple
ExecStart=/usr/local/bin/librespot --name HiFi --username <SPOTIFY_USERNAME> --password <SPOTIFY_PASSWORD> --bitrate 320 --cache /var/cache/librespot
Restart=on-failure

[Install]
Alias=librespot.service

add crontab to empty cache directory every morning at 5am
sudo crontab -e

0 5 * * * rm -r /var/cache/librespot/files/*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment