Skip to content

Instantly share code, notes, and snippets.

@PonDad
Last active April 20, 2016 12:18
Show Gist options
  • Save PonDad/80e66996b441b0a89f0d to your computer and use it in GitHub Desktop.
Save PonDad/80e66996b441b0a89f0d to your computer and use it in GitHub Desktop.
Raspberry Pi とブラウザで会話する。 ref: http://qiita.com/PonDad/items/72bfcbbe8313f46a8890
var express = require('express');
var ejs = require("ejs");
var VoiceText = require('voicetext');
var Sound = require('node-aplay');
var fs = require('fs');
var app = express();
app.engine('ejs',ejs.renderFile);
app.get('/', function(req, res){
res.render('talk.ejs',
{title: 'Raspberry Pi Talk2'});
})
var voice = new VoiceText('uyxo9fc20r3bjp12');
app.get('/control', function (req, res) {
var text = req.query.text ? req.query.text : "";
console.log("Server:" + text);
var name = "ポンダッド"
 if(text == "はじめまして"){
text = "はじめまして、わたしはラズベリーパイって言います。"
}else if(text == "私は誰"){
text = name + "さんですね。よろしくね"
}else if(text == "頑張れよ"){
text = name + "お前も、頑張れよ。"
}
var speaker = req.query.speaker ? req.query.speaker : voice.SPEAKER.HARUKA;
var emotion = req.query.emotion ? req.query.emotion : voice.EMOTION.HAPPINESS;
var emotion_level = req.query.emotion_level ? req.query.emotion_level : voice.EMOTION_LEVEL.LOW;
var pitch = req.query.pitch ? req.query.pitch : 100;
var speed = req.query.speed ? req.query.speed : 100;
var volume = req.query.volume ? req.query.volume : 100;
voice.speaker(speaker)
.emotion(emotion)
.emotion_level(emotion_level)
.pitch(pitch)
.speed(speed)
.volume(volume)
.speak(text, function(e, buf){
return fs.writeFile('./talk.wav', buf, 'binary', function(e){
if(e){
return console.error(e);
}
new Sound('talk.wav').play();
})
});
res.send(text);
});
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('This app running at http://192.168.0.12:'+ port)
});
var express = require('express');
var ejs = require("ejs");
var VoiceText = require('voicetext');
var Sound = require('node-aplay');
var fs = require('fs');
var app = express();
app.engine('ejs',ejs.renderFile);
app.get('/', function(req, res){
res.render('talk.ejs',
{title: 'Raspberry Pi Talk2'});
})
var voice = new VoiceText('uyxo9fc20r3bjp12');
app.get('/control', function (req, res) {
var text = req.query.text ? req.query.text : "";
console.log("Server:" + text);
var name = "ポンダッド"
 if(text == "はじめまして"){
text = "はじめまして、わたしはラズベリーパイって言います。"
}else if(text == "私は誰"){
text = name + "さんですね。よろしくね"
}else if(text == "頑張れよ"){
text = name + "お前も、頑張れよ。"
}
var speaker = req.query.speaker ? req.query.speaker : voice.SPEAKER.HARUKA;
var emotion = req.query.emotion ? req.query.emotion : voice.EMOTION.HAPPINESS;
var emotion_level = req.query.emotion_level ? req.query.emotion_level : voice.EMOTION_LEVEL.LOW;
var pitch = req.query.pitch ? req.query.pitch : 100;
var speed = req.query.speed ? req.query.speed : 100;
var volume = req.query.volume ? req.query.volume : 100;
voice.speaker(speaker)
.emotion(emotion)
.emotion_level(emotion_level)
.pitch(pitch)
.speed(speed)
.volume(volume)
.speak(text, function(e, buf){
return fs.writeFile('./talk.wav', buf, 'binary', function(e){
if(e){
return console.error(e);
}
new Sound('talk.wav').play();
})
});
res.send(text);
});
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('This app running at http://192.168.0.12:'+ port)
});
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="content-type"
content="text/html; charset=UTF-8">
<title><%=title %></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<style>
article{
margin: 20px;
}
</style>
</head>
<body>
<header>
<h1 class="text-center h2"><%=title %></h1>
</header>
<article>
<div id="buttons" class="text-center">
<button id="recBtn" class="btn btn-default btn-lg fa fa-microphone"> マイク 開始</button>
</div>
</article>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
var rec;
var nowRec = false;
var rec = new webkitSpeechRecognition();
rec.continuous = true;
rec.interimResults = false;
rec.lang = 'ja-JP';
rec.onresult = function(e) {
for (var i = e.resultIndex; i < e.results.length; ++i) {
if (e.results[i].isFinal) {
var msg = e.results[i][0].transcript;
msg = msg.replace(/^\s+/g, "");
console.log("Recognised :" + msg);
$.get("http://192.168.0.12:3000/control",{text:msg}, function(data){console.log('Send :' + data);});
};
};
};
$("#recBtn").click(function() {
if(nowRec){
rec.stop();
$(this).attr("class","btn btn-default btn-lg fa fa-microphone");
$(this).text(" マイク 開始");
nowRec = false;
}else{
rec.start();
$(this).attr("class","btn btn-danger btn-lg fa fa-microphone");
$(this).text(" マイク 停止");
nowRec = true;
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment