@audio/effect-flanger 2.0.2 → 2.1.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
@@ -12,9 +12,11 @@ import flanger from '@audio/effect-flanger'
12
12
 
13
13
  Modulated short delay (1–10 ms) with feedback — creates comb filter sweep.
14
14
 
15
- **`rate`** LFO rate in Hz (default 0.3) · **`depth`** modulation depth 0–1 (default 0.7) · **`delay`** center delay in seconds (default 0.003) · **`feedback`** 0–1 (default 0.5) · **`fs`** sample rate
15
+ **`rate`** LFO rate in Hz (default 0.3) · **`depth`** modulation depth 0–1 (default 0.7) · **`delay`** center delay in seconds (default 0.003) · **`feedback`** 0–1 (default 0.5) · **`phase`** LFO phase of each channel ahead of the one before, degrees −180–180 (default 90; 0 moves the channels together) · **`channel`** index of the channel this call processes (default 0) · **`fs`** sample rate
16
16
 
17
- A zero center delay uses a one-sample feedback ring. Sub-sample ring sizes are clamped to one sample.
17
+ In stereo, each channel keeps its own params object with its `channel` index (the `/audio` factory does this), so identical left and right come out different; channel 0 never shifts, so a mono signal is the same at any `phase`. Channel c runs c × `phase` ahead, so at 90 channel 4 is a whole cycle on and moves with channel 0. A change of `phase`, `depth` or `delay` glides across the next call.
18
+
19
+ A zero center delay uses a one-sample feedback ring. The ring is floor(2 · `delay` · `fs`) + 1 samples, enough for the farthest read (twice the center delay, at depth 1), so a center delay under half a sample also gets one sample. A ring made for a longer delay holds a shorter one, every sample at its age; the `/audio` factory makes it for the longest delay, 0.02 seconds. The swept delay is held at one sample or more, as the feedback loop cannot read the sample it is about to write.
18
20
 
19
21
  ```js
20
22
  import flanger from '@audio/effect-flanger'
package/audio.d.ts CHANGED
@@ -18,6 +18,8 @@ export interface FlangerOptions {
18
18
  "delay"?: Auto
19
19
  /** 0..0.9 (default 0.5) */
20
20
  "feedback"?: Auto
21
+ /** -180..180 deg (default 90) */
22
+ "phase"?: Auto
21
23
  at?: number | string
22
24
  duration?: number | string
23
25
  }
@@ -25,15 +27,17 @@ export interface FlangerOptions {
25
27
  export declare const flanger: {
26
28
  (ctx: Ctx): Process
27
29
  channels: "any"
28
- tail: (ctx: { sampleRate: number, params: Live }) => number
30
+ tail: (at: { sampleRate: number, params: Live, buffers: {} }) => number
29
31
  params: {
30
32
  /** 0.02..5 Hz (default 0.3) */
31
33
  "rate": { type: "number", default: 0.3 }
32
34
  /** 0..1 (default 0.7) */
33
35
  "depth": { type: "number", default: 0.7 }
34
- /** 0.0001..0.02 s (default 0.003) [restart] */
36
+ /** 0.0001..0.02 s (default 0.003) */
35
37
  "delay": { type: "number", default: 0.003 }
36
38
  /** 0..0.9 (default 0.5) */
37
39
  "feedback": { type: "number", default: 0.5 }
40
+ /** -180..180 deg (default 90) */
41
+ "phase": { type: "number", default: 90 }
38
42
  }
39
43
  }
package/audio.js CHANGED
@@ -1,12 +1,15 @@
1
- // atom manifest — wraps the flanger atom; state rides per-channel params objects.
2
- // `delay` sizes the buffer at construction (flags: restart); rate/depth/feedback are live.
1
+ // atom manifest — wraps the flanger atom; state rides per-channel params objects, made here:
2
+ // the ring for the longest delay (delay.max, as the host hands it, a Float32), so the first block
3
+ // sets nothing up and every param is live. A new delay glides across the call as depth and phase do,
4
+ // its centre moving no faster than the read's half and double speed allow.
5
+ // Each channel's state carries its index, so its LFO runs `phase` degrees ahead of the channel before.
3
6
 
4
7
  import flangerFn from './flanger.js'
5
8
 
6
9
  export const flanger = (ctx) => {
7
- const chP = []
10
+ const chP = [], ring = Math.floor(Math.fround(flanger.params.delay.max) * 2 * ctx.sampleRate) + 1 // as flanger.js sizes it for a delay
8
11
  for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++)
9
- chP.push({ fs: ctx.sampleRate, delay: ctx.params.delay[0] })
12
+ chP.push({ fs: ctx.sampleRate, channel: c, buffer: new Float64Array(ring), ptr: 0, _phase: 0, _glide: new Float64Array(4).fill(NaN) })
10
13
  return (inputs, outputs, params) => {
11
14
  const inp = inputs[0], out = outputs[0]
12
15
  if (!inp || !inp.length) return
@@ -14,17 +17,55 @@ export const flanger = (ctx) => {
14
17
  const p = chP[c]
15
18
  p.rate = params.rate[0]
16
19
  p.depth = params.depth[0]
20
+ p.delay = params.delay[0]
17
21
  p.feedback = params.feedback[0]
22
+ p.phase = params.phase[0]
18
23
  out[c].set(inp[c])
19
24
  flangerFn(out[c], p)
20
25
  }
21
26
  }
22
27
  }
23
28
  flanger.channels = 'any'
24
- flanger.tail = ({ params }) => { let fb = params.feedback[0]; let reps = fb > 0 ? Math.log(1e-3) / Math.log(fb) : 1; return Math.max(0.05, (reps + 1) * 0.02) } // RT60 from live feedback × delay.max(0.02s)
29
+ // A planning snapshot's number that is not finite (automation whose values are not known yet) reserves the longer of
30
+ // the tails at the ends of its range, as effect-delay's tail reserves its maxima: a NaN feedback read as none gives
31
+ // 5 ms at the defaults (48 kHz), where 0.9 rings 0.45 s. Both ends are tried: the tail is longest at one or the other
32
+ const reserve = (f, tail) => function known (s) {
33
+ for (const k in f.params) if (f.params[k].type === 'number' && !Number.isFinite(s.params[k]?.[0])) {
34
+ const at = v => known({ ...s, params: { ...s.params, [k]: Float32Array.of(v) } })
35
+ return Math.max(at(f.params[k].min), at(f.params[k].max))
36
+ }
37
+ return tail(s)
38
+ }
39
+ // The echoes a loop of feedback fb leaves, the m-th back at fb^(m − 1) beside the dry input, until the rest of them is
40
+ // under 1e-3 (the tail, CONTRACT 1.0: 60 dB under the input's level): after n the rest of Σ|h| is fb^n / (1 − fb),
41
+ // which a held DC meets. One where nothing feeds back
42
+ const echoes = fb => fb > 0 ? Math.max(1, Math.ceil(Math.log(1e-3 * (1 - fb)) / Math.log(fb))) : 1
43
+ // A sweep's tail, in frames: the frozen ring T at 64 points of the LFO's cycle, ring(lfo) for lfo the sine there, a
44
+ // point every dt frames. From the point where the input stops, each step takes dt / T of the ring (T of the slower end
45
+ // of the step), and the ring ends where that sums to 1; the tail is the longest of the 64 starts. Held, it is T
46
+ function swept (ring, dt) {
47
+ let T = Array.from({ length: 64 }, (_, k) => ring(Math.sin(2 * Math.PI * k / 64))), v = T.map((t, k) => 1 / Math.max(t, T[(k + 1) % 64]))
48
+ let cycle = v.reduce((a, b) => a + b, 0) * dt, most = 0
49
+ if (!(cycle > 0)) return Infinity
50
+ for (let k0 = 0; k0 < 64; k0++) {
51
+ let laps = Math.max(0, Math.floor(1 / cycle) - 1), sum = laps * cycle, k = k0
52
+ while (sum + v[k % 64] * dt < 1) sum += v[k++ % 64] * dt
53
+ most = Math.max(most, (laps * 64 + k - k0) * dt + (1 - sum) / v[k % 64])
54
+ }
55
+ return most
56
+ }
57
+ // The read sits delay · (1 + depth · sin) back (flanger.js), one frame at least, between two samples: an echo comes
58
+ // back each ceil of that, so the frozen ring is the echoes times it, and as the LFO moves it a pass takes as long as
59
+ // the read is far. A ring of 128 frames or less needs no tail (CONTRACT 1.0)
60
+ flanger.tail = reserve(flanger, ({ sampleRate: fs, params }) => {
61
+ let n = echoes(params.feedback[0]), depth = params.depth[0], d = lfo => Math.ceil(Math.max(1, params.delay[0] * fs * (1 + depth * lfo)))
62
+ let frames = depth > 0 ? swept(lfo => n * d(lfo), fs / (64 * params.rate[0])) : n * d(0)
63
+ return frames > 128 ? frames / fs : 0
64
+ })
25
65
  flanger.params = {
26
66
  rate: { type: 'number', min: 0.02, max: 5, default: 0.3, unit: 'Hz', curve: 'log' },
27
- depth: { type: 'number', min: 0, max: 1, default: 0.7 },
28
- delay: { type: 'number', min: 0.0001, max: 0.02, default: 0.003, unit: 's', flags: ['restart'] },
67
+ depth: { type: 'number', min: 0, max: 1, default: 0.7, smoothing: 0.02 },
68
+ delay: { type: 'number', min: 0.0001, max: 0.02, default: 0.003, unit: 's', smoothing: 0.02 },
29
69
  feedback: { type: 'number', min: 0, max: 0.9, default: 0.5 },
70
+ phase: { type: 'number', min: -180, max: 180, default: 90, unit: 'deg', smoothing: 0.02 },
30
71
  }
package/flanger.d.ts CHANGED
@@ -2,19 +2,24 @@
2
2
  export interface FlangerOptions {
3
3
  /** LFO rate, Hz, default 0.3 */
4
4
  rate?: number
5
- /** modulation depth, 0–1, default 0.7 */
5
+ /** modulation depth, 0–1, default 0.7; a change glides across the next call */
6
6
  depth?: number
7
- /** center delay, seconds, default 0.003. Changing this reallocates the delay buffer, dropping buffered history. */
7
+ /** center delay, seconds, default 0.003; the swept delay is held at one sample or more, as the feedback loop needs. A change glides across the next call, its centre moving at most half a frame a frame farther or a frame nearer, each over 1 + depth, so that the read runs between half and double speed besides its LFO; a farther move goes on across the calls after. A ring made for a longer delay holds a shorter one; a longer delay than the ring holds makes a ring for it anew, dropping buffered history. */
8
8
  delay?: number
9
9
  /** feedback, 0–0.9, default 0.5 */
10
10
  feedback?: number
11
+ /** LFO phase of each channel ahead of the one before, degrees, −180–180, default 90 (0 moves the channels together); a change glides across the next call */
12
+ phase?: number
13
+ /** index of the channel this call processes, default 0; its LFO runs `channel × phase` degrees ahead */
14
+ channel?: number
11
15
  /** sample rate, default 44100 */
12
16
  fs?: number
13
17
  }
14
18
 
15
19
  /**
16
20
  * Mutates `data` in place and returns the same reference. Pass the same params
17
- * object across calls — the delay ring buffer, write pointer, and LFO phase
18
- * persist on it (`buffer`, `ptr`, `_phase`).
21
+ * object across calls — the delay ring buffer, write pointer, LFO phase,
22
+ * and where the last call's lead, depth and delay ended and where the read's centre was persist on it (`buffer`,
23
+ * `ptr`, `_phase`, `_glide`).
19
24
  */
20
25
  export default function flanger<T extends Float32Array | Float64Array>(data: T, params?: FlangerOptions): T
package/flanger.js CHANGED
@@ -4,48 +4,82 @@
4
4
 
5
5
  let {sin, floor, PI} = Math
6
6
 
7
+ // How far the centre of the read may move in a frame while the delay moves, as effect-delay's read moves while its time
8
+ // does: a tape head runs no slower than half speed and no faster than double. The read sits d · (1 + dp · sin) back, so
9
+ // the centre d moves by at most GROW / (1 + dp) a frame as it grows and SHRINK / (1 + dp) as it shrinks, and the move
10
+ // bends the read by an octave at most, besides its LFO
11
+ const GROW = 0.5, SHRINK = 1
12
+
7
13
  export default function flanger (data, params = {}) {
8
14
  let rate = params.rate == null ? 0.3 : params.rate
9
15
  let depth = params.depth == null ? 0.7 : params.depth
10
16
  let delay = params.delay == null ? 0.003 : params.delay // seconds
11
17
  let feedback = params.feedback == null ? 0.5 : params.feedback
18
+ // stereo: channel c's LFO runs c × `phase` degrees ahead of channel 0's (Logic Pro's Phase: 0 moves the channels
19
+ // together, ±180 sets them farthest apart); default 90, quadrature, as Korg's X5DR Quadrature Chorus
20
+ let offset = params.phase == null ? 90 : params.phase
21
+ let channel = params.channel || 0 // index of the channel this call processes
12
22
  let fs = params.fs || 44100
13
23
 
14
- let maxDelay = Math.max(1, Math.floor(delay * 2 * fs))
24
+ // the read comes before this sample's write, so the ring holds delays 1 to maxDelay. A read at d takes the samples
25
+ // ceil(d) and ceil(d) − 1 back; the farthest d, 2·delay·fs at depth 1 (a hair over when rounding lifts it past a
26
+ // whole number), needs floor(2·delay·fs) + 1 back, and the nearest is held at 1, as the feedback loop cannot read
27
+ // the sample it is about to write
28
+ let maxDelay = Math.floor(delay * 2 * fs) + 1
15
29
  let delaySamples = delay * fs
30
+ let lead = channel ? channel * offset * PI / 180 : 0 // rad; 0 on channel 0, so a mono signal never depends on phase
31
+ // a new lead is reached across this call from the one the last call ended on, so the read glides instead of jumping;
32
+ // a host ramps a smoothed `phase` in short calls, and this joins its steps into one line
33
+ // where the last call ended, for this one to glide from: NaN before the first call (only finite values are kept,
34
+ // in a Float64Array: kept as numbers on the params object, they boxed in V8 once two instances had run)
35
+ let ended = params._glide ??= new Float64Array(4).fill(NaN)
36
+ let from = ended[0] === ended[0] ? ended[0] : lead, glide = lead - from
37
+ // depth moves the read too, so a new depth glides across the call in the same way, and so does the delay, its centre
38
+ // following that line at the speed it asks, up to GROW and SHRINK, a farther move going on across the calls that
39
+ // follow until it is on the delay (where the centre was is kept, in frames)
40
+ let depthFrom = ended[1] === ended[1] ? ended[1] : depth, depthGlide = depth - depthFrom
41
+ let delayFrom = ended[2] === ended[2] ? ended[2] : delaySamples, delayGlide = delaySamples - delayFrom, at = ended[3] === ended[3] ? ended[3] : delayFrom
16
42
 
43
+ // a ring made for a longer delay holds this one: its whole length is the ring, so a shorter delay keeps every
44
+ // sample at its age. A longer delay than it holds makes a ring for it anew
17
45
  if (!params.buffer || params.buffer.length < maxDelay) {
18
46
  params.buffer = new Float64Array(maxDelay)
19
47
  params.ptr = 0
20
48
  params._phase = 0
21
49
  }
22
- let buf = params.buffer, ptr = params.ptr, phase = params._phase
23
- if (ptr >= maxDelay) ptr = 0 // delay shrank live — re-enter the active ring
50
+ let buf = params.buffer, ptr = params.ptr, phase = params._phase, N = buf.length
24
51
  let inc = 2 * PI * rate / fs
25
52
 
26
53
  for (let i = 0, l = data.length; i < l; i++) {
27
- let lfo = sin(phase)
54
+ let lfo = sin(phase + from + glide * (i + 1) / l)
28
55
  phase += inc
29
56
  if (phase > 2 * PI) phase -= 2 * PI
30
57
 
31
- let d = delaySamples * (1 + depth * lfo)
32
- // total wrap — d may reach/exceed maxDelay (depth ≈ 1 vs floored buffer size)
33
- let readPos = ((ptr - d) % maxDelay + maxDelay) % maxDelay
58
+ let dp = depthFrom + depthGlide * (i + 1) / l, want = delayFrom + delayGlide * (i + 1) / l
59
+ at = want > at + GROW / (1 + dp) ? at + GROW / (1 + dp) : want < at - SHRINK / (1 + dp) ? at - SHRINK / (1 + dp) : want
60
+ let d = Math.max(1, at * (1 + dp * lfo))
61
+ // ptr − d is negative near the ring's start; the ring holds every d of depth 0 to 1
62
+ let readPos = ((ptr - d) % N + N) % N
34
63
 
35
64
  let idx = floor(readPos)
36
65
  let frac = readPos - idx
37
- let a = buf[idx % maxDelay]
38
- let b = buf[(idx + 1) % maxDelay]
66
+ let a = buf[idx % N]
67
+ let b = buf[(idx + 1) % N]
39
68
  let delayed = a + frac * (b - a)
40
69
 
41
70
  let x = data[i]
42
71
  buf[ptr] = x + feedback * delayed
43
- ptr = (ptr + 1) % maxDelay
72
+ ptr = (ptr + 1) % N
44
73
  data[i] = x + delayed
45
74
  }
46
75
 
47
76
  params.ptr = ptr
48
77
  params._phase = phase
78
+ // kept only when finite, so the next call never glides from a value that is not finite
79
+ if (data.length && Number.isFinite(lead)) ended[0] = lead
80
+ if (data.length && Number.isFinite(depth)) ended[1] = depth
81
+ if (data.length && Number.isFinite(delaySamples)) ended[2] = delaySamples
82
+ if (data.length && at === at) ended[3] = at
49
83
 
50
84
  return data
51
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/effect-flanger",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "Flanger — modulated short delay with feedback",
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
  "flanger.js",
18
21
  "audio.js",