forked from Wizcorp/AudioManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundGroup.js
69 lines (64 loc) · 3.34 KB
/
SoundGroup.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
//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
/** Set of sound played in sequence each times it triggers
* used for animation sfx
* @author Cedric Stoquer
*
* @param {String} id - sound ground id
* @param {number[]} soundIds - array of sound ids
* @param {number[]} volumes - array of volumes
* @param {number[]} pitches - array of pitches
*/
function SoundGroup(id, soundIds, volumes, pitches, muted) {
this.id = id;
this.soundIds = soundIds;
this.volumes = volumes || [];
this.pitches = pitches || [];
this.soundIndex = 0;
this.volIndex = 0;
this.pitchIndex = 0;
this.poolRef = null;
this._ready = false;
if (this.volumes.length === 0) this.volumes.push(1.0);
if (this.pitches.length === 0) this.pitches.push(0.0);
if (!muted) this._createSounds();
}
module.exports = SoundGroup;
//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
/** Create and load all sound used in group */
SoundGroup.prototype._createSounds = function () {
var soundIds = this.soundIds;
for (var i = 0; i < soundIds.length; i++) {
this.audioManager.loadSound(soundIds[i]);
}
this._ready = true;
};
//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
/** Play sound group.
*
* @param {number} [volume] - optional volume
* @param {number} [pan] - optional panoramic
* @param {number} [pitch] - optional pitch value in semi-tone (available only if using webAudio)
*/
SoundGroup.prototype.play = function (volume, pan, pitch) {
if (this.soundIds.length === 0) return;
if (!this._ready) this._createSounds();
var soundId = this.soundIds[this.soundIndex++];
var sound = this.audioManager.getSound(soundId);
if (!sound) return console.warn('[Sound Group: ' + this.id + '] sound id ' + soundId + ' cannot be played.');
volume = volume || 1.0;
pitch = pitch || 0.0;
volume *= this.volumes[this.volIndex++];
pitch += this.pitches[this.pitchIndex++];
sound.play(volume, pan, pitch);
if (this.soundIndex >= this.soundIds.length) { this.soundIndex = 0; }
if (this.volIndex >= this.volumes.length) { this.volIndex = 0; }
if (this.pitchIndex >= this.pitches.length) { this.pitchIndex = 0; }
};
//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
/** Check that all sounds in group are correctly created */
SoundGroup.prototype.verifySounds = function () {
for (var i = 0; i < this.soundIds.length; i++) {
var soundId = this.soundIds[i];
this.audioManager.createSound(soundId);
}
};