RPi2でVolumio2を試す(目覚ましラジオ編)

音楽(FLAC)が聴けるようになったのはいいのですが、寝室で使うには目覚ましに NACK5を鳴らす必要があります。

まず /boot/config.txtの dtparam=audio=offは今回はやってないです。
Volumio2の設定を先に行います。

volumio2_volume1.jpg

アウトプットデバイスは「HiFiBerry DAC Plus」です。

volumio2_volume2.jpg

ボリュームオプションは、ミキサータイプを「Hardware」にします。
関連するパッケージをインストールします。 cronが入ってなければそれも入れます。

root@volumio:/home/volumio# apt-get update
root@volumio:/home/volumio# apt-get install rtmpdump swftools libxml2-utils mplayer
root@volumio:/home/volumio# apt-get install cron

サウンドデバイスを確認します。

root@volumio:/home/volumio# 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 1: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ HiFi pcm512x-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0

DACドーターボードは「card 1」ですね。
Installing and configuring the HiFiBerry DAC | HiFiBerry」を参考に /etc/asound.confを編集します。

root@volumio:/home/volumio# nano /etc/asound.conf
pcm.!default  {
 type hw card 1
}
ctl.!default {
 type hw card 1
}

受信スクリプトは「Raspberry Pi • View topic - インターネットラジオの受信」さんので基本的に変更ないのですが、Radikoの方は仕様変更があったようで、若干変化があります。
/rootディレクトリに保存し、パーミッションを755に設定します。

#!/bin/bash
 
# https://gist.github.com/matchy2/3956266 を若干修正
 
if [ $# -eq 1 ]; then
  channel=$1
else
  echo "usage : $0 channel_name"
  echo "         channel_name list"
  echo "           TBS Radio: TBS"
  echo "           Nippon Cultural Broadcasting: QRR"
  echo "           Nippon Broadcasting: LFR"
  echo "           Radio Nippon: JORF"
  echo "           Inter FM: INT"
  echo "           Tokyo FM: FMT"
  echo "           J-WAVE: FMJ"
  echo "           bayfm 78.0MHz: BAYFM78"
  echo "           NACK5: NACK5"
  echo "           FM yokohama 84.7: YFM"
  exit 1
fi
 
#
# parameter setting
#
pid=$$
date=`date '+%Y-%m-%d-%H:%M'`
playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf
outdir="${XDG_CACHE_HOME}/radio/"
playerfile="${outdir}/player.swf"
keyfile="${outdir}/authkey.png"
auth1_fms_file="${outdir}/auth1_fms_${pid}"
auth2_fms_file="${outdir}/auth2_fms_${pid}"
channel_file="${outdir}/${channel}.xml"
mkdir -p ${outdir}
 
#
# get player
#
if [ ! -f $playerfile ]; then
  wget -q -O $playerfile $playerurl
 
  if [ $? -ne 0 ]; then
    echo "failed to get player"
    exit 1
  fi
fi
 
#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
  swfextract -b 12 $playerfile -o $keyfile
 
  if [ ! -f $keyfile ]; then
    echo "failed to get keydata"
    exit 1
  fi
fi
 
if [ -f ${auth1_fms_file} ]; then
  rm -f ${auth1_fms_file}
fi
 
#
# access auth1_fms
#
wget -q \
     --header="pragma: no-cache" \
     --header="X-Radiko-App: pc_ts" \
     --header="X-Radiko-App-Version: 4.0.0" \
     --header="X-Radiko-User: test-stream" \
     --header="X-Radiko-Device: pc" \
     --post-data='\r\n' \
     --no-check-certificate \
     --save-headers \
     -O ${auth1_fms_file} \
     https://radiko.jp/v2/api/auth1_fms
 
if [ $? -ne 0 ]; then
  echo "failed auth1 process"
  exit 1
fi
 
#
# get partial key
#
authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' ${auth1_fms_file}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms_file}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms_file}`
 
partialkey=`dd if=$keyfile bs=1 skip=${offset} count=${length} 2> /dev/null | base64`
 
echo "authtoken: ${authtoken} \noffset: ${offset} length: ${length} \npartialkey: $partialkey"
 
rm -f ${auth1_fms_file}
 
if [ -f ${auth2_fms_file} ]; then
  rm -f ${auth2_fms_file}
fi
 
#
# access auth2_fms
#
wget -q \
     --header="pragma: no-cache" \
     --header="X-Radiko-App: pc_ts" \
     --header="X-Radiko-App-Version: 4.0.0" \
     --header="X-Radiko-User: test-stream" \
     --header="X-Radiko-Device: pc" \
     --header="X-Radiko-AuthToken: ${authtoken}" \
     --header="X-Radiko-PartialKey: ${partialkey}" \
     --post-data='\r\n' \
     --no-check-certificate \
     -O ${auth2_fms_file} \
     https://radiko.jp/v2/api/auth2_fms
 
if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
  echo "failed auth2 process"
  exit 1
fi
 
echo "authentication success"
 
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
echo "areaid: $areaid"
 
rm -f ${auth2_fms_file}
 
#
# get stream-url
#
 
if [ -f ${channel_file} ]; then
  rm -f ${channel_file}
fi
 
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}
 
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel_file} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
 
rm -f ${channel_file}
 
#
# rtmpdump and mplayer
#
rtmpdump -v \
         -r ${url_parts[0]} \
         --app ${url_parts[1]} \
         --playpath ${url_parts[2]} \
         -W $playerurl \
         -C S:"" -C S:"" -C S:"" -C S:$authtoken \
         --live\
         | mplayer -

試しに実行してみましょう。

root@volumio:/home/volumio# /root/play_radiko.sh NACK5
authtoken: iH_soUqLXMc6NU--Cy4FWA \noffset: 147207 length: 16 \npartialkey: f0/mKfqAmRu25GTjkc4oXA==
authentication success
areaid: JP11
RTMPDump v2.4
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
WARNING: No application or playpath in URL!
WARNING: You haven't specified an output file (-o filename), using stdout
Connecting ...
WARNING: Trying different position for server digest!
INFO: Connected...
MPlayer2 2.0-728-g2c378c7-4+b1 (C) 2000-2012 MPlayer Team
Cannot open file '/root/.mplayer/input.conf': No such file or directory
Failed to open /root/.mplayer/input.conf.
Cannot open file '/etc/mplayer/input.conf': No such file or directory
Failed to open /etc/mplayer/input.conf.
 
Playing -.
Reading from stdin...
Starting Live Stream
INFO: Metadata:
0.898 kB / 0.09 secDetected file format: FLV (Flash Video) (libavformat)
[flv @ 0x75b645b8]max_analyze_duration 5000000 reached
[flv @ 0x75b645b8]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: audio (aac), -aid 0
64.526 kB / 10.54 secClip info:
 StreamTitle:
Load subtitles in .
Selected audio codec: AAC (Advanced Audio Coding) [libavcodec]
AUDIO: 48000 Hz, 2 ch, floatle, 0.0 kbit/0.00% (ratio: 0->384000)
AO: [pulse] Init failed: Connection refused
[AO_ALSA] Format floatle is not supported by hardware, trying default.
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
[AO_ALSA] Unable to find simple control 'Master',0.
Video: no video
Starting playback...
^C1013.5 (16:53.4) of 0.0 (00.0)  9.6%

DACドーターボードのジャックにイヤホンを接続して、放送が流れてくることを確認したら、cronに起動スクリプトを登録します。

root@volumio:/home/volumio# nano /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
 
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
58 5    * * *   root    /usr/bin/amixer set Digital 80\% > /dev/null 2>&1
59 5    * * *   root    /root/play_radiko.sh NACK5 > /dev/null 2>&1
30 6    * * *   root    /usr/bin/amixer set Digital 83\% > /dev/null 2>&1
45 6    * * *   root    /usr/bin/amixer set Digital 85\% > /dev/null 2>&1
50 6    * * *   root    killall mplayer
51 6    * * *   root    /usr/bin/amixer set Digital 80\% > /dev/null 2>&1

段階的にボリュームを上げていますが、数値はアンプのボリュームとの兼ね合いで決定しています。
大丈夫そうなので、寝室に設置します。

IMG_20170409_224900.jpg

明日の朝はちゃんと鳴るかな?