@audio/effect-subbass 1.2.0 → 1.3.1
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 +9 -6
- package/audio.js +101 -7
- package/package.json +4 -1
- package/subbass.d.ts +3 -2
- package/subbass.js +20 -15
package/audio.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// Generated from the audio.js manifest (params metadata is the source of truth).
|
|
2
|
-
// Regenerate: node tools/dts.js in @audio/
|
|
2
|
+
// Regenerate: node tools/dts.js in @audio/plugin. Do not edit by hand.
|
|
3
3
|
|
|
4
4
|
/** Automatable number — scalar, `t => value` fn, or breakpoint curve {t, v} */
|
|
5
5
|
type Auto = number | ((t: number) => number) | { t: number[], v: number[] }
|
|
6
6
|
/** Per-block param values as delivered by hosts (numbers arrive as 1-length Float32Array) */
|
|
7
7
|
type Live = Record<string, Float32Array | string | boolean>
|
|
8
|
-
type
|
|
8
|
+
type Transport = Readonly<{ playing: boolean, bpm: number, beat: number, barStart: number, timeSig: [number, number] }>
|
|
9
|
+
/** What a host hands a processor it builds: the contract's Context (CONTRACT.d.ts, 1.0) */
|
|
10
|
+
type Ctx = { readonly sampleRate: number, readonly maxBlockSize: number, readonly maxChannels: number, readonly layouts?: { readonly inputs: readonly (string | undefined)[], readonly outputs: readonly (string | undefined)[] }, readonly params: Live, readonly buffers: Readonly<Record<string, Float32Array[] | undefined>>, readonly events?: readonly { time: number, kind: 'on' | 'off', midi: number, velocity: number, id: number }[], emit(name: string, value: number): void, readonly transport: Transport }
|
|
9
11
|
type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Live) => void
|
|
10
12
|
|
|
11
13
|
/** Chainable-host options for 'subbass' */
|
|
@@ -27,14 +29,15 @@ export interface SubbassOptions {
|
|
|
27
29
|
export declare const subbass: {
|
|
28
30
|
(ctx: Ctx): Process
|
|
29
31
|
channels: "any"
|
|
32
|
+
tail: (at: { sampleRate: number, params: Live, buffers?: {}, transport: Transport }) => number
|
|
30
33
|
params: {
|
|
31
34
|
/** 30..200 Hz (default 80) */
|
|
32
|
-
"fc": { type: "number", default: 80, alias: "freq" }
|
|
35
|
+
"fc": { type: "number", min: 30, max: 200, default: 80, unit: "Hz", curve: "log", alias: "freq" }
|
|
33
36
|
/** 0..1 (default 0.5) */
|
|
34
|
-
"amount": { type: "number", default: 0.5 }
|
|
37
|
+
"amount": { type: "number", min: 0, max: 1, default: 0.5, smoothing: 0.02 }
|
|
35
38
|
/** 0..1 (default 0.5) */
|
|
36
|
-
"drive": { type: "number", default: 0.5 }
|
|
39
|
+
"drive": { type: "number", min: 0, max: 1, default: 0.5, smoothing: 0.02 }
|
|
37
40
|
/** 0..1 (default 1) */
|
|
38
|
-
"keep": { type: "number", default: 1 }
|
|
41
|
+
"keep": { type: "number", min: 0, max: 1, default: 1, smoothing: 0.02 }
|
|
39
42
|
}
|
|
40
43
|
}
|
package/audio.js
CHANGED
|
@@ -1,14 +1,65 @@
|
|
|
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
65
|
p.fc = params.fc[0]
|
|
@@ -16,14 +67,57 @@ export const subbass = (ctx) => {
|
|
|
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
119
|
fc: { type: 'number', min: 30, max: 200, default: 80, unit: 'Hz', curve: 'log', alias: 'freq' },
|
|
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 },
|
|
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.
|
|
3
|
+
"version": "1.3.1",
|
|
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
|
@@ -22,6 +22,7 @@ export interface SubbassOptions {
|
|
|
22
22
|
/**
|
|
23
23
|
* Mutates `data` in place and returns the same reference. Pass the same params
|
|
24
24
|
* object across calls — the sub-extraction and harmonic-band filter state persist
|
|
25
|
-
* 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.
|
|
26
27
|
*/
|
|
27
|
-
export default function subbass<T extends Float32Array | Float64Array>(data: T, params?: SubbassOptions): T
|
|
28
|
+
export default function subbass<T extends Float32Array | Float64Array>(data: T, params?: SubbassOptions, lanes?: Float64Array, M?: number): T
|
package/subbass.js
CHANGED
|
@@ -7,15 +7,16 @@
|
|
|
7
7
|
|
|
8
8
|
let { sin, tanh, PI } = Math
|
|
9
9
|
|
|
10
|
-
// state-variable filter tick:
|
|
10
|
+
// state-variable filter tick: mutates s = [l, b], the lowpass and bandpass legs
|
|
11
11
|
function svf (s, x, f, q) {
|
|
12
|
-
s
|
|
13
|
-
let h = x - s
|
|
14
|
-
s
|
|
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
|
-
|
|
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) {
|
|
19
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
|
|
@@ -23,10 +24,11 @@ export default function subbass (data, params = {}) {
|
|
|
23
24
|
let fs = params.fs || 44100
|
|
24
25
|
|
|
25
26
|
if (!params._sub) {
|
|
26
|
-
params._sub =
|
|
27
|
-
params._out =
|
|
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
|
}
|
|
31
|
+
let s = params._sub, o = params._out, dc = params._dc
|
|
30
32
|
let f1 = 2 * sin(PI * Math.min(fc, fs / 4) / fs)
|
|
31
33
|
let f2 = 2 * sin(PI * Math.min(fc * 2.5, fs / 4) / fs)
|
|
32
34
|
let g = 1 + drive * 6
|
|
@@ -34,15 +36,18 @@ export default function subbass (data, params = {}) {
|
|
|
34
36
|
|
|
35
37
|
for (let i = 0, l = data.length; i < l; i++) {
|
|
36
38
|
let x = data[i]
|
|
37
|
-
|
|
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 *
|
|
40
|
-
|
|
41
|
-
h -=
|
|
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
|
|
42
46
|
// keep harmonics in the speaker-friendly band around 2–5× fc
|
|
43
|
-
svf(
|
|
44
|
-
let harm =
|
|
45
|
-
data[i] = x - (1 - keep) * sub + amount * 2 * harm
|
|
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
|
}
|