-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.js
75 lines (58 loc) · 1.84 KB
/
processor.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
class SampleGrabber extends AudioWorkletProcessor {
constructor() {
super();
this.input_idx = 0;
this.channel = 'left';
this.mid_side = 0.0;
this.mid_buffer = new Float32Array(128);
this.side_buffer = new Float32Array(128);
this.port.onmessage = e => {
this.channel = e.data.channel;
this.mid_side = e.data.mid_side;
};
}
onmessage(e) {
}
process(inputs, outputs, parameters) {
const in_idx = Math.min(this.input_idx, inputs.length - 1);
const input = inputs[in_idx];
const output = outputs[0];
let buffer = undefined;
if (input.length === 1) {
output[0].set(input[0]);
output[1].set(input[0]);
buffer = input[0];
} else if (input.length === 2) {
buffer = this.mixStereo(input[0], input[1]);
output[0].set(input[0]);
output[1].set(input[1]);
}
this.port.postMessage(buffer);
return true;
}
mixStereo(left, right) {
for (let i = 0; i < left.length; i++) {
const l = left[i];
const r = right[i];
let m = (l + r);
let s = (l - r);
const factor = this.mid_side * 0.5 + 0.5;
m *= 1 - factor;
s *= factor;
this.mid_buffer[i] = m;
this.side_buffer[i] = s;
left[i] = (m + s);
right[i] = (m - s);
}
if (this.channel === 'left') {
return left;
} else if (this.channel === 'right') {
return right;
} else if (this.channel === 'mid') {
return this.mid_buffer;
} else if (this.channel === 'side') {
return this.side_buffer;
}
}
}
registerProcessor('sample-grabber', SampleGrabber);