-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvelope.js
More file actions
33 lines (30 loc) · 995 Bytes
/
envelope.js
File metadata and controls
33 lines (30 loc) · 995 Bytes
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
import { timeCoef } from './util.js'
// Branching one-pole envelope follower. Returns a stateful per-sample detector.
// Separate attack and release time constants; the classic feed-forward compressor
// topology. Peak or RMS detection selectable.
export function envelope(opts = {}) {
let sr = opts.sampleRate || 44100
let aa = timeCoef(opts.attack ?? 5, sr)
let ar = timeCoef(opts.release ?? 50, sr)
let detector = opts.detector || 'peak'
let rmsWin = Math.max(1, opts.rmsWindow || 256)
let rmsBuf = detector === 'rms' ? new Float32Array(rmsWin) : null
let rmsSum = 0
let rmsIdx = 0
let env = 0
return function detect(x) {
let mag
if (detector === 'rms') {
let sq = x * x
rmsSum += sq - rmsBuf[rmsIdx]
rmsBuf[rmsIdx] = sq
rmsIdx = (rmsIdx + 1) % rmsWin
mag = Math.sqrt(Math.max(0, rmsSum / rmsWin))
} else {
mag = x < 0 ? -x : x
}
let c = mag > env ? aa : ar
env = c * env + (1 - c) * mag
return env
}
}