4

I have a Javascript app that uses SpeechSynthesisUtterance for text to speech. On some browsers it uses the wrong voice (language). For example, on one browser whose language is set to English, the TTS ends up using a German voice.

Is there a configuration option to set the voice used?

joews
  • 29,767
  • 10
  • 79
  • 91
Ziffusion
  • 8,779
  • 4
  • 29
  • 57

2 Answers2

4

The speech API spec says that browsers can decide which voice to use by default, and that each utterance language may have a different default voice.

voice default attribute:

This attribute is true for at most one voice per language. There may be a different default for each language. It is user agent dependent how default voices are determined.

The default utterence language is decided by the HTML lang attribute:

utterance lang attribute:

This attribute specifies the language of the speech synthesis for the utterance, using a valid BCP 47 language tag. [BCP47] If unset it remains unset for getting in script, but will default to use the lang of the html document root element and associated hierachy. This default value is computed and used when the input request opens a connection to the recognition service.

This implies that, to use a British voice by default:

<html lang="en-GB">

<body>
  <script>
  var utterance = new SpeechSynthesisUtterance('Toodle pip');
  window.speechSynthesis.speak(utterance);
  </script>
</body>
</html>

However, this did not change the default voice for me in Chrome 46 (my default language is en-GB; I also get a German voice by default).

You could use navigator.language as the default utterance language, but according to this answer it is not a reliable indicator of browser settings (for me it evaluates to en-US, which is in my list of languages but is not my default).

Community
  • 1
  • 1
joews
  • 29,767
  • 10
  • 79
  • 91
  • Yes - I saw how to do this programmatically. But I don't want to hardcode the language to 'en-US'. Is there a way to set it to the default language of the browser? Is there no config setting for this? – Ziffusion Nov 11 '15 at 22:49
  • I just re-read the spec. It seems that the default utterance lang is picked up from the HTML `lang` attribute. Can you try setting `lang` on your root ``? – joews Nov 11 '15 at 22:52
  • I just tried it (and updated the answer) - no luck. I still get German by default. I could be mis-reading the spec, or perhaps Chrome's default language detection ignores it. – joews Nov 11 '15 at 23:12
  • I haven't got an answer, but this is useful information. So, I am accepting it. – Ziffusion Nov 18 '15 at 19:59
1

Chrome seems to ignore the HTML lang property. The solution is to set the property of the utterance:

<script>
    var utterance = new SpeechSynthesisUtterance('Toodle pip');
    utterance.lang='en-US'; // for US english, en-GR for british
    window.speechSynthesis.speak(utterance);
</script>
approxiblue
  • 6,982
  • 16
  • 51
  • 59
ClanK
  • 31
  • 1