forked from BYU-ARCLITE/subtitle-timeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResampler.js
307 lines (278 loc) · 8.18 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//JavaScript Audio Resampler
var Resampler = (function(){
"use strict";
var blobURL = URL.createObjectURL(
new Blob(
['(' + workerFn.toString() + ')();'],
{type: "text/javascript"}
)
);
var arrayTypes = {
8: Int8Array,
16: Int16Array,
32: Float32Array,
64: Float64Array
};
function Resampler(opts){
var worker, that = this,
fromRate = +opts.from || 0,
toRate = +opts.to || 0,
channels = +opts.channels || 0,
cons = arrayTypes[opts.bitrate] || Float32Array;
//Perform some checks:
if (fromRate <= 0 || toRate <= 0 || channels <= 0) {
throw new Error("Invalid Resampler Settings");
}
//TODO: Implement Reset properly
this.events = {};
if (fromRate === toRate) {
//Bypass- copy inputs to outputs of the appropriate type
//TODO: make this respect the output buffer size option
this.append = function(inputs) {
this.emit('data',inputs.map(function(a){ return new cons(a); }));
};
this.flush = function(){
var i, buffers = [];
for(i = 0; i < channels; ++i){ buffers.push(new ArrayBuffer(0)); }
this.emit('data',buffers);
};
this.reset = function(){};
} else {
worker = new Worker(blobURL);
worker.postMessage({
cmd: "init",
bitrate: opts.bitrate,
channels: channels,
ratio: fromRate / toRate,
outLength: +opts.bufferSize || 4096
});
worker.addEventListener('message',function(e){
that.emit('data',e.data);
},false);
worker.addEventListener('error',function(e){
console.log(e);
that.emit('error',e);
},false);
this.append = function(inputs){
worker.postMessage({
cmd: "exec",
inputs: inputs
});
};
this.flush = function(){ worker.postMessage({cmd: "flush"}); };
this.reset = function(){};
}
}
Resampler.prototype.on = function(ename, handler){
if(!this.events.hasOwnProperty(ename)){
this.events[ename] = [handler];
}else{
this.events[ename].push(handler);
}
};
Resampler.prototype.off = function(ename, handler){
var i, evlist = this.events[ename];
if(!evlist){ return; }
i = evlist.indexOf(handler);
if(~i){ evlist.splice(i,1); }
};
Resampler.prototype.emit = function(ename, obj){
var evlist = this.events[ename];
if(!evlist){ return; }
evlist.forEach(function(h){ h.call(this, obj); }, this);
}
return Resampler;
function workerFn(){
"use strict";
var alg = null,
buffers = null,
excessLength = 0,
channels,
olen, cons;
self.addEventListener('message',function(e){
"use strict";
var data = e.data,
i, ratio, len;
switch(data.cmd){
case "init":
cons = {
8: Int8Array,
16: Int16Array,
32: Float32Array,
64: Float64Array
}[data.bitrate] || Float32Array;
ratio = data.ratio;
channels = data.channels;
olen = data.outLength;
buffers = [];
for(i = 0; i < channels; ++i){ buffers.push(new cons(olen)); }
alg = new Algorithm(ratio, channels);
break;
case "exec":
len = Math.min.apply(Math, data.inputs.map(function(a){ return a.length; }));
if(isFinite(len)){ Exec(data.inputs, len); }
break;
case "flush":
Flush();
alg.reset();
break;
}
},false);
//Re-use the same output buffers over and over,
//copying them to the main thread when they become
//full, until we exhaust the available input
function Exec(inArrays, inLength){
var offsets,
outOffset, inOffset,
outArrays, outLength;
//Acquire output arrays
if(excessLength > 0){
outLength = excessLength;
outOffset = olen - excessLength;
outArrays = buffers.map(function(b){ return b.subarray(outOffset); });
}else{
outLength = olen;
outArrays = buffers;
}
do {
offsets = alg.exec(inLength, outLength, inArrays, outArrays);
outOffset = offsets.outputOffset;
inOffset = offsets.inputOffset;
if(outOffset >= outLength){
//copy output buffers to main thread
self.postMessage(buffers);
//reset output arrays
outLength = olen;
outArrays = buffers;
}
if(inOffset >= inLength){ break; } //input was exhausted
//Otherwise, shift the inputs
inLength -= inOffset;
inArrays = inArrays.map(function(a){ return a.subarray(inOffset); });
}while(true);
excessLength = outLength - outOffset;
}
function Flush(){
var nbufs, index;
if(excessLength > 0){
index = olen = excessLength;
self.postMessage(buffers.map(function(b){ return b.subarray(index); }));
excessLength = 0;
}else{
self.postMessage(buffers.map(function(){ return new cons(0); }));
}
}
function Algorithm(ratio,channels){
this.channels = channels;
this.ratio = ratio;
this.lastWeight = 0;
this.lastOutput = new Float64Array(channels);
//TODO: create mono-optimized versions
this.exec = ratio < 1?LinearInterp:FractionalMean;
}
Algorithm.prototype.reset = function(){
var i;
this.lastWeight = 0;
for(i = 0; i < this.channels; ++i){
this.lastOutput[i] = 0;
}
};
/*
* Each output sample consists of the sum of some window of input samples,
* plus some fraction of a prior sample and some fraction of a following
* sample, all scaled according to the resampling ratio.
*/
function FractionalMean(inLength, outLength, inBuffers, outBuffers) {
var ratio = this.ratio,
lastOutput = this.lastOutput,
channels = this.channels,
inputOffset = 0,
outputOffset = 0,
buffer, weight,
postWeight, preWeight,
start, sum, c, i;
if (inLength > 0 && outLength > 0){
weight = (this.lastWeight >= 1)?this.lastWeight:(ratio - this.lastWeight);
start = inputOffset;
inputOffset = Math.min(start + Math.floor(weight), inLength);
postWeight = weight - (inputOffset - start);
while(postWeight < 1 && outputOffset < outLength && inputOffset < inLength) {
//we can produce a complete output sample
preWeight = 1-postWeight;
//Do one channel at a time to optimize data locality
for(c = 0; c < channels; ++c){
buffer = inBuffers[c];
sum = lastOutput[c]; //partial prior sample
//Sum intermediate full samples
for (i = start; i < inputOffset; ++i) { sum += buffer[i]; }
//add partial following sample and normalize
outBuffers[c][outputOffset] = (sum + buffer[i] * postWeight) / ratio;
//setup prior partials for the next iteration
lastOutput[c] = (preWeight < 1)?buffer[i] * preWeight:0;
}
weight = ratio - preWeight;
start = inputOffset+1;
inputOffset = Math.min(start + Math.floor(weight), inLength);
postWeight = weight - (inputOffset - start);
outputOffset++;
}
//produce partial samples
this.lastWeight = postWeight;
for(c = 0; c < channels; ++c){
buffer = inBuffers[c];
sum = lastOutput[c]; //partial prior sample
//Get as many full input samples as we can
for (sum = 0, i = start; i < inputOffset; ++i) { sum += buffer[i]; }
lastOutput[c] = sum;
}
}
return {
inputOffset: inputOffset,
outputOffset: outputOffset
};
}
function LinearInterp(inLength, outLength, inBuffers, outBuffers) {
var ratioWeight = this.ratio,
lastOutput = this.lastOutput,
channels = this.channels,
outputOffset = 0,
inputOffset = 0,
weight, preweight,
firstSamples,
ibuf, c;
if(inLength > 0 && outLength > 0){
inputOffset = 1;
weight = this.lastWeight;
firstSamples = inBuffers.map(function(a){ return a[0]; });
while(weight < 1 && outputOffset < outLength){
preweight = 1 - weight;
for(c = 0; c < channels; ++c){
outBuffers[c][outputOffset] = (lastOutput[c] * preweight) + (firstSamples[c] * weight);
}
weight += ratioWeight;
outputOffset++;
}
weight -= 1;
while(outputOffset < outLength && inputOffset < inLength) {
preweight = 1 - weight;
for(c = 0; c < channels; ++c){
ibuf = inBuffers[c];
outBuffers[c][outputOffset] = (ibuf[inputOffset-1] * preweight) + (ibuf[inputOffset] * weight);
}
weight += ratioWeight;
outputOffset++;
if(weight >= 1){
inputOffset++;
weight -= 1;
}
}
for(c = 0; c < channels; ++c){ lastOutput[c] = inBuffers[c][inputOffset]; }
this.lastWeight = weight % 1;
}
return {
inputOffset: inputOffset,
outputOffset: outputOffset
};
}
}
}());