make the smart speaker smarter 3

08/12

There’s something about Node.js

前回はApplication Serverを作ったのだが
気がつくと不定期に落ちているときがある。

そこで、プロセスが落ちてたら起動する方法が
必要だと思って自前でcron+shell scriptで用意したのだが
調べたらforeverというモジュールを使えば
簡単にできるらしかったのだが、まいいか。

shell scriptはまぁここでは趣旨から脱線するので
公開はしない。

Let 2s say something through the VoiceText

さて、Application Serverができたので
Google Homeから任意の声で喋らせてみたい。

先のvoicetextをもっと使いやすい形にする。

var fs = require('fs');
var VoiceText = require('voicetext');
var voice = new VoiceText('XXXXXXXXXXXX');
var OUT_PATH = __dirname+'/_temp.wav'
var OUTPUT_URL = 'http://%GOOGLEHOME%:8080/googlehome/get/_temp.wav';

class VoiceTextOut {
	textToURL(text){
		if(!text)return false;
        return new Promise(function(resolve,reject){
            voice
            .speaker(voice.SPEAKER.HIKARI)
            .emotion(voice.EMOTION.HAPPINESS)
            .emotion_level(voice.EMOTION_LEVEL.HIGH)
            .volume(150)
            .speak(text, function(e, buf){
                if(e){
                    console.error(e);
                    reject(e);
                }else{
                    fs.writeFileSync(OUT_PATH, buf, 'binary');
                    resolve(OUTPUT_URL);
                }
            });
        });
    }
}
module.exports = VoiceTextOut;

テキストを渡されてwaveファイルに保存して呼び出し元にURLを返すってやつ。
VoiceTextOutなんてクラスにしてみる。

で、このクラスをgoogle-home-notifier.js内のgetSpeechUrl()メソッドで
使ってみる。
実際にはこんな風になっている。

var getSpeechUrl = function(text, host, callback) {
    googletts(text, language, 1).then(function (url) {
        onDeviceUp(host, url, function(res){
            callback(res)
        });
    }).catch(function (err) {
        console.error(err.stack);
    });
};

ここをこんな感じに変更。

8  const VoiceTextOut = require('./../../VoiceTextOut');
9  const vt = new VoiceTextOut();
.
.
.
.
72 var getSpeechUrl = function(text, host, callback) {
73    vt.textToURL(text).then(function(result, reject){
74 		onDeviceUp(host, result, function(res){
75            callback(res)
76         });
77     }).catch(function onRejected(error){
78         console.error(error);
79    });
80 };

これで準備ができたかな。
さっそく喋らせてみる。
前に作ったこいつを使ってみる。

const googleSpeaker=require('./GoogleSpeaker');
gs=new googleSpeaker();
gs.meow('子どもがまだ食ってる途中でしょうが!');

青大将

よし、大丈夫そうだ。

Next

次は、スケジュールどおりに何かを喋らせてみよう。
何かをトリガーにして喋らせるってところの
基本的なプログラムを作る。


コメント: