First, install Raspbian.
This configuration works with HiFiBerry DACs and the Sabre ES9023 chip.
If you are using WiFi, turn off WiFi Power Management.
$ sudo iwconfig wlan0 power off
Configure the device tree overlay by editing boot config.
$ sudo nano /boot/config.txt
Remove the following line if it exists. This will disable the onboard sound card.
dtparam=audio=on
Add the following line to the end of the file.
dtoverlay=hifiberry-dac
Reboot your Pi and make sure device shows up.
$ sudo reboot now
$ aplay -l
Look for card 0: sndrpihifiberry
. Your card ID may be different from 0
.
Create /etc/asound.conf
with the following content. Replace 0
with your card ID from above.
pcm.!default {
type hw
card 0
}
ctl.!default {
type hw
card 0
}
Reboot your Pi and make sure the ALSA settings have taken effect.
$ sudo reboot now
$ alsamixer
You should see Card: snd_rpi_hifiberry_dac
displayed in the top left corner.
Install https://github.com/librespot-org/librespot using the commands below.
First, install Rust and Cargo using rustup.
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Source the cargo env file to add the cargo
binary into your PATH
.
$ source $HOME/.cargo/env
Now, install git
and build dependencies.
$ sudo apt install git build-essential libasound2-dev
Clone the repo and navigate to the directory.
$ git clone https://github.com/librespot-org/librespot.git
$ cd librespot
Optional step: Change the number of volume steps from 64 to 16. I prefer this because it matches the iPhone's volume control. 🙂
$ nano connect/src/spirc.rsLook for
const VOLUME_STEPS
and change that line and the following line to this:const VOLUME_STEPS: i64 = 16; const VOLUME_STEP_SIZE: u16 = 4096; // (u16::MAX + 1) / VOLUME_STEPS
Build librespot. This step will take a very long time on a Raspberry Pi - most likely 30 minutes or more.
$ cargo build --release
Now, let's create a system service for librespot
.
sudo nano /etc/systemd/system/librespot.service
Copy the following into the service configuration file.
[Unit]
Description=Spotify Connect daemon
StartLimitIntervalSec=0
Documentation=https://github.com/librespot-org/librespot
After=sound.target
After=network-online.target
[Service]
ExecStart=/home/pi/librespot/target/release/librespot -u [spotify_username] -p [spotify_password] -n "Librespot" -b 320 -c /tmp/librespot
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Now we can start the service.
$ sudo systemctl start librespot
And set it to start automatically on every boot.
$ sudo systemctl enable librespot
You can check the status of the service at any time by running the following command.
$ systemctl status librespot
The following is modified from the install instructions located at https://github.com/mikebrady/shairport-sync/blob/master/INSTALL.md.
$ git clone https://github.com/mikebrady/shairport-sync.git
$ sudo apt install build-essential git xmltoman
$ sudo apt install autoconf automake libtool libdaemon-dev libpopt-dev libconfig-dev
$ sudo apt install libasound2-dev
$ sudo apt install avahi-daemon libavahi-client-dev
$ sudo apt install libssl-dev
$ sudo apt install libsoxr-dev
$ autoreconf -i -f
$ ./configure --sysconfdir=/etc --with-alsa --with-avahi --with-ssl=openssl --with-soxr --with-systemd
$ make
$ sudo make install
Now, edit this file:
sudo nano /etc/shairport-sync.conf
Change these values:
general = {
name = "whatever you want!";
interpolation = "soxr";
};
Now we can start the service.
$ sudo systemctl start shairport-sync
And set it to start automatically on every boot.
$ sudo systemctl enable shairport-sync
You can check the status of the service at any time by running the following command.
$ systemctl status shairport-sync
If you have both librespot and shairport-sync running, they'll fight over the sound card and you'll have to manually stop one before using the other. The solution to this is to stop librespot every time shairport-sync begins playing audio. See discussion here: mikebrady/shairport-sync#152.
First, add a sudoer file for shairport-sync by copying the default sudoer file.
sudo cp /etc/sudoers.d/010_pi-nopasswd /etc/sudoers.d/010_shairport-sync-nopasswd
Now edit that file.
$ sudo nano /etc/sudoers.d/010_shairport-sync-nopasswd
Change the username to give shairport-sync permission to use sudo
shairport-sync ALL=(ALL) NOPASSWD: ALL
Now edit the shairport-sync config again.
$ sudo nano /etc/shairport-sync.conf
Find the section called "sessioncontrol" and make it look like this.
sessioncontrol =
{
run_this_before_play_begins = "/usr/bin/sudo systemctl stop librespot";
run_this_after_play_ends = "/usr/bin/sudo systemctl start librespot";
};
Finally, reboot for your changes to take effect.
$ sudo reboot now
Great job! 🥃