Refactoring The UNIAC Playback Code

One of the most problematic parts of the UNIAC project has been the music playback software. The original software used the Mopidy music player for command line playback. Mopidy-spotify provided Spotify connectivity to the software. The mpd python library then allowed me to control playback programmatically.

Old Mopidy + MPD based flow

This had several problems.

  1. Mopidy-spotify is partially broken. Spotify themselves no longer support this interface, and haven’t since 2016. To get it working at all requires a hacked fork of Mopidy-spotify which might break forever at any time.
  2. Response between Python and Mopidy can be really laggy and requires a continuous ping (every 30 seconds) to keep the connection alive.
  3. Loading a new playlist in Mopidy requires significant caching and can have tens of seconds of lag, making the whole system unreasonably slow.

This was all a legacy of the fact that I had designed the software for UNIAC in 2015 and the Mopidy workflow was the only one I could find for getting Spotify playback on a Raspberry Pi. In early 2020, after hearing about Spotifyd from the Diskplayer project, I was awoken to more modern alternatives.

After some rework, I ripped out Mopidy and mpd, and replaced it with raspotify as a player and Spotipy to interface with Spotify.

New Raspotify + Spotipy based flow

This has several advantages.

  1. The control API that Spotipy uses is actively supported by Spotify
  2. Raspotify makes the Pi show up as a Spotify speaker, meaning active playback can be bounced between devices.
  3. Spotipy is controlling my Spotify state, not Mopidy, so it can actually control playback of other devices that are actively playing music.
  4. By the same token, pausing and unpausing on other clients (phone, laptop, etc.) works seamlessly.
  5. Music is streamed, not buffered. This means that loading a new playlist is as instant as it is on the desktop client.

All in all, this means that the switch resulted in a much more pleasant user experience. On top of that, the system is generally more stable than it was before.

Plus, setting up Raspotify was orders of magnitude less difficult than Mopidy. Raspotify managed to work automagically after apt-get installing it and setting it up as a service. This was unlike Mopidy, which took forever to point at the right sound card and get working.

There were some hiccups in figuring out how to connect to Spotify with Spotifyd, but the biggest tricks were:

  1. Learning that the credential saving path should be a full filename, not a path to a directory.
  2. Realizing that the redirect URI could be “http://localhost”. To make that work, all you have to do is run the login script from the command line on your Pi and then copy the URL it feeds you to a browser on a desktop machine. Once you accept the terms, all you have to do is copy the ‘broken’ URL that it takes you to back into your Pi command line and hit enter. Things should work from there.

If you’re looking to build a Raspberry Pi / Spotify player, avoid MPD and go for a Spotipy / Raspotify combo!

The revised code can be found here.