1 /**
  2  * Tutorials:
  3  * http://www.html5rocks.com/en/tutorials/webaudio/games/
  4  * http://www.html5rocks.com/en/tutorials/webaudio/positional_audio/ <- +1 as it is three.js
  5  * http://www.html5rocks.com/en/tutorials/webaudio/intro/
  6  *
  7  * Spec:
  8  * https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html
  9  *
 10  * Chromium Demo:
 11  * http://chromium.googlecode.com/svn/trunk/samples/audio/index.html  <- running page
 12  * http://code.google.com/p/chromium/source/browse/trunk/samples/audio/ <- source
 13 */
 14 
 15 
 16 /**
 17  * Notes on removing tQuery dependancy
 18  * * some stuff depends on tQuery
 19  * * find which one
 20  * * tQuery.Webaudio got a world link for the listener
 21  *   * do a plugin with followListener(world), unfollowListener(world)
 22  * * namespace become WebAudio.* instead of WebAudio.*
 23 */
 24 
 25 //////////////////////////////////////////////////////////////////////////////////
 26 //////////////////////////////////////////////////////////////////////////////////
 27 //////////////////////////////////////////////////////////////////////////////////
 28 //		WebAudio							//
 29 //////////////////////////////////////////////////////////////////////////////////
 30 //////////////////////////////////////////////////////////////////////////////////
 31 //////////////////////////////////////////////////////////////////////////////////
 32 
 33 
 34 /**
 35  * Main class to handle webkit audio
 36  * 
 37  * TODO make the clip detector from http://www.html5rocks.com/en/tutorials/webaudio/games/
 38  *
 39  * @class Handle webkit audio API
 40  *
 41  * @param {tQuery.World} [world] the world on which to run 
 42 */
 43 WebAudio	= function(){
 44 	// sanity check - the api MUST be available
 45 	console.assert(WebAudio.isAvailable === true, 'webkitAudioContext isnt available on your browser');
 46 
 47 	// create the context
 48 	this._ctx	= new webkitAudioContext();
 49 
 50 	// setup the end of the node chain
 51 	// TODO later code the clipping detection from http://www.html5rocks.com/en/tutorials/webaudio/games/ 
 52 	this._gainNode	= this._ctx.createGainNode();
 53 	this._compressor= this._ctx.createDynamicsCompressor();
 54 	this._gainNode.connect( this._compressor );
 55 	this._compressor.connect( this._ctx.destination );	
 56 };
 57 
 58 
 59 /**
 60  * vendor.js way to make plugins ala jQuery
 61  * @namespace
 62 */
 63 WebAudio.fn	= WebAudio.prototype;
 64 
 65 
 66 /**
 67  * destructor
 68 */
 69 WebAudio.prototype.destroy	= function(){
 70 };
 71 
 72 /**
 73  * 
 74  *
 75  * @return {Boolean} true if it is available or not
 76 */
 77 WebAudio.isAvailable	= window.webkitAudioContext ? true : false;
 78 
 79 //////////////////////////////////////////////////////////////////////////////////
 80 //										//
 81 //////////////////////////////////////////////////////////////////////////////////
 82 
 83 /**
 84  * get the audio context
 85  *
 86  * @returns {AudioContext} the audio context
 87 */
 88 WebAudio.prototype.context	= function(){
 89 	return this._ctx;
 90 };
 91 
 92 /**
 93  * Create a sound
 94  *
 95  * @returns {WebAudio.Sound} the sound just created
 96 */
 97 WebAudio.prototype.createSound	= function()
 98 {
 99 	var webaudio	= this;
100 	var sound	= new WebAudio.Sound(webaudio);
101 	return sound;
102 }
103 
104 
105 /**
106  * return the entry node in the master node chains
107 */
108 WebAudio.prototype._entryNode	= function(){
109 	//return this._ctx.destination;
110 	return this._gainNode;
111 }
112 
113 /**
114  * getter/setter on the volume
115 */
116 WebAudio.prototype.volume	= function(value){
117 	if( value === undefined )	return this._gainNode.gain.value;
118 	this._gainNode.gain.value	= value;
119 	return this;
120 };
121 
122