Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active March 16, 2018 17:50
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 AutoSponge/74de6860b4243b925f2587e20b67b780 to your computer and use it in GitHub Desktop.
Save AutoSponge/74de6860b4243b925f2587e20b67b780 to your computer and use it in GitHub Desktop.
async function speak(text) {
const msg = new SpeechSynthesisUtterance();
msg.text = text;
// msg.volume = 1; // 0 to 1
// msg.rate = 1; // 0.1 to 10
// msg.pitch = 1; //0 to 2
// msg.lang = this.DEST_LANG;
msg.voice = await new Promise(resolve => {
// Voice are populated, async.
speechSynthesis.onvoiceschanged = (e) => {
const voices = window.speechSynthesis.getVoices();
// for (const voice of voices) {
// console.log(voice.name, voice.localService)
// }
const name = 'Google UK English Male'; // Note: only works in Google Chrome.
resolve(voices.find(voice => voice.name === name));
};
});
msg.onend = e => console.log('SPEECH_DONE');
speechSynthesis.speak(msg);
}
speak(`yo! what's up?`)
// window.speechSynthesis.getVoices()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment