AudioBufferSourceNode只有在连接后才能开始播放

AudioBufferSourceNode starts playing only after being connected

本文关键字:开始 播放 连接 AudioBufferSourceNode      更新时间:2023-09-26

在我的对象中,我得到了一个AudioBufferSourceNode对象(this.bSrc),连接到一个增益节点(this.audioDestination),该节点播放已经存储在对象(this.audioBuffer)中的(有效)AudioBuffer

如果this.audioDestination连接到音频上下文最终目标this.mainContext.destination并且我在AudioBufferSourceNode对象(this.bSrc)上调用start(0)方法,它将正常播放。

相反,如果this.audioDestination尚未连接到音频上下文最终目标,则缓冲区将不会播放(没关系),但其播放将延迟到建立连接为止(从而使有关 AudioBufferSourceNode 何时完成播放的任何假设无效)。

我目前正在实现一个插件架构,其中每个插件都有其 audioDestination 增益节点,并且不知道(也一定不能)知道它是否间接连接到最终的音频上下文目标。对我来说,合乎逻辑的是,AudioBufferSourceNode应该立即"播放"并完成,即使它没有连接到最终的音频上下文目标。但似乎不是这样工作的。我是对的还是错的?有没有办法改变这种行为?

法典:

/* Create the audio Context. */
this.mainContext = new webkitAudioContext;
/* Create an audio gain node */
this.audioDestination = this.mainContext.createGainNode();
/* [...] An AudioBuffer gets decoded and stored into this.audioBuffer*/
/* Create an AudioBufferSourceNode and try to play it immediately */
this.bSrc = this.audioContext.createBufferSource();
this.bSrc.connect (this.audioDestination);
this.bSrc.buffer = this.audioBuffer;
this.bSrc.start(0);
/* Create a callback for when the AudioBufferSourceNode finishes playing */
if (!this.bSrc.loop) {
       var pbTimer = setTimeout(function() {
            this.playFinishedCallback();
        }.bind(this), this.audioBuffer.duration * 1000 / this.bSrc.playbackRate.value);
    }
 /* Connect this.audioDestination to the final AudioContext destination */
 /* If this statement is executed after the previous ones, playback will start NOW */
 this.audioDestination.connect(this.mainContext.destination);

这是目前在 Chrome 中实现 Web Audio API 的方式,以及其中使用的设计。 有人谈论改变它;事实上,我是你描述的"音频电缆模型"的支持者。 :) 不过,这并不是唯一理智的模型。 在当前实现中没有任何方法可以更改此行为。

但是,有一个简单的解决方法 - 只需将归零增益节点连接到context.destination,并将所有ABSN连接到该节点以及它们(最终)连接到context.destination。

这是有道理的。 将context.destination视为stdout。 如果不连接到context.destination,音频就无处可去。 如果你把最后一行(连接到context.destination)移到你创建增益节点之后,我认为你不会有延迟。