使用W3C Web Speech API生成音频文件 [英] generate audio file with W3C Web Speech API

查看:216
本文介绍了使用W3C Web Speech API生成音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用W3C Web Speech API编写生成音频文件(wav,ogg或mp3)的Javascript代码,并发出语音说明文字?我的意思是,我想做类似的事情:

Is it possible to use W3C Web Speech API to write Javascript code which generates audio file (wav, ogg or mp3) with voice speaking given text? I mean, I want to do something like:

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

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

but I want sound generated with it not to be output to speakers but to file.

推荐答案

仅使用Web Speech API无法满足要求,请参阅 Re:MediaStream,ArrayBuffer,Blob音频结果来自speak()用于录制?如何实现从中返回Blob,ArrayBuffer或AudioBuffer的选项window.speechSynthesis.speak()调用

The requirement is not possible using Web Speech API alone, see Re: MediaStream, ArrayBuffer, Blob audio result from speak() for recording?, How to implement option to return Blob, ArrayBuffer, or AudioBuffer from window.speechSynthesis.speak() call

虽然可以使用库,但 espeak meSpeak ,请参阅如何在Chrome浏览器中创建文本或将文本转换为音频?

Though requirement is possible using a library, for example, espeak or meSpeak, see How to create or convert text to audio at chromium browser?.

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()调用中捕获生成的音频?

There is also workaround using MediaRecorder, depending on system hardware How to capture generated audio from window.speechSynthesis.speak() call?.

这篇关于使用W3C Web Speech API生成音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆