MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

TextToSpeech speed improvement

Open MatrixInception opened this issue 9 years ago • 9 comments

Hello HoloToolkit team,

I have tried out the TextToSpeechManager. Thank you for this great tool. It simplified a lot of work. However, I noticed that the Async calls were too slow for interactions... for example, I would gaze or tap at something and only 5 seconds later (sometimes longer), I hear the speech.

Could this be improved by separating the playing of the audio clip from the synthesizing of the speech from text? In other words, generate speech from Start() or upon each change to the text, and then play the speech only when activated and when AudioSource is not playing.

Cheers,

MI

MatrixInception avatar Dec 31 '16 15:12 MatrixInception

Hi @markingram7, it seems like you've got a good handle on the problem and how to fix it. Would you be able to submit a PR that addresses this?

StephenHodgson avatar Jan 03 '17 14:01 StephenHodgson

@jbienzms any thoughts or help you might be able to provide?

NeerajW avatar Jan 05 '17 18:01 NeerajW

The main problem here is that Unity and UWP use different audio formats. UWP uses a standard WAV format and Unity has it's own proprietary format based on an array of floating point values. Because of this, the speech must be fully generated as WAV data, then we must convert that WAV data to Unity float values before they can be assigned to the AudioSource. Then the AudioSource buffers and begins playback.

This whole process only seems to take about 1 second for me, but that also depends on the length of the text to be spoken. Shortening this would require breaking the text up until multiple chunks so that the playback could begin sooner. Then you would have to monitor the AudioSource for when playback has completed and load the next chunk.

jbienzms avatar Jan 05 '17 18:01 jbienzms

Yes, some of the test texts in my project were longer sentences, and sometimes I activated multiple of them in short sequence, so the delay was longer. I solved the problem for my own project by doing the following:

  1. In the TextToSpeechManager (which is added as a component to each gameobject containing a text), I added a public "GenerateSpeech" method very similar to the "PlaySpeech", which does everything and sets the clip of the AudioSource, but doesn't play it.
  2. In the TextToSpeechManagerTest, I call the GenerateSpeech method upon Start and whenever there's a change in the text.
  3. At another event trigger (such as proximity or tap gesture), I check if the clip is null. If not, the clip is played. This workflow allowed the multiple clips to be played in tight sequences. However, I imagine this would hog up more memory, as all the speech clips would be generated in advance of any user interaction.

MatrixInception avatar Jan 06 '17 01:01 MatrixInception

That's a good approach. I'm guessing your sentences don't change much then? (e.g. it appears you're playing buffers that were previously generated). You're right that it's taking up memory, and depending on the length of the audio that could be notable. But probably not any more than having a lot of textures loaded. You can keep an eye on processor memory and GC pressure if you're interested. You can do this with the VS performance monitoring tools. But in the end, if it works for you then great! And thanks for sharing your workaround. This may be helpful for others too.

jbienzms avatar Jan 06 '17 16:01 jbienzms

P.S. after reading your workaround I did think about one possible optimization for the TextToSpeechManager. I could hold onto the last text spoken and the last audio clip. And if the next text spoken is the same as the last one, I could just play the clip. That would work similar to your approach, especially if multiple managers were used (one for each phrase). However this optimization wouldn't help for cases where there is only one manager and each time a new phrase is spoken. I'm not sure how common it is for the same phrase to be spoken multiple times in a row and if that would warrant keeping the string and clip in memory. Then again, I think I'm currently keeping the last clip in memory anyway.

jbienzms avatar Jan 06 '17 16:01 jbienzms

A good suggestion by Utekai over on the HoloLens Forum

If it's possible for you, I'd recommend to use the Windows APIs directly rather than trying to use the Unity implementation. You might not have this route available due to other considerations. However, the Windows APIs are nicely done and rather easy to use once you get a good code pattern in place for what you need to accomplish.

I use a dictation recognizer in a hololens app and it works fine and continues to work nicely with hololens software updates. See Windows API Speech Recognition Docs.

I bypassed entirely the Unity implementation of dictation recognizer, as it wasn't well presented to the developer via the API and wasn't performant enough for my needs as it seems to do intermediate buffering and inserts delays that add up.

Also, my app does need the Internet Client capability, and removing that capability causes failure in recognition. So it it required and fails with an error of 'sorry, but we can't access the internet right now' when that capability is not there. However, if network access isn't available, but you have the capability, it will still work. I couldn't find this in the documentation, it was a trial/error exercise in determining the need.

Hopefully some of the work I'm doing on https://github.com/Microsoft/HoloToolkit-Unity/pull/533 will see improvements.

StephenHodgson avatar Feb 24 '17 18:02 StephenHodgson

We should continue to investigate this. Consider adding a smallish dictionary or other optional method of caching the generated audio.

david-c-kline avatar Mar 23 '18 20:03 david-c-kline

Closing issues older than 180 days. If this is still an issue, please reactivate with recent information.

wiwei avatar Apr 11 '19 20:04 wiwei