2018年03月25日

Google Home で時報を作って読み上げさせて見た。

前回作ったGoogle-Home-NotifierのServerを更に改造して音声(MP3)を読み上げさせるようにしてみた。

概要
RaspberryPiのcrontabで毎定時にGoogleHomeからランダムなメロディー(時報読み上げMP3)を流す。

サンプル


必要なもの
・自宅サーバー(Raspberry Piなど)
 GoogleHomeNotifierのServerとApacheをPortを分けて起動(1つにしたいな)
・音声ファイル素材(声)
 「あみたろの声素材工房」さんの声素材を使わせていただきました。
・音声ファイル素材(メロティー)
 「ポケットサウンド」さんの効果音素材を使わせて頂きました。

素材フォルダの構成(MP3ファイルの置き場所)
・./ampm
 声(午前、午後 を配置)
・./desu
 声(です。でーす。などをランダムで再生)
・./ji
 声(○時 00.MP3 - 11.MP3を配置)
・./melody
 メロディー(ランダムで再生するのでたくさん置く)

ソースコード
・server.js(前回の記事参照)
・config.js(前回の記事参照)
・commonlib.js(GoogleHomeNotifierのserver.jsにPostするモジュールを作成して汎用化)

// commonlib.js
exports.SendMessage = function(type, speakerNumber, sendText) {

var request = require('request');
var options = {
uri: "http://xxx.xxx.xxx.xxx:xxxx", // RaspberryPiのserver.js
headers: {
"Content-Type" : "application/json",
},
json: {
"type" : type,
"speakerNumber": speakerNumber,
"message": sendText
}
};
request.post(options, function(error, response, body){
console.log(error);
});
}

・timesignal.js

// timesignal.js
"use strict";

const GoogleHomeLib = require('../commonlib'); // 作成したcommonlib.jsを呼び出す
const fs = require('fs');

// フォルダ設定
const melodyDir = './melody';
const ampmDir = './ampm';
const timeVoiceDir = './ji';
const desuDir = './desu';
const talkFile = '/xxx/xxx/xxx/signal.mp3';    // RaspberryPiのApacheでのMP3の場所
const sendUrl = 'http://xxx.xxx.xxx.xxx/xxx/signal.mp3'; // RaspberryPiのApacheでのURL

var melodyFiles = fs.readdirSync(melodyDir);
var melody = GetRandomFile(melodyDir, melodyFiles);
var timeVoice = GetVoiceString();
var desuFiles = fs.readdirSync(desuDir);
var desu = GetRandomFile(desuDir, desuFiles);

// コマンドライン作成
var command = 'cat ' + melody + ' ' + timeVoice + ' ' + desu + ' > ' + talkFile;
console.log(command);

// MP3作成
const execSync = require('child_process').execSync;
const result = execSync(command);

// GoogleHomeへ転送
GoogleHomeLib.SendMessage('sound', 0, sendUrl);

// 時報ファイル取得
function GetVoiceString(){
var todayNow = new Date();
var hours = todayNow.getHours();
// crontabで○時59分に50秒くらいSleepするので次の時刻にする
var signalTime = hours + 1;
var ampmFile;

if (signalTime == 24) {
// 日付変更
ampmFile = ampmDir + '/' + 'gozen.mp3';
signalTime = 0;
} else if (signalTime >= 12) {
// 午後
ampmFile = ampmDir + '/' + 'gogo.mp3';
signalTime -= 12;
} else {
// 午前
ampmFile = ampmDir + '/' + 'gozen.mp3';
}
//
var hourString = ('00' + signalTime).slice(-2);
var timeFile = timeVoiceDir + '/' + hourString + '.mp3';
return ampmFile + ' ' + timeFile;
}

// メロディーファイル取得
function GetRandomFile(Path, fileList){
var min = 0;
var max = fileList.length - 1;
// ランダム選択
var randomIndex = Math.floor( Math.random() * (max + 1 - min) ) + min ;
return Path + '/' + fileList[randomIndex];
}


起動設定
・シェルを作成
timesignal.sh

cd /home/pi/Googlehome/timesignal
node ./timesignal.js

・crontabを設定
毎時59分50秒に開始(7時から23時まで鳴る設定)

59 6-22 * * * sleep 50; /home/pi/Googlehome/timesignal/timesignal.sh >>/tmp/cron.log 2>>/tmp/cronerror.log


最後に
メロディーと声をcatコマンドで繋げられる事を今回初めて知ったけど、サンプリングレート?、それともビットレート?が合っていないと繋いだMP3が正しく再生されないので編集ソフトを使って色々設定を変えるのに苦労した。
posted by Vintage at 14:13| Comment(0) | TrackBack(0) | Google Home
この記事へのコメント
コメントを書く
お名前: [必須入力]

メールアドレス:

ホームページアドレス:

コメント: [必須入力]

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
この記事へのトラックバックURL
http://blog.sakura.ne.jp/tb/182796692

この記事へのトラックバック