ARTICLE AD BOX
I can play simple tone using code below.
But how can I generate sound like a musical instrument (piano, flute etc...) ?
function playTone(freq, duration, delay= 0) { const osc = ctx.createOscillator(); osc.type = "sine"; osc.frequency.value = freq; const gain = ctx.createGain(); gain.gain.setValueAtTime(0.2, ctx.currentTime + delay); osc.connect(gain).connect(ctx.destination); osc.start(ctx.currentTime + delay); osc.stop(ctx.currentTime + delay + duration); }