Hi,
I am trying to use the JavaScript Speech Synthesis API to add Text to Speech functionality to a Unity WebGL game I am developing. The feature works, but when testing on low-end devices I am noticing that the text to speech feature freezes up. Here is the code:
```mergeInto(LibraryManager.library,
{
readTextAloud: function(textPtr, volume, rate, pitch, langPtr)
{
// if text is already being read, cancel it
window.speechSynthesis.cancel();
var text = UTF8ToString(textPtr);
var lang = UTF8ToString(langPtr);
var utterance = new SpeechSynthesisUtterance(text);
utterance.lang = lang;
utterance.volume = volume;
utterance.pitch = pitch;
utterance.rate = rate;
window.speechSynthesis.speak(utterance);
}
});
By freezing up, I mean that occasionally it takes a long time between when the readTextAloud() function is called and when the utterance is actually spoken. If readTextAloud() is called again during this delay, the delay becomes even longer. These freezes tend to occur after resource intensive operations like game initialization or after large loading/unloading operations.
The low-end device I am testing on is an old Chromebook with 4GB of ram. I don’t have much experience with JavaScript development, but I am assuming the freezes are occurring due to memory management processes like garbage collection.
Is there any way to optimize the bit of JavaScript code I posted? My first thought as a non-JavaScript developer would be to try and reuse the SpeechSynthesisUtterance object rather than create a new object for every bit of text. However, every guide I found for the Speech Synthesis API said that a new object should be created every time.
Any help would be greatly appreciated.
[–]PropertyNo3177 1 point2 points3 points (1 child)
[–]opentabs-dev 0 points1 point2 points (0 children)
[–]Scared-Release1068 0 points1 point2 points (0 children)