-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
225 lines (161 loc) · 6.5 KB
/
test.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
var BUFFER_SIZE = 2048;
var div = 1;
var context = new AudioContext();
var masterNode = context.createScriptProcessor(BUFFER_SIZE*4, 2, 2);
var nodes = [];
var eqs = [];
loadSample = function(url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
console.log('url loaded');
context.decodeAudioData(request.response, function(decodedData) {
console.log('decoded data');
var I = nodes.length;
var N = nodes[I] = context.createScriptProcessor(BUFFER_SIZE, 2, 2);
masterNode.connect(nodes[I]);
eqs[I] = new Equalizer(nodes[I], context);
N.buffer = decodedData;
N.pvL = new PhaseVocoder(BUFFER_SIZE/div, 44100); N.pvL.init();
N.pvR = new PhaseVocoder(BUFFER_SIZE/div, 44100); N.pvR.init();
N.outBufferL = [];
N.outBufferR = [];
N.position = 0;
N.pitch = 1;
N.onaudioprocess = function (e) {
var il = this.buffer.getChannelData(0);
var ir = this.buffer.getChannelData(1);
var ol = e.outputBuffer.getChannelData(0);
var or = e.outputBuffer.getChannelData(1);
// Fill output buffers (left & right) until the system has
// enough processed samples to reproduce.
do {
// var bufL = new Float64Array(BUFFER_SIZE/div);
// var bufR = new Float64Array(BUFFER_SIZE/div);
var bufL = il.subarray(this.position, this.position+BUFFER_SIZE/div);
var bufR = ir.subarray(this.position, this.position+BUFFER_SIZE/div);
this.position += this.pvL.get_analysis_hop();
// Process left input channel
this.outBufferL = this.outBufferL.concat(this.pvL.process(bufL));
// Process right input channel
this.outBufferR = this.outBufferR.concat(this.pvR.process(bufR));
} while(this.outBufferL.length < BUFFER_SIZE);
ol.set(this.outBufferL.splice(0, BUFFER_SIZE));
or.set(this.outBufferR.splice(0, BUFFER_SIZE));
};
});
}
console.log('reading url');
request.send();
}
// loadSample('../soundtouchjs/4.mp3');
// loadSample('../soundtouchjs/2.mp3');
// loadSample('../soundtouchjs/3.mp3');
function set_pitch(newPitch) {
pitch = phasevocoderL1.get_synthesis_hop()*newPitch / phasevocoderL1.get_analysis_hop();
phasevocoderL1.set_overlap_factor(pitch);
phasevocoderR1.set_overlap_factor(pitch);
}
function set_alpha(ids, newFactor) {
for (var i=0; i<ids.length; i++) {
nodes[i].pvL.set_alpha(newFactor);
nodes[i].pvR.set_alpha(newFactor);
}
}
function set_position(ids, newPosition) {
for (var i=0; i<ids.length; i++) {
nodes[i].position = newPosition;
}
}
function play(ids) {
for (var i=0; i<ids.length; i++)
eqs[ids[i]].connect();
}
function pause(ids) {
for (var i=0; i<ids.length; i++)
eqs[ids[i]].disconnect();
}
function process_samples(input_start, buffer_size, input_channels, output_start, output_channels, rate) {
var beat, destination_offset, sample_l, sample_r, source_offset, source_offset_float;
while (--buffer_size >= 0) {
source_offset_float = input_start + (buffer_size * rate);
source_offset = Math.round(source_offset_float);
destination_offset = output_start + buffer_size;
sample_l = input_channels[0][source_offset];
sample_r = input_channels[1][source_offset];
output_channels[0][destination_offset] = sample_l;
output_channels[1][destination_offset] = sample_r;
}
return null;
};
function resample(buffer, fromRate /* or speed */, fromFrequency /* or toRate */, toRate, toFrequency) {
var argc = arguments.length,
speed = (argc === 2 ? fromRate : (argc === 3 ? fromRate / fromFrequency : toRate / fromRate * toFrequency / fromFrequency)),
l = buffer.length,
length = Math.ceil(l / speed),
newBuffer = new Array(length),
i, n;
for (i=0, n=0; i<l; i += speed) {
newBuffer[n++] = linear_interpolation(buffer, i);
}
return newBuffer;
};
function nearest_interpolation(arr, pos) {
return pos >= arr.length - 0.5 ? arr[0] : arr[Math.round(pos)];
};
function linear_interpolation(arr, pos) {
var first = Math.floor(pos),
second = first + 1,
frac = pos - first;
second = second < arr.length ? second : 0;
return arr[first] * (1 - frac) + arr[second] * frac;
};
function linearInterpolation (a, b, t) {
return a + (b - a) * t;
};
function hannWindow (length) {
var window = new Float32Array(length);
for (var i = 0; i < length; i++) {
window[i] = 0.5 * (1 - Math.cos(2 * Math.PI * i / (length - 1)));
}
return window;
};
grainSize = 512;
overlapRatio = 0.70;
pitchShifterProcessor = context.createScriptProcessor(grainSize, 1, 1);
pitchShifterProcessor.buffer = new Float32Array(grainSize * 2);
pitchShifterProcessor.grainWindow = hannWindow(grainSize);
pitchRatio = 1;
pitchShifterProcessor.onaudioprocess = function (event) {
var inputData = event.inputBuffer.getChannelData(0);
var outputData = event.outputBuffer.getChannelData(0);
for (i = 0; i < inputData.length; i++) {
// Apply the window to the input buffer
inputData[i] *= this.grainWindow[i];
// Shift half of the buffer
this.buffer[i] = this.buffer[i + grainSize];
// Empty the buffer tail
this.buffer[i + grainSize] = 0.0;
}
// Calculate the pitch shifted grain re-sampling and looping the input
var grainData = new Float32Array(grainSize * 2);
for (var i = 0, j = 0.0;
i < grainSize;
i++, j += pitchRatio) {
var index = Math.floor(j) % grainSize;
var a = inputData[index];
var b = inputData[(index + 1) % grainSize];
grainData[i] += linearInterpolation(a, b, j % 1.0) * this.grainWindow[i];
}
// Copy the grain multiple times overlapping it
for (i = 0; i < grainSize; i += Math.round(grainSize * (1 - overlapRatio))) {
for (j = 0; j <= grainSize; j++) {
this.buffer[i + j] += grainData[j];
}
}
// Output the first half of the buffer
for (i = 0; i < grainSize; i++) {
outputData[i] = this.buffer[i];
}
};