-
Notifications
You must be signed in to change notification settings - Fork 3
/
SegmentProcessorV3.js
496 lines (354 loc) · 11.2 KB
/
SegmentProcessorV3.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* params: {frameSize: Number, bpmTimeline: BPMTimeline, initialBPM: Number}
*/
function SegmentProcessorV3(params) {
var position;
var midBufferL = [];
var midBufferR = [];
var FRAME_SIZE = params.frameSize || 4096;
var olaL = new OLATS(FRAME_SIZE);
var olaR = new OLATS(FRAME_SIZE);
var intervals = [{ tlStart: 0 }];
Object.defineProperties(intervals[0], {
"start": {
get: function() { return Math.round(bpmTimeline.time(this.tlStart) * 44100); }
},
"end": {
get: function() {
if (this.segDuration != undefined)
return Math.round(bpmTimeline.time(this.tlStart + this.segDuration) * 44100);
else
return undefined
}
},
"duration": {
get: function() {
var end = this.end;
if (end != undefined)
return this.end - this.start;
else
return Number.MAX_VALUE;
}
}
});
var currentInterval = 0;
var il = new Float32Array(FRAME_SIZE);
var ir = new Float32Array(FRAME_SIZE);
var zeros = new Float32Array(FRAME_SIZE * 3);
var audioData = {};
var currentTime = 0;
var bpmTimeline = params.bpmTimeline || new BPMTimeline(params.initialBPM);
this.set_current_time = function(newBeatTime) {
currentInterval = undefined;
currentTime = Math.round(bpmTimeline.time(newBeatTime) * 44100);
for (var i=0; i<intervals.length; i++) {
if (intervals[i].tlStart > newBeatTime) {
currentInterval = i-1;
break;
}
}
if (currentInterval == undefined) {
currentInterval = intervals.length - 1;
position = 0;
return;
}
var b0 = intervals[currentInterval].segStart;
var tlt0 = intervals[currentInterval].tlStart;
var id = intervals[currentInterval].audioId;
var t0 = Math.round(audioData[id].beats[b0][0] * 44100);
position = t0 + (currentTime - Math.round(bpmTimeline.time(tlt0) * 44100));
}
this.get_current_time = function(units) {
if (units == undefined || units == "seconds") {
return currentTime / 44100;
} else if (units == "beats") {
return bpmTimeline.beat(currentTime / 44100);
} else {
throw "Unsupported time units";
}
}
this.process = function(outputAudioBuffer) {
// If no intervals to be played are specified, no need to process anything.
if (intervals.length == 0 || currentInterval >= intervals.length) {
outputAudioBuffer.getChannelData(0).set(zeros.subarray(0, outputAudioBuffer.length), 0);
outputAudioBuffer.getChannelData(1).set(zeros.subarray(0, outputAudioBuffer.length), 0);
currentTime += outputAudioBuffer.length;
return;
}
while (midBufferL.length <= outputAudioBuffer.length && midBufferR.length <= outputAudioBuffer.length) {
if (position==undefined)
position = intervals[currentInterval].start;
var midAlpha = 0;
var inputSamplesCount = 0;
var _position = position;
/*
* From the current interval, try to obtain FRAME_SIZE samples. If there are less than
* FRAME_SIZE samples in the current interval, obtain samples from the next intervals.
*/
for (var i=currentInterval; inputSamplesCount < FRAME_SIZE && i<intervals.length; i++) {
if (_position==undefined)
_position = intervals[i].start;
var incr = Math.min(FRAME_SIZE - inputSamplesCount, intervals[i].duration);
if (audioData[intervals[i].audioId]) {
var buffer = audioData[intervals[i].audioId].buffer;
il.set(buffer.getChannelData(0).subarray(_position, _position + incr), inputSamplesCount);
ir.set(buffer.getChannelData(1).subarray(_position, _position + incr), inputSamplesCount);
midAlpha += (incr/FRAME_SIZE) * (intervals[i].bpm / bpmTimeline.bpm_at_time(currentTime + incr));
} else {
il.set(zeros.subarray(0, incr), inputSamplesCount);
ir.set(zeros.subarray(0, incr), inputSamplesCount);
midAlpha += (incr/FRAME_SIZE) * 1
}
inputSamplesCount += incr;
_position = undefined;
}
/*
* If there are no
*/
if (inputSamplesCount < FRAME_SIZE) {
il.set(zeros.subarray(0, FRAME_SIZE - inputSamplesCount), inputSamplesCount);
ir.set(zeros.subarray(0, FRAME_SIZE - inputSamplesCount), inputSamplesCount);
}
var alpha = midAlpha;
olaL.set_alpha(alpha);
olaR.set_alpha(alpha);
if (olaL.is_clean()) {
olaL.process(il);
olaR.process(ir);
} else {
midBufferL = midBufferL.concat(olaL.process(il));
midBufferR = midBufferR.concat(olaR.process(ir));
}
var hop = olaL.get_ra();
var newPosition = position + hop;
if (newPosition > intervals[currentInterval].end) {
var oldIntervalEnd = intervals[currentInterval].end;
currentInterval++;
if (intervals[currentInterval]) {
position = intervals[currentInterval].start + newPosition - oldIntervalEnd;
} else {
position = undefined;
// Cleaning OLATS buffers and the output mid buffers is a good idea because
// to avoid certain problems with artifacts.
// "But what are the artifacts???", the young & innocent mind might ask.
// Experimentation and mistery are part of our lifes.
olaL.clear_buffers();
olaR.clear_buffers();
midBufferL = [];
midBufferR = [];
break;
}
} else {
position = newPosition;
}
}
var ol = outputAudioBuffer.getChannelData(0);
var or = outputAudioBuffer.getChannelData(1);
for (var i=0; i<outputAudioBuffer.length; i++, currentTime++) {
ol[i] = midBufferL.shift();
or[i] = midBufferR.shift();
}
}
this.set_current_interval = function(index, pos) {
currentInterval = index;
var newPos = Math.min(pos, intervals[index].start);
if (!isNaN(newPos) && newPos != undefined)
position = newPos;
else
position = intervals[index].start;
}
/*
* params: { segId: String, tlStart: Number (beat), segStart: Number (beat), segEnd: Number (beat) }
*/
this.add_interval = function(params) {
var p = params;
if (params.tlStart < 0)
throw "tlStart is below 0";
var numToRemove = 0;
var I;
var done = false;
for (var i=0; !done && i<intervals.length; i++) {
if (intervals[i].tlStart == p.tlStart) {
I = i
var newSegEnd = p.tlStart + p.segDuration;
var oldSegEnd = intervals[i].tlStart + intervals[i].segDuration;
if (newSegEnd == oldSegEnd) {
// When both intervals have the same size, start and end, remove the old one.
numToRemove = 1;
done = true;
} else if (newSegEnd < oldSegEnd) {
intervals[i].tlStart = newSegEnd;
numToRemove = 0;
done = true;
} else if (newSegEnd > oldSegEnd) {
numToRemove++;
for (var j=i+1; !done && j<intervals.length; j++) {
var oldSegEnd = intervals[j].tlStart + intervals[j].segDuration;
if (newSegEnd > oldSegEnd) {
numToRemove++;
} else if (newSegEnd == oldSegEnd) {
numToRemove++;
done = true;
} else if (newSegEnd < oldSegEnd) {
intervals[j].tlStart = newSegEnd;
done = true;
} else {
intervals[j].tlStart = newSegEnd
done = true;
}
}
} else {
intervals[i].tlStart = newSegEnd;
numToRemove = 0;
done = true;
}
} else if (intervals[i].tlStart > p.tlStart) {
intervals[i-1].segDuration = p.tlStart - intervals[i-1].tlStart;
I = i;
if (p.tlStart + p.segDuration < intervals[i].tlStart) {
var obj = {
tlStart: p.tlStart + p.segDuration,
segStart: intervals[i-1].segStart,
segDuration: intervals[i].tlStart - p.tlStart + p.segDuration,
segId: intervals[i-1].segId,
audioId: intervals[i-1].audioId,
bpm: intervals[i-1].bpm
};
Object.defineProperties(obj, {
"start": {
get: function() { return Math.round(audioData[this.audioId].beats[this.segStart][0] * 44100); }
},
"end": {
get: function() { return Math.round(audioData[this.audioId].beats[this.segStart + this.segDuration][0] * 44100); }
},
"duration": {
get: function() { return this.end - this.start; }
}
});
intervals.splice(i, 0, obj);
numToRemove = 0;
done = true;
} else if (p.tlStart + p.segDuration == intervals[i].tlStart) {
numToRemove = 0;
done = true;
} else if (p.tlStart + p.segDuration > intervals[i].tlStart) {
for (var j=i+1; !done && j<intervals.length; j++) {
var oldSegEnd = intervals[j].tlStart + intervals[j].segDuration;
if (newSegEnd > oldSegEnd) {
numToRemove++;
} else if (newSegEnd == oldSegEnd) {
numToRemove++;
done = true;
} else if (newSegEnd < oldSegEnd) {
intervals[j].tlStart = newSegEnd;
done = true;
} else {
intervals[j].tlStart = newSegEnd
done = true;
}
}
}
}
}
var obj0 = {
tlStart : p.tlStart,
segStart : p.segStart,
segDuration : p.segDuration,
segId : p.segId,
audioId : p.audioId,
bpm : p.bpm,
};
Object.defineProperties(obj0, {
"start": {
get: function() {
return Math.round(audioData[this.audioId].beats[this.segStart][0] * 44100);
}
},
"end": {
get: function() {
return Math.round(audioData[this.audioId].beats[this.segStart + this.segDuration][0] * 44100);
}
},
"duration": {
get: function() {
return this.end - this.start;
}
}
});
if (I == undefined) {
intervals[i-1].segDuration = p.tlStart - intervals[i-1].tlStart;
var obj1 = { tlStart : p.tlStart + p.segDuration };
Object.defineProperties(obj1, {
"start": {
get: function() { return Math.round(bpmTimeline.time(this.tlStart) * 44100); }
},
"end": {
get: function() {
if (this.segDuration != undefined)
return Math.round(bpmTimeline.time(this.tlStart + this.segDuration) * 44100);
else
return undefined
}
},
"duration": {
get: function() {
var end = this.end;
if (end != undefined)
return this.end - this.start;
else
return Number.MAX_VALUE;
}
}
});
intervals.splice(intervals.length, numToRemove, obj0);
intervals.splice(intervals.length, 0, obj1);
} else {
intervals.splice(I, numToRemove, obj0);
}
}
this.remove_interval = function(index) {
// TODO
}
this.get_intervals = function() {
var _intervals = new Array();
for (var i=0; i<intervals.length; i++) {
if (intervals[i].audioId != undefined) {
_intervals[_intervals.length] = {};
for (var k in intervals[i])
_intervals[_intervals.length-1][k] = intervals[i][k];
}
}
return _intervals;
}
/*
* params: {id, audioBuffer, beats}
* "audioBuffer" is an AudioBuffer.
* "beats" is an Array where each entry is another Array with the following data:
* [TIME, BPM]
*/
this.add_audio_data = function(params) {
audioData[params.id] = {
buffer: params.audioBuffer,
beats : params.beats
};
}
this.remove_audio_data = function(id) {
if (audioData[id]) {
delete audioData[id];
var _intervals = this.get_intervals();
intervals = new Array({ tlStart: 0, segStart: 0 });
for (var i=0; i<_intervals.length; i++) {
if (_intervals[i].audioId != id) {
this.add_interval({
segId: _intervals[i].segId,
audioId: _intervals[i].audioId,
tlStart: _intervals[i].tlStart,
segStart: _intervals[i].segStart,
segDuration: _intervals[i].segDuration
})
}
}
this.set_current_time(bpmTimeline.beat(currentTime));
}
}
}