-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecorderWorklet.ts
48 lines (39 loc) · 1.3 KB
/
RecorderWorklet.ts
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
import Constants from "../../model/Constants";
class RecorderWorklet extends AudioWorkletProcessor {
private recording = false;
constructor() {
super();
this.port.onmessage = (event) => {
if (event.data == "stop") {
this.recording = false;
} else if(event.data == "record") {
this.recording = true;
}
};
}
static get parameterDescriptors() {
return [
{ name: "numChannels", defaultValue: 2 }
];
}
process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Record<string, Float32Array>): boolean {
if (!this.recording) return true;
const input = inputs[0];
const buffer: Float32Array[] = [];
if (input && input.length > 0) {
for (let channel = 0; channel < parameters.numChannels[0]; channel++) {
if (input[channel]) {
buffer.push(input[channel]);
} else {
buffer.push(input[0]);
}
}
this.port.postMessage({
command: "record",
buffer: buffer
});
}
return true;
}
}
registerProcessor(Constants.WORKLET_NAMES.RECORDER_WORKLET, RecorderWorklet);