Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Last active August 13, 2019 00:16
Show Gist options
  • Select an option

  • Save justanotherdot/d314845a882953b707d787b2e4266dd8 to your computer and use it in GitHub Desktop.

Select an option

Save justanotherdot/d314845a882953b707d787b2e4266dd8 to your computer and use it in GitHub Desktop.
const talk = (optText) => {
let text = optText || Array.from(document.querySelectorAll('p')).map(p => p.outerText).join(' ');
let t2s = new SpeechSynthesisUtterance(text);
let voices = window.speechSynthesis.getVoices();
t2s.voice = voices.filter(v => v.name === 'Samantha')[0]; // Samantha is the standard US voice and lives at index 32 when I call this for my browser. Your locale may change this ordering.
t2s.onend = function(e) { console.log('Read in ' + e.elapsedTime + ' seconds'); };
window.speechSynthesis.speak(t2s);
};
// You can stop the output by calling...
const pause = () => window.speechSynthesis.paused ? window.speechSynthesis.resume() : window.speechSynthesis.pause();
const stop = () => window.speechSynthesis.cancel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment