Install a command-line Spotify client on a headless Raspberry Pi

spotify-on-raspberry-piMy Raspberry Pi Model B Revision 2.0 (512MB) came this week, and like many people I opened it up, plugged it in, got it networked, configured it, got to a command prompt and said “hmmmm, now what?”

I love music and decided that getting Spotify running on my headless RasPi would be a fun first project. There are decent nuggets of information out there, but below is a simple, aggregated step-by-step guide to getting Spotify running on your headless Raspberry Pi’s command line.

NB: I believe that you will need to be a Premium Spotify subscriber for this to work. Just get it, it’s so worth it.

Part One: Install Despotify

Spotify has an unofficial “Spotify for Linux” release, but it is GUI-based and I am running my RPi headless and connecting exclusively via SSH, so that doesn’t work for me. Plus, I couldn’t get it to install on the ARM-based Pi.

After a little searching, I found a popular open source, command-line Spotify client called Despotify. Installing it was surprisingly easy:

  1. sudo apt-get install libao-dev libtool git libssl-dev libmpg123-dev libvorbis-dev libncursesw5-dev subversion
  2. svn co https://despotify.svn.sourceforge.net/svnroot/despotify despotify
  3. cd despotify/src/
  4. make
  5. sudo make install

Part Two: Control Spotify from the Command Line

After installing, there are three ways to use Despotify:

  1. “despotify-simple”: a simple command-line client
  2. “despotify”: a pine/vi looking interface
  3. “despotify-gateway”: a backend HTTP REST API which can respond to frontend HTTP requests

Using despotify-simple

  1. Plug a speaker into the audio out of your RPi
  2. run: despotify-simple <username> <password> (protip: lead with a space to keep this command and you password out of your history and process list)
  3. type: search tonight the streets richard hawley
  4. type: play 1

Part Three: How to set your Raspberry Pi’s Volume Levels

The program amixer is a command-line mixer for the ALSA soundcard driver which will allow you to control the volume level of the speaker connected to your Raspberry Pi.

For one-off volume setting, type:
amixer cset numid=1 -- 60% (H/T Linus)

Change the volume by changing the last number. I’ve found that, for my RPi and mini speaker, 50% is effectively equal to a volume of 0 and 100% is 10. I made a simple little bash script to help me quickly set the volume levels (H/T to jerryjvl for his StackOverflow answer detailing a usable conversion algorithm).

The bash script:

#!/bin/bash
#
# Set the volume of the connected speaker using a program
# called "amixer". Standard usage: amixer cset numid=1 -- 60%
# Ranges between 50% and 100% correspond to abstracted
# volume levels 0 and 10. This script does that conversion
# and hides the amixer implementation.
#
# Usage: vol 5

MyMax=10
MyMin=0
RealMax=100
RealMin=50
let MyRange=${MyMax}-${MyMin}
let RealRange=${RealMax}-${RealMin}

MyVol=$1
# MARTY: Why don't you just make ten louder and make ten
#        be the top number and make that a little louder?
# NIGEL: [pause] These go to eleven.
if [ ${MyVol} -gt 11 ]
then
MyVol=11
fi

# This is what I want, but bash no likey parens:
# RealVol=(((${MyVol}-${MyMin})*${RealRange})/${MyRange})+${RealMin}
let a=${MyVol}-${MyMin}
let b=${a}*${RealRange}
let c=${b}/${MyRange}
let d=${c}+${RealMin}

echo "vol ${1} is ${d}% to amixer..."
amixer -q cset numid=1 -- ${d}%

A little helper function to tuck in .bash_aliases

function vol() {
~/SCRIPTS/set_volume.sh "$@";
}

The bash script and helper function allow you to set your speaker volume in a more intuitive way by typing commands like vol 2 or vol 10 at the command line. That’s all there is, now you can use your Raspberry Pi as an interface to your home entertainment or in-house speaker systems. Pretty nice.

Have an interesting first Raspberry Pi project you want to share?

Send me a link or leave it in the comments and I will link to it from this post. Happy hacking!

raspberry-pi-speaker