@audio/dynamics-core 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.
Files changed (4) hide show
  1. package/biquad.js +24 -0
  2. package/index.js +2 -0
  3. package/package.json +41 -0
  4. package/util.js +30 -0
package/biquad.js ADDED
@@ -0,0 +1,24 @@
1
+ // RBJ audio EQ cookbook biquads — only what dynamics-processor needs for
2
+ // sidechain filtering (deesser). For a full filter library see audio-filter.
3
+
4
+ const PI2 = 2 * Math.PI
5
+
6
+ export function bandpass(sr, freq, q) {
7
+ let w0 = PI2 * freq / sr
8
+ let cw = Math.cos(w0)
9
+ let a = Math.sin(w0) / (2 * q)
10
+ let a0 = 1 + a
11
+ return {
12
+ b0: a / a0, b1: 0, b2: -a / a0,
13
+ a1: -2 * cw / a0, a2: (1 - a) / a0,
14
+ }
15
+ }
16
+
17
+ export function biquad(c, s, x) {
18
+ let y = c.b0 * x + c.b1 * s.x1 + c.b2 * s.x2 - c.a1 * s.y1 - c.a2 * s.y2
19
+ s.x2 = s.x1; s.x1 = x
20
+ s.y2 = s.y1; s.y1 = y
21
+ return y
22
+ }
23
+
24
+ export const biquadState = () => ({ x1: 0, x2: 0, y1: 0, y2: 0 })
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './util.js'
2
+ export * from './biquad.js'
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@audio/dynamics-core",
3
+ "version": "0.1.0",
4
+ "description": "Shared dynamics primitives — dB/linear conversion, time-constant coefficients, stream writers, biquad sidechain filters",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "index.js",
8
+ "exports": {
9
+ ".": "./index.js",
10
+ "./package.json": "./package.json",
11
+ "./util": "./util.js",
12
+ "./biquad": "./biquad.js"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "util.js",
17
+ "biquad.js"
18
+ ],
19
+ "keywords": [
20
+ "audio",
21
+ "dsp",
22
+ "dynamics"
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-core"
30
+ },
31
+ "homepage": "https://github.com/audiojs/dynamics/tree/main/packages/dynamics-core",
32
+ "bugs": {
33
+ "url": "https://github.com/audiojs/dynamics/issues"
34
+ },
35
+ "engines": {
36
+ "node": ">=18"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
41
+ }
package/util.js ADDED
@@ -0,0 +1,30 @@
1
+ export const clamp = (v, lo, hi) => v < lo ? lo : v > hi ? hi : v
2
+
3
+ export const db2lin = (db) => Math.pow(10, db / 20)
4
+ export const lin2db = (lin) => 20 * Math.log10(Math.max(Math.abs(lin), 1e-10))
5
+
6
+ // One-pole smoothing coefficient: y[n] = c·y[n-1] + (1-c)·x[n].
7
+ // ms → coefficient. c close to 1 = slow, close to 0 = fast.
8
+ export function timeCoef(ms, sampleRate) {
9
+ if (ms <= 0) return 0
10
+ return Math.exp(-1 / (ms * 0.001 * sampleRate))
11
+ }
12
+
13
+ // Wrap { write, flush } into a single callable: write(chunk) → process, write() → flush.
14
+ export function writer(s) {
15
+ return (chunk) => chunk ? s.write(chunk) : s.flush()
16
+ }
17
+
18
+ // Two-input variant for ducker/sidechain.
19
+ export function writer2(s) {
20
+ return (main, side) => main ? s.write(main, side || main) : s.flush()
21
+ }
22
+
23
+ export function concat(a, b) {
24
+ if (!b || !b.length) return a
25
+ if (!a.length) return b
26
+ let out = new Float32Array(a.length + b.length)
27
+ out.set(a)
28
+ out.set(b, a.length)
29
+ return out
30
+ }