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

点点圈 提交于 2019-12-23 10:52:54

问题


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 testnode 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 testnode 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.destinationas 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!