JSFiddle - React, Tailwind, and code Playground
by fasoeu
HTML
<body>
<button onclick="play();" class="playSound">LA</button>
</body>
JavaScript
var context = AudioContext ? new AudioContext() : new webkitAudioContext(),//webkit browsers only
oscillator = context.createOscillator();
function play(freq, duration) {
try {
freq = typeof freq!=='undefined' && freq !==null ? Number(freq) : 44000;
duration = typeof duration!=='undefined' && duration !==null ? Number(duration) : 1000;
oscillator.type = 0; // sine wave
oscillator.frequency.value = freq;
oscillator.connect(context.destination);
oscillator.noteOn && oscillator.noteOn(0);
} catch(e) {
console.log(e);
}
}