-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.js
More file actions
41 lines (37 loc) · 1.38 KB
/
compressor.js
File metadata and controls
41 lines (37 loc) · 1.38 KB
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
import { envelope } from './envelope.js'
import { writer, concat, db2lin, lin2db } from './util.js'
// Feed-forward soft-knee compressor (Giannoulis-Massberg topology).
// Envelope → log domain → gain curve → apply to input.
export default function compressor(data, opts) {
if (!(data instanceof Float32Array)) return writer(compressorStream(data))
let s = compressorStream(opts)
return concat(s.write(data), s.flush())
}
export function compressorStream(opts = {}) {
let threshold = opts.threshold ?? -20
let ratio = opts.ratio ?? 4
let knee = opts.knee ?? 6
let makeupDb = opts.makeup ?? 0
let env = envelope(opts)
return {
write(chunk) {
let out = new Float32Array(chunk.length)
for (let i = 0; i < chunk.length; i++) {
let x = chunk[i]
let rDb = compressorGain(lin2db(env(x)), threshold, ratio, knee)
out[i] = x * db2lin(rDb + makeupDb)
}
return out
},
flush() { return new Float32Array(0) }
}
}
// Soft-knee downward compression curve. Returns gain reduction in dB (≤ 0).
// Below (T - W/2): no compression. Above (T + W/2): full ratio. In knee: quadratic.
export function compressorGain(levelDb, threshold, ratio, kneeDb) {
let d = levelDb - threshold
if (d < -kneeDb / 2) return 0
if (d > kneeDb / 2) return -(d * (1 - 1 / ratio))
let x = d + kneeDb / 2
return -(1 - 1 / ratio) * x * x / (2 * kneeDb)
}