如何将两个输入通道连接到 ScriptProcessorNode? (网络音频 Api、JavaScript)

How can I connect two input channels to the ScriptProcessorNode? (Web Audio Api, JavaScript)

我正在尝试实现一个具有两个输入通道和一个输出通道的 ScriptProcessorNode。

var source = new Array(2);

source[0] = context.createBufferSource();
source[0].buffer = buffer[0];

source[1] = context.createBufferSource();
source[1].buffer = buffer[1];

var test = context.createScriptProcessor(4096, 2, 1);

source[0].connect(test, 0, 0);
source[1].connect(test, 0, 1);

test.connect(context.destination);

source[0].start();
source[1].start();

当我在 Google Chrome 以及 Mozilla Firefox 中 运行 这段代码时,出现以下错误。它告诉我我的test节点只有一个输入通道。

Uncaught IndexSizeError: Failed to execute 'connect' on 'AudioNode': input index (1) exceeds number of inputs (1).

当我在控制台打印 ScriptProcessorNode 的输入通道数时 test 我得到两个输入通道。

test.onaudioprocess = function(evt){
    console.log("number of input channels: " + evt.inputBuffer.numberOfChannels);
}

然而,将两个节点连接到 test 节点的输入并不像我那样工作。我想在 ScriptProcessorNode 中编写声码器。如何创建一个具有两个输入通道和一个输出通道的 ScriptProcessorNode 并将两个源节点连接为输入通道和 context.destination 作为输出通道?

createScriptProcessor的第二个参数是节点单个输入的输入通道数,不是节点的输入数

因此,方法是使用具有两个输入的 ChannelMergerNode。将您的两个源连接到合并节点的每个输入。合并的输出应该连接到您的脚本处理器节点。 onaudioprocess 回调将被赋予一个包含两个通道的 AudioBuffer。然后您可以根据需要处理这两个通道。