-
Notifications
You must be signed in to change notification settings - Fork 1
/
load-sounds.js
54 lines (43 loc) · 1.12 KB
/
load-sounds.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
// This function requires loadSound
var loadSounds = function(paths, callback, progress) {
try {
var context = new webkitAudioContext();
} catch (e) {
setTimeout(function() {
var p = document.createElement('p');
p.innerHTML = 'Failed to create an audioContext.<br>' + e.message;
document.body.appendChild(p);
}, 10);
return;
}
var loaded = [];
var errors = [];
var sounds = [];
var after = function() {
var done = (loaded.length + errors.length) === paths.length;
// if callback exists, execute it after all sounds have loaded
if (done && typeof callback === 'function') {
callback(sounds);
}
// progress is an optional intermediate callback
if (typeof progress === 'function') {
progress(sounds);
}
};
var err = function(message, request) {
console.log(message, request);
errors.push(request);
after();
};
var requests = paths.map(function(url, i) {
var callback = function(buffer) {
sounds[i] = buffer;
loaded.push(buffer);
after();
};
return loadSound(url, context, callback, err);
});
requests.loaded = loaded;
requests.errors = errors;
return requests;
};