naoqiリファレンスにあったsayToFileメソッドを使って、Pepperが喋る内容をファイル保存するボックスを作りました
作成方法
- Choregrapheのフローダイアグラムを右クリックしたのち、ボックスの新規作成からPythonボックスを作成
- ボックスのonStartを右クリックしてタイプをStringに変更
- ボックスを右クリックしてボックスのスクリプトを編集をクリック
- 以下のsayToFileボックスのスクリプトをコピーしてペースト
ボックスのパラメータに関して
以下の手順でボックスのパラメータを編集すると、ファイル名を指定することができます(今回生成するファイルは必ず日時が付属されます)
- 新規作成したPythonボックスを右クリックしてボックスを編集
- 変数の右側にある***+***をクリックして変数を追加
- 名前にfile nameを入力、タイプを文字列に変更
![Screen Shot 2016-11-20 at 16.02.54.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F91842%2F23fe03d4-c43d-b285-c57d-7824f3873981.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=bf1908aecf7953443bb51f71042c69cf)
sayToFileボックスのスクリプト
from datetime import datetime
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
self.tts = ALProxy('ALTextToSpeech')
self.ttsStop = ALProxy('ALTextToSpeech', True)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
#put clean-up code here
pass
def onInput_onStart(self, p):
#self.onStopped() #activate the output of the box
nowstr = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = ''
if self.getParameter('file name') != '':
filename = self.getParameter('file name') + nowstr + '.wav'
else:
filename = nowstr + '.wav'
# self.logger.debug(filename)
filePath = '/home/nao/recordings/' + filename
sayText = str(p)
try:
self.tts.sayToFile(sayText, filePath)
except RuntimeError:
self.logger.debug("Runtime Error")
pass
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box
使用例
以下は簡単な使用例になります
下記の画像のようにtext editボックスと私が作成したボックスを接続すると簡単に使用できます
保存したファイルの取り出し
Choregrapheからファイルの取り出しができます
- メニューから接続 -> アドバンスト -> **ファイルの転送...**を選択
- ロボットのパスワードが求められるので入力
- 一覧からrecordingsをダブルクリック
- 一覧に日時が記されたwavファイルがあるのでそれをダウンロード