Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active February 23, 2017 07:13
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 JoshCheek/54c3365f2f29168e6918f26b27904fb3 to your computer and use it in GitHub Desktop.
Save JoshCheek/54c3365f2f29168e6918f26b27904fb3 to your computer and use it in GitHub Desktop.
Get the browser to talk to you in Spanish (text to speech)
<!doctype html>
<!-- http://codepen.io/josh_cheek/pen/WpeqKp -->
<meta charset="utf-8"/>
<textarea autofocus cols="30" rows="6">Nuestras gatas beben leche</textarea>
<button onclick="say(document.querySelector('textarea').value)">
¡Hablas!
</button>
<script>
const synth = window.speechSynthesis
const mexicanSpanish = 'es-MX'
function say(text) {
const u = new SpeechSynthesisUtterance(text)
u.voice = synth.getVoices() // returns [] if it hasn't loaded the voices yet :/ there is a `synth.onvoiceschanged` event which mith be able to mitigate this
.find(v => v.lang === mexicanSpanish)
u.rate = 1 // default speed, larger is faster
u.pitch = 1 // default pitch, larger is higher frequency (rhs of piano)
u.volume = 1 // default volume, larger is louder
synth.speak(u)
}
</script>
<style>
textarea, button {
font-size: 2em;
margin-top: 1em;
margin-left: 1em;
padding: 0.3em 0.5em;
display: block;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment