-
Notifications
You must be signed in to change notification settings - Fork 3
/
SegmentProcessor.js
167 lines (131 loc) · 4.71 KB
/
SegmentProcessor.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
function SegmentProcessor(audioData, frameSize) {
var position = 0;
var midBufferL = [];
var midBufferR = [];
var olaL = new OLATS(frameSize);
var olaR = new OLATS(frameSize);
var buffer = audioData;
var FRAME_SIZE = frameSize;
var intervals = [];
var currentInterval;
var refBPM;
var il = new Float32Array(FRAME_SIZE);
var ir = new Float32Array(FRAME_SIZE);
var zeros = new Float32Array(FRAME_SIZE * 2);
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);
return;
}
while (midBufferL.length <= outputAudioBuffer.length && midBufferR.length <= outputAudioBuffer.length) {
if (position==undefined)
position = intervals[currentInterval].start;
var midBPM = 0;
var inputSamplesCount = 0;
var _position = position;
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].end - intervals[i].start);
il.set(buffer.getChannelData(0).subarray(_position, _position + incr), inputSamplesCount);
ir.set(buffer.getChannelData(1).subarray(_position, _position + incr), inputSamplesCount);
midBPM += (incr/FRAME_SIZE) * intervals[i].bpm;
inputSamplesCount += incr;
_position = undefined;
}
il.set(zeros.subarray(0,FRAME_SIZE-inputSamplesCount), inputSamplesCount);
ir.set(zeros.subarray(0,FRAME_SIZE-inputSamplesCount), inputSamplesCount);
var bpm = (refBPM)? refBPM : midBPM;
var alpha = midBPM / bpm;
olaL.set_alpha(alpha);
olaR.set_alpha(alpha);
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++) {
ol[i] = midBufferL.shift();
or[i] = midBufferR.shift();
}
}
this.clear = function() {
// TODO
}
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;
}
this.add_interval = function(params) {
var i = (params.index==undefined)? intervals.length : params.index;
// intervals.splice(i, 0, [params.start, params.end, params.bpm, params.id]);
intervals.splice(i, 0, {
start: params.start,
end: params.end,
bpm: params.bpm,
segId: params.id,
silence: params.silence || false
});
if (intervals.length == 1) {
currentInterval = 0;
position = intervals[0].start;
}
}
this.remove_interval = function(index) {
intervals.splice(index,1);
}
this.remove_interval_q = function(selectorToRemoveFn) {
//TODO
for (var i=0; i<intervals.length; i++) {
if (selectToRemoveFn(intervals[i])) {
//TODO
}
}
}
this.get_intervals = function() {
var _intervals = new Array(intervals.length);
for (var i=0; i<intervals.length; i++) {
_intervals[i] = {};
for (var k in intervals[i])
_intervals[i][k] = intervals[i][k];
}
return _intervals;
}
this.set_audio_data = function(audioData) {
buffer = audioData;
position = 0;
olaL.clear();
olaR.clear();
intervals = [];
}
this.set_reference_bpm = function(newBPM) {
refBPM = newBPM;
}
}