Search code examples
javascriptweb-audio-api

set number of output channels in AudioWorkletProcessor


If I create an audio context, the destination node has 2 channels (for stereo output) and the AudioWorkletNode appears to have 2 channels:

var audioContext = new AudioContext()
console.log(audioContext.destination.channelCount); // 2 channels

audioContext.audioWorklet.addModule('testworker.js').then(()=>{
   var node = new AudioWorkletNode(audioContext, 'test');
   console.log("channel count", node.channelCount); // also 2 channels?
   node.connect(audioContext.destination);
});

However, within the process function, the output only has 1:

testworker.js:

class Test extends AudioWorkletProcessor {
   process(inputs, outputs, parameters) {
      console.log("output channels: ", outputs[0]); // Array [ Float32Array(128) ] (1 channel)
      return false;
   }
}

registerProcessor("test", Test);

Is there a way to specify the number of output channels for an audio worklet processor?

with the now-deprecated script processor system, you specify the number of inputs/outputs in the constructor: audioContext.createScriptProcessor(bufferSize, inputCount, outputCount) but I don't see how to do that with audio worklets


Solution

  • Since you don't give an example of what you're trying to do it's hard to give good advice. But you should begin with looking at AudioWorkletNodeOptions, and the description on configuring channels is a good place to start.