Skip to content

Instantly share code, notes, and snippets.

@amoilanen
Last active October 6, 2016 07:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amoilanen/94e83e4047044380bc99 to your computer and use it in GitHub Desktop.
Save amoilanen/94e83e4047044380bc99 to your computer and use it in GitHub Desktop.
Demo of the JavaScript speech API, can be pasted and executed in the browser console in Google Chrome
//Based on the demo https://gist.github.com/wesbos/cd16b8b1815825f111a2
(function(host) {
if (typeof speechSynthesis === 'undefined') {
console.warn('No speech syntesis feature available,'
+ 'calls to "say" will be ignored, try latest Google Chrome');
host.say = function() {};
return;
}
var messageQueue = [];
var defaultVoiceName = 'Google US English';
function say(message, voiceName) {
if (areVoicesLoaded()) {
doSay(message, voiceName);
} else {
messageQueue.push({
message: message,
voiceName: voiceName
});
}
}
function areVoicesLoaded() {
return speechSynthesis.getVoices().length > 0;
}
function doSay(message, voiceName) {
var utterance = new SpeechSynthesisUtterance();
voiceName = voiceName || defaultVoiceName;
utterance.voice = speechSynthesis.getVoices().find(voice => voice.name == voiceName);
utterance.text = message;
speechSynthesis.speak(utterance);
}
speechSynthesis.onvoiceschanged = function() {
messageQueue.forEach(queueItem => doSay(queueItem.message, queueItem.voiceName));
messageQueue = [];
};
host.say = say;
})(this);
//Demo
say('JavaScript apps can talk in Google Chrome');
say('JavaScript aplicaciones pueden hablar en Google Chrome', 'Google español');
say('JavaScript-apps kunnen spreken in Google Chrome', 'Google Nederlands');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment