Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@NiallMoody NiallMoody commented Aug 17, 2021

bipsi speech synthesis

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