我希望在Web Audio API中实现音频效果,这需要连续访问两个或更多音频流。
我可以定义一个带有2个输入通道和2个输出通道的脚本处理器:
var mod = context.createScriptProcessor(4096,2,2);
然后我可以将几个正弦波连接到这个处理器:
mySine.connect(mod);
mySine2.connect(mod);
有没有办法将它们连接到音频处理器的特定输入通道?
最后,当我编写onaudioprocess函数并单独收听每个输入通道时,每个输入通道都包含所有连接到处理器的声音。我无法在onaudioprocess函数中单独访问每个正弦波。它是否正确?或者有没有办法将声音连接到脚本处理器的单个输入通道?
答案 0 :(得分:3)
您可以创建两个相同的单通道ScriptProcessors,也可以使用通道合并将两个正弦波分配给脚本处理器的每个通道,如下所示:
var context = new AudioContext();
var sineA = context.createOscillator();
sineA.type = 'sine';
sineA.frequency.value = 300;
var sineB = context.createOscillator();
sineB.type = 'sine';
sineB.frequency.value = 100;
var script = context.createScriptProcessor(4096, 2, 2);
// create 2 channel merger node
var merger = context.createChannelMerger(2);
// connect sineA to channel 0
sineA.connect(merger, 0, 0);
// connect sineA to channel 1
sineB.connect(merger, 0, 1);
// connect the script to the merger
merger.connect(script);
// process the audio data of each channel
script.onaudioprocess = function(event) {
var input = event.inputBuffer;
var output = event.outputBuffer;
var inputA = input.getChannelData(0);
var inputB = input.getChannelData(1);
var outputA = output.getChannelData(0);
var outputB = output.getChannelData(1);
for (var i = 0; i < input.length; i++) {
outputA[i] = inputA[i];
outputB[i] = inputB[i];
}
}
script.connect(context.destination);
sineA.start();
sineB.start();