JSFiddle - React, Tailwind, and code Playground
HTML
<button onclick="main()">Click to play</button>
JavaScript
let audioCtx = null;
const main = () => {
// avoid multiple clicking
if (audioCtx) return;
// construct Audio Context
audioCtx = new AudioContext();
// osc1 represents 440Hz sine wave
const osc1 = new OscillatorNode(audioCtx, { frequency: 440 });
// gain represents amplification level of input sound before it goes to output
const gain1 = new GainNode(audioCtx, { gain: 0.5 });
// connect osc1 -> gain -> your audio output device (speakers, headphones, etc...)
osc1.connect(gain1).connect(audioCtx.destination);
// start to play osc1
osc1.start();
}