Skip to content

Instantly share code, notes, and snippets.

@dialupdev
Last active September 8, 2024 19:10
Show Gist options
  • Select an option

  • Save dialupdev/e070e99ebf04f2d278a2b6b49c3aa908 to your computer and use it in GitHub Desktop.

Select an option

Save dialupdev/e070e99ebf04f2d278a2b6b49c3aa908 to your computer and use it in GitHub Desktop.
Raspberry Pi Music Server

First, install Raspbian.

Configuring your sound card

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.

Setting up Spotify Connect

Install https://github.com/librespot-org/librespot using the commands below.

First, install Rust and Cargo using rustup.

$ curl https://sh.rustup.rs -sSf | sh

Log out of your account and log back in to add the cargo binary into your PATH.

Now, install git and build dependencies.

$ sudo apt install git build-essential portaudio19-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.rs

Look for repeated.push(64) and change it to repeated.push(16).

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
After=network.target
StartLimitIntervalSec=0
Documentation=https://github.com/librespot-org/librespot

[Service]
Type=simple
Restart=always
RestartSec=1
User=pi
ExecStart=/home/pi/librespot/target/release/librespot -u [spotify_username] -p [spotify_password] -n "Librespot" -b 320 -c /home/pi/librespot/cache --enable-volume-normalisation

[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

Setting up Airplay

The following is taken from the install instructions located at https://github.com/mikebrady/shairport-sync/tree/development.

$ git clone https://github.com/mikebrady/shairport-sync.git

$ git checkout development
$ autoreconf -i -f
$ ./configure --sysconfdir=/etc --with-alsa --with-pa --with-avahi --with-ssl=openssl --with-soxr --with-systemd

$ make
$ sudo make install
$ sudo systemctl enable shairport-sync

Now, edit this file:

sudo nano /etc/shairport-sync.conf

Change these values:

general = {
	name = "whatever you want!";
};
alsa = {
	output_device = "hw:1,0"; // (or whatever number card,device "sndrpihifiberry" is above)
	interpolation = "soxr";
};

If audio is underflowing, set audio_backend_buffer_desired_length to 22050.

Restart Shairport-sync to apply the new settings.

$ sudo service shairport-sync restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment