Monday, January 18, 2016

Raspberry PI (B+) as Spotify Connect device (Work in progress)


Well I've been buying these little Rapsberry Pis since they started so I keep some of them around and to be honest I end up changing them for something else, as my primary interest is music (then video) and for there is always something that's more powerful (Intel NUC) or smaller, or easier to handle (Windows, doh).

In the end as application I use my dear Kodi, whenever possible with a bootable Openelec image (both great pieces of software).

With the Rpi2 (1GB RAM!) I found that Openelec runs so much better and there is little excuse not to use as a Kodi player.  But given I've had the other boxes around I went for something that's quite interesting: a music headless server that can manage Spotify Connect and maybe Airplay (but anyway that's something available on Kodi). For that I will use the next best thing to the Rpi2, the Raspberry Pi B+.

As of today, Spotify ain't granting rights to everyone out there to write apps to get Spotify Connect built into devices (Chromecast does now; Gramophon too).

So what can we do? There's a little development ("Spotify Connect Web") to allow the rPi to act as a Spotify Connect target and that will be the start of the project.

So what am I using?

REQUIREMENTS
- Rapsberry PI B+: Accesories/peripherals
    * Wireless K400R Logitech Keyboard/Mouse
    * Connected via Ethernet, HDMI monitor (then SSH)
    * External usb power source.
    * Kingston Micro SD Card 8GB Class 10.

- An external amplifier/(amplified) speakers: as I plan to use it hooked to my stereo amp, there you go, stereo amp + speakers (or active/amplified speakers, I used that for the test)

- Raspbian Jessie LITE, latest from the site. As I won't need a GUI I figured the Lite version will do fine (and my internet speed sucks, a smaller size is better!)

- Spotify Connect Web:  Available at https://github.com/Fornoth/spotify-connect-web
  There are quite a numerous forks out there.. For now I'll stick to this one, the original.

- A Spotify Premium Account

PREPARING THE RPI:
- SD Formatter 4 on the card, with size adjustment
- WIN32DiskImager; pick the .img from Jessie and burn it to the SD Card.
- Start the Rpi with the card and the peripherals.

CONFIGURING THE RPI:
- Login as pi/raspberry
- First thing, run sudo raspi-config and expand the filesystem to use the 8gb, you're gonna need them.
- Reboot to apply the change

CONFIGURING SOUND & SPOTIFY CONNECT:

- These are mostly the instructions found in the github site aforementioned.
- First, follow the instructions in the github site to get the developer key from Spotify. YOU'LL NEED A PREMIUM ACCOUNT.
- I added the "-- name" option to have your output named as you like. I used "rpiConnect1". That name will appear in the devices list in Spotify.
curl -O http://spotify-connect-web.s3-website.eu-central-1.amazonaws.com/spotify-connect-web.sh
chmod u+x spotify-connect-web.sh
# Download the current chroot (~ 180 MB)
./spotify-connect-web.sh install
# Copy your `spotify_appkey.key` into the app directory. (See below for information on how to get that file.)
sudo cp spotify_appkey.key spotify-connect-web-chroot/usr/src/app/
# Run using normal cmdline options
./spotify-connect-web.sh --username 12345678 --password xyz123 --bitrate 320 --name Devicename

- That should work if you test the output to this device.


FURTHER CONFIGURATION, AUTOSTART & TROUBLESHOOTING

AUTOSTART:
To have the service start with our rpi, what I did (after extensive googling of course) is:
- Create a script in your pi home directory, called spserver.sh. Contents should be:

# set sound output to analog:
sudo amixer -c 0 cset numid=3 1

# run the spotify connect server in the background
/home/pi/spotify-connect-web.sh --name rpiConnect1 --username my_spotify_user --password my_spotify_pwd --bitrate 320


- Add in etc/rc.local, the lines below. Put them above the exit 0 command (leave it there). The ampersand (&) will get the process running in the background.

#start spotify connect
su pi -c /home/pi/spserver.sh &

FURTHER CONFIGURATION

In the link below I got some nice information on how to change a line to provide a default volume of 1. For that change line 74 in the file below (commenting out the math, and fixing "1" as volume)
/spotify-connect-web-chroot/usr/src/app/connect.py


ref. link:
http://support.hifiberry.com/hc/en-us/community/posts/201863051-hifiberry-digi-spotify-connect#community_comment_206970525
mixer_volume = 1 #int(mixer.getvolume()[0] * 655.35)

That together with the amixer setting solved the "no volume" situation I had at first.

TROUBLESHOOTING/NOTES
-  The analog output setting was necessary in my case as I (believe) I couldn't hear anything because I had an HDMI monitor connected (and so there goes my sound). The amixer setting (sudo amixer -c 0 cset numid=3 n) is selecting analog (where n is 0=auto, 1=headphones/analog, 2=hdmi). Probably it's a one time setting, but I couldn't check that yet.

ref. links:     
http://raspberrypi.stackexchange.com/questions/44/why-is-my-audio-sound-# output-not-working
http://elinux.org/R-Pi_Troubleshooting#Sound_does_not_work_at_all.2C_or_in_some_applications

- There can be static on the Rpi analog output; that's something to consider, there may be some solutions out there, the best is to change over to a better sound device for the rPi.

TODO:
- A wifi dongle (I have a TPLink device but it's attached for now to the Openelec rPI, also it's not anything with fancy speed)
- A better sound device for the Rpi:  either a HifiBerry DACor similar, or a USB inexpensive sound dongle. That will require more investigation for sure.
- Installing an Airplay solution too; how would it work together with spotify connect web? Trying now:
http://raspberrypihq.com/how-to-turn-your-raspberry-pi-into-a-airplay-receiver-to-stream-music-from-your-iphone/
- hardware volume handling: see at
https://github.com/Fornoth/spotify-connect-web/issues/11




2 comments:

  1. Hey there. Fascinating project you have documented. I followed your link from another forum after I read that you had a 70s marantz amp. I have the 2238b and have only just started getting into the world of hifi berry but found that I wanted Spotify connect above all else. I took delivery of the hifi berry today and assembled it. I got as far as the loading ok music box and got stumped at not being able to hear anything. I am reasonably command line savvy but after reading some of the stuff in forums, I'm beginning to wonder if I've chosen the right path. Getting Spotify connect would be great on the RPI but I'm simply lost at this point. Keep up the great work!

    ReplyDelete
  2. Thanks a lot Craig! Yeah you're in a similar situation I see. I'm not a skilled Linux guy so goes to show that we all can dive into this stuff. But takes a lot of effort even if you're a guru. The thing as others have pointed is that Spotify connect web is something that may be broken any day, as it depends on stuff not totally documented by Spotify or still in the works. I've not come back to this lately but I guess there were no changes.

    The box is working! that's good. The bad thing is that I can't manage the volume for the hifiberry amp from the cellphone app or anywhere remote for the fact. So I'm depending on the Marantz to do the volume up/down. No biggie but oh well.

    I am just buying a Hifiberry amp these days; so far I've been running on the DAC. I think I'll put VOLUMIO in there, directly. the thing is that there is no Connect there.. So you need to go to the app in the cell (or in the PC) and manage stuff from there, and I'd really prefer the Spotify app interface.

    Sorry for the late reply, just saw this comment now. Hope you've made progress on any path you've followed! Please let me know!

    ReplyDelete