/
dc_remove_worker.ts
50 lines (41 loc) · 1.25 KB
/
dc_remove_worker.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
49
50
class DcRemoveProcessor extends AudioWorkletProcessor {
private stopped = false
private dc: Float32Array = new Float32Array()
constructor() {
super()
this.port.onmessage = (ev) => {
switch (ev.data.action) {
case 'stop':
this.stopped = true
break
}
}
}
public process(inputs: Float32Array[][], outputs: Float32Array[][], _parameters: Record<string, Float32Array>): boolean {
if (this.stopped)
return false
const output = outputs[0]
const numberOfChannels = output.length
const input = inputs[0]
if (this.dc.length !== numberOfChannels) {
this.dc = new Float32Array(numberOfChannels)
this.dc.fill(0)
}
for (let channel = 0; channel < numberOfChannels; ++channel) {
const inCh = input[channel]
if (inCh == null) {
this.dc[channel] = 0
continue
}
let dc = this.dc[channel]
const sum = input[0].reduce((acc, val) => acc + val, 0.0)
dc += (sum / input[0].length - dc) * (1.0 / 32)
this.dc[channel] = dc
const outCh = output[channel]
for (let i = 0, len = inCh.length; i < len; ++i)
outCh[i] = inCh[i] - dc
}
return true
}
}
registerProcessor('dc_remove_worklet', DcRemoveProcessor)