File size: 1,174 Bytes
b2ecf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// for js/src/lib/components/InferenceWidget/shared/WidgetRealtimeRecorder/Recorder.ts
class AudioDataExtractor extends AudioWorkletProcessor {
	_updateIntervalInMS;
	_sampleInFrames;
	_index;
	_buffer;

	constructor() {
		super();
		this._updateIntervalInMS = 50;
		this._sampleInFrames = parseInt((this._updateIntervalInMS / 1000.0) * sampleRate);
		this._index = 0;
		this._buffer = new Float32Array(this._sampleInFrames);
	}

	process(inputs, outputs, parameters) {
		// Note that the input will be down-mixed to mono; however, if no inputs are
		// connected then zero channels will be passed in.
		if (inputs.length > 0 && inputs[0].length > 0) {
			const rest = this._buffer.length - this._index;
			if (rest < inputs[0][0].length) {
				this._buffer.set(inputs[0][0].slice(0, rest), this._index);
				this.port.postMessage({
					buffer: this._buffer.slice(0),
					sampling_rate: sampleRate,
				});
				this._buffer.fill(0);
				this._index = inputs[0][0].length - rest;
			} else {
				this._buffer.set(inputs[0][0], this._index);
				this._index += inputs[0][0].length;
			}
		}

		return true;
	}
}

registerProcessor("AudioDataExtractor", AudioDataExtractor);