-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_pv_worker_2.js
76 lines (55 loc) · 1.62 KB
/
test_pv_worker_2.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
var BUFFER_SIZE = 2048;
var context = new AudioContext();
var buffer = context.createBuffer(2, BUFFER_SIZE, context.sampleRate);
var node = context.createScriptProcessor(BUFFER_SIZE, 2, 2);
var myWorker = new Worker('pv_worker_2.js');
var alpha = 1;
myWorker.onmessage = function(e) {
bufL = bufL.concat(e.data.left);
bufR = bufR.concat(e.data.right);
node.connect(context.destination);
}
var bufL = [];
var bufR = [];
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) {
var o = {
left: decodedData.getChannelData(0),
right: decodedData.getChannelData(1)
}
myWorker.postMessage(o, [o.left.buffer, o.right.buffer]);
});
}
console.log('reading url');
request.send();
}
loadSample('../soundtouchjs/2.mp3');
node.onaudioprocess = function (e) {
// console.log("MAIN THREAD PROCESSING");
if (bufL.length<BUFFER_SIZE) {
// pause();
myWorker.postMessage({
type: "process",
alpha: alpha
});
return;
}
var ol = e.outputBuffer.getChannelData(0);
var or = e.outputBuffer.getChannelData(1);
ol.set(bufL.splice(0, BUFFER_SIZE));
or.set(bufR.splice(0, BUFFER_SIZE));
};
function play() {
node.connect(context.destination);
}
function pause() {
node.disconnect();
}
function setAlpha(newAlpha) {
alpha = newAlpha;
}