使用Web Audio API AudioContext确定语音是否暂停

时间:2019-02-13 14:58:16

标签: javascript web-audio-api audiocontext

试图更好地理解Web Audio API。我们正在使用它来创建AudioContext,然后发送要转录的音频。我希望能够确定何时有自然的语音暂停或用户何时停止讲话。

onaudioprocess回调中是否有一些数据可用于确定语音的暂停/中断?

let context = new AudioContext();
context.onstatechange = () => {};

this.setState({ context: context });

let source = context.createMediaStreamSource(stream);
let processor = context.createScriptProcessor(4096, 1, 1);
source.connect(processor);
processor.connect(context.destination);
processor.onaudioprocess = (event) => {
   // Do some magic here
}

我尝试了本文中建议的解决方案,但未达到我需要的结果。帖子:HTML Audio recording until silence?

当我按照帖子的建议进行沉默分析时,我得到的结果相同-0128

let context = new AudioContext();

let source = context.createMediaStreamSource(stream);
let processor = context.createScriptProcessor(4096, 1, 1);
source.connect(processor);
processor.connect(context.destination);

/***
   * Crete analyser
   *
   **/
let analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0;
analyser.fftSize = 2048;
let buffLength = analyser.frequencyBinCount;
let arrayFreqDomain = new Uint8Array(buffLength);
let arrayTimeDomain = new Uint8Array(buffLength);
processor.connect(analyser);

processor.onaudioprocess = (event) => {
   /**
   *
   * Parse live real-time buffer looking for silence
   *
   **/
   let f, t;
   analyser.getByteFrequencyData(arrayFreqDomain);
   analyser.getByteTimeDomainData(arrayTimeDomain);
   for (var i = 0; i < buffLength; i++) {
       arrayFreqDomain[i]; <---- gives 0 value always
       arrayTimeDomain[i]; <---- gives 128 value always
   }
}

看一下getByteFrequencyData方法的文档,我可以看到它应该如何给出不同的值(在文档示例中,它会给出不同的barHeight),但是它对我不起作用。 https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData#Example

0 个答案:

没有答案