问题
I am trying to implement a ScriptProcessorNode with two input and one output channels.
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();
When I run this code in Google Chrome as well as in Mozilla Firefox I get the following error thrown. It tells me my test
node has only one input channel.
Uncaught IndexSizeError: Failed to execute 'connect' on 'AudioNode': input index (1) exceeds number of inputs (1).
When I console print the number of input channels of the ScriptProcessorNode test
I get two input channels.
test.onaudioprocess = function(evt){
console.log("number of input channels: " + evt.inputBuffer.numberOfChannels);
}
Nevertheless connecting two nodes to the input of the test
node does not work the way I do it. I want to program a vocoder inside the ScriptProcessorNode. How can I create a ScriptProcessorNode with two input and one output channels and connect two source nodes as input channel and the context.destination
as output channel?
回答1:
The second parameter of createScriptProcessor
is the number of input channels to the single input of the node, not the number of inputs to the node.
So the way to do this is to use a ChannelMergerNode
with two inputs. Connect your two sources to each of the inputs of the merger node. The output of the merger should be connected to your script processor node. The onaudioprocess
callback will be given an AudioBuffer
that has two channels in it. You can then process these two channels however you want.
来源:https://stackoverflow.com/questions/37508035/how-can-i-connect-two-input-channels-to-the-scriptprocessornode-web-audio-api