Zubair
Zubair

Reputation: 61

How can I get female voice by Web Speech API in Google Chrome

In a webpage, I want a female voice to speak my texts. I tried to do this by following code. But still now male voice is talking. How can I arrange a female voice to talk my texts? Can anybody share me a correct code that works in Google Chrome.

var voices = speechSynthesis.getVoices();
var msg = new SpeechSynthesisUtterance("Hello World!");
msg.default=false; 
msg.localservice=true;
msg.lang = "en-GB";
msg.voice = voices[3].name;
speechSynthesis.speak(msg);

Upvotes: 4

Views: 10906

Answers (5)

Zubair
Zubair

Reputation: 61

After a long time of troubleshooting, I could figure out the solution.

4 persons already suggested me some solutions. But these did not work for me. But helped me to figure out the solution. Thanks to all of them.

To solve the problem, I did two things.

  1. I had to load voices during onvoiceschanged event as follows:

    var voices;    
    window.speechSynthesis.onvoiceschanged = function() {
        voices=window.speechSynthesis.getVoices();
    };
    

I found this tip from this link.

  1. 'Google UK English Female' does not work for me. So I used 'Microsoft Zira Desktop - English (United States)' as follows:

    speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];
    

Upvotes: 1

Jamille
Jamille

Reputation: 99

This happens to me that the voice doesn't change at the first time the page is loaded.

I found a solution that works for me.
getVoices() should be triggered when the document is ready.
So I add at the top of my js like this.

   $(document).ready(function() {
        var voices = window.speechSynthesis.getVoices();
    })

Upvotes: 2

Abhineet Modi
Abhineet Modi

Reputation: 11

This code worked for me.

  var speakObj = new SpeechSynthesisUtterance();
  speakObj.text = text;
  speakObj.voice = speechSynthesis.getVoices().filter(function(voice) {
    return voice.name == "Google UK English Female"

  })[0];
  window.speechSynthesis.speak(speakObj);

Upvotes: 1

mab
mab

Reputation: 810

On Chrome, I do something like this:

<html>
<head>
<script>
    function speak(language, country, preferredNames, text) {
        var resolvePromise;
        var promise = new Promise(r => resolvePromise = r);

        var voices = window.speechSynthesis.getVoices();
        if (voices.length == 0) {
            new Promise(r => {
                window.speechSynthesis.onvoiceschanged = () => { 
                        window.speechSynthesis.onvoiceschanged = null;
                        r(); 
                    };
            })
            .then(() => speak(language, country, preferredNames, text))
            .then(resolvePromise);
        } else {
            var msg = new SpeechSynthesisUtterance(text);
            voices.sort((a, b) => {
                if (language != null) {
                    var matchA = a.lang.indexOf(language + "-") == 0;
                    var matchB = b.lang.indexOf(language + "-") == 0;
                    if (! matchA && ! matchB) return 0;
                    if (! matchA && matchB) return 1;
                    if (! matchB && matchA) return -1;
                }
                if (country != null) {
                    var matchA = a.lang.indexOf("-" + country) == a.lang.length - ("-" + country).length;
                    var matchB = b.lang.indexOf("-" + country) == b.lang.length - ("-" + country).length;
                    if (! matchA && ! matchB) return 0;
                    if (! matchA && matchB) return 1;
                    if (! matchB && matchA) return -1;
                }
                if (preferredNames != null) {
                    var indexA = voices.length;
                    var indexB = voices.length;
                    preferredNames.forEach((e, i) => {
                        if (indexA == voices.length && a.name.match(e) != null) indexA = i; 
                        if (indexB == voices.length && b.name.match(e) != null) indexB = i; 
                    });
                    return indexA - indexB;
                }
                return 0;
            });
            if (voices.length > 0) msg.voice = voices[0];
            if (language != null) {
                msg.lang = language;
                if (country != null) msg.lang += "-" + country;
            }
            msg.onend = resolvePromise;
            window.speechSynthesis.speak(msg);

            // msg.onend not triggered without call to console.log(msg)?
            console.log(msg);
        }

        return promise;
    }

    speak("en", null, [ /Google US English/, /Samantha/, /Fiona/, /Victoria/, /female/i ], "Hello, world.")
    .then(() => speak("en", null, [ /female/i ], "Hello, world."))
    .then(() => speak("en", "US", [ /female/i ], "Hello, world."))
    .then(() => speak("en", "US", null, "Hello, world."))
    .then(() => speak("en", "GB", [ /\Wmale/i ], "Hello, world."));
</script>
</head>
<body>
</body>
</html>

Upvotes: 1

Teddy Payne
Teddy Payne

Reputation: 109

The below code worked for me. I hope it works for you.

var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[3];
msg.text = "Hello World";
speechSynthesis.speak(msg);

On the 1st try it might give out male's voice. But on the 2nd attempt (without refreshing) it will give out the female's voice and try deploying it on a dummy server, there it will work like a charm in the first go.

Upvotes: 3

Related Questions