phaser/Phaser/sound/Sound.ts

158 lines
3.5 KiB
TypeScript
Raw Normal View History

2013-04-18 15:49:08 +00:00
/// <reference path="../Game.ts" />
2013-05-28 20:38:37 +00:00
/// <reference path="SoundManager.ts" />
2013-04-18 15:49:08 +00:00
/**
* Phaser - Sound
*
* A Sound file, used by the Game.SoundManager for playback.
*/
module Phaser {
export class Sound {
2013-05-03 11:32:39 +00:00
/**
* Sound constructor
* @param context {object} The AudioContext instance.
* @param gainNode {object} Gain node instance.
* @param data {object} Sound data.
2013-05-04 16:18:45 +00:00
* @param [volume] {number} volume of this sound when playing.
* @param [loop] {boolean} loop this sound when playing? (Default to false)
2013-05-03 11:32:39 +00:00
*/
2013-04-18 15:49:08 +00:00
constructor(context, gainNode, data, volume?: number = 1, loop?: bool = false) {
this._context = context;
this._gainNode = gainNode;
this._buffer = data;
this._volume = volume;
this.loop = loop;
// Local volume control
if (this._context !== null)
{
this._localGainNode = this._context.createGainNode();
this._localGainNode.connect(this._gainNode);
this._localGainNode.gain.value = this._volume;
}
if (this._buffer === null)
{
this.isDecoding = true;
}
else
{
this.play();
}
}
2013-05-03 11:32:39 +00:00
/**
* Local private reference to AudioContext.
*/
2013-04-18 15:49:08 +00:00
private _context;
2013-05-03 11:32:39 +00:00
/**
* Reference to gain node of SoundManager.
*/
2013-04-18 15:49:08 +00:00
private _gainNode;
2013-05-03 11:32:39 +00:00
/**
* GainNode of this sound.
*/
2013-04-18 15:49:08 +00:00
private _localGainNode;
2013-05-03 11:32:39 +00:00
/**
* Decoded data buffer.
*/
2013-04-18 15:49:08 +00:00
private _buffer;
2013-05-03 11:32:39 +00:00
/**
* Volume of this sound.
*/
2013-04-18 15:49:08 +00:00
private _volume: number;
2013-05-03 11:32:39 +00:00
/**
* The real sound object (buffer source).
*/
2013-04-18 15:49:08 +00:00
private _sound;
loop: bool = false;
duration: number;
isPlaying: bool = false;
isDecoding: bool = false;
public setDecodedBuffer(data) {
this._buffer = data;
this.isDecoding = false;
//this.play();
2013-04-18 15:49:08 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Play this sound.
*/
2013-04-18 15:49:08 +00:00
public play() {
if (this._buffer === null || this.isDecoding === true)
{
return;
}
this._sound = this._context.createBufferSource();
this._sound.buffer = this._buffer;
this._sound.connect(this._localGainNode);
if (this.loop)
{
this._sound.loop = true;
}
this._sound.noteOn(0); // the zero is vitally important, crashes iOS6 without it
this.duration = this._sound.buffer.duration;
this.isPlaying = true;
}
2013-05-03 11:32:39 +00:00
/**
* Stop playing this sound.
*/
2013-04-18 15:49:08 +00:00
public stop() {
if (this.isPlaying === true)
{
this.isPlaying = false;
this._sound.noteOff(0);
}
}
2013-05-03 11:32:39 +00:00
/**
* Mute the sound.
*/
2013-04-18 15:49:08 +00:00
public mute() {
this._localGainNode.gain.value = 0;
}
2013-05-03 11:32:39 +00:00
/**
* Enable the sound.
*/
2013-04-18 15:49:08 +00:00
public unmute() {
this._localGainNode.gain.value = this._volume;
}
public set volume(value: number) {
this._volume = value;
this._localGainNode.gain.value = this._volume;
}
public get volume(): number {
return this._volume;
}
}
}