diff --git a/build/webaudiox.js b/build/webaudiox.js index 34836a7..41433c7 100644 --- a/build/webaudiox.js +++ b/build/webaudiox.js @@ -55,7 +55,7 @@ var WebAudiox = WebAudiox || {} * display an analyser node in a canvas * * @param {AnalyserNode} analyser the analyser node - * @param {Number} smoothFactor the smooth factor for smoothed volume + * @param {Number} smoothFactor the smooth factor for smoothed volume */ WebAudiox.Analyser2Volume = function(analyser, smoothFactor){ // arguments default values @@ -121,7 +121,7 @@ var WebAudiox = WebAudiox || {} * @param {Number} beatRate the beat rate of the binaural sound (e.g. around 2-10hz) * @param {Number} gain the gain applied on the result */ -WebAudiox.BinauralSource = function(pitch, beatRate, gain){ +WebAudiox.BinauralSource = function(context, pitch, beatRate, gain){ pitch = pitch !== undefined ? pitch : 440 beatRate= beatRate !== undefined ? beatRate : 5 gain = gain !== undefined ? gain : 1 @@ -270,7 +270,7 @@ WebAudiox.LineOut = function(context){ return volumeNode.gain.value; }, set : function(value){ - volumeNode.gain.value = value; + volumeNode.gain.value = value; } }); @@ -321,6 +321,8 @@ var WebAudiox = WebAudiox || {} * @param {Function} onError callback to notify when an error occured */ WebAudiox.loadBuffer = function(context, url, onLoad, onError){ + onLoad = onload || function(buffer){} + onError = onError || function(){} var request = new XMLHttpRequest() request.open('GET', url, true) request.responseType = 'arraybuffer' @@ -331,12 +333,12 @@ WebAudiox.loadBuffer = function(context, url, onLoad, onError){ // counter inProgress request WebAudiox.loadBuffer.inProgressCount-- // notify the callback - onLoad && onLoad(buffer) + onLoad(buffer) // notify WebAudiox.loadBuffer.onLoad(context, url, buffer) }, function(){ // notify the callback - onError && onError() + onError() // counter inProgress request WebAudiox.loadBuffer.inProgressCount-- }) @@ -424,7 +426,7 @@ WebAudiox.ListenerSetObject3D = function(context, object3d){ */ WebAudiox.ListenerObject3DUpdater = function(context, object3d){ var prevPosition= null - this.update = function(delta, now){ + this.update = function(delta){ // update the position/orientation WebAudiox.ListenerSetObject3D(context, object3d) @@ -667,21 +669,13 @@ WebAudiox.AnalyserBeatDetector.compute = function(analyser, width, offset){ } -var WebAudiox = WebAudiox || {} - /** - * ## how to implement 3d in this - * * GameSounds.update(delta) - - * * GameSounds.follow(camera) - * * sound.follow(object3d).play() - * * sound.lookAt(vector3).play() - * * sound.at(vector3).play() - * * sound.velocity(vector3).play() + * @namespace */ +var WebAudiox = WebAudiox || {} /** - * attempts to a more structure sound banks + * a specific helpers for gamedevs to make WebAudio API easy to use for their case */ WebAudiox.GameSounds = function(){ // create WebAudio API context @@ -697,6 +691,7 @@ WebAudiox.GameSounds = function(){ /** * show if the Web Audio API is detected or not + * * @type {boolean} */ this.webAudioDetected = AudioContext ? true : false @@ -704,7 +699,12 @@ WebAudiox.GameSounds = function(){ ////////////////////////////////////////////////////////////////////////////////// // update loop // ////////////////////////////////////////////////////////////////////////////////// - + + /** + * the update function + * + * @param {Number} delta seconds since the last iteration + */ this.update = function(delta){ if( this.listenerUpdater ){ this.listenerUpdater.update(delta) @@ -722,16 +722,24 @@ WebAudiox.GameSounds = function(){ /** * create a sound from this context - * @param {Object} defaultOptions the default option for this sound, optional + * @param {Object} options the default option for this sound, optional * @return {WebAudiox.GameSound} the created sound */ - this.createSound = function(label, defaultOptions){ - return new WebAudiox.GameSound(label, this, defaultOptions) + this.createSound = function(options){ + return new WebAudiox.GameSound(this, options) } - // set the listener position - this.setPosition = function(position){ + ////////////////////////////////////////////////////////////////////////////////// + // handle .listenerAt // + ////////////////////////////////////////////////////////////////////////////////// + + /** + * Set the listener position + * @param {THREE.Vector3|THREE.Object3D} position the position to copy + * @return {WebAudiox.GameSounds} the object itself for linked API + */ + this.listenerAt = function(position){ if( position instanceof THREE.Vector3 ){ WebAudiox.ListenerSetPosition(context, position) }else if( position instanceof THREE.Object3D ){ @@ -744,23 +752,32 @@ WebAudiox.GameSounds = function(){ // handle .follow/.unFollow // ////////////////////////////////////////////////////////////////////////////////// - this.follow = function(object3d){ + /** + * Make the listener follow a three.js THREE.Object3D + * + * @param {THREE.Object3D} object3d the object to follow + * @return {WebAudiox.GameSounds} the object itself for linked API + */ + this.listenerFollow = function(object3d){ // put a ListenerObject3DUpdater var listenerUpdater = new WebAudiox.ListenerObject3DUpdater(context, object3d) this.listenerUpdater = listenerUpdater return this } - this.unFollow = function(){ + + /** + * Make the listener Stop Following the object + * @return {WebAudiox.GameSounds} the object itself for linked API + */ + this.listenerStopFollow = function(){ context.listener.setVelocity(0,0,0); - this.listenerUpdater = null return this } - } ////////////////////////////////////////////////////////////////////////////////// -// comment // +// WebAudiox.GameSound // ////////////////////////////////////////////////////////////////////////////////// /** @@ -768,22 +785,26 @@ WebAudiox.GameSounds = function(){ * @param {WebAudiox.GameSounds} gameSounds * @param {Object} defaultOptions the default play options */ -WebAudiox.GameSound = function(label, gameSounds, defaultOptions){ - this.label = label || console.assert(false) +WebAudiox.GameSound = function(gameSounds, defaultOptions){ this.gameSounds = gameSounds || console.assert(false) this.defaultOptions = defaultOptions|| {} ////////////////////////////////////////////////////////////////////////////////// // register/unregister in gameSound // ////////////////////////////////////////////////////////////////////////////////// - - console.assert(gameSounds.bank[label] === undefined, 'label already defined') - gameSounds.bank[label] = this - this.destroy = function(){ + + this.label = null; + this.register = function(label){ + console.assert(gameSounds.bank[label] === undefined, 'label already defined') + gameSounds.bank[label] = this + return this; + } + this.unregister = function(){ + if( this.label === null ) return; delete gameSounds.bank[label] + return this; } - ////////////////////////////////////////////////////////////////////////////////// // update loop // ////////////////////////////////////////////////////////////////////////////////// @@ -801,6 +822,7 @@ WebAudiox.GameSound = function(label, gameSounds, defaultOptions){ this.load = function(url, onLoad, onError){ this.loaded = false + this.buffer = null WebAudiox.loadBuffer(gameSounds.context, url, function(decodedBuffer){ this.loaded = true this.buffer = decodedBuffer; @@ -889,4 +911,4 @@ WebAudiox.GameSound = function(label, gameSounds, defaultOptions){ return utterance } -} \ No newline at end of file +} diff --git a/examples/binaural.html b/examples/binaural.html index 6921c34..b10a663 100644 --- a/examples/binaural.html +++ b/examples/binaural.html @@ -25,7 +25,7 @@ var lineOut = new WebAudiox.LineOut(context) // create and connect the sound - var binauralSrc = new WebAudiox.BinauralSource() + var binauralSrc = new WebAudiox.BinauralSource(context) binauralSrc.output.connect(lineOut.destination) binauralSrc.start() diff --git a/lib/webaudiox.analyser2volume.js b/lib/webaudiox.analyser2volume.js index 54e868c..481cd76 100644 --- a/lib/webaudiox.analyser2volume.js +++ b/lib/webaudiox.analyser2volume.js @@ -5,7 +5,7 @@ var WebAudiox = WebAudiox || {} * display an analyser node in a canvas * * @param {AnalyserNode} analyser the analyser node - * @param {Number} smoothFactor the smooth factor for smoothed volume + * @param {Number} smoothFactor the smooth factor for smoothed volume */ WebAudiox.Analyser2Volume = function(analyser, smoothFactor){ // arguments default values diff --git a/lib/webaudiox.binaural.js b/lib/webaudiox.binaural.js index 5fe5650..efddb84 100644 --- a/lib/webaudiox.binaural.js +++ b/lib/webaudiox.binaural.js @@ -9,7 +9,7 @@ var WebAudiox = WebAudiox || {} * @param {Number} beatRate the beat rate of the binaural sound (e.g. around 2-10hz) * @param {Number} gain the gain applied on the result */ -WebAudiox.BinauralSource = function(pitch, beatRate, gain){ +WebAudiox.BinauralSource = function(context, pitch, beatRate, gain){ pitch = pitch !== undefined ? pitch : 440 beatRate= beatRate !== undefined ? beatRate : 5 gain = gain !== undefined ? gain : 1 diff --git a/lib/webaudiox.lineout.js b/lib/webaudiox.lineout.js index 06fe4b3..bed31f1 100644 --- a/lib/webaudiox.lineout.js +++ b/lib/webaudiox.lineout.js @@ -38,7 +38,7 @@ WebAudiox.LineOut = function(context){ return volumeNode.gain.value; }, set : function(value){ - volumeNode.gain.value = value; + volumeNode.gain.value = value; } }); diff --git a/lib/webaudiox.loadbuffer.js b/lib/webaudiox.loadbuffer.js index 2cbc378..44bd022 100644 --- a/lib/webaudiox.loadbuffer.js +++ b/lib/webaudiox.loadbuffer.js @@ -9,6 +9,8 @@ var WebAudiox = WebAudiox || {} * @param {Function} onError callback to notify when an error occured */ WebAudiox.loadBuffer = function(context, url, onLoad, onError){ + onLoad = onload || function(buffer){} + onError = onError || function(){} var request = new XMLHttpRequest() request.open('GET', url, true) request.responseType = 'arraybuffer' @@ -19,12 +21,12 @@ WebAudiox.loadBuffer = function(context, url, onLoad, onError){ // counter inProgress request WebAudiox.loadBuffer.inProgressCount-- // notify the callback - onLoad && onLoad(buffer) + onLoad(buffer) // notify WebAudiox.loadBuffer.onLoad(context, url, buffer) }, function(){ // notify the callback - onError && onError() + onError() // counter inProgress request WebAudiox.loadBuffer.inProgressCount-- }) diff --git a/lib/webaudiox.three.js b/lib/webaudiox.three.js index 1222f54..c19c0cd 100644 --- a/lib/webaudiox.three.js +++ b/lib/webaudiox.three.js @@ -57,7 +57,7 @@ WebAudiox.ListenerSetObject3D = function(context, object3d){ */ WebAudiox.ListenerObject3DUpdater = function(context, object3d){ var prevPosition= null - this.update = function(delta, now){ + this.update = function(delta){ // update the position/orientation WebAudiox.ListenerSetObject3D(context, object3d)