【问题标题】:speech synthesis - How to change gender?语音合成 - 如何改变性别?
【发布时间】:2018-05-15 11:38:51
【问题描述】:

我创建了一个使用 window.speechSynthesis 的简单文本转语音网络应用程序,并且运行良好。我想添加将某些文本设置为女性的功能,以及将其他文本设置为男性语音的功能。

  1. 似乎如果我使用网络语音 API 中的 getVoices(),列表会因浏览器而异。似乎没有“性别”的属性。有些 chrome 名称中有“男性”,有些则没有。有些有女性名字和男性名字。 谷歌浏览器似乎没有列出美国男性演讲者。我错过了什么吗?

  2. 如果我知道所有名称可能是什么,那么我可以存储该列表,然后遍历该列表,直到找到一个有效的名称。 有没有办法知道该设备上是否启用了语音?

  3. 或者,有没有办法下载(使用我的应用程序)适用于所有浏览器的男性和女性声音?

我需要它在所有设备上工作。

【问题讨论】:

  • 不,没有规范定义的方式来了解 SpeechSynthesisVoice 的性别,也没有任何默认语音列表可以在所有浏览器 + 系统上使用。

标签: javascript speech-synthesis


【解决方案1】:

如果我是你,我会创建一个包含所需声音名称(男性和女性)的数组,然后遍历 getVoices 数组以找到匹配项。例如:

window.speechSynthesis.onvoiceschanged = () => {
  const maleVoices = [
    'Google US English Male',
    'Microsoft David Desktop - English (United States)',
  ];
  const foundVoice = speechSynthesis.getVoices()
    .find(({ name }) => maleVoices.includes(name));
  console.log('speaking');
  speechSynthesis.cancel(); // sometimes needed due to Chrome's buggy implementation
  const utter = new SpeechSynthesisUtterance('foo bar');
  if (foundVoice) utter.voice = foundVoice;
  else console.log('no voice found, using default');
  speechSynthesis.speak(utter);
};

可用的声音由浏览器和用户的计算机决定;如果用户没有所需的声音,则无法通过 Javascript 下载其他声音。

我的 Google Chrome 66 确实有 'Google US English Male''Microsoft David Desktop - English (United States)'

【讨论】:

  • 如何获取可能的语音名称列表?有没有网上的清单?
  • By googling 我找到了一堆列表。
  • CertainPerformance 感谢您的帮助!上面的代码在 chrome 中有效,但在我在移动 safari 上测试时无效。我得到的只是默认声音。我是否缺少一些额外的步骤?
  • 尝试在移动版 Safari 中调用 getVoices() 以找出它拥有的声音列表,然后插入所需声音的名称(如果有)。
  • 是的,我这样做了,我得到了一个完整的列表。我加入了 Tom 和 Samantha,但我只得到了手机上设置的默认值,无论是 Tom 还是 Samantha,它都不会切换。我在 iPhone7 版本 11.3.1 上。有什么想法吗?
猜你喜欢
  • 2017-07-30
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多