Skip to content

Instantly share code, notes, and snippets.

@jussi-kalliokoski
Created September 12, 2014 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jussi-kalliokoski/8dd140276d2db2084025 to your computer and use it in GitHub Desktop.
Save jussi-kalliokoski/8dd140276d2db2084025 to your computer and use it in GitHub Desktop.
Single type, two nodes, closures
var context = new AudioContext();
var audioWorker = context.createAudioWorker("worker.js");
var sine1 = context.createAudioWorkerNode(audioWorker, {
numberOfInputChannels: 0,
numberOfOutputChannels: 1,
parameters: {
frequency: 440,
},
});
var sine2 = context.createAudioWorkerNode(audioWorker, {
numberOfInputChannels: 0,
numberOfOutputChannels: 1,
parameters: {
frequency: 440,
},
});
sine1.connect(context.destination);
sine2.connect(context.destination);
self.addEventListener("onaudionodecreated", function createSine (event) {
var clock = 0;
event.node.onaudioprocess = function processBlock (event) {
let blockSize = event.parameters.frequency.length;
for ( let output of event.outputBuffers ) {
for ( let i = 0; i < blockSize; i++ ) {
let phase = (clock + i) * event.parameters.frequency[i] / event.sampleRate;
output[i] = Math.sin(Math.PI * phase);
}
}
clock += blockSize;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment