Skip to content
Jason Gray edited this page Sep 19, 2022 · 58 revisions

Raspotify is built on and for Debian Stable and requires Debian Stable or a Debian Stable based/compatible OS.

armhf, arm64 and amd64 builds are provided. Support for ARMv6 (Pi v1 and Pi Zero v1.x) has been dropped.

This is a very basic guide that assumes you're starting from a fresh Debian Stable Minimal Server or Raspberry Pi OS Lite (Stable) install.

These instructions will install and configure Raspotify and will reconfigure system wide audio settings.

It will NOT explain how to install Debian, use a text editor, set up Raspotify/librespot as a whatever-cast source or make you a sandwich.

Disclaimer

You should NOT follow these instructions on desktop systems. Raspotify is intended to be used on headless systems.

If following these instructions breaks your system you get to keep all the pieces.

Install

sudo apt-get -y install curl && curl -sL https://dtcooper.github.io/raspotify/install.sh | sh

Configure Output

The Easy Way

wget https://raw.githubusercontent.com/dtcooper/raspotify/master/asound_conf_setup.py
chmod +x asound_conf_setup.py
sudo ./asound_conf_setup.py

Follow the prompts.

Skip ahead to Configure librespot.

The Hard Way

List audio cards

aplay -l

Example output:

**** List of PLAYBACK Hardware Devices ****
card 0: Generic [HD-Audio Generic], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: D10s [D10s], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

Find your audio card's supported formats and bit-depths

aplay -Dhw:"your DACs actual card #" --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

For example:

aplay -Dhw:1 --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

Example output:

HW Params of device "hw:1":
--------------------
ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S32_LE SPECIAL DSD_U32_BE
SUBFORMAT:  STD
SAMPLE_BITS: 32
FRAME_BITS: 64
CHANNELS: 2
RATE: [44100 384000]
PERIOD_TIME: [125 1486078)
PERIOD_SIZE: [8 65536]
PERIOD_BYTES: [64 524288]
PERIODS: [2 1024]
BUFFER_TIME: (41 2972155)
BUFFER_SIZE: [16 131072]
BUFFER_BYTES: [128 1048576]
TICK_TIME: ALL
--------------------

Edit /etc/asound.conf

It may or may not already exist.

If it does exist it means that either you or some random package or script has created it at some point in the past, as it does not exist by default on a clean Debian install.

Choose the highest supported FORMAT and desired sampling rate.

Avoid resampling by choosing 44100, if 44100 is in the RATE range, otherwise use 48000 or the lowest integer multiple of either.

Add:

defaults.ctl.card "your DACs actual card #"
defaults.pcm.card "your DACs actual card #"
defaults.pcm.dmix.rate "the actual rate that you want"
defaults.pcm.dmix.format "the actual format you want"

For example:

defaults.ctl.card 1
defaults.pcm.card 1
defaults.pcm.dmix.rate 44100
defaults.pcm.dmix.format S32_LE

Resampling

Skip this if you don't need to resample.

Install higher quality sample rate converters

sudo apt install -y --no-install-recommends libasound2-plugins

List available sample rate converters

echo "$(ls /usr/lib/*/alsa-lib | grep "libasound_module_rate_")" | sed -e "s/^libasound_module_rate_//" -e "s/.so$//"

Choose a sample rate converter

Edit /etc/asound.conf

Add:

defaults.pcm.rate_converter "the actual converter you want"

For example:

defaults.pcm.rate_converter speexrate_medium

speexrate_medium is a good default. Lower spec devices and SBC's may struggle with speexrate_best.

Test it

This will play a noise test tone to verify everything is configured correctly.

Turn your volume down to a reasonable level 1st!!!

speaker-test -c2 -l1

Configure librespot

Edit /etc/raspotify/conf

librespot doesn't care about the endianness (the LE or BE at the end) it will take care of that for us.

To set the format change:

# Output format {F64|F32|S32|S24|S24_3|S16}. Defaults to S16.
#LIBRESPOT_FORMAT="S16"

To:

# Output format {F64|F32|S32|S24|S24_3|S16}. Defaults to S16.
LIBRESPOT_FORMAT="match the audio output format"

For example:

# Output format {F64|F32|S32|S24|S24_3|S16}. Defaults to S16.
LIBRESPOT_FORMAT="S32"

If you're not limited by data caps or connection speeds you may also want set the audio quality to 320kbps by changing:

# Bitrate (kbps) {96|160|320}. Defaults to 160.
#LIBRESPOT_BITRATE="160"

To:

# Bitrate (kbps) {96|160|320}. Defaults to 160.
LIBRESPOT_BITRATE="320"

Spotify credentials

Unless you have disabled zeroconf (mDNS) discovery, librespot does not need your Spotify credentials, zeroconf with take care of authentication.

However if you have disabled discovery for whatever reason and you provided your credentials you will also want to enable credential caching so that librespot will cache the auth token and not have to login every time you connect. Unless you like nag emails ofc?

To enable credential caching change this in /etc/raspotify/conf:

# Disable caching of credentials.
# Caching of credentials is not necessary so long as
# LIBRESPOT_DISABLE_DISCOVERY is not set.
LIBRESPOT_DISABLE_CREDENTIAL_CACHE=

To this:

# Disable caching of credentials.
# Caching of credentials is not necessary so long as
# LIBRESPOT_DISABLE_DISCOVERY is not set.
# LIBRESPOT_DISABLE_CREDENTIAL_CACHE=

Make Raspotify use tmpfs for /tmp

By default librespot "download buffers" tracks, meaning that it downloads the tracks to disk and plays them from the disk and then deletes them when the track is over. This practice is very common, many other audio frameworks and players do the exact same thing as a disk based tmp cache is easy to use and very resilient. That being said there may be cases where a user may want to minimize disk read/writes.

If you don't know what this means or understand the ramifications do NOT do this.

To make Raspotify use tmpfs for /tmp change this in /etc/raspotify/conf:

# Commenting this out will cause `librespot` to use a tmpfs so that provided there
# is enough RAM to hold the track nothing is written to disk but instead to a tmpfs.
# See https://github.com/dtcooper/raspotify/discussions/567
# And https://www.kernel.org/doc/html/latest/filesystems/tmpfs.html
TMPDIR=/tmp

To this:

# Commenting this out will cause librespot to use a tmpfs so that provided there
# is enough RAM to hold the track nothing is written to disk but instead to a tmpfs.
# See https://github.com/dtcooper/raspotify/discussions/567
# And https://www.kernel.org/doc/html/latest/filesystems/tmpfs.html
#TMPDIR=/tmp

Wrap it up

To make sure all changes take effect you must restart the Raspotify service:

sudo systemctl restart raspotify

Revert Everything

Completely uninstall Raspotify:

sudo apt purge -y raspotify

Remove the repo:

sudo rm -f /etc/apt/sources.list.d/raspotify.list

Delete the repo's key:

sudo rm -f /usr/share/keyrings/raspotify_key.asc 

Delete /etc/asound.conf:

sudo rm -f /etc/asound.conf