使用W3C Web Speech API生成音频文件

generate audio file with W3C Web Speech API

本文关键字:音频 文件 API W3C Web Speech 使用      更新时间:2023-09-26

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

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

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

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

尽管使用库(例如espeakmeSpeak)可以满足要求,但请参阅如何在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()调用捕获生成的音频?。