ま"’s blog

電子工作の部屋?

SampleAppの設定して起動する(ラズパイZeroにAlexaをインストールできるか?その6)

この記事は「ラズパイZeroにAlexaをインストールできるか?」のサブ記事です。
「AIYをAlexa化」というカテゴリーを作ってそれに入れたので、これら関連の記事だけを見たい方はこのカテゴリーを選択してください。

今までの前振りで、ようやっとラズパイZeroでAlexa(avs-device-sdk)をビルドすることができました。

  1. ラズパイZeroの環境セットアップ(ラズパイZeroにAlexaをインストールできるか?その1) - ま"’s blog
  2. ラズパイZeroに最新ninjaをインストール(ラズパイZeroにAlexaをインストールできるか?その2) - ま"’s blog
  3. ラズパイZeroに最新CMakeをインストール(ラズパイZeroにAlexaをインストールできるか?その3) - ま"’s blog
  4. ラズパイZeroに最新curlをインストール(ラズパイZeroにAlexaをインストールできるか?その4) - ま"’s blog
  5. Alexaをmake(ラズパイZeroにAlexaをインストールできるか?その5) - ま"’s blog

あとは設定して起動するだけです。

設定ファイルの修正

AlexaClientSDKConfig.jsonに追加が必要なようなんですが、sdk-build以下にできるのは自動生成されるものなので、変更してもあまり意味がない、というか修正できませんでした。 なので、元ファイルの方を修正します。

cd $HOME/sdk-folder/sdk-source/avs-device-sdk/Integration/
cp -p AlexaClientSDKConfig.json{,.org}
vi AlexaClientSDKConfig.json

viを使い慣れてない人はnanoを使ってください。以下の3行を追加します。

diff -c AlexaClientSDKConfig.json{.org,}
*** AlexaClientSDKConfig.json.org	2023-04-02 13:50:57.473039726 +0900
--- AlexaClientSDKConfig.json	2023-04-02 14:00:57.272618390 +0900
***************
*** 11,16 ****
--- 11,19 ----
          "manufacturerName": "${SDK_CONFIG_MANUFACTURER_NAME}",
          "description": "${SDK_CONFIG_DEVICE_DESCRIPTION}"
      },
+     "gstreamerMediaPlayer":{
+         "audioSink":"alsasink"
+     },
      "capabilitiesDelegate":{
          "databaseFilePath":"${SDK_CAPABILITIES_DELEGATE_DATABASE_FILE_PATH}"
      },

デフォルトではautoでそれで問題ないのだそうですが、音が出なかったので音声出力先を明示的に指定しています。

設定ファイルの用意

Quick Start: Set Up the AVS Device SDK on Raspberry Pi for Voice-Only Devices | Alexa Voice Serviceを参考に進めます。

config.jsonを$HOME/sdk-folder/sdk-source/avs-device-sdk/tools/Install ディレクトリに置く

はじめにStep.1で作成した鍵ファイル(config.json)をラズパイのホームディレクトリにscpなりsftpなりで送っておきます。 私の母艦はMacなので、

Mac % scp config.json pi@raspberrypi:

とすれば、ラズパイのホームディレクトリにコピーされます。Windowsとかの人はteratermとか使うんでしょうかね?
んで、ラズパイの方で以下のようにターゲットディレクトリにコピーします。

cd $HOME/sdk-folder/sdk-source/avs-device-sdk/tools/Install
cp -p  $HOME/config.json .

genConfig.shを実行してAlexaClientSDKConfig.jsonを作成

cd $HOME/sdk-folder/sdk-source/avs-device-sdk/tools/Install
bash $HOME/sdk-folder/sdk-source/avs-device-sdk/tools/Install/genConfig.sh \
config.json \
12345 \
$HOME/sdk-folder/db \
$HOME/sdk-folder/sdk-source/avs-device-sdk \
$HOME/sdk-folder/sdk-build/Integration/AlexaClientSDKConfig.json \
-DSDK_CONFIG_MANUFACTURER_NAME="raspberrypi" \
-DSDK_CONFIG_DEVICE_DESCRIPTION="raspberrypi"

"raspberrypi"の所は好きに変えて問題ありません。私は登録した時に使った"VoiceKitAlexa"にしています。

マイクとスピーカーの設定をする

設定する前に、サウンドカードの番号とデバイス番号を調べる必要があります。

pi@raspberrypi:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: aiyvoicebonnet [aiy-voicebonnet], device 0: Google AIY Voice Bonnet SoundCard HiFi rt5645-aif1-0 [Google AIY Voice Bonnet SoundCard HiFi rt5645-aif1-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
pi@raspberrypi:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: aiyvoicebonnet [aiy-voicebonnet], device 0: Google AIY Voice Bonnet SoundCard HiFi rt5645-aif1-0 [Google AIY Voice Bonnet SoundCard HiFi rt5645-aif1-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

私のところではマイクもスピーカーもカード番号=0、デバイス番号=0でした。USBオーディオバイスとか繋げてると、番号が変わると思います。
以下のようにマイクで録音できるか、スピーカーで再生できるか確認します。

cd /home/pi
arecord rectest.wav
※マイクに向かってしゃべり音を録音する。録音の停止は、「Ctrl + C」を入力する。
aplay rectest.wav

録音と再生が確認できたら、以下のように音声入出力設定ファイルを編集します。

vi $HOME/.asoundrc
cat $HOME/.asoundrc
pcm.!default {
  type asym
   playback.pcm {
     type plug
     slave.pcm "hw:0,0" #← スピーカーの"hw:カード番号,デバイス番号"で設定する
   }
   capture.pcm {
     type plug
     slave.pcm "hw:0,0" #← マイクの"hw:カード番号,デバイス番号"で設定する
   }
}

SampleAppの起動と認証

cd /home/pi/sdk-folder/sdk-build
export PA_ALSA_PLUGHW=1 
./SampleApplications/ConsoleSampleApplication/src/SampleApp ./Integration/AlexaClientSDKConfig.json

Quick Start Step.7ではDEBUG9が指定されてますが、DEBUG出力に流されて重要な情報が遥か彼方に押し流されて見失ってしまうので、認証時には外した方がいいと思います。

##################################
#       NOT YET AUTHORIZED       #
##################################

################################################################################################
#       To authorize, browse to: 'https://amazon.com/us/code' and enter the code: ABCXYZ       #
################################################################################################

というようなメッセージがでるので、'https://amazon.com/us/code' をブラウザで開き、指定されたコード(ここのABCXYZに相当する文字列)を入力します。
ブラウザで入力するのはラズパイからでなくともOKでした。
ここら辺のやりとりは「 Alexaサンプルアプリの起動と認証」が参考になります。
最初は

#################################################
#       Checking for authorization (1)...       #
#################################################

こんな感じのが続きますが、成功すると:

###########################
#       Authorized!       #
###########################

というメッセージが表示されます。

言語とタイムゾーンの設定

########################################
#       Alexa is currently idle!       #
########################################

というメッセージが出たら、待ち受け状態になりました。ここで"i"に続いてRETURNを押すとヘルプが表示されます。

i
+----------------------------------------------------------------------------+
|                                  Options:                                  |
| Tap to talk:                                                               |
|       Press 't' and Enter followed by your query (no need for the 'Alexa').|
| Hold to talk:                                                              |
|       Press 'h' followed by Enter to simulate holding a button.            |
|       Then say your query (no need for the 'Alexa').                       |
|       Press 'h' followed by Enter to simulate releasing a button.          |
| Stop an interaction:                                                       |
|       Press 's' and Enter to stop an ongoing interaction.                  |
| Playback Controls:                                                         |
|       Press '1' for a 'PLAY' button press.                                 |
|       Press '2' for a 'PAUSE' button press.                                |
|       Press '3' for a 'NEXT' button press.                                 |
|       Press '4' for a 'PREVIOUS' button press.                             |
| Settings:                                                                  |
|       Press 'c' followed by Enter at any time to see the settings screen.  |
| Speaker Control:                                                           |
|       Press 'p' followed by Enter at any time to adjust speaker settings.  |
| Firmware Version:                                                          |
|       Press 'f' followed by Enter at any time to report a different        |
|       firmware version.                                                    |
| Info:                                                                      |
|       Press 'i' followed by Enter at any time to see the help screen.      |
| Reset device:                                                              |
|       Press 'k' followed by Enter at any time to reset your device. This   |
|       will erase any data stored in the device and you will have to        |
|       re-register your device.                                             |
|       This option will also exit the application.                          |
| Reauthorize device:                                                        |
|       Press 'z' followed by Enter at any time to re-authorize your device. |
|       This will erase any data stored in the device and initiate           |
|       re-authorization.                                                    |
| Device Setup Complete:                                                     |
|       Press 'v' followed by Enter at any time to indicate that device      |
|       setup is complete.                                                   |
|                                                                            |
| Quit:                                                                      |
|       Press 'q' followed by Enter at any time to quit the application.     |
+----------------------------------------------------------------------------+

'c'に続いてRETURNを押すと設定モードになります。

c
+----------------------------------------------------------------------------+
|                          Setting Options:                                  |
|  Press '1' followed by Enter to see language options.                      |
|  Press '2' followed by Enter to see Do Not Disturb options.                |
|  Press '3' followed by Enter to see wake word confirmation options.        |
|  Press '4' followed by Enter to see speech confirmation options.           |
|  Press '5' followed by Enter to see time zone options.                     |
|  Press '6' followed by Enter to see the network options.                   |
|  Press '7' followed by Enter to see the Alarm Volume Ramp options.         |
|  Press 'q' followed by Enter to exit Settings Options.                     |
+----------------------------------------------------------------------------+

'1'とRETURNで言語を設定します。バージョンによって番号が変わるので、ja-JPの番号を入力します。
3と4のoptionを設定しておくとAlexaっぽい挙動になります。このメニューのタイムゾーン設定ではアメリカ国内しか選択肢が出てこないので、別な方法で設定する必要があります。

デベロッパーサイトはamazon.comドメインなので、登録したデバイスもそっちのドメインhttps://alexa.amazon.com/spa/index.htmlのアカウントのデバイスとして表示されます。
ログイン後、設定→デバイス一覧から開発ターゲットをクリックし
バイスタイムゾーンの設定でAsia/Tokyoを選択します。

これで「今何時?」と聞いてもアメリカ時間を答えられることはなくなります。

動作確認

't'に続いてRETURNを押すことで、wakeキーワードなしで質問受付モードになります。「今何時?」と聞けば現時刻を音声で答えてくれます。残念ながら「Alexa」と呼びかけても反応しません。質問は聞き取れているようなので、マイクの問題ではなく、wakeキーワード認識時のマイク音量とかの設定をいじる必要があるのでしょうね。

結果

ハンズフリーという目標は未達ですけれど、ラズパイZeroでAlexa(avs-device-sdk)のビルドおよび実行にまではたどり着きました。残念ながら現時点ではこれ以上進めなさそうなので、ここでこのプロジェクトは凍結とさせていただきます。また進展があったらご報告しますね。