2017-11-14 15:00:24 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
var Extend = require('../utils/object/Extend');
|
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
|
|
|
// Phaser.Sound.BaseSound
|
|
|
|
var BaseSound = new Class({
|
|
|
|
initialize: function BaseSound(manager, key, config) {
|
|
|
|
/**
|
2017-11-15 16:26:20 +00:00
|
|
|
* Local reference to the sound manager.
|
|
|
|
*
|
2017-11-14 15:00:24 +00:00
|
|
|
* @property {Phaser.Sound.BaseSoundManager} manager
|
|
|
|
*/
|
|
|
|
this.manager = manager;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {string} key
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.key = key;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* Duration set explicitly, rest of default values
|
|
|
|
* will be set by other properties setters.
|
|
|
|
*
|
|
|
|
* @property {ISoundConfig} config
|
|
|
|
*/
|
2017-11-22 16:45:57 +00:00
|
|
|
this.config = {};
|
2017-11-16 13:29:28 +00:00
|
|
|
/**
|
|
|
|
* Reference to the currently used config.
|
|
|
|
* It could be default config or marker config.
|
|
|
|
*
|
|
|
|
* @property {ISoundConfig} currentConfig
|
|
|
|
*/
|
|
|
|
this.currentConfig = this.config;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {boolean} mute
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.mute = false;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {number} volume
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.volume = 1;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
2017-11-16 14:23:04 +00:00
|
|
|
* Defines the speed at which the audio asset will be played.
|
|
|
|
* Value of 1.0 plays the audio at full speed, 0.5 plays the audio
|
|
|
|
* at half speed and 2.0 doubles the audio's playback speed.
|
|
|
|
* This value gets multiplied by global rate to have the final playback speed.
|
2017-11-15 16:26:20 +00:00
|
|
|
*
|
|
|
|
* @property {number} rate
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.rate = 1;
|
2017-11-16 15:41:48 +00:00
|
|
|
/**
|
|
|
|
* Represents detuning of sound in [cents](https://en.wikipedia.org/wiki/Cent_%28music%29).
|
|
|
|
* The range of the value is -1200 to 1200, but we recommend setting it to [50](https://en.wikipedia.org/wiki/50_Cent).
|
|
|
|
*
|
2017-11-20 13:54:33 +00:00
|
|
|
* Standards based Web Audio implementation only.
|
|
|
|
* Webkit Web Audio implementation and HTML5 Audio don't support this.
|
|
|
|
*
|
2017-11-16 15:41:48 +00:00
|
|
|
* @property {number} detune
|
|
|
|
*/
|
|
|
|
this.detune = 0;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {number} seek
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.seek = 0;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {boolean} loop
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.loop = false;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {number} pan
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.pan = 0;
|
2017-11-15 16:26:20 +00:00
|
|
|
this.config = Extend(this.config, config);
|
|
|
|
/**
|
|
|
|
* Flag indicating if sound is currently playing.
|
|
|
|
*
|
|
|
|
* @property {boolean} isPlaying
|
|
|
|
*/
|
|
|
|
this.isPlaying = false;
|
2017-11-17 13:51:23 +00:00
|
|
|
/**
|
|
|
|
* Flag indicating if sound is currently paused.
|
|
|
|
*
|
|
|
|
* @property {boolean} isPaused
|
|
|
|
*/
|
|
|
|
this.isPaused = false;
|
2017-11-15 16:26:20 +00:00
|
|
|
/**
|
|
|
|
* Object containing markers definitions.
|
|
|
|
*
|
|
|
|
* @property {{}} markers
|
|
|
|
*/
|
|
|
|
this.markers = {};
|
|
|
|
/**
|
|
|
|
* Name of the currently played marker.
|
|
|
|
* If no marker is played, but instead the whole sound
|
|
|
|
* the value is an empty string - ''.
|
|
|
|
*
|
|
|
|
* @property {string} currentMarker
|
|
|
|
*/
|
|
|
|
this.currentMarker = '';
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Tween}
|
|
|
|
*/
|
|
|
|
this.fadeTween = null; // TODO see how to use global tween
|
|
|
|
/**
|
|
|
|
* Event dispatches used to handle all sound instance
|
|
|
|
* relevant events.
|
|
|
|
*
|
|
|
|
* @property {Phaser.Events.EventDispatcher}
|
|
|
|
*/
|
2017-11-14 15:00:24 +00:00
|
|
|
this.events = new EventDispatcher();
|
|
|
|
},
|
2017-11-15 16:26:20 +00:00
|
|
|
// TODO set default methods to NOOP if not used
|
2017-11-14 15:00:24 +00:00
|
|
|
addMarker: function (marker) {
|
2017-11-14 15:26:52 +00:00
|
|
|
return false;
|
2017-11-14 15:00:24 +00:00
|
|
|
},
|
|
|
|
removeMarker: function (markerName) {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
play: function (marker, config) {
|
2017-11-16 13:19:04 +00:00
|
|
|
if (marker === void 0) { marker = ''; }
|
2017-11-16 13:54:08 +00:00
|
|
|
if (typeof marker === 'object') {
|
|
|
|
config = marker;
|
|
|
|
marker = '';
|
|
|
|
}
|
2017-11-16 13:19:04 +00:00
|
|
|
if (typeof marker !== 'string') {
|
|
|
|
console.error('Sound marker name has to be a string!');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!marker) {
|
|
|
|
this.currentConfig = this.config;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!this.markers[marker]) {
|
|
|
|
console.error('No marker with name \'' + marker + '\' found for sound \'' + this.key + '\'!');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this.currentMarker = marker;
|
|
|
|
this.currentConfig = this.markers[marker].config;
|
|
|
|
}
|
|
|
|
this.currentConfig = Extend(this.currentConfig, config);
|
2017-11-17 14:14:57 +00:00
|
|
|
this.isPlaying = true;
|
|
|
|
this.isPaused = false;
|
2017-11-14 15:00:24 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
pause: function () {
|
2017-11-17 17:37:49 +00:00
|
|
|
if (this.isPaused || !this.isPlaying) {
|
2017-11-17 16:16:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-17 14:16:25 +00:00
|
|
|
this.isPlaying = false;
|
|
|
|
this.isPaused = true;
|
2017-11-17 16:16:06 +00:00
|
|
|
return true;
|
2017-11-14 15:00:24 +00:00
|
|
|
},
|
|
|
|
resume: function () {
|
2017-11-17 17:37:49 +00:00
|
|
|
if (!this.isPaused || this.isPlaying) {
|
2017-11-17 16:16:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-17 14:18:51 +00:00
|
|
|
this.isPlaying = true;
|
|
|
|
this.isPaused = false;
|
2017-11-17 16:16:06 +00:00
|
|
|
return true;
|
2017-11-14 15:00:24 +00:00
|
|
|
},
|
|
|
|
stop: function () {
|
2017-11-17 17:37:49 +00:00
|
|
|
if (!this.isPaused && !this.isPlaying) {
|
2017-11-17 16:16:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-17 14:03:59 +00:00
|
|
|
this.isPlaying = false;
|
|
|
|
this.isPaused = false;
|
2017-11-17 16:16:06 +00:00
|
|
|
return true;
|
2017-11-14 15:00:24 +00:00
|
|
|
},
|
2017-11-16 12:32:35 +00:00
|
|
|
applyConfig: function () {
|
|
|
|
this.mute = this.currentConfig.mute;
|
|
|
|
this.volume = this.currentConfig.volume;
|
2017-11-16 14:23:04 +00:00
|
|
|
this.rate = this.currentConfig.rate;
|
2017-11-16 16:21:00 +00:00
|
|
|
this.detune = this.currentConfig.detune;
|
2017-11-16 12:32:35 +00:00
|
|
|
// TODO assign other config values to buffer source
|
|
|
|
},
|
2017-11-14 15:00:24 +00:00
|
|
|
fadeTo: function (volume, duration) {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
update: function () {
|
|
|
|
},
|
|
|
|
destroy: function () {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = BaseSound;
|