Created
April 25, 2024 14:51
-
-
Save greggman/f3b43223ea7123fe7a47d8029b1f0303 to your computer and use it in GitHub Desktop.
AudioWorklet example (mdn)
This file contains 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
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains 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
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains 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
const src = ` | |
// random-noise-processor.js | |
class RandomNoiseProcessor extends AudioWorkletProcessor { | |
process(inputs, outputs, parameters) { | |
const output = outputs[0]; | |
output.forEach((channel) => { | |
for (let i = 0; i < channel.length; i++) { | |
channel[i] = Math.random() * 2 - 1; | |
} | |
}); | |
return true; | |
} | |
} | |
registerProcessor("random-noise-processor", RandomNoiseProcessor); | |
`; | |
const blob = new Blob([src], {type: 'application/javascript'}); | |
const url = URL.createObjectURL(blob); | |
async function start() { | |
const audioContext = new AudioContext(); | |
await audioContext.audioWorklet.addModule(url); | |
const randomNoiseNode = new AudioWorkletNode( | |
audioContext, | |
"random-noise-processor", | |
); | |
randomNoiseNode.connect(audioContext.destination); | |
} | |
window.addEventListener('click', start, {once: true}); |
This file contains 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
{"name":"AudioWorklet example (mdn)","settings":{},"filenames":["index.html","index.css","index.js"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment