@audio/effect-pingpong 1.1.4 → 1.1.5
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 +6 -2
- package/audio.js +9 -4
- package/package.json +1 -1
- package/ping-pong.d.ts +10 -7
- package/ping-pong.js +9 -4
package/README.md
CHANGED
|
@@ -12,15 +12,19 @@ import pingPong from '@audio/effect-pingpong'
|
|
|
12
12
|
|
|
13
13
|
Cross-fed stereo delay — left echo bounces to right, right to left.
|
|
14
14
|
|
|
15
|
-
**`time`**
|
|
15
|
+
**`time`** non-negative seconds (default 0.25) · **`feedback`** gain per repeat (default 0.4) · **`mix`** wet/dry 0–1 (default 0.5) · **`fs`** positive sample rate in Hz (default 44100)
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
import
|
|
18
|
+
import pingPong from '@audio/effect-pingpong'
|
|
19
19
|
|
|
20
20
|
let p = { time: 0.15, feedback: 0.5, mix: 0.5, fs: 44100 }
|
|
21
21
|
for (let [L, R] of stereoStream) pingPong(L, R, p)
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
Both channels must have equal lengths; a mismatch throws before processing. Delay time uses `max(1, floor(time * fs))` samples. Changing the effective length resets both rings; empty channels do not advance or reset them. Reuse one params object for successive stereo chunks, or create a fresh one to reset. Time and sample-rate validation match the mono delay. Keep `abs(feedback) < 1` for decay; the direct function does not clamp it.
|
|
25
|
+
|
|
26
|
+
The `/audio` factory limits feedback to 0–0.9 and time to 0.01–2 seconds. Time changes restart it. Its tail estimate describes 60 dB decay at the supplied settings, not a hard silence boundary; disconnected inputs drain through silent blocks.
|
|
27
|
+
|
|
24
28
|
**Use when**: stereo width from delays, spatial depth, rhythmic bounce effects<br>
|
|
25
29
|
**Not for**: mono output
|
|
26
30
|
|
package/audio.js
CHANGED
|
@@ -9,16 +9,21 @@ export const pingpong = (ctx) => {
|
|
|
9
9
|
const p = { fs: ctx.sampleRate, time: ctx.params.time[0] }
|
|
10
10
|
return (inputs, outputs, params) => {
|
|
11
11
|
const inp = inputs[0], out = outputs[0]
|
|
12
|
-
if (!inp || inp.length < 2) return
|
|
13
12
|
p.feedback = params.feedback[0]
|
|
14
13
|
p.mix = params.mix[0]
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
for (let c = 0; c < 2; c++) {
|
|
15
|
+
if (inp?.[c]?.length) out[c].set(inp[c])
|
|
16
|
+
else out[c].fill(0)
|
|
17
|
+
}
|
|
17
18
|
pingPongFn(out[0], out[1], p)
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
21
|
pingpong.channels = 2
|
|
21
|
-
pingpong.tail = ({ params }) => {
|
|
22
|
+
pingpong.tail = ({ sampleRate, params }) => {
|
|
23
|
+
const fb = Math.abs(params.feedback[0])
|
|
24
|
+
const time = Math.max(1, Math.floor(params.time[0] * sampleRate)) / sampleRate
|
|
25
|
+
return time * (fb === 0 ? 1 : 1 + Math.ceil(Math.log(1e-3) / Math.log(fb)))
|
|
26
|
+
} // Time to decay by 60 dB at the supplied settings; not a hard silence boundary.
|
|
22
27
|
pingpong.params = {
|
|
23
28
|
time: { type: 'number', min: 0.01, max: 2, default: 0.25, unit: 's', flags: ['restart'] },
|
|
24
29
|
feedback: { type: 'number', min: 0, max: 0.9, default: 0.4 },
|
package/package.json
CHANGED
package/ping-pong.d.ts
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
/** Ping-pong delay — stereo cross-fed delay; left echo bounces to right, right to left. */
|
|
2
2
|
export interface PingPongOptions {
|
|
3
|
-
/**
|
|
3
|
+
/** Non-negative seconds, default 0.25. Uses max(1, floor(time * fs)) samples. Changing that length resets both rings; empty buffers leave state unchanged. */
|
|
4
4
|
time?: number
|
|
5
|
-
/**
|
|
5
|
+
/** Gain per repeat, default 0.4. abs(feedback) < 1 decays; direct calls do not clamp. */
|
|
6
6
|
feedback?: number
|
|
7
7
|
/** wet/dry, 0–1, default 0.5 */
|
|
8
8
|
mix?: number
|
|
9
|
-
/** sample rate, default 44100 */
|
|
9
|
+
/** Finite positive sample rate in Hz, default 44100 */
|
|
10
10
|
fs?: number
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
+
* Omitted options use fresh defaults; use a fresh options object to reset.
|
|
15
|
+
* Invalid time/sample rate throws RangeError before input/state changes.
|
|
16
|
+
* Channel lengths must match; otherwise throws RangeError before processing.
|
|
14
17
|
* Mutates `left` and `right` in place and returns `[left, right]` (the same two
|
|
15
18
|
* references). Pass the same params object across calls — the two delay ring
|
|
16
19
|
* buffers and shared write pointer persist on it (`_bufL`, `_bufR`, `_ptr`).
|
|
17
20
|
*/
|
|
18
|
-
export default function pingPong(
|
|
19
|
-
left:
|
|
20
|
-
right:
|
|
21
|
+
export default function pingPong<L extends Float32Array | Float64Array, R extends Float32Array | Float64Array>(
|
|
22
|
+
left: L,
|
|
23
|
+
right: R,
|
|
21
24
|
params?: PingPongOptions
|
|
22
|
-
): [
|
|
25
|
+
): [L, R]
|
package/ping-pong.js
CHANGED
|
@@ -3,15 +3,20 @@
|
|
|
3
3
|
* Takes two channel buffers, modifies both in-place.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
export default function pingPong (left, right, params) {
|
|
6
|
+
export default function pingPong (left, right, params = {}) {
|
|
7
|
+
if (left.length !== right.length) throw new RangeError('channel lengths must match')
|
|
7
8
|
let time = params.time ?? 0.25
|
|
8
9
|
let feedback = params.feedback ?? 0.4
|
|
9
10
|
let mix = params.mix ?? 0.5
|
|
10
|
-
let fs = params.fs
|
|
11
|
+
let fs = params.fs ?? 44100
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
if (!Number.isFinite(time) || time < 0) throw new RangeError('time must be a finite non-negative number of seconds')
|
|
14
|
+
if (!Number.isFinite(fs) || fs <= 0) throw new RangeError('fs must be a finite positive sample rate')
|
|
15
|
+
let delaySamples = Math.max(1, Math.floor(time * fs))
|
|
16
|
+
if (!Number.isSafeInteger(delaySamples)) throw new RangeError('delay length must be a safe integer')
|
|
17
|
+
if (!left.length) return [left, right]
|
|
13
18
|
|
|
14
|
-
if (!params._bufL || params._bufL.length
|
|
19
|
+
if (!params._bufL || params._bufL.length !== delaySamples) {
|
|
15
20
|
params._bufL = new Float64Array(delaySamples)
|
|
16
21
|
params._bufR = new Float64Array(delaySamples)
|
|
17
22
|
params._ptr = 0
|