-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomSound.js
More file actions
41 lines (36 loc) · 985 Bytes
/
Copy pathRandomSound.js
File metadata and controls
41 lines (36 loc) · 985 Bytes
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
class RandomSound {
constructor(files) {
this.sounds = [];
for (let i = 0; i < files.length; i++)
this.addSound(...Object.values(files[i]));
}
addSound(file, volume, pool=false, numpool=0) {
let sound = null;
if (!pool)
sound = new Audio(file);
else
sound = new PoolAudio(file, numpool);
sound.volume = volume;
this.sounds.push(sound);
console.log(this.sounds)
}
play() {
this.sounds[Math.floor(Math.random()*this.sounds.length)].play();
}
}
class PoolAudio {
constructor(file, numPool) {
this.pool = [];
for (let i = 0; i < numPool; i++)
this.pool.push(new Audio(file));
this.id = 0;
}
set volume(newValue) {
this.pool.forEach(audio => audio.volume = newValue);
}
play() {
this.pool[this.id].play();
this.id += 1;
this.id = this.id % this.pool.length;
}
}