-
-
Save wesbos/cd16b8b1815825f111a2 to your computer and use it in GitHub Desktop.
// paste in your console | |
speechSynthesis.onvoiceschanged = function() { | |
var msg = new SpeechSynthesisUtterance(); | |
msg.voice = this.getVoices().filter(v => v.name == 'Cellos')[0]; | |
msg.text = Object.keys(window).join(' '); | |
this.speak(msg); | |
}; |
In case you'd like to know what they all sound like (there's a bunch of funny ones)
var msgs = speechSynthesis.getVoices().map( (v, i) => { return { voice: v, text: `hi there from the voice named ${v.name}` }; }),
speaker = new SpeechSynthesisUtterance(),
halt = false;
function speakOne(i) {
if (halt) { halt = false; return; }
speaker.voice = msgs[i].voice;
speaker.text = msgs[i].text;
speechSynthesis.speak(speaker);
console.log(`Saying "${speaker.voice.name}"`);
}
function speakAll() {
var cursor = 0;
speaker.onend = () => (++cursor) >= msgs.length? true : speakOne(cursor); // chain each end
speakOne(cursor); // start the series
}
To bail, set halt
to true in the console. speechSynthesis.cancel()
will only cancel the currently speaking voice.
I like Bad News, Bells, Cellos, Hysterical, and the way the language-specific Google voices butcher the words "hi there from."
Call speakOne(i)
to listen to any specific voice by index, or speakAll()
to listen to them all.
I also gist
ed this here.
I've got Japanese too. Is this standard in all Chrome installs?
var msg = new SpeechSynthesisUtterance();
msg.voice = speechSynthesis.getVoices().filter(v => v.name == 'Google 日本語')[0];
msg.text = 'こんにちは皆さん!';
speechSynthesis.speak(msg);
@akenn The reason it's wrapped in the voiceschanged
eventlistener, is that the voices are loaded asynchronously and this way you can make sure that they actually exist before you start using them.
It's no problem when you just run the snippet in the dev console, but if it's embedded on a page and set to run immediately, you'll get an error.
Here's a pretty cool Pen to play around with the different voices.
@ToreJuloe oh good to know - thanks!
Why not use Array#find
? .filter(v => v.name == 'Cellos')[0]
→ .find(v => v.name == 'Cellos')
@msikma Not in my Opera/Chrome.
Its not working in Linux window.speechSynthesis.getVoices()
returns empty array no "voices" :(
Wow 👍
Love it!
Awesome!!
I really hope I wasn't the last one to find out this! Amazing 🎉
Awesome! You can also invoke speech synthesis from outside the
onvoiceschanged
method: