@audio/effect-exciter 1.2.0 → 1.2.2

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/audio.d.ts CHANGED
@@ -25,6 +25,7 @@ export interface ExciterOptions {
25
25
  export declare const exciter: {
26
26
  (ctx: Ctx): Process
27
27
  channels: "any"
28
+ tail: (at: { sampleRate: number, params: Live, buffers: {} }) => number
28
29
  params: {
29
30
  /** 500..8000 Hz (default 3000) */
30
31
  "fc": { type: "number", default: 3000, alias: "freq" }
package/audio.js CHANGED
@@ -1,11 +1,13 @@
1
1
  // atom manifest — wraps the exciter 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. fc retunes the highpass each call, smoothed so
4
+ // that a host ramps it.
3
5
 
4
6
  import exciterFn from './exciter.js'
5
7
 
6
8
  export const exciter = (ctx) => {
7
9
  const chP = []
8
- for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) chP.push({ fs: ctx.sampleRate })
10
+ for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) chP.push({ fs: ctx.sampleRate, _lp: 0, _bp: 0 })
9
11
  return (inputs, outputs, params) => {
10
12
  const inp = inputs[0], out = outputs[0]
11
13
  if (!inp || !inp.length) return
@@ -20,8 +22,45 @@ export const exciter = (ctx) => {
20
22
  }
21
23
  }
22
24
  exciter.channels = 'any'
25
+ // The tail (CONTRACT 1.0: until the output stays 60 dB under the input's level) by the rule for a fixed linear filter:
26
+ // the frames after an input stops until the rest of Σ|h| is under eps, h the impulse response. A peak-1 input then
27
+ // 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
28
+ // the ring starts at is in the count. `make` returns [tick, energy]: tick(x) runs a frame, energy() sums the squares of
29
+ // the state. h runs until that energy has fallen 200 dB from its largest, past which what is left is negligible, then
30
+ // again from the impulse, the rest being the sum less what has passed; Infinity where it has not fallen so far in 2^22
31
+ // frames
32
+ const rest = (make, eps) => {
33
+ let [tick, energy] = make(), sum = 0, top = 0, n = 0
34
+ 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 }
35
+ if (n === 2 ** 22) return Infinity
36
+ tick = make()[0]
37
+ for (let j = 0; j < n; j++) if ((sum -= Math.abs(tick(j ? 0 : 1))) <= eps) return j
38
+ return n
39
+ }
40
+ // the highpass frozen at f, as exciter.js runs it: the trapezoidal SVF at g = tan(π f / fs), damping k
41
+ const highpass = (f, k, fs) => () => {
42
+ let g = Math.tan(Math.PI * Math.min(f, 0.49 * fs) / fs), a = 1 / (1 + g * (g + k)), lp = 0, bp = 0
43
+ return [x => { let v1 = a * (bp + g * (x - lp)), v2 = lp + g * v1; bp = 2 * v1 - bp; lp = 2 * v2 - lp; return x - k * v1 - v2 }, () => lp * lp + bp * bp]
44
+ }
45
+ // A planning snapshot's number that is not finite (automation whose values are not known yet) reserves the longer of
46
+ // the tails at the ends of its range, as effect-delay's tail reserves its maxima: through the formula it gave NaN, which
47
+ // a host takes as no tail, cutting the decay. Both ends are tried: the ring is longest at one or the other
48
+ const reserve = (f, tail) => function known (s) {
49
+ for (const k in f.params) if (f.params[k].type === 'number' && !Number.isFinite(s.params[k]?.[0])) {
50
+ const at = v => known({ ...s, params: { ...s.params, [k]: Float32Array.of(v) } })
51
+ return Math.max(at(f.params[k].min), at(f.params[k].max))
52
+ }
53
+ return tail(s)
54
+ }
55
+ // What the input leaves is 0.5 · amount · tanh(g · hp), g = 1 + 9 · drive (exciter.js): at most 0.5 · amount · g · |hp|,
56
+ // the saturator's slope at 0, which a quiet input meets; so the highpass's rest is held under 1e-3 over that gain. A ring
57
+ // of 128 frames or less needs no tail (CONTRACT 1.0)
58
+ exciter.tail = reserve(exciter, ({ sampleRate: fs, params }) => {
59
+ let gain = 0.5 * params.amount[0] * (1 + 9 * params.drive[0]), n = gain > 1e-3 ? rest(highpass(params.fc[0], 0.5, fs), 1e-3 / gain) : 0
60
+ return n > 128 ? n / fs : 0
61
+ })
23
62
  exciter.params = {
24
- fc: { type: 'number', min: 500, max: 8000, default: 3000, unit: 'Hz', curve: 'log', alias: 'freq' },
63
+ fc: { type: 'number', min: 500, max: 8000, default: 3000, unit: 'Hz', curve: 'log', alias: 'freq', smoothing: 0.02 },
25
64
  drive: { type: 'number', min: 0, max: 1, default: 0.5 },
26
65
  amount: { type: 'number', min: 0, max: 1, default: 0.5 },
27
66
  }
package/exciter.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * mixed back into dry signal. Aphex-style aural exciter.
5
5
  */
6
6
 
7
- let {sin, tanh, PI} = Math
7
+ let {tan, tanh, min, PI} = Math
8
8
 
9
9
  export default function exciter (data, params = {}) {
10
10
  let fc = params.fc ?? params.freq ?? 3000 // highpass cutoff Hz (`freq`: former name)
@@ -17,16 +17,19 @@ export default function exciter (data, params = {}) {
17
17
  params._bp = 0
18
18
  }
19
19
  let lp = params._lp, bp = params._bp
20
- let f = 2 * sin(PI * fc / fs)
21
- let q = 0.5 // moderate damping
20
+ let w = tan(PI * min(fc, 0.49 * fs) / fs) // prewarped cutoff
21
+ let k = 0.5 // moderate damping (Q 2)
22
+ let a = 1 / (1 + w * (w + k))
22
23
  let g = 1 + drive * 9 // 1×–10×
23
24
 
24
25
  for (let i = 0, l = data.length; i < l; i++) {
25
26
  let x = data[i]
26
- // Chamberlin SVF — highpass tap generates high-band
27
- lp += f * bp
28
- let hp = x - lp - q * bp
29
- bp += f * hp
27
+ // trapezoidal state-variable filter (Zavalishin, The Art of VA Filter Design, ch. 3–4; Simper 2013),
28
+ // highpass tap: stable at any fc below Nyquist, where Chamberlin's form diverged once f² + 2fk > 4
29
+ let v1 = a * (bp + w * (x - lp)), v2 = lp + w * v1
30
+ bp = 2 * v1 - bp
31
+ lp = 2 * v2 - lp
32
+ let hp = x - k * v1 - v2
30
33
  // Tanh saturation synthesizes harmonics above `fc`
31
34
  data[i] = x + amount * tanh(hp * g) * 0.5
32
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/effect-exciter",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Exciter — psychoacoustic harmonic synthesis for presence/air",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -13,6 +13,9 @@
13
13
  "default": "./audio.js"
14
14
  }
15
15
  },
16
+ "scripts": {
17
+ "prepublishOnly": "node ../../../compile/tools/tested.js"
18
+ },
16
19
  "files": [
17
20
  "exciter.js",
18
21
  "audio.js",