-
-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathrecorderProcessor.js
95 lines (80 loc) · 2.47 KB
/
recorderProcessor.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
// import processor name via preval.require so that it's available as a value at compile time
const processorNames = preval.require('./processorNames');
class RecorderProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
const processorOptions = options.processorOptions || {};
this.numInputChannels = processorOptions.numInputChannels || 2;
this.recording = false;
this.clear();
this.port.onmessage = (event) => {
const data = event.data;
if (data.name === 'start') {
this.record(data.duration);
} else if (data.name === 'stop') {
this.stop();
}
};
}
process(inputs, outputs) {
if (!this.recording) {
return true;
} else if (this.sampleLimit && this.recordedSamples >= this.sampleLimit) {
this.stop();
return true;
}
const input = inputs[0];
const output = outputs[0];
for (let channel = 0; channel < output.length; ++channel) {
const inputChannel = input[channel];
if (channel === 0) {
this.leftBuffers.push(inputChannel);
if (this.numInputChannels === 1) {
this.rightBuffers.push(inputChannel);
}
} else if (channel === 1 && this.numInputChannels > 1) {
this.rightBuffers.push(inputChannel);
}
}
this.recordedSamples += output[0].length;
return true;
}
record(duration) {
if (duration) {
this.sampleLimit = Math.round(duration * sampleRate);
}
this.recording = true;
}
stop() {
this.recording = false;
const buffers = this.getBuffers();
const leftBuffer = buffers[0].buffer;
const rightBuffer = buffers[1].buffer;
this.port.postMessage({ name: 'buffers', leftBuffer: leftBuffer, rightBuffer: rightBuffer }, [leftBuffer, rightBuffer]);
this.clear();
}
getBuffers() {
const buffers = [];
buffers.push(this.mergeBuffers(this.leftBuffers));
buffers.push(this.mergeBuffers(this.rightBuffers));
return buffers;
}
mergeBuffers(channelBuffer) {
let result = new Float32Array(this.recordedSamples);
let offset = 0;
let lng = channelBuffer.length;
for (var i = 0; i < lng; i++) {
let buffer = channelBuffer[i];
result.set(buffer, offset);
offset += buffer.length;
}
return result;
}
clear() {
this.leftBuffers = [];
this.rightBuffers = [];
this.recordedSamples = 0;
this.sampleLimit = null;
}
}
registerProcessor(processorNames.recorderProcessor, RecorderProcessor);