Using Mopidy with Spotify and ncmpcpp

Around 2 days ago I discovered Mopidy, an MPD “clone” that adds support for Spotify, Soundcloud, and other services. The first thing I thought was: “Hey! I can use ncmpcpp again!”. It turned out only one other person has an article on this, and it’s for Mac. So for all you Linux users, here you go:

Install Mopidy

The first step is to install Mopidy, this is pretty straight forward:

  1. Add the Archive GPG Key: wget -q -O — https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
  2. Add the APT repo: sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/jessie.list
  3. Update and Install: sudo apt-get update;sudo apt-get install mopidy mopidy-spotify

Configure Mopidy

NOTE: If you are planning to run Mopidy as a service, configure it in /etc/mopidy/mopidy.conf, if you are planning to run it under a user, use ~/.config/mopidy/mopidy.conf . The config is the same in either place.

If all you want is Spotify, you’ll need to copy and paste this into your config file: (P.S: If you plan to use other services, Mopidy has documentation here)

[audio]
output = tee name=t ! queue ! autoaudiosink t. ! queue ! udpsink port=5555
[spotify]
enabled = true
username = [username] # Please note you need Spotify Premium!
password = [password]
bitrate = 320

In a nutshell, the output = tee name=t ! queue ! autoaudiosink t. ! queue ! udpsink port=5555 line is telling GStreamer to feed the music into your normal audio sink (speakers), as well as spit the raw audio stream out on UDP port 5555. This will allow us to catch it later using nc.

All you’re going to do now is either start Mopidy as a service using service mopidy start or start it under a user account by doing nohup mopidy & which will fork it into the background and put the logs under nohup.out .

Configure ncmpcpp

As I presume you reading this article means you already know how to use ncmpcpp, I am not going to explain how to use it, but here is a getting started guide. All we are going to do here is add the visualization config to ncmpcpp. You’re going to want to add this block to~/.ncmpcpp/config .

visualizer_fifo_path = "/tmp/mpd.fifo"
visualizer_output_name = "my_fifo"
visualizer_sync_interval = "30"
visualizer_in_stereo = "yes"
visualizer_type = "spectrum"
visualizer_look = "+|"

This block just enables the visualizer in ncmpcpp and tells it where to look to find the audio data. This file will be come populated in the next step.

Setup mpd.fifo

This is the final step! You are almost there. This final step is to finish off enabling visualization, if you don’t care for visualization, you are done, and can launch ncmpcpp. Now, to finish visualization:

  1. mkfifo /tmp/mpd.fifo
  2. while :; do; yes $’\n’ | nc -lu 127.0.0.1 5555 > /tmp/mpd.fifo; done &

The first command creates a FIFO file in /tmp/mpd.fifo. This will be the pipe between netcat and ncmpcpp.

The second command tells netcat to listen to UDP port 5555, and pass all data into /tmp/mpd.fifo . These 2 commands will have to be re-run every time you reboot or whenever your /tmp directory gets cleared.

NOTE: I did not write the second command, it was pulled from user s-ol’s comment on this GitHub issue.

You’re done!

All you need to do now is run ncmpcpp and everything will work as normal! When you reboot, here’s a one-line to start everything: nohup mopidy &;mkfifo /tmp/mpd.fifo;while :; do yes $’\n’ | nc -lu 127.0.0.1 5555 > /tmp/mpd.fifo; done &;ncmpcpp . Enjoy!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.