touch, before any of the
let voice = new SpeechSynthesisUtterance(); voice.pitch = 1; voice.rate = 1; voice.text = "TEXT HERE"; window.speechSynthesis.speak(voice);
TEXT HERE with your own text.
voice.pitch is the pitch of the voice. 1 is default, 2 is 2x the default pitch, 0.5 is half the default pitch.
voice.rate is the speed your text will be read out at. 1 is default, 2 is 2x as fast as the default, 0.5 is half as fast as the default.
To explain the other lines:
let voice = new SpeechSynthesisUtterance(); This creates a
SpeechSynthesisUtterance object called
voice. This is just a piece of code that contains all of the information the browser will need to speak your text. Once the object is created we can then set its pitch, rate, and text values.
window.speechSynthesis.speak(voice); This tells the browser to read the values we stored in our
SpeechSynthesisUtterance object, and speak them aloud using its speech synthesizer.
Note that this just uses the browser's default voice, and different browsers seem to have different default voices (I get an english voice on chrome, but an american one on firefox, for instance).
You can use the following code to grab the text directly from your character's 'say' field, if you don't want to type it out twice:
let voice = new SpeechSynthesisUtterance(); voice.pitch = 1; voice.rate = 1; voice.text = FIELD(EVENT, "say"); window.speechSynthesis.speak(voice);
I've found that the text-to-speech can mispronounce certain words, in which case it's better to use the first method (in my game it pronounces
blow-blin, so I write it as