初老のボケ防止日記

おっさんのひとりごとだから気にしないようにな。

Raspberry Pi 3でSpotify Connectしてみた

HAHAHA. 知人とは繋がらないのにSpotifyには繋がったぜ

最近愛用しているSpotify

www.spotify.com

AppleにはAirPlay、GoogleにはGoogle Castがあるように、Spotifyには

Spotify Connectがあるのだ。

Spotify Connectってなんぞ

Spotify Connect を使うと、以下のデバイスでSpotifyアプリをリモコンみたいに操作して、他のデバイスでSpotifyを再生することができます。

Spotify Connect を使ってデバイスに接続する - Spotify

ようは、スマホアプリをリモコン代わりにして別の機器で音楽再生するやつです。で、具体的にSpotify Connectに接続可能なデバイスは以下にあるのですが

Spotify Gear - Spotify loves speakers, TVs, cars and gaming

基本的に製品だ。
実のところ、この辺りはAirPlayもGoogle Castも同じでウチのサービス使って製品売るならライセンス契約せいやっていう話なんだが、なんとOSSでConnectすることができるらしいというのを見つけたので試してみました。

前提

因みに、Spotify Connectを利用するには

PREMIUMアカウントが必要。

でもって、Raspberry PiでSpotify Connectする為に必要なLibSpotify は

既に廃止が確定しており、2017年迄にいつか突然使えなくなるのだ。
https://developer.spotify.com/technologies/libspotify/

ということを理解した上でやってみたいヒトは以下の手順でどうぞ。

ディストリビューション

今回はいつのまにか正式版がリリースされているVolumio2を使いますが、Raspbian系なら多分問題なく動作すると思われます。

volumio.org

VERSION 2.041
RELEASE DATE 12-12-2016

Raspberry Piのセットアップが出来てないヒトはまずはこいつをインストールしましょう。

なお、Spotify Connectに拘る必要がなければVolumio2のSpotify Pluginをインストールすればブラウザ経由でSpotifyを利用できるのでそれはそれでオススメ(コチラもPREMIUMアカウントは必要)。

f:id:osa030:20161220231525j:plain

Spotify Connect Web

Raspberry PiでSpotify Connectをするには以下をインストールするだけ。

github.com

以降はsshでの作業。Volumioのログインアカウントは以下の通り。

user volumio
password volumio

インストール

$ wget https://github.com/Fornoth/spotify-connect-web/releases/download/0.0.3-alpha/spotify-connect-web_0.0.3-alpha.tar.gz
$ tar zxvf spotify-connect-web_0.0.3-alpha.tar.gz

完了。

$ cd spotify-connect-web/
$ ./spotify-connect-web
Loading Spotify library...
min_volume_range: 0.0
Using libspotify_embedded version: release-esdk-1.20.0-v1.20.0-g594175d4
Error opening app key: [Errno 2] No such file or directory: 'spotify_appkey.key'.
If you don't have one, it can be obtained from https://developer.spotify.com/my-account/keys

キーがないとダメよ、と。

アプリケーションキーの申請

上記メッセージのURLに飛ぶと以下にリダイレクトされる。

Libspotify Application Keys - Spotify Developer

The libspotify key generation and retrieval service has been deprecated, along with libspotify.
You may apply to get a key using our
App-key Request Form for private, individual usage. Bear in mind that you should do no new development using libspotify.

フォームから申請しろと言われるので、以下の項目を入力して気長に待つ。

  • メールアドレス
  • Describe your integration
  • List the Spotify technology/ies used in your integration
  • Your Name
  • Your e-mail
  • Spotify premium account to be connected to app-key

"Describe your integration"には「RPIでSpotifyConnectしたいのです」というアツいメッセージを、"List the Spotify technology/ies used in your integration"には「Spotify Connect」を入れて気長に待つ*1

f:id:osa030:20161220231545j:plain

1週間位でメールが来たのでメールに添付された「spotify_appkey.key」をscpでRaspberry Piに転送して先程の展開したディレクトリに配置。

起動してみる

spotify-connect-webの起動オプションは以下。

$ ./spotify-connect-web --help
Loading Spotify library...
min_volume_range: 0.0
usage: spotify-connect-web [-h]
                           [--device DEVICE | --playback_device PLAYBACK_DEVICE]
                           [--mixer_device_index MIXER_DEVICE_INDEX]
                           [--mixer MIXER] [--dbrange DBRANGE] [--debug]
                           [--key KEY] [--username USERNAME]
                           [--password PASSWORD] [--name NAME]
                           [--bitrate {90,160,320}]
                           [--credentials CREDENTIALS]

Web interface for Spotify Connect

optional arguments:
  -h, --help            show this help message and exit
  --device DEVICE, -D DEVICE
                        alsa output device (deprecated, use --playback_device)
  --playback_device PLAYBACK_DEVICE, -o PLAYBACK_DEVICE
                        alsa output device (get name from aplay -L)
  --mixer_device_index MIXER_DEVICE_INDEX
                        alsa card index of the mixer device
  --mixer MIXER, -m MIXER
                        alsa mixer name for volume control
  --dbrange DBRANGE, -r DBRANGE
                        alsa mixer volume range in Db
  --debug, -d           enable libspotify_embedded/flask debug output
  --key KEY, -k KEY     path to spotify_appkey.key (can be obtained from
                        https://developer.spotify.com/my-account/keys )
  --username USERNAME, -u USERNAME
                        your spotify username
  --password PASSWORD, -p PASSWORD
                        your spotify password
  --name NAME, -n NAME  name that shows up in the spotify client
  --bitrate {90,160,320}, -b {90,160,320}
                        Sets bitrate of audio stream (may not actually work)
  --credentials CREDENTIALS, -c CREDENTIALS
                        File to load and save credentials from/to

最低限指定するのは「username」と「password」で、PREMIUMアカウントのログイン情報を指定すればよい。
RaspberryPiの音声出力先を指定するにはまず以下でデバイス名を調べる

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 5:DA10 [DA-10], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

今回はRaspberry Pi3にDENONのDA-10をUSB接続しているので「DA10」を指定する。ついでにビットレートも最高の320に指定。

$ ./spotify-connect-web --username XXXXX --password XXXXX -b 320 -D DA10
Loading Spotify library...
min_volume_range: 0.0
Using libspotify_embedded version: release-esdk-1.20.0-v1.20.0-g594175d4
SpInit: 0
playback_volume: 39321
corected_playback_volume: 60
public key: XXXXXX
device id: XXXXX
remote name: TestConnect
account req: PREMIUM
device type: AUDIODONGLE

この状態でスマホのSpotifyアプリを見ると...

f:id:osa030:20161220231603j:plain

「TestConnect」というのが増えている。コイツがRaspberry Piだ。

f:id:osa030:20161220231608j:plain

「TestConnect」を選択すると見事、音が流れました。

自動起動設定

動作確認できたので、Raspberry Piが起動したら自動的に起動するようにする。

$ sudo sudo apt-get install vim
$ sudo vi /usr/lib/systemd/system/spotify-connect.service
  • /usr/lib/systemd/system/spotify-connect.service
[Unit]
Description=Spotify Connect
After=network.target

[Service]
ExecStart=/home/volumio/spotify-connect-web/spotify-connect-web --name RpiConnect --username XXXXX --password XXXXX -b 320 -D DA10 -k /home/volumio/spotify-connect-web/spotify_appkey.key

[Install]
WantedBy=multi-user.target

パラメータに「name」というのを追加しているがこれはスマホ等のSpotifyアプリ側で表示される名称。

$ sudo systemctl start spotify-connect
$ sudo systemctl enable spotify-connect
Created symlink from /etc/systemd/system/multi-user.target.wants/spotify-connect.service to /usr/lib/systemd/system/spotify-connect.service.

ここまでやったら再起動してみる。

$ sudo systemctl status spotify-connect
● spotify-connect.service - Spotify Connect
   Loaded: loaded (/usr/lib/systemd/system/spotify-connect.service; enabled)
   Active: active (running) since Tue 2016-12-20 13:22:06 UTC; 3s ago
 Main PID: 2161 (spotify-connect)
   CGroup: /system.slice/spotify-connect.service
           tq2161 /home/volumio/spotify-connect-web/spotify-connect-web --name RpiConnect --username o...
           mq2162 /home/volumio/spotify-connect-web/spotify-connect-web --name RpiConnect --username o...

Dec 20 13:22:06 volumio systemd[1]: Started Spotify Connect.

サービスとして起動しているのを確認。

f:id:osa030:20161220231615j:plain

「RpiConnect」が起動パラメータで指定した名称。

f:id:osa030:20161220231619j:plain

Connectされました。

現状のLibSpotifyはいつ使えなくなるかわからないけども、現状は他のAPIでConnectはできないと思うので選択肢はこれしかないのであるが、AirPlayはともかく、Google CastのレシーバーのAPIが公開されてRasspbery Piで実装できたらそれがいいのになあと思ったけれども、SpotifyはGoogle Castにも対応しているので
Chromecast Audioを買えば解決なんじゃねという結論に至りました。

*1:念のため、SpotifyアカウントとDeveloperアカウントを紐付けておいたが必要かどうかは不明