@audio/dynamics 0.2.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.
Files changed (5) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +276 -0
  3. package/index.d.ts +104 -0
  4. package/index.js +19 -0
  5. package/package.json +81 -0
package/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ MIT License
2
+
3
+ Copyright (c) Dmitry Iv
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ This work is offered under the Krishnized License (https://github.com/krishnized/license).
package/README.md ADDED
@@ -0,0 +1,276 @@
1
+ ## @audio/dynamics [![test](https://github.com/audiojs/dynamics/actions/workflows/test.yml/badge.svg)](https://github.com/audiojs/dynamics/actions/workflows/test.yml) [![npm](https://img.shields.io/npm/v/@audio/dynamics)](https://npmjs.org/dynamics-processor) [![license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/audiojs/dynamics/blob/main/LICENSE)
2
+
3
+ Dynamics processing — compressor, limiter, gate, expander, de-esser, ducker, softclip, compand. All built on a single branching envelope follower; differences are purely in the gain curve. Part of [audiojs](https://github.com/audiojs).
4
+
5
+ | | Kind | Gain function | Typical use |
6
+ |---|---|---|---|
7
+ | [compressor](#compressor) | envelope | soft-knee above threshold | leveling vocals, mix glue |
8
+ | [limiter](#limiter) | lookahead | brickwall at ceiling | master bus, peak control |
9
+ | [gate](#gate) | envelope | hard cut below threshold | silence between phrases |
10
+ | [expander](#expander) | envelope | gentle below-threshold reduction | soft gating, noise bed shaping |
11
+ | [deesser](#deesser) | sidechain | compressor on sibilance band | harsh 's' / 't' in voice |
12
+ | [ducker](#ducker) | ext. sidechain | compressor keyed by side signal | music-under-voice, podcast |
13
+ | [softclip](#softclip) | waveshaper | static transfer curve | gentle peak limiting + coloration |
14
+ | [compand](#compand) | envelope | piecewise-linear transfer | SoX-style multi-segment |
15
+
16
+
17
+ ## Usage
18
+
19
+ ```
20
+ npm install @audio/dynamics
21
+ ```
22
+
23
+ ```js
24
+ import { compressor, limiter, gate, ducker } from '@audio/dynamics'
25
+
26
+ let glued = compressor(samples, { threshold: -18, ratio: 4, attack: 5, release: 100 })
27
+ let safe = limiter(glued, { ceiling: -0.3, lookahead: 5 })
28
+
29
+ let write = compressor({ threshold: -18, ratio: 4 }) // streaming
30
+ write(block1)
31
+ write(block2)
32
+ write() // → remaining samples
33
+
34
+ let ducked = ducker(music, voice, { threshold: -30, range: -12 })
35
+ ```
36
+
37
+ > Mono `Float32Array` in/out. For stereo, process channels independently or feed a linked detector. Sample rate defaults to 44100; pass `sampleRate` for anything else.
38
+
39
+
40
+ ## envelope
41
+
42
+ Every processor except `softclip` is built on this: a branching one-pole follower with separate attack/release time constants, peak or RMS detection.
43
+
44
+ ```js
45
+ import { envelope } from '@audio/dynamics'
46
+
47
+ let follow = envelope({ attack: 5, release: 100, detector: 'peak' })
48
+ let level = []
49
+ for (let x of samples) level.push(follow(x))
50
+ ```
51
+
52
+ | Param | Default | |
53
+ |---|---|---|
54
+ | `sampleRate` | `44100` | — |
55
+ | `attack` | `5` | ms |
56
+ | `release` | `50` | ms |
57
+ | `detector` | `'peak'` | `'peak'` or `'rms'` |
58
+ | `rmsWindow` | `256` | samples, for RMS detector |
59
+
60
+
61
+ ## compressor
62
+
63
+ Feed-forward soft-knee downward compressor — Giannoulis-Massberg topology. Envelope → log domain → quadratic soft-knee gain curve → linear gain applied to input.
64
+
65
+ ```js
66
+ import { compressor } from '@audio/dynamics'
67
+
68
+ compressor(data, { threshold: -18, ratio: 4 })
69
+ compressor(data, { threshold: -24, ratio: 2, knee: 12, attack: 10, release: 200, makeup: 6 })
70
+ ```
71
+
72
+ | Param | Default | |
73
+ |---|---|---|
74
+ | `threshold` | `-20` | dB |
75
+ | `ratio` | `4` | — |
76
+ | `knee` | `6` | dB (soft-knee width) |
77
+ | `attack` | `5` | ms |
78
+ | `release` | `100` | ms |
79
+ | `makeup` | `0` | dB |
80
+
81
+ **Use when:** vocals, bass, drum bus, mix glue.<br>
82
+ **Not for:** peak control at the master bus — use [limiter](#limiter). Transparent loudness shaping — use [compand](#compand) with gentle slope.
83
+
84
+
85
+ ## limiter
86
+
87
+ Lookahead brickwall limiter. A sliding-window maximum over the lookahead span drives the envelope, so gain reduction always covers every sample in transit — instant attack `lookahead` ms before a peak emerges, exponential release after it passes.
88
+
89
+ ```js
90
+ import { limiter } from '@audio/dynamics'
91
+
92
+ limiter(data, { ceiling: -0.3 })
93
+ limiter(data, { ceiling: -1, lookahead: 10, release: 100 })
94
+ ```
95
+
96
+ | Param | Default | |
97
+ |---|---|---|
98
+ | `ceiling` | `-0.3` | dB (brickwall) |
99
+ | `lookahead` | `5` | ms (introduces delay) |
100
+ | `release` | `50` | ms |
101
+
102
+ **Use when:** master bus ceiling, true-peak safety, preventing inter-sample clipping.<br>
103
+ **Not for:** musical dynamics shaping — use [compressor](#compressor). Low-latency paths — use [softclip](#softclip).
104
+
105
+
106
+ ## gate
107
+
108
+ Noise gate with hold-then-close logic. Below threshold, signal is attenuated by `range` dB. A `hold` timer keeps the gate open after a drop-out to avoid chatter; attack/release smooth the gain transitions.
109
+
110
+ ```js
111
+ import { gate } from '@audio/dynamics'
112
+
113
+ gate(data, { threshold: -40 })
114
+ gate(data, { threshold: -35, range: -80, hold: 20, attack: 1, release: 150 })
115
+ ```
116
+
117
+ | Param | Default | |
118
+ |---|---|---|
119
+ | `threshold` | `-40` | dB |
120
+ | `range` | `-60` | dB attenuation when closed |
121
+ | `hold` | `10` | ms |
122
+ | `attack` | `0.1` | ms (opening) |
123
+ | `release` | `100` | ms (closing) |
124
+
125
+ **Use when:** drum mics, voice dialogue with ambient noise, removing hiss between phrases.<br>
126
+ **Not for:** subtle low-level reduction — use [expander](#expander).
127
+
128
+
129
+ ## expander
130
+
131
+ Downward expander — a softer gate. Below threshold, gain is reduced by `(threshold − level) × (ratio − 1)` dB, clamped at `range`.
132
+
133
+ ```js
134
+ import { expander } from '@audio/dynamics'
135
+
136
+ expander(data, { threshold: -30, ratio: 2 })
137
+ ```
138
+
139
+ | Param | Default | |
140
+ |---|---|---|
141
+ | `threshold` | `-30` | dB |
142
+ | `ratio` | `2` | — |
143
+ | `knee` | `6` | dB |
144
+ | `range` | `-40` | dB, max reduction |
145
+ | `attack` | `5` | ms |
146
+ | `release` | `50` | ms |
147
+
148
+ **Use when:** gentle noise-floor suppression without the abruptness of a gate.<br>
149
+ **Not for:** hard removal of sound between phrases — use [gate](#gate).
150
+
151
+
152
+ ## deesser
153
+
154
+ Sibilance compressor. A biquad bandpass drives the envelope follower; the resulting gain reduction is applied broadband. Simple and transparent.
155
+
156
+ ```js
157
+ import { deesser } from '@audio/dynamics'
158
+
159
+ deesser(data, { freq: 6500, threshold: -20 })
160
+ deesser(data, { freq: 5500, q: 3, threshold: -24, ratio: 6 })
161
+ ```
162
+
163
+ | Param | Default | |
164
+ |---|---|---|
165
+ | `freq` | `6500` | Hz, sibilance center |
166
+ | `q` | `2` | bandpass Q |
167
+ | `threshold` | `-20` | dB (on sidechain level) |
168
+ | `ratio` | `4` | — |
169
+ | `knee` | `6` | dB |
170
+ | `attack` | `1` | ms |
171
+ | `release` | `40` | ms |
172
+
173
+ **Use when:** harsh 's' / 't' / 'sh' in close-miked voice, bright vocal takes.<br>
174
+ **Not for:** broadband brightness — use an EQ. Generic compression — use [compressor](#compressor).
175
+
176
+
177
+ ## ducker
178
+
179
+ External-sidechain compressor. Main signal's gain tracks the level of a separate side signal.
180
+
181
+ ```js
182
+ import { ducker } from '@audio/dynamics'
183
+
184
+ // batch
185
+ let podcast = ducker(music, voice, { threshold: -30, range: -12 })
186
+
187
+ // streaming — callable takes (main, side); call with no args to flush
188
+ let duck = ducker({ threshold: -30, range: -15 })
189
+ let out1 = duck(musicBlock1, voiceBlock1)
190
+ let out2 = duck(musicBlock2, voiceBlock2)
191
+ let tail = duck()
192
+ ```
193
+
194
+ | Param | Default | |
195
+ |---|---|---|
196
+ | `threshold` | `-30` | dB (on side level) |
197
+ | `ratio` | `4` | — |
198
+ | `knee` | `6` | dB |
199
+ | `range` | `-24` | dB, max reduction |
200
+ | `attack` | `20` | ms |
201
+ | `release` | `300` | ms |
202
+
203
+ **Use when:** music-under-voice podcasts, dialogue ducking, sidechain-pumped mixes.<br>
204
+ **Not for:** sidechain from the same signal — use [compressor](#compressor).
205
+
206
+
207
+ ## softclip
208
+
209
+ Static waveshaping — no time state, no pumping. Maps input through a fixed transfer curve; peaks saturate smoothly, introducing controlled harmonic content.
210
+
211
+ ```js
212
+ import { softclip } from '@audio/dynamics'
213
+
214
+ softclip(data, { curve: 'tanh', drive: 1.5 })
215
+ softclip(data, { curve: 'cubic', drive: 2, ceiling: 0.9 })
216
+ ```
217
+
218
+ | Param | Default | |
219
+ |---|---|---|
220
+ | `curve` | `'tanh'` | `'tanh'`, `'atan'`, `'cubic'`, `'sin'`, `'hard'` |
221
+ | `drive` | `1` | input pre-gain |
222
+ | `ceiling` | `1` | output asymptote |
223
+
224
+ **Use when:** gentle peak control with musical saturation, avoiding pumping artifacts of a limiter, lo-fi character.<br>
225
+ **Not for:** transparent true-peak safety — use [limiter](#limiter). Clean gain reduction — use [compressor](#compressor).
226
+
227
+
228
+ ## compand
229
+
230
+ SoX-style multi-segment compander. Arbitrary piecewise-linear transfer in dB unifies compression, expansion, and gating under one curve — points below the identity line compress; above, they expand.
231
+
232
+ ```js
233
+ import { compand } from '@audio/dynamics'
234
+
235
+ // Default: compress above -20 dB
236
+ compand(data)
237
+
238
+ // Broadcast leveler: lift -40..-20 dB, compress above -10 dB
239
+ compand(data, {
240
+ points: [[-90, -90], [-40, -30], [-20, -18], [-10, -10], [0, -4]],
241
+ attack: 20, release: 500
242
+ })
243
+ ```
244
+
245
+ | Param | Default | |
246
+ |---|---|---|
247
+ | `points` | `[[-90,-90],[-60,-60],[-20,-20],[0,-8]]` | `[[inDb, outDb], ...]` |
248
+ | `attack` | `5` | ms |
249
+ | `release` | `200` | ms |
250
+
251
+ **Use when:** broadcast leveling, speech normalization, any time a single compressor's fixed ratio is too rigid.<br>
252
+ **Not for:** simple threshold compression — use [compressor](#compressor).
253
+
254
+
255
+ ## See also
256
+
257
+ * [denoise](https://github.com/audiojs/denoise) — gate belongs here too; umbrella for everything noise
258
+ * [filter](https://github.com/audiojs/filter) — biquads for deesser sidechain
259
+ * [effect](https://github.com/audiojs/effect) — modulation effects
260
+ * [stretch](https://github.com/audiojs/stretch) — sibling package
261
+
262
+
263
+ ## References
264
+
265
+ * Giannoulis, D., Massberg, M. & Reiss, J.D. (2012). "Digital dynamic range compressor design — a tutorial and analysis." _JAES_, 60(6).
266
+ * Zölzer, U. (ed., 2011). _DAFX — Digital Audio Effects_ (2nd ed.), chapter on dynamics processing.
267
+ * Reiss, J.D. & McPherson, A. (2014). _Audio Effects — Theory, Implementation and Application_, Ch. 6.
268
+ * Bristow-Johnson, R. (2005). "Audio EQ Cookbook." (RBJ biquad formulae, used in deesser sidechain.)
269
+ * SoX manual — `compand` (piecewise-linear compander semantics).
270
+
271
+
272
+ <div align="center">
273
+
274
+ [MIT](https://github.com/audiojs/dynamics/blob/main/LICENSE) [ॐ](https://github.com/krishnized/license)
275
+
276
+ </div>
package/index.d.ts ADDED
@@ -0,0 +1,104 @@
1
+ type Writer = (chunk?: Float32Array) => Float32Array
2
+ type Writer2 = (main?: Float32Array, side?: Float32Array) => Float32Array
3
+
4
+ export interface EnvelopeOpts {
5
+ sampleRate?: number
6
+ attack?: number // ms
7
+ release?: number // ms
8
+ detector?: 'peak' | 'rms'
9
+ rmsWindow?: number // samples
10
+ }
11
+
12
+ export interface CompressorOpts extends EnvelopeOpts {
13
+ threshold?: number // dB
14
+ ratio?: number
15
+ knee?: number // dB
16
+ makeup?: number // dB
17
+ }
18
+
19
+ export interface LimiterOpts {
20
+ sampleRate?: number
21
+ ceiling?: number // dB (e.g. -0.3)
22
+ lookahead?: number // ms
23
+ release?: number // ms
24
+ }
25
+
26
+ export interface GateOpts extends EnvelopeOpts {
27
+ threshold?: number // dB
28
+ range?: number // dB reduction when closed (e.g. -60)
29
+ hold?: number // ms
30
+ }
31
+
32
+ export interface ExpanderOpts extends EnvelopeOpts {
33
+ threshold?: number
34
+ ratio?: number
35
+ knee?: number
36
+ range?: number // dB, max reduction (e.g. -40)
37
+ }
38
+
39
+ export interface DeesserOpts extends EnvelopeOpts {
40
+ freq?: number // Hz, sibilance center
41
+ q?: number
42
+ threshold?: number
43
+ ratio?: number
44
+ knee?: number
45
+ }
46
+
47
+ export interface DuckerOpts extends EnvelopeOpts {
48
+ threshold?: number
49
+ ratio?: number
50
+ knee?: number
51
+ range?: number // dB, max reduction
52
+ }
53
+
54
+ export interface SoftclipOpts {
55
+ curve?: 'tanh' | 'atan' | 'cubic' | 'sin' | 'hard'
56
+ drive?: number
57
+ ceiling?: number
58
+ }
59
+
60
+ export interface CompandOpts extends EnvelopeOpts {
61
+ points?: [number, number][] // [[inDb, outDb], ...]
62
+ }
63
+
64
+ export declare const compressor: {
65
+ (data: Float32Array, opts?: CompressorOpts): Float32Array
66
+ (opts?: CompressorOpts): Writer
67
+ }
68
+
69
+ export declare const limiter: {
70
+ (data: Float32Array, opts?: LimiterOpts): Float32Array
71
+ (opts?: LimiterOpts): Writer
72
+ }
73
+
74
+ export declare const gate: {
75
+ (data: Float32Array, opts?: GateOpts): Float32Array
76
+ (opts?: GateOpts): Writer
77
+ }
78
+
79
+ export declare const expander: {
80
+ (data: Float32Array, opts?: ExpanderOpts): Float32Array
81
+ (opts?: ExpanderOpts): Writer
82
+ }
83
+
84
+ export declare const deesser: {
85
+ (data: Float32Array, opts?: DeesserOpts): Float32Array
86
+ (opts?: DeesserOpts): Writer
87
+ }
88
+
89
+ export declare const ducker: {
90
+ (main: Float32Array, side: Float32Array, opts?: DuckerOpts): Float32Array
91
+ (opts?: DuckerOpts): Writer2
92
+ }
93
+
94
+ export declare const softclip: {
95
+ (data: Float32Array, opts?: SoftclipOpts): Float32Array
96
+ (opts?: SoftclipOpts): Writer
97
+ }
98
+
99
+ export declare const compand: {
100
+ (data: Float32Array, opts?: CompandOpts): Float32Array
101
+ (opts?: CompandOpts): Writer
102
+ }
103
+
104
+ export declare function envelope(opts?: EnvelopeOpts): (x: number) => number
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ // @audio/dynamics — dynamics processing umbrella re-exporting every @audio/dynamics-* atom.
2
+ // For smaller bundles, depend directly on the individual atom.
3
+
4
+ export { default as compressor, compressorGain } from '@audio/dynamics-compressor'
5
+ export { default as limiter } from '@audio/dynamics-limiter'
6
+ export { default as gate } from '@audio/dynamics-gate'
7
+ export { default as expander } from '@audio/dynamics-expander'
8
+ export { default as deesser } from '@audio/dynamics-deesser'
9
+ export { default as ducker } from '@audio/dynamics-ducker'
10
+ export { default as softclip } from '@audio/dynamics-softclip'
11
+ export { default as compand } from '@audio/dynamics-compand'
12
+ export { default as transientShaper } from '@audio/dynamics-transient-shaper'
13
+ export { default as multiband } from '@audio/dynamics-multiband'
14
+ export { envelope } from '@audio/dynamics-envelope'
15
+ export { default as opto } from '@audio/dynamics-opto'
16
+ export { default as fet } from '@audio/dynamics-fet'
17
+ export { default as vca } from '@audio/dynamics-vca'
18
+ export { default as varimu } from '@audio/dynamics-varimu'
19
+ export { default as leveler } from '@audio/dynamics-leveler'
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "@audio/dynamics",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "types": "index.d.ts",
6
+ "description": "Dynamics processing — umbrella for @audio/dynamics-* atoms (compressor, limiter, gate, expander, de-esser, ducker, softclip, compand, transient shaper)",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "index.js",
17
+ "index.d.ts"
18
+ ],
19
+ "scripts": {
20
+ "test": "node test.js",
21
+ "test:all": "npm test --workspaces --if-present && npm test",
22
+ "publish:all": "npm publish --workspaces && npm publish"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/audiojs/dynamics.git"
27
+ },
28
+ "keywords": [
29
+ "dynamics",
30
+ "compressor",
31
+ "limiter",
32
+ "gate",
33
+ "expander",
34
+ "deesser",
35
+ "ducker",
36
+ "softclip",
37
+ "compand",
38
+ "sidechain",
39
+ "audio",
40
+ "audiojs",
41
+ "dsp"
42
+ ],
43
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
44
+ "license": "MIT",
45
+ "bugs": {
46
+ "url": "https://github.com/audiojs/dynamics/issues"
47
+ },
48
+ "homepage": "https://github.com/audiojs/dynamics#readme",
49
+ "engines": {
50
+ "node": ">=18"
51
+ },
52
+ "devDependencies": {
53
+ "tst": "^9.4.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "main": "index.js",
59
+ "workspaces": [
60
+ "packages/*"
61
+ ],
62
+ "dependencies": {
63
+ "@audio/dynamics-core": "^0.1.0",
64
+ "@audio/dynamics-envelope": "^0.1.0",
65
+ "@audio/dynamics-compressor": "^0.1.0",
66
+ "@audio/dynamics-limiter": "^0.1.0",
67
+ "@audio/dynamics-gate": "^0.1.0",
68
+ "@audio/dynamics-expander": "^0.1.0",
69
+ "@audio/dynamics-deesser": "^0.1.0",
70
+ "@audio/dynamics-ducker": "^0.1.0",
71
+ "@audio/dynamics-softclip": "^0.1.0",
72
+ "@audio/dynamics-compand": "^0.1.0",
73
+ "@audio/dynamics-transient-shaper": "^0.1.0",
74
+ "@audio/dynamics-multiband": "^0.1.0",
75
+ "@audio/dynamics-opto": "^0.1.0",
76
+ "@audio/dynamics-fet": "^0.1.0",
77
+ "@audio/dynamics-vca": "^0.1.0",
78
+ "@audio/dynamics-varimu": "^0.1.0",
79
+ "@audio/dynamics-leveler": "^0.1.0"
80
+ }
81
+ }