JSFiddle - React, Tailwind, and code Playground
Babel + JSX
let audioCtx = new AudioContext();
// Create nodes
let osc = audioCtx.createOscillator();
let gain = audioCtx.createGain();
let limiter = audioCtx.createScriptProcessor(4096, 1, 1);
// Set parameters
osc.frequency.value = 440;
gain.gain.value = 3;
limiter.onaudioprocess = (evt) => {
for (let ch = 0 ; ch < evt.outputBuffer.numberOfChannels ; ch++) {
let inputData = evt.inputBuffer.getChannelData(ch);
let outputData = evt.outputBuffer.getChannelData(ch);
for (let sample = 0 ; sample < inputData.length ; sample++) {
// Replace anything outside -1..1 range with the limit value
outputData[sample] = Math.min(1, Math.max(-1, inputData[sample]));
}
}
}
// Connect graph
osc.connect(gain);
gain.connect(limiter);
limiter.connect(audioCtx.destination);
// Schedule start and stop
osc.start();
osc.stop(audioCtx.currentTime + 3);