GoogleChromeLabs/audioworklet-polyfill

Crash after creating AudioWorkletNode

warpdesign opened this issue · 1 comments

The following code crashes when ran in Firefox with audioworklet-polyfill:

this.context.audioWorklet.addModule('js/mod-processor.js').then(() => {
            this.workletNode = new AudioWorkletNode(this.context, 'mod-processor');
            this.workletNode.port.onmessage = this.handleMessage.bind(this);
            // ...
}

Because this.workletNode.port isn't defined.

Looking at the source code of the polyfill, it seems new AudioWorkletNode returns the ScriptProcessorNode instead of the AudioWorkletNode (which has the port correctly set).

if (typeof AudioWorkletNode !== 'function') {
window.AudioWorkletNode = function AudioWorkletNode (context, name, options) {
const processor = getProcessorsForContext(context)[name];
const scriptProcessor = context.createScriptProcessor();
scriptProcessor.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
scriptProcessor.parameters.set(prop.name, node);
}
}
const mc = new MessageChannel();
this.port = mc.port1;
nextPort = mc.port2;
const inst = new processor.Processor(options || {});
nextPort = null;
scriptProcessor.processor = processor;
scriptProcessor.instance = inst;
scriptProcessor.onaudioprocess = onAudioProcess;
return scriptProcessor;
};

Native AudioWorkletNode correctly returns the AudioWorkletNode object which has the port property set so the code works as expected here.

Ah - whoops, this is an issue with the release that went out 2 days ago. Good catch!