Skip to content

Instantly share code, notes, and snippets.

@chrisritter
Last active February 17, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisritter/25faedd4c8fa569fc2f8 to your computer and use it in GitHub Desktop.
Save chrisritter/25faedd4c8fa569fc2f8 to your computer and use it in GitHub Desktop.
/*
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