@audio/amp-cabinet 1.2.4 → 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/README.md +5 -1
- package/audio.d.ts +3 -2
- package/audio.js +73 -13
- package/cabinet.d.ts +24 -0
- package/cabinet.js +62 -5
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ npm install @audio/amp-cabinet
|
|
|
10
10
|
import cabinet from '@audio/amp-cabinet'
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
With an `ir` (capture your own with `@audio/measure-ir`), convolves through `@audio/reverb-convolution` (`normalize: true`, so output level tracks input level regardless of the IR's own loudness) — the accurate path. Without one, a fixed classical speaker-sim biquad chain approximates a closed-back 12" cab: 70 Hz highpass, a low-mid bump, a presence peak around 2.4 kHz, and a steep rolloff above ~4.5 kHz.
|
|
13
|
+
With an `ir` (capture your own with `@audio/measure-ir`), convolves through `@audio/reverb-convolution` (`normalize: true`, so output level tracks input level regardless of the IR's own loudness) — the accurate path. Without one, a fixed classical speaker-sim biquad chain approximates a closed-back 12" cab: 70 Hz highpass, a low-mid bump, a presence peak around 2.4 kHz, and a steep rolloff above ~4.5 kHz. A section's frequency is held at 0.49 of the sample rate (past Nyquist its poles leave the unit circle), so at 8 kHz the rolloff sits at 3.92 kHz.
|
|
14
14
|
|
|
15
15
|
```js
|
|
16
16
|
cabinet(data, { fs: 44100 }) // biquad approximation, no IR
|
|
@@ -25,6 +25,10 @@ cabinet(data, { fs: 44100, ir: measuredCabinetIr }) // measured IR, accur
|
|
|
25
25
|
|
|
26
26
|
**Mutates `data` in place** and returns it (same array reference, not a copy). Mono only. A delta IR (`[1, 0, 0, …]`) is an exact identity through the convolution path — useful for sanity-checking your own IRs.
|
|
27
27
|
|
|
28
|
+
`stream(block, { mix: [1] }, state(44100, ir))` is the same in calls of any size, with no latency: the chain carries its state from call to call, or the IR (scaled to unit energy, as above) runs in direct form over the input it has kept. That is bit for bit the whole call's output without an IR and with one of up to 1024 taps; `@audio/reverb-convolution` 1.0 runs a longer one by FFT, whose rounding differs from the direct sum's only where the output is silent (measured: by 1e-15 at most). The direct form costs `ir.length` multiplications a sample, so a long IR costs far more than the whole call's FFT did. Values arrive as a host gives them to a processor, a 1-length array, read at each call.
|
|
29
|
+
|
|
30
|
+
The `/audio` processor takes the IR as its buffer `ir`, of one channel, and runs every channel of the signal through `stream`, with `mix` as above, smoothed over 20 ms and glided sample by sample along the host's ramp, 32 samples late (`stream`'s `mixes` after the length: the mix for each sample), so a held mix is the value itself, bit for bit. Without one it runs the biquad chain above, sample for sample as `cabinet(data, { fs })` does. Its `tail(ctx)` is the IR's length, or without an IR the chain's slowest pole falling 60 dB, ln(1000) / −ln(r) samples (about 22 ms at 44.1 kHz).
|
|
31
|
+
|
|
28
32
|
**Use when:** finishing a guitar/bass amp chain — pair after `@audio/amp-tube`.<br>
|
|
29
33
|
**Not for:** room/hall ambience (use `@audio/reverb-*`) — this is a fixed speaker-and-mic coloration, not a spatial reverb.
|
|
30
34
|
|
package/audio.d.ts
CHANGED
|
@@ -17,9 +17,10 @@ export interface CabinetOptions {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export declare const cabinet: {
|
|
20
|
-
(ctx: Ctx): Process
|
|
20
|
+
(ctx: Ctx & { buffers: { "ir": Float32Array[] | undefined } }): Process
|
|
21
21
|
channels: "any"
|
|
22
|
-
|
|
22
|
+
buffers: {"ir":{"channels":1}}
|
|
23
|
+
tail: (at: { sampleRate: number, params: Live, buffers: { "ir": Float32Array[] | undefined } }) => number
|
|
23
24
|
params: {
|
|
24
25
|
/** 0..1 (default 1) */
|
|
25
26
|
"mix": { type: "number", default: 1 }
|
package/audio.js
CHANGED
|
@@ -1,20 +1,80 @@
|
|
|
1
|
-
// atom manifest — speaker cabinet sim.
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// atom manifest — speaker cabinet sim. With a captured impulse response in the buffer `ir`
|
|
2
|
+
// (one channel, as the kernel takes it, for every channel of the signal) the kernel convolves
|
|
3
|
+
// with it; without one it runs its classical closed-back 12" biquad chain. Streaming, with no
|
|
4
|
+
// latency: the chain keeps its state, the convolution its input, from call to call. mix
|
|
5
|
+
// blends the IR's output with the input, with nothing behind it, so it is smoothed (20 ms)
|
|
6
|
+
// and glided sample by sample (glide, below): a host's ramp of it is one line, however the
|
|
7
|
+
// host cut its calls; held, the kernel runs on the value as it ran.
|
|
5
8
|
|
|
6
|
-
import
|
|
9
|
+
import { chain, state, stream } from './cabinet.js'
|
|
7
10
|
|
|
8
|
-
export const cabinet = (ctx) =>
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
for (let c = 0; c <
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
export const cabinet = (ctx) => {
|
|
12
|
+
const ir = ctx.buffers?.ir?.[0], st = [] // a host older than contract 1.0 gives no ctx.buffers
|
|
13
|
+
const g = new Float64Array(GLIDE).fill(NaN), mixes = new Float64Array(ctx.maxBlockSize)
|
|
14
|
+
for (let c = 0; c < ctx.maxChannels; c++) st.push(state(ctx.sampleRate, ir))
|
|
15
|
+
return (inputs, outputs, params) => {
|
|
16
|
+
const inp = inputs[0], out = outputs[0]
|
|
17
|
+
if (!inp || !inp.length) return
|
|
18
|
+
const n = inp[0].length, moving = glide(g, 0, params.mix, mixes, 0, n, 0)
|
|
19
|
+
for (let c = 0; c < inp.length; c++) {
|
|
20
|
+
out[c].set(inp[c])
|
|
21
|
+
stream(out[c], params, st[c], n, moving ? mixes : undefined)
|
|
22
|
+
}
|
|
14
23
|
}
|
|
15
24
|
}
|
|
16
25
|
cabinet.channels = 'any'
|
|
17
|
-
cabinet.
|
|
26
|
+
cabinet.buffers = { ir: { channels: 1 } }
|
|
27
|
+
// a section's slower pole: the larger root of z² + a1·z + a2
|
|
28
|
+
const radius = ({ a1, a2 }) => { let d = a1 * a1 - 4 * a2; return d < 0 ? Math.sqrt(a2) : (Math.abs(a1) + Math.sqrt(d)) / 2 }
|
|
29
|
+
// The IR's length: the last input sample rings through all of it. Without one, the chain's slowest pole, of radius r,
|
|
30
|
+
// falls 60 dB in ln(1000) / -ln(r) samples (J. O. Smith, Introduction to Digital Filters,
|
|
31
|
+
// https://ccrma.stanford.edu/~jos/filters/: its response is r^n)
|
|
32
|
+
cabinet.tail = ({ sampleRate, buffers }) => buffers?.ir ? buffers.ir[0].length / sampleRate
|
|
33
|
+
: Math.log(1000) / -Math.log(Math.max(...chain(sampleRate).map(radius))) / sampleRate
|
|
18
34
|
cabinet.params = {
|
|
19
|
-
mix: { type: 'number', min: 0, max: 1, default: 1 },
|
|
35
|
+
mix: { type: 'number', min: 0, max: 1, default: 1, smoothing: 0.02 },
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A value that scales the signal, glided sample by sample so that a host's steps of it join into one line: the dynamics
|
|
39
|
+
// family's helper, copied (families share no code), one text in each manifest here that glides. A host hands each call
|
|
40
|
+
// the value at its first frame and, while a value moves (a `smoothing` ramp), calls with 32 frames at most (contract
|
|
41
|
+
// 1.0: Parameters, Time; What a host owes, 3). So the line through the values the calls bring, each at its call's first
|
|
42
|
+
// frame, is known 32 frames behind any call's first frame, however the host cut the calls; the glide plays it 32 frames
|
|
43
|
+
// late: the host's ramp itself, the same whether the host calls with 1, 32 or a mix of frames. A value that follows a
|
|
44
|
+
// call of more than 32 frames came with no ramp: it is reached in a straight line over 32 frames. The first call of an
|
|
45
|
+
// instance starts on its value, and a value held 32 frames is the value itself, so a value that does not move is bit
|
|
46
|
+
// for bit what it was before the glide existed.
|
|
47
|
+
// `g`: the memory, GLIDE slots a value from j·GLIDE (made NaN): the value the last call brought, where in the ring its
|
|
48
|
+
// first frame is, its frames, the frames the line had been at that value before it (64 once held and the ring all of
|
|
49
|
+
// 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
|
|
50
|
+
// 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
|
|
51
|
+
// 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
|
|
52
|
+
// dB. Returns 1 while they move, 0 when every one is the value (then `row` holds it, filled once, and a caller may run
|
|
53
|
+
// its kernel on the value alone). Only arrays and whole numbers cross the call.
|
|
54
|
+
const GLIDE = 69
|
|
55
|
+
function glide (g, j, src, row, at, n, lin) {
|
|
56
|
+
let o = j * GLIDE, r = o + 5, v = src[0], was = g[o]
|
|
57
|
+
if (!(was === was)) { g[o] = v; g[o + 1] = 0; g[o + 2] = n; g[o + 3] = 0; g[o + 4] = 0 }
|
|
58
|
+
else if (v !== was || g[o + 3] < 64) {
|
|
59
|
+
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
|
|
60
|
+
for (let q = 0; q < L; q++) g[r + ((w - L + q) & 63)] = was + d * q / L
|
|
61
|
+
g[r + w] = v
|
|
62
|
+
g[o] = v; g[o + 1] = w; g[o + 2] = n; g[o + 3] = held; g[o + 4] = 0
|
|
63
|
+
if (held < 32) {
|
|
64
|
+
for (let i = 0; i < n; i++) {
|
|
65
|
+
let u = i < 32 ? g[r + ((w + i - 32) & 63)] : v
|
|
66
|
+
row[at + i] = lin ? Math.exp(u * (Math.LN10 / 20)) : u
|
|
67
|
+
}
|
|
68
|
+
return 1
|
|
69
|
+
}
|
|
70
|
+
} else { g[o + 1] = (g[o + 1] + g[o + 2]) & 63; g[o + 2] = n }
|
|
71
|
+
// held 32 frames: the ring made all of the value once (a move from it reads the line from there), the row filled
|
|
72
|
+
// 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
|
|
73
|
+
if (g[o + 3] < 64) { for (let q = 0; q < 64; q++) g[r + q] = v; g[o + 3] = 64 }
|
|
74
|
+
if (g[o + 4] < n) {
|
|
75
|
+
let u = lin ? Math.exp(v * (Math.LN10 / 20)) : v
|
|
76
|
+
for (let i = 0; i < n; i++) row[at + i] = u
|
|
77
|
+
g[o + 4] = n
|
|
78
|
+
}
|
|
79
|
+
return 0
|
|
20
80
|
}
|
package/cabinet.d.ts
CHANGED
|
@@ -10,3 +10,27 @@ export interface CabinetOptions {
|
|
|
10
10
|
|
|
11
11
|
/** Process a mono buffer in place; returns the same array. */
|
|
12
12
|
export default function cabinet(data: Float32Array, options?: CabinetOptions): Float32Array
|
|
13
|
+
|
|
14
|
+
/** A biquad section, normalized (a0 = 1), as `@audio/biquad` makes it. */
|
|
15
|
+
export interface Section { b0: number; b1: number; b2: number; a1: number; a2: number }
|
|
16
|
+
|
|
17
|
+
/** The biquad chain's sections at `fs`, in order: 70 Hz highpass, 110 Hz bump, 2.4 kHz peak, 4.5 and 5.2 kHz lowpasses, each frequency held at 0.49 of `fs`. */
|
|
18
|
+
export function chain(fs: number): Section[]
|
|
19
|
+
|
|
20
|
+
/** A number as a host gives a processor its values: a 1-length array. */
|
|
21
|
+
export type Value = ArrayLike<number>
|
|
22
|
+
|
|
23
|
+
/** State of `stream`: the chain's, or with an IR, that IR at unit energy and the input history it reads. */
|
|
24
|
+
export interface CabinetState { chain: Section[] | null; z: Float64Array | null; h: Float32Array | null; x: Float32Array | null; w: number }
|
|
25
|
+
|
|
26
|
+
/** State for `stream` at `fs` (default 44100); with `ir`, convolving with it. */
|
|
27
|
+
export function state(fs?: number, ir?: Float32Array): CabinetState
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Streaming form, with no latency: `length` samples of `data` in place (default all), `state` carried from call to
|
|
31
|
+
* call; `mix` read at each call, applying to the IR only. The IR runs in direct form, ir.length multiplications a
|
|
32
|
+
* sample: bit for bit the whole call with an IR of up to 1024 taps, which `@audio/reverb-convolution` 1.0 sums
|
|
33
|
+
* directly too. `mixes`: the mix for each sample, in place of `values`' (the ./audio processor's glide). Returns
|
|
34
|
+
* `data`.
|
|
35
|
+
*/
|
|
36
|
+
export function stream(data: Float32Array, values: { mix: Value }, state: CabinetState, length?: number, mixes?: Float64Array): Float32Array
|
package/cabinet.js
CHANGED
|
@@ -5,16 +5,73 @@
|
|
|
5
5
|
import convolve from '@audio/reverb-convolution'
|
|
6
6
|
import { lowpass, highpass, peaking, process as biquad } from '@audio/biquad'
|
|
7
7
|
|
|
8
|
+
/** The biquad chain's sections at `fs`, in order, each frequency held at 0.49 of the rate: past Nyquist a section's
|
|
9
|
+
* poles leave the unit circle (the 4.5 and 5.2 kHz lowpasses at rates under 10.4 kHz wrote NaN), the rule every
|
|
10
|
+
* /audio processor of @audio/filter and @audio/eq keeps */
|
|
11
|
+
export const chain = fs => {
|
|
12
|
+
const at = f => Math.min(f, fs * 0.49)
|
|
13
|
+
return [
|
|
14
|
+
highpass(at(70), 0.707, fs),
|
|
15
|
+
peaking(at(110), 1, fs, 2),
|
|
16
|
+
peaking(at(2400), 1.2, fs, 3),
|
|
17
|
+
lowpass(at(4500), 0.707, fs),
|
|
18
|
+
lowpass(at(5200), 0.707, fs),
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
|
|
8
22
|
/**
|
|
9
23
|
* @param {Float32Array} data — mono PCM, processed in place
|
|
10
24
|
* @param {object} opts — { fs=44100, ir (Float32Array — measured cab IR), mix=1 }
|
|
11
25
|
*/
|
|
12
26
|
export default function cabinet (data, { fs = 44100, ir, mix = 1 } = {}) {
|
|
13
27
|
if (ir) return convolve(data, { ir, mix, normalize: true })
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
for (let c of chain(fs)) biquad(data, c)
|
|
29
|
+
return data
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* State for stream() at `fs`: the chain's, or with `ir` that IR at unit energy (as cabinet() scales it) and the last
|
|
34
|
+
* ir.length samples of input, twice over so that they read in one run
|
|
35
|
+
*/
|
|
36
|
+
export function state (fs = 44100, ir) {
|
|
37
|
+
if (!ir) return { chain: chain(fs), z: new Float64Array(10), h: null, x: null, w: 0 }
|
|
38
|
+
let e = 0
|
|
39
|
+
for (let j = 0; j < ir.length; j++) e += ir[j] * ir[j]
|
|
40
|
+
let g = e > 0 ? 1 / Math.sqrt(e) : 1
|
|
41
|
+
return { chain: null, z: null, h: Float32Array.from(ir, v => v * g), x: new Float32Array(2 * ir.length), w: 0 }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Streaming form, with no latency: `n` samples of `data` in place, `s` carried from call to call. `p` holds mix as a
|
|
46
|
+
* host gives a processor its values, a 1-length array, read at each call; as in cabinet(), only the IR blends by it.
|
|
47
|
+
* The IR runs in direct form, ir.length multiplications a sample, the sum in cabinet()'s order (bit for bit its
|
|
48
|
+
* direct path, an IR of up to 1024 taps; @audio/reverb-convolution 1.0 runs a longer one by FFT). `mixes` (optional, a
|
|
49
|
+
* Float64Array): the mix for each of the `n` samples, in place of p's, for a mix that moves within the call (the
|
|
50
|
+
* ./audio processor's glide).
|
|
51
|
+
*/
|
|
52
|
+
export function stream (data, p, s, n = data.length, mixes) {
|
|
53
|
+
let h = s.h
|
|
54
|
+
if (!h) {
|
|
55
|
+
for (let k = 0, z = s.z; k < 5; k++) {
|
|
56
|
+
let c = s.chain[k], z1 = z[2 * k], z2 = z[2 * k + 1]
|
|
57
|
+
for (let i = 0; i < n; i++) {
|
|
58
|
+
let x = data[i], y = c.b0 * x + z1
|
|
59
|
+
z1 = c.b1 * x - c.a1 * y + z2
|
|
60
|
+
z2 = c.b2 * x - c.a2 * y
|
|
61
|
+
data[i] = y
|
|
62
|
+
}
|
|
63
|
+
z[2 * k] = z1; z[2 * k + 1] = z2
|
|
64
|
+
}
|
|
65
|
+
return data
|
|
66
|
+
}
|
|
67
|
+
let L = h.length, x = s.x, w = s.w, mix = p.mix[0]
|
|
68
|
+
for (let i = 0; i < n; i++, w = w + 1 < L ? w + 1 : 0) {
|
|
69
|
+
let v = data[i], acc = 0
|
|
70
|
+
x[w] = x[w + L] = v
|
|
71
|
+
for (let j = 0, o = w + L; j < L; j++) acc += h[j] * x[o - j]
|
|
72
|
+
let m = mixes ? mixes[i] : mix
|
|
73
|
+
data[i] = v * (1 - m) + acc * m
|
|
74
|
+
}
|
|
75
|
+
s.w = w
|
|
19
76
|
return data
|
|
20
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/amp-cabinet",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Cabinet simulation — measured-IR convolution (pair with @audio/measure-ir) or classical speaker-sim biquads",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
},
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"prepublishOnly": "node ../../../compile/tools/tested.js"
|
|
18
|
+
},
|
|
16
19
|
"files": [
|
|
17
20
|
"cabinet.js",
|
|
18
21
|
"audio.js",
|