Skip to content

Commit

Permalink
new build
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromeetienne committed Feb 22, 2014
1 parent b992c90 commit 5842bf4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 43 deletions.
94 changes: 58 additions & 36 deletions build/webaudiox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -270,7 +270,7 @@ WebAudiox.LineOut = function(context){
return volumeNode.gain.value;
},
set : function(value){
volumeNode.gain.value = value;
volumeNode.gain.value = value;
}
});

Expand Down Expand Up @@ -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'
Expand All @@ -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--
})
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -697,14 +691,20 @@ WebAudiox.GameSounds = function(){

/**
* show if the Web Audio API is detected or not
*
* @type {boolean}
*/
this.webAudioDetected = AudioContext ? true : false

//////////////////////////////////////////////////////////////////////////////////
// update loop //
//////////////////////////////////////////////////////////////////////////////////


/**
* the update function
*
* @param {Number} delta seconds since the last iteration
*/
this.update = function(delta){
if( this.listenerUpdater ){
this.listenerUpdater.update(delta)
Expand All @@ -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 ){
Expand All @@ -744,46 +752,59 @@ 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 //
//////////////////////////////////////////////////////////////////////////////////

/**
* a sound from WebAudiox.GameSounds
* @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 //
//////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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;
Expand Down Expand Up @@ -889,4 +911,4 @@ WebAudiox.GameSound = function(label, gameSounds, defaultOptions){

return utterance
}
}
}
2 changes: 1 addition & 1 deletion examples/binaural.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion lib/webaudiox.analyser2volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/webaudiox.binaural.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/webaudiox.lineout.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ WebAudiox.LineOut = function(context){
return volumeNode.gain.value;
},
set : function(value){
volumeNode.gain.value = value;
volumeNode.gain.value = value;
}
});

Expand Down
6 changes: 4 additions & 2 deletions lib/webaudiox.loadbuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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--
})
Expand Down
2 changes: 1 addition & 1 deletion lib/webaudiox.three.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 5842bf4

Please sign in to comment.