Generating sound on the fly with javascript/html5 Generating sound on the fly with javascript/html5 javascript javascript

Generating sound on the fly with javascript/html5


You can use the Web Audio API in most browsers now (excepting IE and Opera Mini).

Try out this code:

// one context per documentvar context = new (window.AudioContext || window.webkitAudioContext)();var osc = context.createOscillator(); // instantiate an oscillatorosc.type = 'sine'; // this is the default - also square, sawtooth, triangleosc.frequency.value = 440; // Hzosc.connect(context.destination); // connect it to the destinationosc.start(); // start the oscillatorosc.stop(context.currentTime + 2); // stop 2 seconds after the current time