Web Audio API: outputChannelCount not working when I inherit from AudioWorkletProcessor

411 Views Asked by At

I have the following example. I try to pass the options parameter via calling super(...). When I do this, I don't get 2 output channels as I specified, instead I only get 1 output channel.

class Processor extends AudioWorkletProcessor {
    constructor() {
        super({outputChannelCount: [2]});
    }

    process(inputs, outputs, parameters) {
        console.log("output channels: ", outputs[0].length);
        return true;
    }
}

registerProcessor('processor', Processor);

When I try the same passing the options to the AudioWorkletNode instead, like in the following example, it works as expected.

    let audioContext = new AudioContext();
    (async () => {
        await audioContext.audioWorklet.addModule('processor.js');
        audioWorkletNode = new AudioWorkletNode(audioContext, 'processor', {outputChannelCount: [2]});
        audioWorkletNode.connect(audioContext.destination);
    })();

This is not working in any browser I tried.

Am I doing something wrong? Is this a bug? Is there something in the Web Audio API specification that I am missing here?

1

There are 1 best solutions below

2
On

I think what you describe is expected. The constructor of an AudioWorkletProcessor doesn't take any arguments as defined by the IDL definition here:

https://webaudio.github.io/web-audio-api/#AudioWorkletProcessor

The options that you pass to the AudioWorkletNode constructor on the main thread get stored as something called the pending processor construction data. They get applied automatically and there is no way to change them from within the processor.

I guess this is to make sure the AudioWorkletNode and its corresponding AudioWorkletProcessor always have the same configuration. If you could change the configuration within the processor the AudioWorkletNode would be out of sync until the information travelled back again.