@audio/effect-slew 1.1.5 → 1.1.6

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
@@ -21,6 +21,7 @@ export interface SlewOptions {
21
21
  export declare const slew: {
22
22
  (ctx: Ctx): Process
23
23
  channels: "any"
24
+ tail: (at: { sampleRate: number, params: Live, buffers: {} }) => number
24
25
  params: {
25
26
  /** 10..20000 (default 1000) */
26
27
  "rise": { type: "number", default: 1000 }
package/audio.js CHANGED
@@ -1,12 +1,14 @@
1
1
  // atom manifest — wraps the slew-limiter atom; purely reactive rate-of-change
2
2
  // clamp, no sizing params — every param stays live, per-channel state is just the
3
- // running output sample.
3
+ // running output sample. The factory gives it as NaN, unseeded: a number from the start, so V8
4
+ // keeps it in place (added on the first block as 0, then made a double, it boxed once a second
5
+ // instance had run).
4
6
 
5
7
  import slewLimiterFn from './slew-limiter.js'
6
8
 
7
9
  export const slew = (ctx) => {
8
10
  const chP = []
9
- for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) chP.push({ fs: ctx.sampleRate })
11
+ for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) chP.push({ fs: ctx.sampleRate, y: NaN })
10
12
  return (inputs, outputs, params) => {
11
13
  const inp = inputs[0], out = outputs[0]
12
14
  if (!inp || !inp.length) return
@@ -20,6 +22,20 @@ export const slew = (ctx) => {
20
22
  }
21
23
  }
22
24
  slew.channels = 'any'
25
+ // once the input stops, the output falls to 0 at `fall` a second (or rises at `rise`, from below): a full-scale output,
26
+ // |y| ≤ 1, is there within 1 / min(rise, fall) seconds, and a held DC of ±1 against the slower of the two takes all of
27
+ // it. Louder signals take longer, in proportion. A ring of 128 frames or less needs no tail (CONTRACT 1.0).
28
+ // A planning snapshot's number that is not finite (automation whose values are not known yet) reserves the longer of
29
+ // the tails at the ends of its range, as effect-delay's tail reserves its maxima: through the formula it gave NaN, which
30
+ // a host takes as no tail, cutting the decay. Both ends are tried: the tail is longest at one or the other
31
+ const reserve = (f, tail) => function known (s) {
32
+ for (const k in f.params) if (f.params[k].type === 'number' && !Number.isFinite(s.params[k]?.[0])) {
33
+ const at = v => known({ ...s, params: { ...s.params, [k]: Float32Array.of(v) } })
34
+ return Math.max(at(f.params[k].min), at(f.params[k].max))
35
+ }
36
+ return tail(s)
37
+ }
38
+ slew.tail = reserve(slew, ({ sampleRate: fs, params }) => { let frames = Math.ceil(fs / Math.min(params.rise[0], params.fall[0])); return frames > 128 ? frames / fs : 0 })
23
39
  slew.params = {
24
40
  rise: { type: 'number', min: 10, max: 20000, default: 1000, curve: 'log' },
25
41
  fall: { type: 'number', min: 10, max: 20000, default: 1000, curve: 'log' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/effect-slew",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "Rate-of-change limiter",
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
  "slew-limiter.js",
18
21
  "audio.js",
package/slew-limiter.d.ts CHANGED
@@ -11,6 +11,6 @@ export interface SlewLimiterOptions {
11
11
  /**
12
12
  * Mutates `data` in place and returns the same reference. Pass the same params
13
13
  * object across calls — the running output sample persists on it (`y`, seeded
14
- * from `data[0]` on the first call if absent).
14
+ * from `data[0]` on the first call if absent or NaN).
15
15
  */
16
16
  export default function slewLimiter<T extends Float32Array | Float64Array>(data: T, params?: SlewLimiterOptions): T
package/slew-limiter.js CHANGED
@@ -11,7 +11,7 @@ export default function slewLimiter (data, params = {}) {
11
11
  let maxRise = rise / fs
12
12
  let maxFall = fall / fs
13
13
 
14
- let y = params.y != null ? params.y : data[0]
14
+ let y = params.y == null || params.y !== params.y ? data[0] : params.y // seeded from the first sample when absent or NaN
15
15
 
16
16
  for (let i = 0, l = data.length; i < l; i++) {
17
17
  let diff = data[i] - y