Last active
February 23, 2017 07:13
-
-
Save JoshCheek/54c3365f2f29168e6918f26b27904fb3 to your computer and use it in GitHub Desktop.
Get the browser to talk to you in Spanish (text to speech)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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