phaser/Phaser/SoundManager.ts

163 lines
4.2 KiB
TypeScript
Raw Normal View History

2013-04-18 13:16:18 +00:00
/// <reference path="Game.ts" />
2013-04-18 15:52:20 +00:00
/// <reference path="system/Sound.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - SoundManager
*
* This is an embroyonic web audio sound management class. There is a lot of work still to do here.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
export class SoundManager {
2013-04-12 16:19:56 +00:00
/**
* SoundManager constructor
* Create a new <code>SoundManager</code>.
*/
2013-04-18 13:16:18 +00:00
constructor(game: Game) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (game.device.webaudio == true)
{
if (!!window['AudioContext'])
{
this._context = new window['AudioContext']();
}
else if (!!window['webkitAudioContext'])
{
this._context = new window['webkitAudioContext']();
}
if (this._context !== null)
{
this._gainNode = this._context.createGainNode();
this._gainNode.connect(this._context.destination);
this._volume = 1;
}
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Local private reference to game.
*/
2013-04-18 13:16:18 +00:00
private _game: Game;
2013-04-12 16:19:56 +00:00
/**
* Reference to AudioContext instance.
*/
2013-04-18 13:16:18 +00:00
private _context = null;
/**
* Gain node created from audio context.
*/
2013-04-18 13:16:18 +00:00
private _gainNode;
/**
* Volume of sounds.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
private _volume: number;
2013-04-12 16:19:56 +00:00
/**
* Mute sounds.
*/
2013-04-18 13:16:18 +00:00
public mute() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._gainNode.gain.value = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Enable sounds.
*/
2013-04-18 13:16:18 +00:00
public unmute() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._gainNode.gain.value = this._volume;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public set volume(value: number) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._volume = value;
this._gainNode.gain.value = this._volume;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get volume(): number {
return this._volume;
2013-04-12 16:19:56 +00:00
}
/**
* Decode a sound with its assets key.
* @param key {string} Assets key of the sound to be decoded.
* @param callback {function} This will be invoked when finished decoding.
2013-05-04 16:18:45 +00:00
* @param [sound] {Sound} its bufer will be set to decoded data.
*/
2013-04-18 13:16:18 +00:00
public decode(key: string, callback = null, sound?: Sound = null) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var soundData = this._game.cache.getSound(key);
if (soundData)
{
if (this._game.cache.isSoundDecoded(key) === false)
{
var that = this;
this._context.decodeAudioData(soundData, function (buffer) {
that._game.cache.decodedSound(key, buffer);
if (sound)
{
sound.setDecodedBuffer(buffer);
}
callback();
});
}
}
2013-04-12 16:19:56 +00:00
}
/**
* Play a sound with its assets key.
* @param key {string} Assets key of the sound you want to play.
2013-05-04 16:18:45 +00:00
* @param [volume] {number} volume of the sound you want to play.
* @param [loop] {boolean} loop when it finished playing? (Default to false)
* @return {Sound} The playing sound object.
*/
2013-04-18 13:16:18 +00:00
public play(key: string, volume?: number = 1, loop?: bool = false): Sound {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._context === null)
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
return;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
var soundData = this._game.cache.getSound(key);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (soundData)
{
// Does the sound need decoding?
if (this._game.cache.isSoundDecoded(key) === true)
{
return new Sound(this._context, this._gainNode, soundData, volume, loop);
}
else
{
var tempSound: Sound = new Sound(this._context, this._gainNode, null, volume, loop);
// this is an async process, so we can return the Sound object anyway, it just won't be playing yet
this.decode(key, () => tempSound.play(), tempSound);
2013-04-18 13:16:18 +00:00
return tempSound;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
2013-04-12 16:19:56 +00:00
}
}
}