I really hope this question stays a programming question and do not end up an Sound Mechanics question... Here goes...
I am doing some experiments in order to figure
Looks like the Web Audio API gives the developer an easy way of stopping a sound source from playing without abruptly stopping the waveform and avoid any noise and sound artifacts.
var audioContext = new(AudioContext || webkitAudioContext)();
var frequencyOffset = 0
// Our sound source is a simple triangle oscillator
var oscillator = audioContext.createOscillator(); // Create sound source
oscillator.type = 'triangle';
// Adding a gain node just to lower the volume a bit and to make the
// sound less ear-piercing. It will also allow us to mute and replay
// our sound on demand
var gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
gainNode.gain.value = 0;
oscillator.frequency.value = 200;
oscillator.start(0);
function boop() {
gainNode.gain.value = 0.1;
// The sound should last for 250ms
setTimeout(function() {
gainNode.gain.value = 0;
}, 250);
oscillator.frequency.value++;
}
setInterval(boop, 500);