Created
September 12, 2014 05:58
-
-
Save jussi-kalliokoski/8dd140276d2db2084025 to your computer and use it in GitHub Desktop.
Single type, two nodes, closures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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