Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Speech Synthesis API voice changing when function run more than 1 time?

I have been using the new Speech Synthesis API in Chrome (33 and above) to make a web based communication aid. I would like the user to be able to change the voice between male and female, which the API allows me to do. However, when the page is first loaded and the first time the function is run (from an onclick event) it uses the default female voice. Then any time it is run after that, it uses the male voice that I am trying to use to. How can I make the male voice run the first time as well?

Here is the button which calls the javascript:

<button type="button" name="speakMe"id="speakMe" onclick="speakPhrase($('phraseBar').getValue());" ><img src="images/speakMe.png" /></button>

And here is the speakPhrase function which is being called:

function speakPhrase(phrase) {
    if(phrase =="")
    {
        alert("Please enter a phrase before asking me to speak for you. Thank you!");
    }
    else
    {
        var speech = new SpeechSynthesisUtterance(phrase);
        var voices = window.speechSynthesis.getVoices();
        speech.voice = voices.filter(function(voice) { return voice.name == 'Google UK English Male'; })[0];
        window.speechSynthesis.speak(speech);

    }
}

Can anyone help?

like image 524
Phil Young Avatar asked Apr 02 '14 12:04

Phil Young


People also ask

How do you change the voice synthesis in speech?

Android uses Device Default Voice SpeechSynthesis. getVoices() will return several options for English (United States, Australia, Nigeria, India, and United Kingdom) but only one is available at a time. You can pick which one by going to the Settings app, then Controls->Language and input->Text-to-speech options.

What does speak() do?

The speak() method of the SpeechSynthesis interface adds an utterance to the utterance queue; it will be spoken when any other utterances queued before it have been spoken.

How do I enable speech synthesis in Chrome?

Click on the icon on the left of the URL bar, and open Site settings. Change the Sound setting from "Automatic (default)" to "Allow"

What is window SpeechSynthesis?

speechSynthesis. The speechSynthesis read-only property of the Window object returns a SpeechSynthesis object, which is the entry point into using Web Speech API speech synthesis functionality.


2 Answers

It seems that the voices array is empty on the first call. From what I read it has something to do with an asynchronous call to load the voices. So, it's always empty the first time it's called. For me this did the magic:

var speech_voices;
if ('speechSynthesis' in window) {
  speech_voices = window.speechSynthesis.getVoices();
  window.speechSynthesis.onvoiceschanged = function() {
    speech_voices = window.speechSynthesis.getVoices();
  };
}

Call from somewhere outside your speech function.

like image 56
agoldev Avatar answered Nov 08 '22 22:11

agoldev


I solved issue.

Root Cause: When you call API for the first time voices don't load for some reason. And default voice loads for the first time.

SO I added below line of code at page load or start or ready state which is even before my code gets call:

voices = window.speechSynthesis.getVoices();

That solved my issue, hope that helps others.

like image 37
user5958722 Avatar answered Nov 08 '22 21:11

user5958722