Skip to content

Instantly share code, notes, and snippets.

@kypflug
Last active May 4, 2023 10:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kypflug/d5cce2c55bd3b8c62eb74403ec089a55 to your computer and use it in GitHub Desktop.
Save kypflug/d5cce2c55bd3b8c62eb74403ec089a55 to your computer and use it in GitHub Desktop.
function speak(textToSpeech) {
var synUtterance = new SpeechSynthesisUtterance(textToSpeech);
if (voiceSelect.value) {
synUtterance.voice = speechSynthesis.getVoices().filter(function (voice) { return voice.name == voiceSelect.value; })[0];
}
synUtterance.lang = langSelect.value;
synUtterance.volume = parseFloat(volumeRange.value);
synUtterance.rate = parseFloat(rateRange.value);
synUtterance.pitch = parseFloat(pitchRange.value);
const eventList = ["start", "end", "mark", "pause", "resume", "error", "boundary"];
eventList.forEach((event) => {
synUtterance.addEventListener(event, (speechSynthesisEvent) => {
log(`Fired '${speechSynthesisEvent.type}' event at time '${speechSynthesisEvent.elapsedTime}' and character '${speechSynthesisEvent.charIndex}'.`);
});
});
window.speechSynthesis.speak(synUtterance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment