Skip to content

Instantly share code, notes, and snippets.

@greggman
Created April 25, 2024 14:51
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 greggman/f3b43223ea7123fe7a47d8029b1f0303 to your computer and use it in GitHub Desktop.
Save greggman/f3b43223ea7123fe7a47d8029b1f0303 to your computer and use it in GitHub Desktop.
AudioWorklet example (mdn)
/*bug-in-github-api-content-can-not-be-empty*/
/*bug-in-github-api-content-can-not-be-empty*/
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});
{"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