-
Notifications
You must be signed in to change notification settings - Fork 5
/
abbey-load.js
65 lines (52 loc) · 1.59 KB
/
abbey-load.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
/*
* abbeyload.js
* A music asset loader by Stuart Memo
*/
(function (window, undefined) {
var filesLoaded = 0,
numberOfFiles = 0,
context = new AudioContext(),
buffers = [];
var AbbeyLoad = function (files, callback) {
this.files = files || {};
filesLoaded = 0;
numberOfFiles = 0;
loadFiles(this.files, callback);
};
AbbeyLoad.size = function(obj) {
var size = 0;
for (var key in obj) {
if (obj.hasOwnProperty(key)){
size++;
}
}
return size;
};
var loadFile = function (fileKey, file, returnObj, callback) {
var request = new XMLHttpRequest();
request.open('GET', file[fileKey], true);
request.responseType = 'arraybuffer';
request.onload = function () {
filesLoaded++;
context.decodeAudioData(request.response, function (decodedBuffer) {
returnObj[fileKey] = decodedBuffer;
if (AbbeyLoad.size(returnObj) === numberOfFiles) {
callback(returnObj);
}
});
};
request.send();
};
var loadFiles = function (files, callback) {
var returnObj = {};
files.forEach(function (file, index) {
numberOfFiles = AbbeyLoad.size(file);
for (var key in file) {
if (file.hasOwnProperty(key)) {
loadFile(key, file, returnObj, callback);
}
}
});
};
window.AbbeyLoad = AbbeyLoad;
})(window);