Preloading Web Speech API before calling speak
Asked Answered
M

1

6

So I've noticed that after you do the first speak using speechSynthesis.speak, it speeds up dramatically in providing results. So my aim below was to speed it up by pre-initializing the synthesis so when we call speakIt() we don't have to wait for it. It hasn't sped up at all; any suggestions on why it's not speeding up and how I fix it?

Full Script:

var speech = new SpeechSynthesisUtterance("test");
var voices = window.speechSynthesis.getVoices();
speech.default = false;
speech.voice = voices.filter(function(voice) { return voice.name == 'Google  UK English Male'; })[0];
speech.lang = 'en-GB';

function speakIt(word){
        speech.text = word;
        window.speechSynthesis.speak(speech);
}

chrome.tts.speak seems to be a bit quicker but certainly isn't there, but that's not the point — this should still work. Until someone finds the answer I will migrate to Chrome's usage.

Mccabe answered 8/4, 2015 at 18:51 Comment(2)
This might be very unsavory, but have you tried calling speakIt(' .') to prime the pump a little more?Manion
I also tried this method! It doesn't seem to fix it, it seems like the speech utterance kind of expires after a while until you speak again.. The other problem is with speechsynthesisutterance ("") makes a sound like the guy on the other end has suddenly come down with something! I tried doing that and then cancel as well, still no luckMccabe
C
0

You need to do some setup before performing speech. I wish more examples included this. Specifically you should call getVoices() and add an event handler to populate voices ahead of time when the page loads. I've written a class that does this for you. See the Codepen example.

var speech = new Speech();

if (speech.supported()) {
  speech.speak('hello, speech is working fine');
}

Codepen Example: http://codepen.io/anon/pen/qNwOAO

Chantey answered 6/1, 2016 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.