Created
October 6, 2017 03:32
-
-
Save kevinrobinson/abdd8a9f334deaef5a3549376adc7aa6 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
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