-
Notifications
You must be signed in to change notification settings - Fork 7
/
pv_worker.js
86 lines (64 loc) · 2.15 KB
/
pv_worker.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
importScripts("jensnockert-fft.js/lib/complex.js", "jensnockert-fft.js/lib/real.js", "PV_fast.js");
var BUFFER_SIZE = 2048;
var div = 1;
var sample_rate = 44100;
var pvL = new PhaseVocoder(BUFFER_SIZE/div, sample_rate); pvL.init();
var pvR = new PhaseVocoder(BUFFER_SIZE/div, sample_rate); pvR.init();
var pv3 = new PhaseVocoder(BUFFER_SIZE/div, sample_rate); pv3.init();
var audioDataL = [];
var audioDataR = [];
var myInterval;
var outBufferL = [];
var outBufferR = [];
var position = 0;
var alphas = [];
var last_alpha = 1;
/*
* Message format: [type, channel, op-data1, ..., op-dataN]
*/
onmessage = function(e) {
var type = e.data.type;
// console.log(e);
if (type=="set-alpha") {
alphas.push(e.data.alpha);
console.log("added new alpha: " + e.data.alpha);
} else if (type=="play") {
myInterval = setInterval(function () {
// console.log("processing");
do {
if (alphas.length!=0) {
last_alpha = alphas.splice(0,1)[0];
console.log("using new alpha: " + last_alpha);
}
pvL.set_alpha(last_alpha);
pvR.set_alpha(last_alpha);
var bufL = new Float32Array(BUFFER_SIZE);
var bufR = new Float32Array(BUFFER_SIZE);
bufL = audioDataL.subarray(position, position+BUFFER_SIZE);
bufR = audioDataR.subarray(position, position+BUFFER_SIZE);
position += pvL.get_analysis_hop();
// Process left input channel
outBufferL = outBufferL.concat(pvL.process(bufL));
// Process right input channel
outBufferR = outBufferR.concat(pvR.process(bufR));
} while(outBufferL.length < BUFFER_SIZE);
postMessage({
left: outBufferL.splice(0,BUFFER_SIZE),
right: outBufferR.splice(0,BUFFER_SIZE)
})
// console.log("done");
}, 1/(1024/sample_rate));
} else if (type=="pause") {
clearInterval(myInterval);
} else if (type=="position") {
outBufferL = [];
outBufferR = [];
position = 0;
pvL.reset_phases_and_overlap_buffers();
pvR.reset_phases_and_overlap_buffers();
} else {
audioDataL = new Float32Array(e.data.left);
audioDataR = new Float32Array(e.data.right);
console.log("loaded into the worker");
}
}