Last active
February 17, 2017 14:09
-
-
Save chrisritter/25faedd4c8fa569fc2f8 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
/* | |
https://rawgit.com/chrisritter/25faedd4c8fa569fc2f8/raw/speech.js | |
*/ | |
//listen (callback) | |
window.listen = function(callback) { | |
if(window.webkitSpeechRecognition) { | |
var recognition = new webkitSpeechRecognition(); | |
recognition.onresult = function(e) { | |
callback(e.results[0][0].transcript, | |
e.results[0][0].confidence); | |
recognition.stop(); | |
} | |
recognition.start(); | |
} else { | |
var answer = prompt("What would you like to say?"); | |
callback(answer, 1); | |
} | |
}; | |
//talk | |
window.talk = function(text) { | |
if(window.SpeechSynthesisUtterance) { | |
var msg = new SpeechSynthesisUtterance(); | |
var voices = window.speechSynthesis.getVoices(); | |
//msg.voice = voices[10]; // Note: some voices don't support altering params | |
msg.voiceURI = 'native'; | |
msg.text = text; | |
msg.lang = 'en-US'; | |
speechSynthesis.speak(msg); | |
} else { | |
alert(text); | |
} | |
}; | |
if (!String.prototype.includes) { | |
String.prototype.includes = function(search, start) { | |
'use strict'; | |
if (typeof start !== 'number') { | |
start = 0; | |
} | |
if (start + search.length > this.length) { | |
return false; | |
} else { | |
return this.indexOf(search, start) !== -1; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment