Skip to content

Commit

Permalink
Return a decorated ScriptProcessorNode instead of an AudioNode subcla…
Browse files Browse the repository at this point in the history
…ss. Fixes #6.
  • Loading branch information
developit committed Aug 28, 2018
1 parent 36d7731 commit 35f9953
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,33 @@ const PARAMS = [];
let nextPort;

if (typeof AudioWorkletNode !== 'function') {
window.AudioWorkletNode = function AudioWorkletNode (context, name) {
window.AudioWorkletNode = function AudioWorkletNode (context, name, options) {
const processor = getProcessorsForContext(context)[name];
const scriptProcessor = context.createScriptProcessor();

this.parameters = new Map();
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
this.parameters.set(prop.name, node);
scriptProcessor.parameters.set(prop.name, node);
}
}

const inst = new processor.Processor({});
const mc = new MessageChannel();
this.port = mc.port1;
nextPort = mc.port2;
const inst = new processor.Processor(options || {});
nextPort = null;

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 });
return scriptProcessor;
};

Object.defineProperties(window.AudioWorkletNode.prototype = Object.create(AudioNode.prototype), {
bufferSize: {
get () {
return this.$$node.bufferSize;
}
},
connect: { value (to) {
return this.$$node.connect(to);
} },
disconnect: { value () {
return this.$$node.disconnect();
} }
});

Object.defineProperty(AudioContext.prototype, 'audioWorklet', {
get () {
return this.$$audioWorklet || (this.$$audioWorklet = new window.AudioWorklet(this));
Expand Down Expand Up @@ -103,7 +91,7 @@ if (typeof AudioWorkletNode !== 'function') {
function onAudioProcess (e) {
const parameters = {};
let index = -1;
this.node.parameters.forEach((value, key) => {
this.parameters.forEach((value, key) => {
const arr = PARAMS[++index] || (PARAMS[index] = new Float32Array(this.bufferSize));
// @TODO proper values here if possible
arr.fill(value.value);
Expand Down

0 comments on commit 35f9953

Please sign in to comment.