-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.js
84 lines (74 loc) · 2.59 KB
/
lib.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var IS_LOCALHOST = false;
function Lib() {
};
Lib.debug = true;
Lib.path = function(p) {
var prefix = typeof ROOT_URL_PATH == 'undefined' ? undefined : ROOT_URL_PATH;
return IS_LOCALHOST ? p : [prefix,p].join('/');
};
Lib.listClone = function(list) {
return list.slice(0);
};
Lib.listRemoveElement = function(list,e) {
var i = list.indexOf(e);
i > -1 && list.splice(i,1);
return list;
};
Lib.uniqFast = function(list) {
var seen = {}, out = [], len = list.length, j = 0;
for(var i = 0; i < len; i++) {
var item = list[i];
if(seen[item] !== 1) { seen[item] = 1; out[j++] = item; }
}
return out;
}
Lib.uniq = function(list) {
return Array.from(new Set(list));
}
Lib.hashLength = function(hash) { Object.keys(hash).length; };
Lib.loadResource = function(url, callbackComplete, callbackFailed, responseType) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
// xhr.overrideMimeType("audio/ogg;");
xhr.responseType = responseType || 'arraybuffer';
// oReq.addEventListener("progress", callback);
// xhr.addEventListener("load", callbackComplete);
xhr.addEventListener("load", function(e){
// Lib.debug && console.log('L '+xhr.status+' '+xhr.readyState);
if(xhr.readyState == 4) { xhr.status == (IS_LOCALHOST ? 0 : 200) ? callbackComplete(e) : callbackFailed(e); } // status 0 with FS serving
});
// xhr.onreadystatechange = function(e) {
// Lib.debug && console.log(xhr.status);
// };
xhr.addEventListener("error", callbackFailed);
xhr.addEventListener("abort", callbackFailed);
// xhr.addEventListener("error", function(e){
// // Lib.debug && console.log('E '+xhr.status+' '+xhr.readyState);
// callbackFailed(e);
// });
// xhr.addEventListener("abort", function(e){
// Lib.debug && console.log('A '+xhr.status+' '+xhr.readyState);
// callbackFailed(e);
// });
xhr.send();
};
// Lib.audioLoad = function(url, audioCtx, callback) {
// Lib.loadResource(url, function(e) {
// audioCtx.decodeAudioData(e.target.response, function(decodedArrayBuffer) {
// callback(decodedArrayBuffer);
// }, function(e) {
// Lib.debug && console.log('Error decoding file', e);
// callback(false);
// });
// }, function(e) {
// Lib.debug && console.log('Error loading file', e);
// callback(null);
// });
// };
Lib.audioPlay = function(source, decodedArrayBuffer) {
source.buffer = decodedArrayBuffer;
source.start(0);
}
Lib.audioStop = function(source) {
source.stop(0);
};