我正在尝试实现一个具有两个输入 channel 和一个输出 channel 的 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 中运行此代码时,出现以下错误。它告诉我我的测试
节点只有一个输入 channel 。
Uncaught IndexSizeError: Failed to execute 'connect' on 'AudioNode': input index (1) exceeds number of inputs (1).
当我在控制台打印 ScriptProcessorNode test
的输入 channel 数时,我得到了两个输入 channel 。
test.onaudioprocess = function(evt){
console.log("number of input channels: " + evt.inputBuffer.numberOfChannels);
}
尽管如此,将两个节点连接到 test
节点的输入并不像我那样工作。我想在 ScriptProcessorNode 中编写声码器。如何创建一个具有两个输入 channel 和一个输出 channel 的 ScriptProcessorNode,并将两个源节点连接为输入 channel ,将 context.destination
连接为输出 channel ?
最佳答案
createScriptProcessor
的第二个参数是节点单个输入的输入 channel 数,而不是节点的输入数。
因此,执行此操作的方法是使用具有两个输入的 ChannelMergerNode
。将您的两个源连接到合并节点的每个输入。合并的输出应该连接到您的脚本处理器节点。 onaudioprocess
回调将被赋予一个包含两个 channel 的 AudioBuffer
。然后,您可以根据需要处理这两个 channel 。
关于javascript - 如何将两个输入 channel 连接到 ScriptProcessorNode? (网络音频 API,JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508035/