Created
July 22, 2016 06:41
-
-
Save MacTanomsup/ce19661f56eb601db5d4870083f46aeb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Home Voice Control</title> | |
<link rel="stylesheet" href="mystyle.css"> | |
</head> | |
<body> | |
<center> | |
<a href="https://medium.com/@maccismyname"> | |
<img src="logo.png"/> | |
</a> | |
</center> | |
<div class="container"> | |
<br> | |
<center> | |
<h2 id="info_microphone">สวัสดี, วันนี้ต้องการให้ช่วยอะไร?</h2> | |
</center> | |
<br> | |
<center> | |
<button id="start_button" onclick="startButton(event)"> | |
<img id="start_img" src="mic.gif" alt="Start"></button> | |
</center> | |
<br> | |
<div id="results"> | |
<p>คำสั่ง : </p> | |
<p id="final_span" class="final"></p> | |
<br> | |
<p>ผลลัพธ์ : </p> | |
<p id="final_result"></p> | |
</div> | |
<p>ลองพูดคำสั่งต่อไปนี้สิ : สวัสดี, สบายดีไหม, เปิดไฟหน้าบ้าน, ปิดไฟทั้งหมด </p> | |
</div> | |
</body> | |
<script> | |
//Welcome sound !! | |
var msg = new SpeechSynthesisUtterance('สวัสดี, วันนี้ต้องการให้ช่วยอะไร?'); | |
msg.lang = 'th-TH'; | |
window.speechSynthesis.speak(msg); | |
var final_transcript = ''; | |
var recognizing = false; | |
var ignore_onend; | |
if (!('webkitSpeechRecognition' in window)) { | |
//if don't have a webkitSpeechRecognition | |
} else { | |
var recognition = new webkitSpeechRecognition(); | |
recognition.lang = "th-TH"; | |
recognition.onstart = function() { | |
recognizing = true; | |
start_img.src = 'mic-animate.gif'; | |
}; | |
recognition.onresult = function(event) { | |
console.log(event) | |
var interim_transcript = ''; | |
for (var i = event.resultIndex; i < event.results.length; ++i) { | |
if (event.results[i].isFinal) { | |
final_transcript += event.results[i][0].transcript; | |
} else { | |
interim_transcript += event.results[i][0].transcript; | |
} | |
} | |
final_span.innerHTML = final_transcript; | |
speak(final_transcript); | |
} | |
} | |
function startButton(event) { | |
if (recognizing) { | |
recognition.stop(); | |
return; | |
} | |
final_transcript = ''; | |
recognition.start(); | |
ignore_onend = false; | |
start_img.src = 'mic-slash.gif'; | |
} | |
recognition.onend = function() { | |
recognizing = false; | |
if (ignore_onend) { | |
return; | |
} | |
start_img.src = 'mic.gif'; | |
}; | |
// ignore // | |
// var first_char = /\S/; | |
// function capitalize(s) { | |
// return s.replace(first_char, function(m) { return m.toUpperCase(); }); | |
// } | |
// | |
// var two_line = /\n\n/g; | |
// var one_line = /\n/g; | |
// function linebreak(s) { | |
// return s.replace(two_line, '<p></p>').replace(one_line, '<br>'); | |
// } | |
// ignore // | |
function speak(text) { | |
//Hardcode becuz it's a tutorial. Got it? | |
var msg_light = 'เปิดไฟหน้าบ้าน'; | |
var turnoffAllLight = 'ปิดไฟทั้งหมด' | |
var whatsup = 'สบายดีไหม'; | |
var sawasdee = 'สวัสดี'; | |
//Text to speech | |
var speechSynthesis = new SpeechSynthesisUtterance(); | |
speechSynthesis.voiceURI = 'native'; | |
speechSynthesis.volume = 1; // 0 to 1 | |
speechSynthesis.rate = 1; // 0.1 to 10 | |
speechSynthesis.pitch = 2; //0 to 2 | |
speechSynthesis.lang = 'th-TH'; | |
var msg = ''; | |
//Do whatever you want. | |
if(text.search(msg_light) != -1 || text.search(turnoffAllLight) != -1 ) { | |
msg = "ทำการ" + text + "เรียบร้อยแล้วค่ะ"; | |
speechSynthesis.text = msg; | |
} else if(text.search(whatsup) != -1) { | |
msg = 'สบายดีค่ะ, แล้วคุณหล่ะคะ '; | |
speechSynthesis.text = msg; | |
} else if(text.search(sawasdee) != -1) { | |
msg = 'สวัสดีค่ะ หนีฮ่าว สบายดี ฮัลโหล'; | |
speechSynthesis.text = msg; | |
} else { | |
msg = 'คำสั่งดังกล่าวยังไม่มีในฐานข้อมูลค่ะ'; | |
speechSynthesis.text = msg; | |
} | |
window.speechSynthesis.speak(speechSynthesis); | |
final_result.innerHTML = msg; | |
} | |
</script> | |
</html> |
<script src="https://static.line-scdn.net/liff/edge/2/sdk.js"></script>
Hello
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wow