HTML5 Spatial Audio

When messing around with Phaser I wanted a way to use HTML5 3D audio. 3D audio allows people using headphones to hear noises coming from different directions.

Instead of creating a phaser plugin, I instead created a stand alone library that you can interface with phaser. This means that this library can be used with other game frameworks or by itself. The library is essentially a wrapper for the HTML 3D audio api. The library uses code from here to load sounds.

Loading Sounds

 // Creates the buffer to load sounds - createBufferLoader(array of sound locations, function to run after sounds are loaded)
audioLIB.createBufferLoader([ 
    "sound.mp3"
    ], function(){
		console.log("loaded");
	});
});

// load sounds
audioLIB.loadBuffers();

//is true if sounds have loaded
audioLIB.loaded;

Manipulating Listener (the listener is like where the persons ears would be in the scene)

//Set the listener's orientation - setListOrientation(x, y, z, x up vector, y up vector, z up vector)
audioLIB.setListOrientation(x, y, z, Ux, Uy, Uz);

//Set the listener's position
audioLIB.setListPosition(x, y, z);

//Set the listener's velocity
audioLIB.setListVelocity(x, y, z);

//Update the listener's orientation and position based on a sprite - updateFromSprite(sprite, [optional] sprite body)
//Doesn't set the listener's velocity
audioLIB.updateFromSprite(sprite);

Mainpulationg Sounds

//Array containing all sounds
audioLIB.sounds;

//Shortcut for setting whether the sounds should loop or not - setSoundLoops(boolean)
audioLIB.setSoundLoops(loops);

//Starts playing all sounds
audioLIB.startAll();

//Stops playing all sounds
audioLIB.stopAll();

//Runs a function for each sound
audioLIB.forEachSound(function (sound) {
});

//Functions in sound objects

//Set sound object orientation, position and velocity
audioLIB.Sound.prototype.setOrientation(x,y,z);
audioLIB.Sound.prototype.setPosition(x,y,z);
audioLIB.Sound.prototype.setVelocity(x,y,z);

//Set sound object direction - setDirection(coneInnerAngle, coneOuterAngle, coneOuterGain)
audioLIB.Sound.prototype.setDirection(iA, oA, oG);

//Set sound object gain - setGain(float)
audioLIB.Sound.prototype.setGain(v)

//Set whether sound object loops - setLoop(boolean)
audioLIB.Sound.prototype.setLoop(v)

//Set the sound object's buffer (the buffer contains the sound objects sound data)
audioLIB.Sound.prototype.setBuffer(b)

//Update the sound object's orientation and position with a sprite - updateFromSprite(sprite, [optional] sprite body)
audioLIB.Sound.prototype.updateFromSprite(sprite, body);

//Start playing the sound at position v
audioLIB.Sound.prototype.start(v);

//Stop playing the sound
audioLIB.Sound.prototype.stop();
Download Link: Click here
Please credit me if used

Download Link for BufferLoader: Click here