@audio/dynamics-envelope 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/envelope.js +33 -0
- package/package.json +41 -0
package/envelope.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { timeCoef } from '@audio/dynamics-core'
|
|
2
|
+
|
|
3
|
+
// Branching one-pole envelope follower. Returns a stateful per-sample detector.
|
|
4
|
+
// Separate attack and release time constants; the classic feed-forward compressor
|
|
5
|
+
// topology. Peak or RMS detection selectable.
|
|
6
|
+
export function envelope(opts = {}) {
|
|
7
|
+
let sr = opts.sampleRate || 44100
|
|
8
|
+
let aa = timeCoef(opts.attack ?? 5, sr)
|
|
9
|
+
let ar = timeCoef(opts.release ?? 50, sr)
|
|
10
|
+
let detector = opts.detector || 'peak'
|
|
11
|
+
let rmsWin = Math.max(1, opts.rmsWindow || 256)
|
|
12
|
+
|
|
13
|
+
let rmsBuf = detector === 'rms' ? new Float32Array(rmsWin) : null
|
|
14
|
+
let rmsSum = 0
|
|
15
|
+
let rmsIdx = 0
|
|
16
|
+
let env = 0
|
|
17
|
+
|
|
18
|
+
return function detect(x) {
|
|
19
|
+
let mag
|
|
20
|
+
if (detector === 'rms') {
|
|
21
|
+
let sq = x * x
|
|
22
|
+
rmsSum += sq - rmsBuf[rmsIdx]
|
|
23
|
+
rmsBuf[rmsIdx] = sq
|
|
24
|
+
rmsIdx = (rmsIdx + 1) % rmsWin
|
|
25
|
+
mag = Math.sqrt(Math.max(0, rmsSum / rmsWin))
|
|
26
|
+
} else {
|
|
27
|
+
mag = x < 0 ? -x : x
|
|
28
|
+
}
|
|
29
|
+
let c = mag > env ? aa : ar
|
|
30
|
+
env = c * env + (1 - c) * mag
|
|
31
|
+
return env
|
|
32
|
+
}
|
|
33
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/dynamics-envelope",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Branching one-pole envelope follower. Returns a stateful per-sample detector",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "envelope.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./envelope.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"envelope.js"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@audio/dynamics-core": "^0.1.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"audio",
|
|
20
|
+
"dsp",
|
|
21
|
+
"dynamics",
|
|
22
|
+
"envelope"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/audiojs/dynamics.git",
|
|
29
|
+
"directory": "packages/dynamics-envelope"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/audiojs/dynamics/tree/main/packages/dynamics-envelope",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/audiojs/dynamics/issues"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|