如何在 Chrome 浏览器中创建或将文本转换为音频?

IT技术 javascript audio text-to-speech chromium webspeech-api
2021-02-16 21:50:03

在尝试确定如何在 Chromium 上使用 Web Speech API的解决方案时?发现

var voices = window.speechSynthesis.getVoices();

返回一个空数组作为voices标识符。

不能肯定,如果缺乏在Chromium浏览器的支持有关,这个问题不正常,谷歌:铬语音扩展间谍的担忧后拉升

问题:

1) 是否有任何解决方法可以实现在 Chrome 浏览器中从文本创建或转换音频的要求?

2)我们作为开发者社区,如何创建一个开源的音频文件数据库,包含常用词和不常用词;提供适当的CORS标题?

1个回答

已经发现了几种可能的解决方法,它们提供了从文本创建音频的能力;其中两个需要请求外部资源,另一个使用@masswerk的 meSpeak.js。

使用从 Google 下载单词的音频发音中描述的方法,该方法无法预先确定哪些单词作为文件实际存在于资源中,而无需编写 shell 脚本或执行HEAD检查是否发生网络错误请求。例如,下面使用的资源中没有“do”这个词。

window.addEventListener("load", () => {

  const textarea = document.querySelector("textarea");

  const audio = document.createElement("audio");

  const mimecodec = "audio/webm; codecs=opus";

  audio.controls = "controls";

  document.body.appendChild(audio);

  audio.addEventListener("canplay", e => {
    audio.play();
  });

  let words = textarea.value.trim().match(/\w+/g);

  const url = "https://ssl.gstatic.com/dictionary/static/sounds/de/0/";

  const mediatype = ".mp3";

  Promise.all(
    words.map(word =>
      fetch(`https://query.yahooapis.com/v1/public/yql?q=select * from data.uri where url="${url}${word}${mediatype}"&format=json&callback=`)
      .then(response => response.json())
      .then(({query: {results: {url}}}) =>
        fetch(url).then(response => response.blob())
        .then(blob => blob)
      )
    )
  )
  .then(blobs => {
    // const a = document.createElement("a");
    audio.src = URL.createObjectURL(new Blob(blobs, {
                  type: mimecodec
                }));
    // a.download = words.join("-") + ".webm";
    // a.click()
  })
  .catch(err => console.log(err));
});
<textarea>what it does my ninja?</textarea>

维基共享资源类别中的资源:公共域不需要从同一目录提供,请参阅如何检索维基词典的单词内容?, wikionary API - 词的含义

如果知道资源的精确位置,则可以请求音频,尽管 URL 可能包含单词本身以外的前缀。

fetch("https://upload.wikimedia.org/wikipedia/commons/c/c5/En-uk-hello-1.ogg")
.then(response => response.blob())
.then(blob => new Audio(URL.createObjectURL(blob)).play());

不完全确定如何使用维基百科 API如何使用维基百科的 API 获取维基百科内容?,是否有一个干净的维基百科 API 只用于检索内容摘要?只获取音频文件。JSONreact将需要解析的文本结尾的.ogg,那么第二个请求需要的资源本身作出。

fetch("https://en.wiktionary.org/w/api.php?action=parse&format=json&prop=text&callback=?&page=hello")
.then(response => response.text())
.then(data => {
  new Audio(location.protocol + data.match(/\/\/upload\.wikimedia\.org\/wikipedia\/commons\/[\d-/]+[\w-]+\.ogg/).pop()).play()
})
// "//upload.wikimedia.org/wikipedia/commons/5/52/En-us-hello.ogg\"

哪些日志

Fetch API cannot load https://en.wiktionary.org/w/api.php?action=parse&format=json&prop=text&callback=?&page=hello. No 'Access-Control-Allow-Origin' header is present on the requested resource

当不是来自同一来源的请求时。我们需要YQL再次尝试使用,尽管不确定如何制定查询以避免错误。

第三种方法使用稍微修改过的版本meSpeak.js来生成音频,而无需发出外部请求。修改是为.loadConfig()方法创建一个适当的回调

fetch("https://gist.githubusercontent.com/guest271314/f48ee0658bc9b948766c67126ba9104c/raw/958dd72d317a6087df6b7297d4fee91173e0844d/mespeak.js")
  .then(response => response.text())
  .then(text => {
    const script = document.createElement("script");
    script.textContent = text;
    document.body.appendChild(script);

    return Promise.all([
      new Promise(resolve => {
        meSpeak.loadConfig("https://gist.githubusercontent.com/guest271314/8421b50dfa0e5e7e5012da132567776a/raw/501fece4fd1fbb4e73f3f0dc133b64be86dae068/mespeak_config.json", resolve)
      }),
      new Promise(resolve => {
        meSpeak.loadVoice("https://gist.githubusercontent.com/guest271314/fa0650d0e0159ac96b21beaf60766bcc/raw/82414d646a7a7ef11bb04ddffe4091f78ef121d3/en.json", resolve)
      })
    ])
  })
  .then(() => {
    // takes approximately 14 seconds to get here
    console.log(meSpeak.isConfigLoaded());
    meSpeak.speak("what it do my ninja", {
      amplitude: 100,
      pitch: 5,
      speed: 150,
      wordgap: 1,
      variant: "m7"
    });
})
.catch(err => console.log(err));

上述方法的一个警告是,在播放音频之前加载三个文件需要大约 14 秒半的时间。但是,避免了外部请求。

这将是一个正的之一或两者1)创建FOSS两种常见的和不常见的话声音,显影剂保持的数据库或目录; 2) 进一步开发meSpeak.js以减少三个必要文件的加载时间;并使用Promise基于方法的方法来提供文件加载进度和应用程序准备情况的通知。

根据该用户的估计,如果开发人员自己创建并贡献给一个在线文件数据库,该数据库以特定单词的音频文件作为响应,这将是一个有用的资源。不完全确定github是否是托管音频文件的合适场所?如果显示出对此类项目的兴趣,则必须考虑可能的选项。