GoogleChromeLabs/audioworklet-polyfill

Doesn't work as an argument to 'AudioNode.connect'

JohnWeisz opened this issue · 1 comments

Every single browser (Firefox, Edge, Safari tested) throws a TypeError when attempting to connect to a polyfilled AudioWorkletNode, and I believe it's caused by these lines:

  window.AudioWorkletNode = function AudioWorkletNode (context, name) {
    const processor = getProcessorsForContext(context)[name];

    this.parameters = new Map();
    if (processor.properties) {
      for (let i = 0; i < processor.properties.length; i++) {
        const prop = processor.properties[i];
        const node = context.createGain().gain;
        node.value = prop.defaultValue;
        // @TODO there's no good way to construct the proxy AudioParam here
        this.parameters.set(prop.name, node);
      }
    }

    const inst = new processor.Processor({});

    this.port = processor.port;
    const scriptProcessor = context.createScriptProcessor();
    scriptProcessor.node = this;
    scriptProcessor.processor = processor;
    scriptProcessor.instance = inst;
    scriptProcessor.onaudioprocess = onAudioProcess;
    Object.defineProperty(this, '$$node', { value: scriptProcessor });
  };

Here, a custom class instance is constructed, but that's not accepted by any browser as the first argument to AudioNode.connect. A true, real, native AudioNode object is required for that to work, and this cannot be faked with setting the prototype manually.

The correct course of action would be returning an augmented ScriptProcessorNode.

Agreed, I'd like to switch to returning a ScriptProcessorNode.