forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseSoundManager.js
More file actions
392 lines (392 loc) · 12.6 KB
/
Copy pathBaseSoundManager.js
File metadata and controls
392 lines (392 loc) · 12.6 KB
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
var Class = require('../utils/Class');
var NOOP = require('../utils/NOOP');
var EventEmitter = require('eventemitter3');
/*!
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
*/
var BaseSoundManager = new Class({
Extends: EventEmitter,
/**
* The sound manager is responsible for playing back audio via Web Audio API or HTML Audio tag as fallback.
* The audio file type and the encoding of those files are extremely important.
* Not all browsers can play all audio formats.
* There is a good guide to what's supported [here](https://developer.mozilla.org/en-US/Apps/Fundamentals/Audio_and_video_delivery/Cross-browser_audio_basics#Audio_Codec_Support).
*
* @class Phaser.Sound.BaseSoundManager
* @constructor
* @param {Phaser.Game} game - Reference to the current game instance.
*/
initialize: function BaseSoundManager(game) {
EventEmitter.call(this);
/**
* Local reference to game.
*
* @readonly
* @property {Phaser.Game} game
*/
this.game = game;
/**
* An array containing all added sounds.
*
* @private
* @property {ISound[]} sounds
* @default []
*/
this.sounds = [];
/**
* Global mute setting.
*
* @property {boolean} mute
* @default false
*/
this.mute = false;
/**
* Global volume setting.
*
* @property {number} volume
* @default 1
*/
this.volume = 1;
/**
* Global playback rate at which all the sounds 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.
*
* @property {number} rate
* @default 1
*/
this.rate = 1;
/**
* Global detuning of all sounds 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).
*
* @property {number} detune
* @default 0
*/
this.detune = 0;
/**
* Flag indicating if sounds should be paused when game looses focus,
* for instance when user switches to another tab/program/app.
*
* @property {boolean} pauseOnBlur
* @default true
*/
this.pauseOnBlur = true;
game.events.on('blur', function () {
if (this.pauseOnBlur) {
this.onBlur();
}
}, this);
game.events.on('focus', function () {
if (this.pauseOnBlur) {
this.onFocus();
}
}, this);
/**
* Property that actually holds the value of global playback rate.
*
* @private
* @property {number} _rate
* @default 1
*/
this._rate = 1;
/**
* Property that actually holds the value of global detune.
*
* @private
* @property {number} _detune
* @default 0
*/
this._detune = 0;
/**
* Mobile devices require sounds to be triggered from an explicit user action,
* such as a tap, before any sound can be loaded/played on a web page.
* Set to true if the audio system is currently locked awaiting user interaction.
*
* @readonly
* @property {boolean} locked
*/
this.locked = this.locked || false;
/**
* Flag used internally for handling when the audio system
* has been unlocked, if there ever was a need for it.
*
* @private
* @property {boolean} unlocked
* @default false
*/
this.unlocked = false;
if (this.locked) {
this.unlock();
}
},
/**
* Adds a new sound into the sound manager.
*
* @override
* @method Phaser.Sound.BaseSoundManager#add
* @param {string} key - Asset key for the sound.
* @param {ISoundConfig} [config] - An optional config object containing default sound settings.
* @returns {ISound} The new sound instance.
*/
add: NOOP,
/**
* Adds a new audio sprite sound into the sound manager.
*
* @method Phaser.Sound.BaseSoundManager#addAudioSprite
* @param {string} key - Asset key for the sound.
* @param {ISoundConfig} [config] - An optional config object containing default sound settings.
* @returns {IAudioSpriteSound} The new audio sprite sound instance.
*/
addAudioSprite: function (key, config) {
var sound = this.add(key, config);
/**
* Local reference to 'spritemap' object form json file generated by audiosprite tool.
*
* @property {object} spritemap
*/
sound.spritemap = this.game.cache.json.get(key).spritemap;
for (var markerName in sound.spritemap) {
if (!sound.spritemap.hasOwnProperty(markerName)) {
continue;
}
var marker = sound.spritemap[markerName];
sound.addMarker({
name: markerName,
start: marker.start,
duration: marker.end - marker.start,
config: config
});
}
return sound;
},
/**
* Enables playing sound on the fly without the need to keep a reference to it.
* Sound will auto destroy once its playback ends.
*
* @method Phaser.Sound.BaseSoundManager#play
* @param {string} key - Asset key for the sound.
* @param {ISoundConfig | ISoundMarker} [extra] - An optional additional object containing settings to be applied to the sound. It could be either config or marker object.
*/
play: function (key, extra) {
var sound = this.add(key);
// TODO document all events
sound.once('ended', sound.destroy, sound);
if (extra) {
if (extra.name) {
sound.addMarker(extra);
sound.play(extra.name);
}
else {
sound.play(extra);
}
}
else {
sound.play();
}
},
/**
* Enables playing audio sprite sound on the fly without the need to keep a reference to it.
* Sound will auto destroy once its playback ends.
*
* @method Phaser.Sound.BaseSoundManager#playAudioSprite
* @param {string} key - Asset key for the sound.
* @param {string} spriteName - The name of the sound sprite to play.
* @param {ISoundConfig} [config] - An optional config object containing default sound settings.
*/
playAudioSprite: function (key, spriteName, config) {
var sound = this.addAudioSprite(key);
sound.once('ended', sound.destroy, sound);
sound.play(spriteName, config);
},
/**
* Removes a sound from the sound manager.
* The removed sound is destroyed before removal.
*
* @method Phaser.Sound.BaseSoundManager#remove
* @param {ISound} sound - The sound object to remove.
* @returns {boolean} True if the sound was removed successfully, otherwise false.
*/
remove: function (sound) {
var index = this.sounds.indexOf(sound);
if (index !== -1) {
sound.destroy();
this.sounds.splice(index, 1);
return true;
}
return false;
},
/**
* Removes all sounds from the sound manager that have an asset key matching the given value.
* The removed sounds are destroyed before removal.
*
* @method Phaser.Sound.BaseSoundManager#removeByKey
* @param {string} key - The key to match when removing sound objects.
* @returns {number} The number of matching sound objects that were removed.
*/
removeByKey: function (key) {
var removed = 0;
for (var i = this.sounds.length - 1; i >= 0; i--) {
var sound = this.sounds[i];
if (sound.key === key) {
sound.destroy();
this.sounds.splice(i, 1);
removed++;
}
}
return removed;
},
/**
* Pauses all the sounds in the game.
*
* @method Phaser.Sound.BaseSoundManager#pauseAll
*/
pauseAll: function () {
this.forEachActiveSound(function (sound) {
sound.pause();
});
this.emit('pauseall', this);
},
/**
* Resumes all the sounds in the game.
*
* @method Phaser.Sound.BaseSoundManager#resumeAll
*/
resumeAll: function () {
this.forEachActiveSound(function (sound) {
sound.resume();
});
this.emit('resumeall', this);
},
/**
* Stops all the sounds in the game.
*
* @method Phaser.Sound.BaseSoundManager#stopAll
*/
stopAll: function () {
this.forEachActiveSound(function (sound) {
sound.stop();
});
this.emit('stopall', this);
},
/**
* Method used internally for unlocking audio playback on devices that
* require user interaction before any sound can be played on a web page.
*
* Read more about how this issue is handled here in [this article](TODO add link).
*
* @override
* @protected
* @method Phaser.Sound.BaseSoundManager#unlock
*/
unlock: NOOP,
/**
* Method used internally for pausing sound manager if
* Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.
*
* @override
* @protected
* @method Phaser.Sound.BaseSoundManager#onBlur
*/
onBlur: NOOP,
/**
* Method used internally for resuming sound manager if
* Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.
*
* @override
* @protected
* @method Phaser.Sound.BaseSoundManager#onFocus
*/
onFocus: NOOP,
/**
* Update method called on every game step.
* Removes destroyed sounds and updates every active sound in the game.
*
* @protected
* @method Phaser.Sound.BaseSoundManager#update
* @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: function (time, delta) {
if (this.unlocked) {
this.unlocked = false;
this.locked = false;
this.emit('unlocked', this);
}
for (var i = this.sounds.length - 1; i >= 0; i--) {
if (this.sounds[i].pendingRemove) {
this.sounds.splice(i, 1);
}
}
this.sounds.forEach(function (sound) {
sound.update(time, delta);
});
},
/**
* Destroys all the sounds in the game and all associated events.
*
* @method Phaser.Sound.BaseSoundManager#destroy
*/
destroy: function () {
this.game = null;
this.removeAllListeners();
this.forEachActiveSound(function (sound) {
sound.destroy();
});
this.sounds.length = 0;
this.sounds = null;
},
/**
* Method used internally for iterating only over active sounds and skipping sounds that are marked for removal.
*
* @private
* @method Phaser.Sound.BaseSoundManager#forEachActiveSound
* @param {(sound: ISound, index: number, array: ISound[]) => void} callbackfn - Callback function.
* @param [thisArg=this] - Callback context.
*/
forEachActiveSound: function (callbackfn, thisArg) {
var _this = this;
this.sounds.forEach(function (sound, index) {
if (!sound.pendingRemove) {
callbackfn.call(thisArg || _this, sound, index, _this.sounds);
}
});
}
});
/**
* Global playback rate.
*
* @name Phaser.Sound.BaseSoundManager#rate
* @property {number} rate
*/
Object.defineProperty(BaseSoundManager.prototype, 'rate', {
get: function () {
return this._rate;
},
set: function (value) {
this._rate = value;
this.forEachActiveSound(function (sound) {
sound.setRate();
});
this.emit('rate', this, value);
}
});
/**
* Global detune.
*
* @name Phaser.Sound.BaseSoundManager#detune
* @property {number} detune
*/
Object.defineProperty(BaseSoundManager.prototype, 'detune', {
get: function () {
return this._detune;
},
set: function (value) {
this._detune = value;
this.forEachActiveSound(function (sound) {
sound.setRate();
});
this.emit('detune', this, value);
}
});
module.exports = BaseSoundManager;