JSFiddle - React, Tailwind, and code Playground
by oysteinmoseng
HTML
<button id="start">
Start
</button>
JavaScript
console.clear();
document.getElementById('start').onclick = () => {
const ctx = new AudioContext();
const gain = new GainNode(ctx, { gain: 0.6 });
const osc = new OscillatorNode(ctx, { frequency: 320 });
osc.connect(gain);
gain.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + 4);
gain.gain.setValueAtTime(0.1, ctx.currentTime + 1);
gain.gain.setValueAtTime(0.3, ctx.currentTime + 2);
gain.gain.setValueAtTime(0.6, ctx.currentTime + 3);
gain.gain.cancelScheduledValues(ctx.currentTime + 2.1);
gain.gain.setValueAtTime(0.6, ctx.currentTime + 3);
};