5

是否可以使用 W3C Web Speech API 编写 Javascript 代码来生成带有语音给定文本的音频文件(wav、ogg 或 mp3)?我的意思是,我想做类似的事情:

window.speechSynthesis.speak(new SpeechSynthesisUtterance("0 1 2 3"))

但我希望用它生成的声音不是输出到扬声器而是输出到文件。

4

1 回答 1

5

仅使用 Web Speech API 无法满足要求,请参阅Re: MediaStream, ArrayBuffer, Blob audio result from speak() for recording?,如何实现从 window.speechSynthesis.speak() 调用返回 Blob、ArrayBuffer 或 AudioBuffer 的选项

尽管可以使用库来满足要求,例如,espeak或者meSpeak,请参阅如何在 chromium 浏览器中创建文本或将文本转换为音频?.

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());
    console.log(meSpeak.speak("what it do my ninja", {
      amplitude: 100,
      pitch: 5,
      speed: 150,
      wordgap: 1,
      variant: "m7",
      rawdata: "mime"
    }));
})
.catch(err => console.log(err));

也有使用的解决方法MediaRecorder,具体取决于系统硬件如何从 window.speechSynthesis.speak() 调用捕获生成的音频?.

于 2017-09-27T14:44:01.603 回答