Skip to content

Instantly share code, notes, and snippets.

@NiallMoody
Last active August 21, 2021 10:10
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 NiallMoody/4c61d3e1d447d42f05a1cf0c606c350f to your computer and use it in GitHub Desktop.
Save NiallMoody/4c61d3e1d447d42f05a1cf0c606c350f to your computer and use it in GitHub Desktop.
Code snippet for using javascript text-to-speech in bipsi

To use javascript text to speech in bipsi, add the following code to a javascript event field called touch, before any of the await... lines:

let voice = new SpeechSynthesisUtterance();
voice.pitch = 1;
voice.rate = 1;
voice.text = "TEXT HERE";
window.speechSynthesis.speak(voice);

Replace 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).

There's technical documentation on the speech synthesis code on MDN.

Alternately...

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 bloblin as blow-blin, so I write it as blobblin in the javascript to get the correct pronounciation).

@NiallMoody
Copy link
Author

bipsi speech synthesis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment