forked from BYU-ARCLITE/Ayamel.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResampler.js
263 lines (246 loc) · 7.68 KB
/
Resampler.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//JavaScript Audio Resampler
var Resampler = (function(){
"use strict";
var blobURL = URL.createObjectURL(
new Blob(
['(' + workerFn.toString() + ')();'],
{type: "text/javascript"}
)
);
//Constructor
return function(fromRate, toRate, channels) {
var worker, that = this;
//Perform some checks:
if (fromRate <= 0 || toRate <= 0 || channels <= 0) {
throw new Error("Invalid Resampler Settings");
}
this.receive = function(){};
if (fromRate === toRate) {
this.run = Bypass;
} else {
worker = new Worker(blobURL);
worker.postMessage({
type: "setup",
channels: channels || 1,
ratio: fromRate / toRate
});
worker.addEventListener('message',function(e){
var data = e.data;
data.inBuffer = new Float32Array(data.inBuffer);
data.outBuffer = new Float32Array(data.outBuffer);
that.receive(data);
},false);
this.run = function(inBuffer, outBuffer){
//Have to transfer ArrayBuffers, not TypedArrays
worker.postMessage({
type: "exec",
inBuffer: inBuffer.buffer,
outBuffer: outBuffer.buffer
},[inBuffer.buffer,outBuffer.buffer]);
};
}
}
function Bypass(inbuf,outbuf) {
var offset = Math.min(inbuf.length,outbuf.length);
outbuf.set(inbuf.subarray(0,offset));
this.receive({
inBuffer: inbuf,
outBuffer: outbuf,
sourceOffset: offset,
outputOffset: offset
});
}
function workerFn(){
this.addEventListener('message',function(e){
"use strict";
var data = e.data, offsets;
switch(data.type){
case "exec":
offsets = this.exec(new Float32Array(data.inBuffer),new Float32Array(data.outBuffer));
this.postMessage({
inBuffer: data.inBuffer,
outBuffer: data.outBuffer,
sourceOffset: offsets.sourceOffset,
outputOffset: offsets.outputOffset
},[data.inBuffer,data.outBuffer])
break;
case "setup":
this.lastOutput = 0; //for mono
if (data.ratio < 1) {
// Use generic linear interpolation for upsampling
this.exec = data.channels === 1?
MonoLinearInterp.bind(this,data.ratio):
LinearInterp.bind(this,data.ratio,new Float32Array(data.channels),data.channels);
this.lastWeight = 1;
} else {
//Downsampling based on algorithm by Grant Galitz
//https://github.com/grantgalitz/XAudioJS
this.exec = data.channels === 1?
MonoMultiTap.bind(this,data.ratio):
MultiTap.bind(this,data.ratio,new Float32Array(data.channels),data.channels);
this.tailExists = false;
this.lastWeight = 0;
}
}
},false);
function MonoLinearInterp(ratioWeight, inBuffer, outBuffer) {
var weight = this.lastWeight,
lastOutput = this.lastOutput,
inLength = inBuffer.length,
outLength = outBuffer.length,
firstWeight = 0,
secondWeight = 0,
sourceOffset = 0,
outputOffset = 0;
if(inLength > 0 && outLength > 0){
for (; weight < 1; weight += ratioWeight) {
secondWeight = weight % 1;
firstWeight = 1 - secondWeight;
outBuffer[outputOffset++] = (lastOutput * firstWeight) + (inBuffer[0] * secondWeight);
}
weight -= 1;
for (inLength -= channels, sourceOffset = Math.floor(weight); outputOffset < outLength && sourceOffset < bufferLength;) {
secondWeight = weight % 1;
firstWeight = 1 - secondWeight;
outBuffer[outputOffset++] = (inBuffer[sourceOffset] * firstWeight) + (inBuffer[sourceOffset+1] * secondWeight);
weight += ratioWeight;
sourceOffset = Math.floor(weight);
}
this.lastOutput = inBuffer[sourceOffset];
this.lastWeight = weight % 1;
}else{ sourceOffset = -1; }
return {
sourceOffset: sourceOffset+1,
outputOffset: outputOffset
};
}
function LinearInterp(ratioWeight, lastOutput, channels, inBuffer, outBuffer) {
var inLength = inBuffer.length,
outLength = outBuffer.length,
weight = this.lastWeight,
firstWeight = 0,
secondWeight = 0,
sourceOffset = 0,
outputOffset = 0,
sourceEnd, c, c2;
inLength -= inLength % channels;
outLength -= outLength % channels;
if(inLength > 0 && outLength > 0){
for (; weight < 1; weight += ratioWeight) {
secondWeight = weight % 1;
firstWeight = 1 - secondWeight;
for(c = 0; c < channels; ++c){
outBuffer[outputOffset++] = (lastOutput[c] * firstWeight) + (inBuffer[c] * secondWeight);
}
}
weight -= 1;
for (bufferLength -= channels, sourceOffset = Math.floor(weight) * channels; outputOffset < outLength && sourceOffset < inLength;) {
secondWeight = weight % 1;
firstWeight = 1 - secondWeight;
sourceEnd = channels + sourceOffset;
c2 = sourceOffset + channels;
for(c = sourceOffset; c < sourceEnd; ++c){
outBuffer[outputOffset++] = (inBuffer[c] * firstWeight) + (inBuffer[c2++] * secondWeight);
}
weight += ratioWeight;
sourceOffset = Math.floor(weight) * channels;
}
lastOutput.set(inBuffer.subarray(sourceOffset));
this.lastWeight = weight % 1;
}else{ sourceOffset -= channels; }
return {
sourceOffset: sourceOffset+channels,
outputOffset: outputOffset
};
}
function MonoMultiTap(ratioWeight, inBuffer, outBuffer) {
var inLength = inBuffer.length,
outLength = outBuffer.length,
weight = 0,
amountToNext = 0,
processTail = this.tailExists,
sourceOffset = 0,
outputOffset = 0,
currentPosition = 0,
output;
if (inLength > 0 && outLength > 0){
do {
if (processTail) {
weight = this.lastWeight;
output = this.lastOutput;
processTail = false;
} else {
weight = ratioWeight;
output = 0;
}
while (weight > 0 && sourceOffset < inLength) {
amountToNext = 1 + sourceOffset - currentPosition;
if (weight >= amountToNext) {
output += inBuffer[sourceOffset++] * amountToNext;
currentPosition = sourceOffset;
weight -= amountToNext;
} else {
output += inBuffer[sourceOffset] * weight;
currentPosition += weight;
weight = 0;
}
}
if (weight != 0) { break; }
outBuffer[outputOffset++] = output / ratioWeight;
} while (sourceOffset < inLength && outputOffset < outLength);
this.lastWeight = weight;
this.lastOutput = output;
this.tailExists = true;
}
return {
sourceOffset: sourceOffset,
outputOffset: outputOffset
};
}
function MultiTap(ratioWeight, output, channels, inBuffer, outBuffer) {
var inLength = inBuffer.length,
outLength = outBuffer.length,
weight = 0,
amountToNext = 0,
processTail = this.tailExists,
sourceOffset = 0,
outputOffset = 0,
currentPosition = 0,
c, c2;
inLength -= inLength % channels;
outLength -= outLength % channels;
if (inLength > 0 && outLength > 0){
do {
if (processTail) {
weight = this.lastWeight;
processedTail = false;
} else {
weight = ratioWeight;
for(c = 0; c < channels; ++c){ output[c] = 0; }
}
while (weight > 0 && sourceOffset < inLength) {
amountToNext = 1 + sourceOffset - currentPosition;
if (weight >= amountToNext) {
for(c = 0; c < channels; ++c){ output[c] += inBuffer[sourceOffset++] * amountToNext; }
currentPosition = sourceOffset;
weight -= amountToNext;
} else {
c2 = sourceOffset;
for(c = 0; c < channels; ++c){ output[c] += inBuffer[c2++] * weight; }
currentPosition += weight;
weight = 0;
}
}
if (weight != 0) { break; }
for(c = 0; c < channels; ++c){ outBuffer[outputOffset++] = output[c] / ratioWeight; }
} while (sourceOffset < bufferLength && outputOffset < outLength);
this.lastWeight = weight;
this.tailExists = true;
}
return {
sourceOffset: sourceOffset,
outputOffset: outputOffset
};
}
}
}());