-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
BaseSound.js
497 lines (429 loc) · 13.3 KB
/
BaseSound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var Extend = require('../utils/object/Extend');
var NOOP = require('../utils/NOOP');
/**
* @classdesc
* Class containing all the shared state and behavior of a sound object, independent of the implementation.
*
* @class BaseSound
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Sound
* @constructor
* @since 3.0.0
*
* @param {Phaser.Sound.BaseSoundManager} manager - Reference to the current sound manager instance.
* @param {string} key - Asset key for the sound.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*/
var BaseSound = new Class({
Extends: EventEmitter,
initialize:
function BaseSound (manager, key, config)
{
EventEmitter.call(this);
/**
* Local reference to the sound manager.
*
* @name Phaser.Sound.BaseSound#manager
* @type {Phaser.Sound.BaseSoundManager}
* @since 3.0.0
*/
this.manager = manager;
/**
* Asset key for the sound.
*
* @name Phaser.Sound.BaseSound#key
* @type {string}
* @readonly
* @since 3.0.0
*/
this.key = key;
/**
* Flag indicating if sound is currently playing.
*
* @name Phaser.Sound.BaseSound#isPlaying
* @type {boolean}
* @default false
* @readonly
* @since 3.0.0
*/
this.isPlaying = false;
/**
* Flag indicating if sound is currently paused.
*
* @name Phaser.Sound.BaseSound#isPaused
* @type {boolean}
* @default false
* @readonly
* @since 3.0.0
*/
this.isPaused = false;
/**
* A property that holds the value of sound's actual playback rate,
* after its rate and detune values has been combined with global
* rate and detune values.
*
* @name Phaser.Sound.BaseSound#totalRate
* @type {number}
* @default 1
* @readonly
* @since 3.0.0
*/
this.totalRate = 1;
/**
* A value representing the duration, in seconds.
* It could be total sound duration or a marker duration.
*
* @name Phaser.Sound.BaseSound#duration
* @type {number}
* @readonly
* @since 3.0.0
*/
this.duration = this.duration || 0;
/**
* The total duration of the sound in seconds.
*
* @name Phaser.Sound.BaseSound#totalDuration
* @type {number}
* @readonly
* @since 3.0.0
*/
this.totalDuration = this.totalDuration || 0;
/**
* A config object used to store default sound settings' values.
* Default values will be set by properties' setters.
*
* @name Phaser.Sound.BaseSound#config
* @type {Phaser.Types.Sound.SoundConfig}
* @private
* @since 3.0.0
*/
this.config = {
mute: false,
volume: 1,
rate: 1,
detune: 0,
seek: 0,
loop: false,
delay: 0,
pan: 0
};
/**
* Reference to the currently used config.
* It could be default config or marker config.
*
* @name Phaser.Sound.BaseSound#currentConfig
* @type {Phaser.Types.Sound.SoundConfig}
* @private
* @since 3.0.0
*/
this.currentConfig = this.config;
this.config = Extend(this.config, config);
/**
* Object containing markers definitions.
*
* @name Phaser.Sound.BaseSound#markers
* @type {Object.<string, Phaser.Types.Sound.SoundMarker>}
* @default {}
* @readonly
* @since 3.0.0
*/
this.markers = {};
/**
* Currently playing marker.
* 'null' if whole sound is playing.
*
* @name Phaser.Sound.BaseSound#currentMarker
* @type {Phaser.Types.Sound.SoundMarker}
* @default null
* @readonly
* @since 3.0.0
*/
this.currentMarker = null;
/**
* Flag indicating if destroy method was called on this sound.
*
* @name Phaser.Sound.BaseSound#pendingRemove
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.pendingRemove = false;
},
/**
* Adds a marker into the current sound. A marker is represented by name, start time, duration, and optionally config object.
* This allows you to bundle multiple sounds together into a single audio file and use markers to jump between them for playback.
*
* @method Phaser.Sound.BaseSound#addMarker
* @since 3.0.0
*
* @param {Phaser.Types.Sound.SoundMarker} marker - Marker object.
*
* @return {boolean} Whether the marker was added successfully.
*/
addMarker: function (marker)
{
if (!marker || !marker.name || typeof marker.name !== 'string')
{
return false;
}
if (this.markers[marker.name])
{
// eslint-disable-next-line no-console
console.error('addMarker ' + marker.name + ' already exists in Sound');
return false;
}
marker = Extend(true, {
name: '',
start: 0,
duration: this.totalDuration - (marker.start || 0),
config: {
mute: false,
volume: 1,
rate: 1,
detune: 0,
seek: 0,
loop: false,
delay: 0,
pan: 0
}
}, marker);
this.markers[marker.name] = marker;
return true;
},
/**
* Updates previously added marker.
*
* @method Phaser.Sound.BaseSound#updateMarker
* @since 3.0.0
*
* @param {Phaser.Types.Sound.SoundMarker} marker - Marker object with updated values.
*
* @return {boolean} Whether the marker was updated successfully.
*/
updateMarker: function (marker)
{
if (!marker || !marker.name || typeof marker.name !== 'string')
{
return false;
}
if (!this.markers[marker.name])
{
// eslint-disable-next-line no-console
console.warn('Audio Marker: ' + marker.name + ' missing in Sound: ' + this.key);
return false;
}
this.markers[marker.name] = Extend(true, this.markers[marker.name], marker);
return true;
},
/**
* Removes a marker from the sound.
*
* @method Phaser.Sound.BaseSound#removeMarker
* @since 3.0.0
*
* @param {string} markerName - The name of the marker to remove.
*
* @return {?Phaser.Types.Sound.SoundMarker} Removed marker object or 'null' if there was no marker with provided name.
*/
removeMarker: function (markerName)
{
var marker = this.markers[markerName];
if (!marker)
{
return null;
}
this.markers[markerName] = null;
return marker;
},
/**
* Play this sound, or a marked section of it.
*
* It always plays the sound from the start. If you want to start playback from a specific time
* you can set 'seek' setting of the config object, provided to this call, to that value.
*
* @method Phaser.Sound.BaseSound#play
* @since 3.0.0
*
* @param {(string|Phaser.Types.Sound.SoundConfig)} [markerName=''] - If you want to play a marker then provide the marker name here. Alternatively, this parameter can be a SoundConfig object.
* @param {Phaser.Types.Sound.SoundConfig} [config] - Optional sound config object to be applied to this marker or entire sound if no marker name is provided. It gets memorized for future plays of current section of the sound.
*
* @return {boolean} Whether the sound started playing successfully.
*/
play: function (markerName, config)
{
if (markerName === undefined) { markerName = ''; }
if (typeof markerName === 'object')
{
config = markerName;
markerName = '';
}
if (typeof markerName !== 'string')
{
return false;
}
if (!markerName)
{
this.currentMarker = null;
this.currentConfig = this.config;
this.duration = this.totalDuration;
}
else
{
if (!this.markers[markerName])
{
// eslint-disable-next-line no-console
console.warn('Marker: ' + markerName + ' missing in Sound: ' + this.key);
return false;
}
this.currentMarker = this.markers[markerName];
this.currentConfig = this.currentMarker.config;
this.duration = this.currentMarker.duration;
}
this.resetConfig();
this.currentConfig = Extend(this.currentConfig, config);
this.isPlaying = true;
this.isPaused = false;
return true;
},
/**
* Pauses the sound. This only works if the sound is currently playing.
*
* You can inspect the `isPlaying` and `isPaused` properties to check the state.
*
* @method Phaser.Sound.BaseSound#pause
* @since 3.0.0
*
* @return {boolean} Whether the sound was paused successfully.
*/
pause: function ()
{
if (this.isPaused || !this.isPlaying)
{
return false;
}
this.isPlaying = false;
this.isPaused = true;
return true;
},
/**
* Resumes the sound. This only works if the sound is paused and not already playing.
*
* You can inspect the `isPlaying` and `isPaused` properties to check the state.
*
* @method Phaser.Sound.BaseSound#resume
* @since 3.0.0
*
* @return {boolean} Whether the sound was resumed successfully.
*/
resume: function ()
{
if (!this.isPaused || this.isPlaying)
{
return false;
}
this.isPlaying = true;
this.isPaused = false;
return true;
},
/**
* Stop playing this sound.
*
* @method Phaser.Sound.BaseSound#stop
* @since 3.0.0
*
* @return {boolean} Whether the sound was stopped successfully.
*/
stop: function ()
{
if (!this.isPaused && !this.isPlaying)
{
return false;
}
this.isPlaying = false;
this.isPaused = false;
this.resetConfig();
return true;
},
/**
* Method used internally for applying config values to some of the sound properties.
*
* @method Phaser.Sound.BaseSound#applyConfig
* @since 3.0.0
*/
applyConfig: function ()
{
this.mute = this.currentConfig.mute;
this.volume = this.currentConfig.volume;
this.rate = this.currentConfig.rate;
this.detune = this.currentConfig.detune;
this.loop = this.currentConfig.loop;
this.pan = this.currentConfig.pan;
},
/**
* Method used internally for resetting values of some of the config properties.
*
* @method Phaser.Sound.BaseSound#resetConfig
* @since 3.0.0
*/
resetConfig: function ()
{
this.currentConfig.seek = 0;
this.currentConfig.delay = 0;
},
/**
* Update method called automatically by sound manager on every game step.
*
* @method Phaser.Sound.BaseSound#update
* @since 3.0.0
*
* @param {number} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
* @param {number} delta - The delta time elapsed since the last frame.
*/
update: NOOP,
/**
* Method used internally to calculate total playback rate of the sound.
*
* @method Phaser.Sound.BaseSound#calculateRate
* @since 3.0.0
*/
calculateRate: function ()
{
var cent = 1.0005777895065548; // Math.pow(2, 1/1200);
var totalDetune = this.currentConfig.detune + this.manager.detune;
var detuneRate = Math.pow(cent, totalDetune);
this.totalRate = this.currentConfig.rate * this.manager.rate * detuneRate;
},
/**
* Destroys this sound and all associated events and marks it for removal from the sound manager.
*
* @method Phaser.Sound.BaseSound#destroy
* @fires Phaser.Sound.Events#DESTROY
* @since 3.0.0
*/
destroy: function ()
{
if (this.pendingRemove)
{
return;
}
this.stop();
this.emit(Events.DESTROY, this);
this.removeAllListeners();
this.pendingRemove = true;
this.manager = null;
this.config = null;
this.currentConfig = null;
this.markers = null;
this.currentMarker = null;
}
});
module.exports = BaseSound;