{"id":277,"date":"2020-04-18T16:39:24","date_gmt":"2020-04-18T23:39:24","guid":{"rendered":"http:\/\/eequalspi.com\/?p=277"},"modified":"2020-04-18T16:44:45","modified_gmt":"2020-04-18T23:44:45","slug":"refactoring-the-uniac-playback-code","status":"publish","type":"post","link":"http:\/\/eequalspi.com\/index.php\/2020\/04\/18\/refactoring-the-uniac-playback-code\/","title":{"rendered":"Refactoring The UNIAC Playback Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1024x453.png\" alt=\"\" class=\"wp-image-281\" width=\"531\" height=\"234\" srcset=\"http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1024x453.png 1024w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-300x133.png 300w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-768x339.png 768w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1536x679.png 1536w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-2048x905.png 2048w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1200x530.png 1200w\" sizes=\"auto, (max-width: 531px) 85vw, 531px\" \/><figcaption>Old Mopidy + MPD based flow<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This had several problems.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Mopidy-spotify is partially broken. Spotify themselves no longer support this interface, and haven&#8217;t since 2016. To get it working at all requires a hacked fork of Mopidy-spotify which might break forever at any time.<\/li><li>Response between Python and Mopidy can be really laggy and requires a continuous ping (every 30 seconds) to keep the connection alive.<\/li><li>Loading a new playlist in Mopidy requires significant caching and can have tens of seconds of lag, making the whole system unreasonably slow.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">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 <a href=\"https:\/\/www.dinofizzotti.com\/blog\/2020-02-05-diskplayer-using-3.5-floppy-disks-to-play-albums-on-spotify\/\">the Diskplayer project<\/a>, I was awoken to more modern alternatives.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After some rework, I ripped out Mopidy and mpd, and replaced it with <a href=\"https:\/\/github.com\/dtcooper\/raspotify\">raspotify<\/a> as a player and <a href=\"https:\/\/spotipy.readthedocs.io\/en\/2.11.1\/\">Spotipy<\/a> to interface with Spotify.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-1024x439.png\" alt=\"\" class=\"wp-image-282\" width=\"521\" height=\"223\" srcset=\"http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-1024x439.png 1024w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-300x128.png 300w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-768x329.png 768w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-1536x658.png 1536w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-2048x877.png 2048w, http:\/\/eequalspi.com\/wp-content\/uploads\/2020\/04\/image-1-1200x514.png 1200w\" sizes=\"auto, (max-width: 521px) 85vw, 521px\" \/><figcaption>New Raspotify + Spotipy based flow<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This has several advantages.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The control API that Spotipy uses is actively supported by Spotify<\/li><li>Raspotify makes the Pi show up as a Spotify speaker, meaning active playback can be bounced between devices.<\/li><li>Spotipy is controlling my Spotify state, not Mopidy, so it can actually control playback of other devices that are actively playing music.<\/li><li>By the same token, pausing and unpausing on other clients (phone, laptop, etc.) works seamlessly.<\/li><li>Music is streamed, not buffered. This means that loading a new playlist is as instant as it is on the desktop client.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There were some hiccups in figuring out how to connect to Spotify with Spotifyd, but the biggest tricks were:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Learning that the credential saving path should be a full filename, not a path to a directory.<\/li><li>Realizing that the redirect URI could be &#8220;http:\/\/localhost&#8221;. 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 &#8216;broken&#8217; URL that it takes you to back into your Pi command line and hit enter. Things should work from there.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re looking to build a Raspberry Pi \/ Spotify player, avoid MPD and go for a Spotipy \/ Raspotify combo!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/wespo\/UNIAC\/tree\/spotifyd\">The revised code can be found here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. This had several problems. Mopidy-spotify is partially broken. Spotify &hellip; <a href=\"http:\/\/eequalspi.com\/index.php\/2020\/04\/18\/refactoring-the-uniac-playback-code\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Refactoring The UNIAC Playback Code&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[27,16,28,25,29,21],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-code","tag-python","tag-raspberrypi","tag-raspotify","tag-spotify","tag-spotipy","tag-uniac"],"_links":{"self":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":5,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":285,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts\/277\/revisions\/285"}],"wp:attachment":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}