Audio Source Abstraction Trying to make it modular
by Ehsan Ziya
HTML
<input type="button" id="play" value = "PLAY">
<input type="button" id="stop" value = "STOP">
JavaScript
context = new webkitAudioContext();
mastergain = context.createGainNode();
mastergain.gain.value = 0.9;
mastergain.connect(context.destination);
//setting up a loader object
//looks like this var test = new Sound(url);
//test.play(destination , loop true or false, playback rate)
function Sound(source){
//for later use and avoiding the scope issues
var that = this;
that.source = source;
that.buffer = null;
//creating a isLoaded property so later we can check
//if it has been loaded or not
that.isLoaded = false;
//loading files to the buffer
var request = new XMLHttpRequest();
request.open('GET', source , true);
request.responseType = "arraybuffer";
request.onload = function(){
context.decodeAudioData(request.response, function(buffer){
that.buffer = buffer;
that.isLoaded = true;
});
};
request.send();
}
//adding a play method for the sound object
Sound.prototype.play = function(destination, loop, speed, gain){
_sound = context.createBufferSource();
_soundGain = context.createGainNode();
_sound.buffer = this.buffer;
this.destination = destination;
this.speed = speed;
this.gain = gain;
_sound.loop = loop;
_sound.playbackRate.value = speed;
_soundGain.gain.value = gain;
_sound.connect(_soundGain);
_soundGain.connect(destination);
_sound.start(0);
};
//adding a stop method
Sound.prototype.stop = function(){
_sound.stop(0);
};
//attemtping to create osc object literal
//looks like this testOsc = new osc(type, frequency, detune);
//testOsc.play(destination, gain value);
function osc(type, frequency, detune){
_oscillator = context.createOscillator();
this.type = type;
this.frequency = frequency;
this.detune = detune;
_oscillator.frequency.value = frequency;
_oscillator.type = type;
_oscillator.detune.value = detune;
_oscGain =...