phaser/Phaser/SoundManager.ts

125 lines
3.1 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
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
2013-04-18 13:16:18 +00:00
private _game: Game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
private _context = null;
private _gainNode;
private _volume: number;
2013-04-12 16:19:56 +00:00
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
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
}
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
}
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
}
}
}