-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreverb.js
166 lines (156 loc) · 5.81 KB
/
reverb.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*global ArrayBuffer, Uint8Array, window, XMLHttpRequest*/
var reverbjs = {
extend : function (audioContext) {
function decodeBase64ToArrayBuffer(input) {
function encodedValue(input, index) {
var encodedCharacter, x = input.charCodeAt(index);
if (index < input.length) {
if (x >= 65 && x <= 90) {
encodedCharacter = x - 65;
} else if (x >= 97 && x <= 122) {
encodedCharacter = x - 71;
} else if (x >= 48 && x <= 57) {
encodedCharacter = x + 4;
} else if (x === 43) {
encodedCharacter = 62;
} else if (x === 47) {
encodedCharacter = 63;
} else if (x !== 61) {
console.log('base64 encountered unexpected character code: ' + x);
}
}
return encodedCharacter;
}
if (input.length === 0 || (input.length % 4) > 0) {
console.log('base64 encountered unexpected length: ' + input.length);
return;
}
var padding = input.match(/[=]*$/)[0].length,
decodedLength = input.length * 3 / 4 - padding,
buffer = new ArrayBuffer(decodedLength),
bufferView = new Uint8Array(buffer),
encoded = [],
d = 0,
e = 0,
i;
while (d < decodedLength) {
for (i = 0; i < 4; i += 1) {
encoded[i] = encodedValue(input, e);
e += 1;
}
bufferView[d] = (encoded[0] * 4) + Math.floor(encoded[1] / 16);
d += 1;
if (d < decodedLength) {
bufferView[d] = ((encoded[1] % 16) * 16) + Math.floor(encoded[2] / 4);
d += 1;
}
if (d < decodedLength) {
bufferView[d] = ((encoded[2] % 4) * 64) + encoded[3];
d += 1;
}
}
return buffer;
}
function decodeAndSetupBuffer(node, arrayBuffer, callback) {
audioContext.decodeAudioData(arrayBuffer, function (audioBuffer) {
console.log('Finished decoding audio data.');
node.buffer = audioBuffer;
if (typeof callback === "function" && audioBuffer !== null) {
callback(node);
}
}, function (e) {
console.log('Could not decode audio data: ' + e);
});
}
audioContext.createReverbFromBase64 = function (audioBase64, callback) {
var reverbNode = audioContext.createConvolver();
decodeAndSetupBuffer(reverbNode, decodeBase64ToArrayBuffer(audioBase64),
callback);
return reverbNode;
};
audioContext.createSourceFromBase64 = function (audioBase64, callback) {
var sourceNode = audioContext.createBufferSource();
decodeAndSetupBuffer(sourceNode, decodeBase64ToArrayBuffer(audioBase64),
callback);
return sourceNode;
};
audioContext.createReverbFromUrl = function (audioUrl, callback) {
console.log('Downloading impulse response from ' + audioUrl);
var reverbNode = audioContext.createConvolver(),
request = new XMLHttpRequest();
request.open('GET', audioUrl, true);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
console.log('Downloaded impulse response');
decodeAndSetupBuffer(reverbNode, request.response, callback);
}
};
request.onerror = function (e) {
console.log('There was an error receiving the response: ' + e);
reverbjs.networkError = e;
};
request.responseType = 'arraybuffer';
request.send();
return reverbNode;
};
audioContext.createSourceFromUrl = function (audioUrl, callback) {
console.log('Downloading sound from ' + audioUrl);
var sourceNode = audioContext.createBufferSource(),
request = new XMLHttpRequest();
request.open('GET', audioUrl, true);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
console.log('Downloaded sound');
decodeAndSetupBuffer(sourceNode, request.response, callback);
}
};
request.onerror = function (e) {
console.log('There was an error receiving the response: ' + e);
reverbjs.networkError = e;
};
request.responseType = 'arraybuffer';
request.send();
return sourceNode;
};
audioContext.createReverbFromBase64Url = function (audioUrl, callback) {
console.log('Downloading base64 impulse response from ' + audioUrl);
var reverbNode = audioContext.createConvolver(),
request = new XMLHttpRequest();
request.open('GET', audioUrl, true);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
console.log('Downloaded impulse response');
decodeAndSetupBuffer(reverbNode,
decodeBase64ToArrayBuffer(request.response),
callback);
}
};
request.onerror = function (e) {
console.log('There was an error receiving the response: ' + e);
reverbjs.networkError = e;
};
request.send();
return reverbNode;
};
audioContext.createSourceFromBase64Url = function (audioUrl, callback) {
console.log('Downloading base64 sound from ' + audioUrl);
var sourceNode = audioContext.createBufferSource(),
request = new XMLHttpRequest();
request.open('GET', audioUrl, true);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
console.log('Downloaded sound');
decodeAndSetupBuffer(sourceNode,
decodeBase64ToArrayBuffer(request.response),
callback);
}
};
request.onerror = function (e) {
console.log('There was an error receiving the response: ' + e);
reverbjs.networkError = e;
};
request.send();
return sourceNode;
};
}
};