Skip to content

Instantly share code, notes, and snippets.

@kevinrobinson
Created October 6, 2017 03:32
Show Gist options
  • Save kevinrobinson/abdd8a9f334deaef5a3549376adc7aa6 to your computer and use it in GitHub Desktop.
Save kevinrobinson/abdd8a9f334deaef5a3549376adc7aa6 to your computer and use it in GitHub Desktop.
function randomizeVoice(text, callback) {
const voices = window.speechSynthesis.getVoices();
const words = text.split(' ');
sayWords(words, voices, callback);
}
function sayWords(words, voices, callback) {
if (words.length === 0) return callback();
var voice = voices[Math.round(Math.random() * voices.length)];
sayWord(words[0], voice, () => sayWords(words.slice(1), voices));
}
function sayWord(word, voice, callback) {
var message = new SpeechSynthesisUtterance();
message.text = word;
message.voice = voice;
message.rate = 0.9;
message.lang = 'en-US';
message.addEventListener('end', callback);
window.speechSynthesis.speak(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment