@audio/effect-subbass 1.1.5 → 1.3.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > Psychoacoustic bass enhancer — harmonic synthesis below cutoff (MaxxBass/RBass class)
4
4
 
5
- Extracts the band below `freq`, generates its harmonic series with an even/odd waveshaper, band-limits the harmonics to the audible low-mids, and mixes against dry. Small speakers reproduce the harmonics; the ear reconstructs the missing fundamental.
5
+ Extracts the band below `fc`, generates its harmonic series with an even/odd waveshaper, band-limits the harmonics to the audible low-mids, and mixes against dry. Small speakers reproduce the harmonics; the ear reconstructs the missing fundamental.
6
6
 
7
7
  ## Install
8
8
 
@@ -15,13 +15,13 @@ npm install @audio/effect-subbass
15
15
  ```js
16
16
  import subbass from '@audio/effect-subbass'
17
17
 
18
- let p = { freq: 80, amount: 0.5, drive: 0.5, keep: 1, fs: 44100 }
18
+ let p = { fc: 80, amount: 0.5, drive: 0.5, keep: 1, fs: 44100 }
19
19
  for (let buf of stream) subbass(buf, p)
20
20
  ```
21
21
 
22
22
  `subbass(data, params)` mutates `data` (`Float32Array`/`Float64Array`) in place and returns it. Pass the same `params` object across calls — the sub-extraction and harmonic-band filter state persists on it (`_sub`, `_out`, `_dc`).
23
23
 
24
- **`freq`** sub cutoff, Hz — harmonics built from below this (default 80) · **`amount`** harmonic level 0–1 (default 0.5) · **`drive`** waveshaper intensity 0–1 (default 0.5) · **`keep`** how much original sub to keep, 0–1, 0 = replace entirely (default 1) · **`fs`** sample rate
24
+ **`fc`** sub cutoff, Hz — harmonics built from below this (default 80; `freq` still accepted) · **`amount`** harmonic level 0–1 (default 0.5) · **`drive`** waveshaper intensity 0–1 (default 0.5) · **`keep`** how much original sub to keep, 0–1, 0 = replace entirely (default 1) · **`fs`** sample rate
25
25
 
26
26
  **Use when**: small-speaker/phone playback, bass on systems that can't reproduce true sub<br>
27
27
  **Not for**: sources already reproduced on full-range monitors, general saturation (use `@audio/effect-distortion`)
package/audio.d.ts CHANGED
@@ -11,6 +11,8 @@ type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Liv
11
11
  /** Chainable-host options for 'subbass' */
12
12
  export interface SubbassOptions {
13
13
  /** 30..200 Hz (default 80) */
14
+ "fc"?: Auto
15
+ /** @deprecated former name of "fc" */
14
16
  "freq"?: Auto
15
17
  /** 0..1 (default 0.5) */
16
18
  "amount"?: Auto
@@ -25,9 +27,10 @@ export interface SubbassOptions {
25
27
  export declare const subbass: {
26
28
  (ctx: Ctx): Process
27
29
  channels: "any"
30
+ tail: (at: { sampleRate: number, params: Live, buffers: {} }) => number
28
31
  params: {
29
32
  /** 30..200 Hz (default 80) */
30
- "freq": { type: "number", default: 80 }
33
+ "fc": { type: "number", default: 80, alias: "freq" }
31
34
  /** 0..1 (default 0.5) */
32
35
  "amount": { type: "number", default: 0.5 }
33
36
  /** 0..1 (default 0.5) */
package/audio.js CHANGED
@@ -1,29 +1,123 @@
1
1
  // atom manifest — wraps the subbass atom; SVF filter state, no sizing params —
2
- // every param stays live, per-channel state is just the running filter integrators.
2
+ // every param stays live, per-channel state is just the running filter integrators,
3
+ // made here so the first block sets nothing up.
3
4
 
4
5
  import subbassFn from './subbass.js'
5
6
 
7
+ // A value that scales the signal, glided sample by sample so that a host's steps of it join into one line. A host hands
8
+ // each call the value at its first frame and, while a value moves (a `smoothing` ramp), calls with 32 frames at most
9
+ // (contract 1.0: Parameters, Time; What a host owes, 3). So the line through the values the calls bring, each at its
10
+ // call's first frame, is known 32 frames behind any call's first frame, however the host cut the calls; the glide plays
11
+ // it 32 frames late. Straight from the value of one call to the next's, it is the host's ramp itself, in the value's
12
+ // own unit, the same whether the host calls with 1, 32 or a mix of frames; drawn between each call's end and the next
13
+ // (a glide across each call), a ramp cut 32, 4, 32 would put 32 frames of it into 4. A value that follows a call of
14
+ // more than 32 frames came with no ramp: it is reached in a straight line over 32 frames. The first call of an
15
+ // instance starts on its value, and a value held 32 frames is the value itself, so a value that does not move is bit
16
+ // for bit what the kernel makes of it alone.
17
+ // `g`: the memory, GLIDE slots a value from j·GLIDE (made NaN): the value the last call brought, where in the ring its
18
+ // first frame is, its frames, the frames the line had been at that value before it (64 once held and the ring all of
19
+ // it; counted to 63 at most, so that a long call after a move cannot reach 64 before the ring is made all of it), how
20
+ // many of `row`'s hold it, then the line, a frame a slot, in a ring of 64. `src`: the param's array. The line's values
21
+ // for this call's `n` frames go to `row` from `at`: as they are, or with `lin` 1 the gain e^(v·ln10/20) of a value in
22
+ // dB, per sample, so the gain follows the host's ramp of the dB value. Returns 1 while they move, 0 when every one is
23
+ // the value (then `row` holds it, filled once, and a caller may run its kernel on the value alone). Only arrays and
24
+ // whole numbers cross the call.
25
+ const GLIDE = 69
26
+ function glide (g, j, src, row, at, n, lin) {
27
+ let o = j * GLIDE, r = o + 5, v = src[0], was = g[o]
28
+ if (!(was === was)) { g[o] = v; g[o + 1] = 0; g[o + 2] = n; g[o + 3] = 0; g[o + 4] = 0 }
29
+ else if (v !== was || g[o + 3] < 64) {
30
+ let len = g[o + 2], L = len < 32 ? len : 32, w = (g[o + 1] + len) & 63, d = v - was, held = v === was ? Math.min(g[o + 3] + len, 63) : 0
31
+ for (let q = 0; q < L; q++) g[r + ((w - L + q) & 63)] = was + d * q / L
32
+ g[r + w] = v
33
+ g[o] = v; g[o + 1] = w; g[o + 2] = n; g[o + 3] = held; g[o + 4] = 0
34
+ if (held < 32) {
35
+ for (let i = 0; i < n; i++) {
36
+ let u = i < 32 ? g[r + ((w + i - 32) & 63)] : v
37
+ row[at + i] = lin ? Math.exp(u * (Math.LN10 / 20)) : u
38
+ }
39
+ return 1
40
+ }
41
+ } else { g[o + 1] = (g[o + 1] + g[o + 2]) & 63; g[o + 2] = n }
42
+ // held 32 frames: the ring made all of the value once (a move from it reads the line from there), the row filled
43
+ // with it as far as no call since it was held has filled it; from then on a call only moves the ring's position
44
+ if (g[o + 3] < 64) { for (let q = 0; q < 64; q++) g[r + q] = v; g[o + 3] = 64 }
45
+ if (g[o + 4] < n) {
46
+ let u = lin ? Math.exp(v * (Math.LN10 / 20)) : v
47
+ for (let i = 0; i < n; i++) row[at + i] = u
48
+ g[o + 4] = n
49
+ }
50
+ return 0
51
+ }
52
+
6
53
  export const subbass = (ctx) => {
7
- const chP = []
8
- for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) chP.push({ fs: ctx.sampleRate })
54
+ const chP = [], M = ctx.maxBlockSize
55
+ for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) chP.push({ fs: ctx.sampleRate, _sub: new Float64Array(2), _out: new Float64Array(2), _dc: 0 })
56
+ // amount, drive and keep scale the signal: each is smoothed (20 ms) and glided sample by sample (glide, above), its
57
+ // values for the call a lane of `lanes`; while none moves the kernel runs on the values
58
+ const g = new Float64Array(3 * GLIDE).fill(NaN), lanes = new Float64Array(3 * M)
9
59
  return (inputs, outputs, params) => {
10
60
  const inp = inputs[0], out = outputs[0]
11
61
  if (!inp || !inp.length) return
62
+ const n = inp[0].length, moving = glide(g, 0, params.amount, lanes, 0, n, 0) | glide(g, 1, params.drive, lanes, M, n, 0) | glide(g, 2, params.keep, lanes, 2 * M, n, 0)
12
63
  for (let c = 0; c < inp.length; c++) {
13
64
  const p = chP[c]
14
- p.freq = params.freq[0]
65
+ p.fc = params.fc[0]
15
66
  p.amount = params.amount[0]
16
67
  p.drive = params.drive[0]
17
68
  p.keep = params.keep[0]
18
69
  out[c].set(inp[c])
19
- subbassFn(out[c], p)
70
+ subbassFn(out[c], p, moving ? lanes : undefined, M)
20
71
  }
21
72
  }
22
73
  }
23
74
  subbass.channels = 'any'
75
+ // The tail (CONTRACT 1.0: until the output stays 60 dB under the input's level) by the rule for a fixed linear filter:
76
+ // the frames after an input stops until the rest of Σ|h| is under eps, h the impulse response. A peak-1 input then
77
+ // leaves at most eps, and a held tone at a resonance, or DC into a loop, comes within a few percent of it, so the gain
78
+ // the ring starts at is in the count. `make` returns [tick, energy]: tick(x) runs a frame, energy() sums the squares of
79
+ // the state. h runs until that energy has fallen 200 dB from its largest, past which what is left is negligible, then
80
+ // again from the impulse, the rest being the sum less what has passed; Infinity where it has not fallen so far in 2^22
81
+ // frames
82
+ const rest = (make, eps) => {
83
+ let [tick, energy] = make(), sum = 0, top = 0, n = 0
84
+ for (; n < 2 ** 22; n++) { sum += Math.abs(tick(n ? 0 : 1)); let e = energy(); if (e > top) top = e; else if (n > 64 && e <= 1e-20 * top) break }
85
+ if (n === 2 ** 22) return Infinity
86
+ tick = make()[0]
87
+ for (let j = 0; j < n; j++) if ((sum -= Math.abs(tick(j ? 0 : 1))) <= eps) return j
88
+ return n
89
+ }
90
+ // the two paths frozen as subbass.js runs them, each Chamberlin SVF at f = 2 sin(π fc / fs): the sub lowpass (damping
91
+ // 1.2), and after it the harmonic path, the waveshaper a gain here (counted outside), the DC tracker and the bandpass
92
+ // at 2.5 fc (damping 0.8)
93
+ const lowpass = f => () => { let l = 0, b = 0; return [x => { l += f * b; b += f * (x - l - 1.2 * b); return l }, () => l * l + b * b] }
94
+ const harmonic = (f, f2, aDc) => () => {
95
+ let l = 0, b = 0, dc = 0, L = 0, P = 0
96
+ return [x => { l += f * b; b += f * (x - l - 1.2 * b); dc += aDc * (l - dc); L += f2 * P; P += f2 * (l - dc - L - 0.8 * P); return P }, () => l * l + b * b + dc * dc + L * L + P * P]
97
+ }
98
+ // A planning snapshot's number that is not finite (automation whose values are not known yet) reserves the longer of
99
+ // the tails at the ends of its range, as effect-delay's tail reserves its maxima: through the formula it gave no tail
100
+ // (a NaN amount reads as none, a NaN fc as a ring of NaN), cutting the decay. Both ends are tried: the tail is longest
101
+ // at one or the other
102
+ const reserve = (f, tail) => function known (s) {
103
+ for (const k in f.params) if (f.params[k].type === 'number' && !Number.isFinite(s.params[k]?.[0])) {
104
+ const at = v => known({ ...s, params: { ...s.params, [k]: Float32Array.of(v) } })
105
+ return Math.max(at(f.params[k].min), at(f.params[k].max))
106
+ }
107
+ return tail(s)
108
+ }
109
+ // What the input leaves (subbass.js) is −(1 − keep) of the sub and 2 · amount of the harmonic path, its waveshaper
110
+ // tanh(g · s) · 0.6 + |s| · g · 0.4, g = 1 + 6 · drive, at most g · |s|, its slope at 0, which a quiet input meets. Each
111
+ // path's rest is held under half of 1e-3 over its gain. A ring of 128 frames or less needs no tail (CONTRACT 1.0)
112
+ subbass.tail = reserve(subbass, ({ sampleRate: fs, params }) => {
113
+ let fc = params.fc[0], f = 2 * Math.sin(Math.PI * Math.min(fc, fs / 4) / fs), f2 = 2 * Math.sin(Math.PI * Math.min(fc * 2.5, fs / 4) / fs)
114
+ let dry = 1 - params.keep[0], wet = 2 * params.amount[0] * (1 + 6 * params.drive[0]), aDc = 1 - Math.exp(-2 * Math.PI * 10 / fs)
115
+ let n = Math.max(dry > 0 ? rest(lowpass(f), 5e-4 / dry) : 0, wet > 0 ? rest(harmonic(f, f2, aDc), 5e-4 / wet) : 0)
116
+ return n > 128 ? n / fs : 0
117
+ })
24
118
  subbass.params = {
25
- freq: { type: 'number', min: 30, max: 200, default: 80, unit: 'Hz', curve: 'log' },
26
- amount: { type: 'number', min: 0, max: 1, default: 0.5 },
27
- drive: { type: 'number', min: 0, max: 1, default: 0.5 },
28
- keep: { type: 'number', min: 0, max: 1, default: 1 },
119
+ fc: { type: 'number', min: 30, max: 200, default: 80, unit: 'Hz', curve: 'log', alias: 'freq' },
120
+ amount: { type: 'number', min: 0, max: 1, default: 0.5, smoothing: 0.02 },
121
+ drive: { type: 'number', min: 0, max: 1, default: 0.5, smoothing: 0.02 },
122
+ keep: { type: 'number', min: 0, max: 1, default: 1, smoothing: 0.02 },
29
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/effect-subbass",
3
- "version": "1.1.5",
3
+ "version": "1.3.0",
4
4
  "description": "Psychoacoustic bass enhancer — harmonic synthesis below cutoff (MaxxBass/RBass class)",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -24,6 +24,9 @@
24
24
  "default": "./audio.js"
25
25
  }
26
26
  },
27
+ "scripts": {
28
+ "prepublishOnly": "node ../../../compile/tools/tested.js"
29
+ },
27
30
  "files": [
28
31
  "subbass.js",
29
32
  "audio.js",
package/subbass.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * Psychoacoustic bass enhancer (MaxxBass/RBass class) — extracts the band below
3
- * `freq`, generates its harmonic series with an even/odd waveshaper, band-limits
3
+ * `fc`, generates its harmonic series with an even/odd waveshaper, band-limits
4
4
  * the harmonics to the audible low-mids, and mixes against dry. Small speakers
5
5
  * reproduce the harmonics; the ear reconstructs the missing fundamental.
6
6
  */
7
7
  export interface SubbassOptions {
8
8
  /** sub cutoff, Hz — harmonics built from below this, default 80 */
9
+ fc?: number
10
+ /** @deprecated former name of `fc` */
9
11
  freq?: number
10
12
  /** harmonic level, 0–1, default 0.5 */
11
13
  amount?: number
@@ -20,6 +22,7 @@ export interface SubbassOptions {
20
22
  /**
21
23
  * Mutates `data` in place and returns the same reference. Pass the same params
22
24
  * object across calls — the sub-extraction and harmonic-band filter state persist
23
- * on it (`_sub`, `_out`, `_dc`).
25
+ * on it (`_sub`, `_out`, `_dc`). `lanes`, when given, holds amount, drive and keep for each sample of `data`, `M`
26
+ * apart, in place of the params': the `/audio` factory's glide of moving values.
24
27
  */
25
- export default function subbass(data: Float32Array, params?: SubbassOptions): Float32Array
28
+ export default function subbass<T extends Float32Array | Float64Array>(data: T, params?: SubbassOptions, lanes?: Float64Array, M?: number): T
package/subbass.js CHANGED
@@ -7,42 +7,47 @@
7
7
 
8
8
  let { sin, tanh, PI } = Math
9
9
 
10
- // state-variable filter tick: returns [lp, bp, hp], mutates s = {l, b}
10
+ // state-variable filter tick: mutates s = [l, b], the lowpass and bandpass legs
11
11
  function svf (s, x, f, q) {
12
- s.l += f * s.b
13
- let h = x - s.l - q * s.b
14
- s.b += f * h
15
- return [s.l, s.b, h]
12
+ s[0] += f * s[1]
13
+ let h = x - s[0] - q * s[1]
14
+ s[1] += f * h
16
15
  }
17
16
 
18
- export default function subbass (data, params) {
19
- let freq = params.freq ?? 80 // sub cutoff, Hz — harmonics built from below here
17
+ // `lanes`, when given, holds amount, drive and keep for each sample of `data`, M apart (the /audio manifest's glide of
18
+ // moving values), in place of the params'
19
+ export default function subbass (data, params = {}, lanes, M) {
20
+ let fc = params.fc ?? params.freq ?? 80 // sub cutoff, Hz — harmonics built from below here (`freq`: former name)
20
21
  let amount = params.amount ?? 0.5 // harmonic level 0..1
21
22
  let drive = params.drive ?? 0.5 // waveshaper intensity 0..1
22
23
  let keep = params.keep ?? 1 // how much original sub to keep (0 = replace)
23
24
  let fs = params.fs || 44100
24
25
 
25
26
  if (!params._sub) {
26
- params._sub = { l: 0, b: 0 } // sub extraction LP
27
- params._out = { l: 0, b: 0 } // harmonic band BP
27
+ params._sub = new Float64Array(2) // sub extraction LP
28
+ params._out = new Float64Array(2) // harmonic band BP
28
29
  params._dc = 0
29
30
  }
30
- let f1 = 2 * sin(PI * Math.min(freq, fs / 4) / fs)
31
- let f2 = 2 * sin(PI * Math.min(freq * 2.5, fs / 4) / fs)
31
+ let s = params._sub, o = params._out, dc = params._dc
32
+ let f1 = 2 * sin(PI * Math.min(fc, fs / 4) / fs)
33
+ let f2 = 2 * sin(PI * Math.min(fc * 2.5, fs / 4) / fs)
32
34
  let g = 1 + drive * 6
33
35
  let aDc = 1 - Math.exp(-2 * PI * 10 / fs)
34
36
 
35
37
  for (let i = 0, l = data.length; i < l; i++) {
36
38
  let x = data[i]
37
- let [sub] = svf(params._sub, x, f1, 1.2)
39
+ svf(s, x, f1, 1.2)
40
+ let sub = s[0] // lowpass output
41
+ let gi = lanes ? 1 + lanes[M + i] * 6 : g
38
42
  // even harmonics from |x| rectification + odd from tanh — full series
39
- let h = tanh(sub * g) * 0.6 + Math.abs(sub) * g * 0.4
40
- params._dc += aDc * (h - params._dc) // DC-block the rectifier output
41
- h -= params._dc
42
- // keep harmonics in the speaker-friendly band around 2–5× freq
43
- svf(params._out, h, f2, 0.8)
44
- let harm = params._out.b // bandpass output
45
- data[i] = x - (1 - keep) * sub + amount * 2 * harm
43
+ let h = tanh(sub * gi) * 0.6 + Math.abs(sub) * gi * 0.4
44
+ dc += aDc * (h - dc) // DC-block the rectifier output
45
+ h -= dc
46
+ // keep harmonics in the speaker-friendly band around 2–5× fc
47
+ svf(o, h, f2, 0.8)
48
+ let harm = o[1] // bandpass output
49
+ data[i] = x - (1 - (lanes ? lanes[2 * M + i] : keep)) * sub + (lanes ? lanes[i] : amount) * 2 * harm
46
50
  }
51
+ params._dc = dc
47
52
  return data
48
53
  }