@codexo/exojs-audio-fx 0.19.0 → 0.19.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/dist/esm/AudioAnalyser.d.ts +6 -8
- package/dist/esm/AudioAnalyser.d.ts.map +1 -1
- package/dist/esm/AudioAnalyser.js +6 -80
- package/dist/esm/AudioAnalyser.js.map +1 -1
- package/dist/esm/AudioTap.d.ts +33 -0
- package/dist/esm/AudioTap.d.ts.map +1 -0
- package/dist/esm/AudioTap.js +126 -0
- package/dist/esm/AudioTap.js.map +1 -0
- package/dist/esm/BeatDetector.d.ts +14 -9
- package/dist/esm/BeatDetector.d.ts.map +1 -1
- package/dist/esm/BeatDetector.js +30 -84
- package/dist/esm/BeatDetector.js.map +1 -1
- package/dist/esm/PcmStreamSource.d.ts +6 -1
- package/dist/esm/PcmStreamSource.d.ts.map +1 -1
- package/dist/esm/PcmStreamSource.js +6 -1
- package/dist/esm/PcmStreamSource.js.map +1 -1
- package/dist/esm/ReadyGate.d.ts +25 -0
- package/dist/esm/ReadyGate.d.ts.map +1 -0
- package/dist/esm/ReadyGate.js +69 -0
- package/dist/esm/ReadyGate.js.map +1 -0
- package/dist/esm/effects/AutoWahEffect.d.ts +0 -1
- package/dist/esm/effects/AutoWahEffect.d.ts.map +1 -1
- package/dist/esm/effects/AutoWahEffect.js +3 -8
- package/dist/esm/effects/AutoWahEffect.js.map +1 -1
- package/dist/esm/effects/ChorusEffect.d.ts +11 -5
- package/dist/esm/effects/ChorusEffect.d.ts.map +1 -1
- package/dist/esm/effects/ChorusEffect.js +19 -16
- package/dist/esm/effects/ChorusEffect.js.map +1 -1
- package/dist/esm/effects/CompressorEffect.d.ts +0 -1
- package/dist/esm/effects/CompressorEffect.d.ts.map +1 -1
- package/dist/esm/effects/CompressorEffect.js +3 -8
- package/dist/esm/effects/CompressorEffect.js.map +1 -1
- package/dist/esm/effects/ConvolutionEffect.d.ts +0 -1
- package/dist/esm/effects/ConvolutionEffect.d.ts.map +1 -1
- package/dist/esm/effects/ConvolutionEffect.js +3 -8
- package/dist/esm/effects/ConvolutionEffect.js.map +1 -1
- package/dist/esm/effects/DelayEffect.d.ts +0 -1
- package/dist/esm/effects/DelayEffect.d.ts.map +1 -1
- package/dist/esm/effects/DelayEffect.js +3 -8
- package/dist/esm/effects/DelayEffect.js.map +1 -1
- package/dist/esm/effects/DistortionEffect.d.ts +9 -13
- package/dist/esm/effects/DistortionEffect.d.ts.map +1 -1
- package/dist/esm/effects/DistortionEffect.js +11 -16
- package/dist/esm/effects/DistortionEffect.js.map +1 -1
- package/dist/esm/effects/DuckingEffect.d.ts.map +1 -1
- package/dist/esm/effects/DuckingEffect.js +1 -7
- package/dist/esm/effects/DuckingEffect.js.map +1 -1
- package/dist/esm/effects/EqualizerEffect.d.ts +0 -1
- package/dist/esm/effects/EqualizerEffect.d.ts.map +1 -1
- package/dist/esm/effects/EqualizerEffect.js +3 -8
- package/dist/esm/effects/EqualizerEffect.js.map +1 -1
- package/dist/esm/effects/FlangerEffect.d.ts +12 -4
- package/dist/esm/effects/FlangerEffect.d.ts.map +1 -1
- package/dist/esm/effects/FlangerEffect.js +17 -15
- package/dist/esm/effects/FlangerEffect.js.map +1 -1
- package/dist/esm/effects/LimiterEffect.d.ts +12 -8
- package/dist/esm/effects/LimiterEffect.d.ts.map +1 -1
- package/dist/esm/effects/LimiterEffect.js +12 -13
- package/dist/esm/effects/LimiterEffect.js.map +1 -1
- package/dist/esm/effects/PhaserEffect.d.ts +0 -1
- package/dist/esm/effects/PhaserEffect.d.ts.map +1 -1
- package/dist/esm/effects/PhaserEffect.js +3 -8
- package/dist/esm/effects/PhaserEffect.js.map +1 -1
- package/dist/esm/effects/PingPongDelayEffect.d.ts +0 -1
- package/dist/esm/effects/PingPongDelayEffect.d.ts.map +1 -1
- package/dist/esm/effects/PingPongDelayEffect.js +3 -8
- package/dist/esm/effects/PingPongDelayEffect.js.map +1 -1
- package/dist/esm/effects/ReverbEffect.d.ts +0 -1
- package/dist/esm/effects/ReverbEffect.d.ts.map +1 -1
- package/dist/esm/effects/ReverbEffect.js +3 -8
- package/dist/esm/effects/ReverbEffect.js.map +1 -1
- package/dist/esm/effects/RingModulatorEffect.d.ts +0 -1
- package/dist/esm/effects/RingModulatorEffect.d.ts.map +1 -1
- package/dist/esm/effects/RingModulatorEffect.js +3 -8
- package/dist/esm/effects/RingModulatorEffect.js.map +1 -1
- package/dist/esm/effects/TremoloEffect.d.ts +12 -15
- package/dist/esm/effects/TremoloEffect.d.ts.map +1 -1
- package/dist/esm/effects/TremoloEffect.js +15 -20
- package/dist/esm/effects/TremoloEffect.js.map +1 -1
- package/dist/esm/effects/VocoderEffect.d.ts.map +1 -1
- package/dist/esm/effects/VocoderEffect.js +2 -7
- package/dist/esm/effects/VocoderEffect.js.map +1 -1
- package/dist/esm/worklets/beat-detector.worklet.js +1 -1
- package/dist/esm/worklets/beat-detector.worklet.js.map +1 -1
- package/dist/esm/worklets/bit-crusher.worklet.js +1 -1
- package/dist/esm/worklets/bit-crusher.worklet.js.map +1 -1
- package/dist/esm/worklets/disposable-processor.d.ts +13 -0
- package/dist/esm/worklets/disposable-processor.d.ts.map +1 -0
- package/dist/esm/worklets/ducking.worklet.js +1 -1
- package/dist/esm/worklets/ducking.worklet.js.map +1 -1
- package/dist/esm/worklets/granular.worklet.js +1 -1
- package/dist/esm/worklets/granular.worklet.js.map +1 -1
- package/dist/esm/worklets/pitch-shift.worklet.js +1 -1
- package/dist/esm/worklets/pitch-shift.worklet.js.map +1 -1
- package/dist/esm/worklets/vocoder.worklet.js +1 -1
- package/dist/esm/worklets/vocoder.worklet.js.map +1 -1
- package/package.json +3 -3
|
@@ -2,11 +2,8 @@ import { AudioEffect } from '@codexo/exojs';
|
|
|
2
2
|
/** Construction options for {@link DistortionEffect}. */
|
|
3
3
|
export interface DistortionEffectOptions {
|
|
4
4
|
/**
|
|
5
|
-
* Drive amount
|
|
6
|
-
*
|
|
7
|
-
* distortion; lower values produce a milder soft-clip. Note that even at
|
|
8
|
-
* drive=0 the curve is not perfectly linear - the formula
|
|
9
|
-
* `tanh(x) / tanh(1)` has a gain of `1/tanh(1) ≈ 1.31` at the origin.
|
|
5
|
+
* Drive amount, 0..1, default 0.4. At 0 the wet path is transparent; higher
|
|
6
|
+
* values blend in a steeper tanh soft-clip, up to a near hard clip at 1.
|
|
10
7
|
*/
|
|
11
8
|
drive?: number;
|
|
12
9
|
/**
|
|
@@ -61,7 +58,6 @@ export declare class DistortionEffect extends AudioEffect {
|
|
|
61
58
|
private _wet;
|
|
62
59
|
private _oversample;
|
|
63
60
|
private _tone;
|
|
64
|
-
private readonly _onAudioContextReady;
|
|
65
61
|
constructor(options?: DistortionEffectOptions);
|
|
66
62
|
/**
|
|
67
63
|
* The node where audio enters this effect. Throws if the audio context is
|
|
@@ -74,7 +70,7 @@ export declare class DistortionEffect extends AudioEffect {
|
|
|
74
70
|
*/
|
|
75
71
|
get outputNode(): AudioNode;
|
|
76
72
|
/**
|
|
77
|
-
* Drive amount
|
|
73
|
+
* Drive amount, 0..1: 0 is transparent, 1 a near hard clip. Default 0.4.
|
|
78
74
|
* Changing this rebuilds the wave-shaper curve immediately (sample-accurate
|
|
79
75
|
* on the next render quantum).
|
|
80
76
|
*/
|
|
@@ -101,13 +97,13 @@ export declare class DistortionEffect extends AudioEffect {
|
|
|
101
97
|
set tone(value: number);
|
|
102
98
|
destroy(): void;
|
|
103
99
|
/**
|
|
104
|
-
* Build a 256-point
|
|
100
|
+
* Build a 256-point curve for the given drive (0..1): the identity blended
|
|
101
|
+
* with a soft clip `tanh(x * a) / tanh(a)`, `a = drive * 400 + 1`, by `drive`.
|
|
105
102
|
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* linear. At drive=1, `a = 401` and the curve approximates a hard-clipper.
|
|
103
|
+
* The blend is what makes drive=0 exactly transparent. A bare soft clip is
|
|
104
|
+
* never linear (at a = 1 its small-signal gain is 1/tanh(1) ≈ 1.31), and
|
|
105
|
+
* special-casing 0 would make the sound jump at the first step above it.
|
|
106
|
+
* Both curves run from -1 to 1 and rise monotonically, so the blend does too.
|
|
111
107
|
*/
|
|
112
108
|
private static _buildCurve;
|
|
113
109
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DistortionEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/DistortionEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"DistortionEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/DistortionEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,yDAAyD;AACzD,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,UAAU,CAAC,EAAE,cAAc,CAAC;IAC5B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAWD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,gBAAiB,SAAQ,WAAW;IAC/C,OAAO,CAAC,MAAM,CAAsC;IACpD,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,KAAK,CAAS;IAEtB,YAAmB,OAAO,GAAE,uBAA4B,EAOvD;IAED;;;OAGG;IACH,IAAW,SAAS,IAAI,SAAS,CAGhC;IAED;;;OAGG;IACH,IAAW,UAAU,IAAI,SAAS,CAGjC;IAED;;;;OAIG;IACH,IAAW,KAAK,IAAI,MAAM,CAEzB;IAED,IAAW,KAAK,CAAC,KAAK,EAAE,MAAM,EAK7B;IAED;;;OAGG;IACH,IAAW,GAAG,IAAI,MAAM,CAEvB;IAED,IAAW,GAAG,CAAC,KAAK,EAAE,MAAM,EAO3B;IAED;;;OAGG;IACH,IAAW,UAAU,IAAI,cAAc,CAEtC;IAED,IAAW,UAAU,CAAC,KAAK,EAAE,cAAc,EAK1C;IAED;;;;OAIG;IACH,IAAW,IAAI,IAAI,MAAM,CAExB;IAED,IAAW,IAAI,CAAC,KAAK,EAAE,MAAM,EAM5B;IAEe,OAAO,IAAI,IAAI,CAW9B;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,MAAM,CAAC,WAAW;IAY1B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAK/B,OAAO,CAAC,WAAW;CA+BpB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AudioEffect
|
|
1
|
+
import { AudioEffect } from "@codexo/exojs";
|
|
2
2
|
|
|
3
3
|
//#region src/effects/DistortionEffect.ts
|
|
4
4
|
/**
|
|
@@ -36,18 +36,13 @@ var DistortionEffect = class DistortionEffect extends AudioEffect {
|
|
|
36
36
|
_wet;
|
|
37
37
|
_oversample;
|
|
38
38
|
_tone;
|
|
39
|
-
_onAudioContextReady = (ctx) => {
|
|
40
|
-
onAudioContextReady.remove(this._onAudioContextReady);
|
|
41
|
-
this._setupNodes(ctx);
|
|
42
|
-
};
|
|
43
39
|
constructor(options = {}) {
|
|
44
40
|
super();
|
|
45
41
|
this._drive = Math.max(0, Math.min(1, options.drive ?? .4));
|
|
46
42
|
this._wet = Math.max(0, Math.min(1, options.wet ?? 1));
|
|
47
43
|
this._oversample = options.oversample ?? "2x";
|
|
48
44
|
this._tone = Math.max(0, Math.min(1, options.tone ?? 1));
|
|
49
|
-
|
|
50
|
-
else onAudioContextReady.add(this._onAudioContextReady);
|
|
45
|
+
this._deferSetup((context) => this._setupNodes(context));
|
|
51
46
|
}
|
|
52
47
|
/**
|
|
53
48
|
* The node where audio enters this effect. Throws if the audio context is
|
|
@@ -66,7 +61,7 @@ var DistortionEffect = class DistortionEffect extends AudioEffect {
|
|
|
66
61
|
return this._setup.outputGain;
|
|
67
62
|
}
|
|
68
63
|
/**
|
|
69
|
-
* Drive amount
|
|
64
|
+
* Drive amount, 0..1: 0 is transparent, 1 a near hard clip. Default 0.4.
|
|
70
65
|
* Changing this rebuilds the wave-shaper curve immediately (sample-accurate
|
|
71
66
|
* on the next render quantum).
|
|
72
67
|
*/
|
|
@@ -119,7 +114,7 @@ var DistortionEffect = class DistortionEffect extends AudioEffect {
|
|
|
119
114
|
}
|
|
120
115
|
}
|
|
121
116
|
destroy() {
|
|
122
|
-
|
|
117
|
+
this._teardown();
|
|
123
118
|
if (this._setup) {
|
|
124
119
|
this._setup.inputGain.disconnect();
|
|
125
120
|
this._setup.waveShaper.disconnect();
|
|
@@ -131,13 +126,13 @@ var DistortionEffect = class DistortionEffect extends AudioEffect {
|
|
|
131
126
|
}
|
|
132
127
|
}
|
|
133
128
|
/**
|
|
134
|
-
* Build a 256-point
|
|
129
|
+
* Build a 256-point curve for the given drive (0..1): the identity blended
|
|
130
|
+
* with a soft clip `tanh(x * a) / tanh(a)`, `a = drive * 400 + 1`, by `drive`.
|
|
135
131
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* linear. At drive=1, `a = 401` and the curve approximates a hard-clipper.
|
|
132
|
+
* The blend is what makes drive=0 exactly transparent. A bare soft clip is
|
|
133
|
+
* never linear (at a = 1 its small-signal gain is 1/tanh(1) ≈ 1.31), and
|
|
134
|
+
* special-casing 0 would make the sound jump at the first step above it.
|
|
135
|
+
* Both curves run from -1 to 1 and rise monotonically, so the blend does too.
|
|
141
136
|
*/
|
|
142
137
|
static _buildCurve(drive) {
|
|
143
138
|
const n = 256;
|
|
@@ -146,7 +141,7 @@ var DistortionEffect = class DistortionEffect extends AudioEffect {
|
|
|
146
141
|
const norm = Math.tanh(amount);
|
|
147
142
|
for (let i = 0; i < n; i++) {
|
|
148
143
|
const x = i * 2 / 255 - 1;
|
|
149
|
-
curve[i] = Math.tanh(x * amount) / norm;
|
|
144
|
+
curve[i] = x * (1 - drive) + Math.tanh(x * amount) / norm * drive;
|
|
150
145
|
}
|
|
151
146
|
return curve;
|
|
152
147
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DistortionEffect.js","names":[],"sources":["../../../src/effects/DistortionEffect.ts"],"sourcesContent":["import { AudioEffect, getAudioContext, isAudioContextReady, onAudioContextReady } from '@codexo/exojs';\n\n/** Construction options for {@link DistortionEffect}. */\nexport interface DistortionEffectOptions {\n /**\n * Drive amount: controls the intensity of the tanh soft-clip curve.\n * Range 0..1, default 0.4. Higher values produce heavier, more saturated\n * distortion; lower values produce a milder soft-clip. Note that even at\n * drive=0 the curve is not perfectly linear - the formula\n * `tanh(x) / tanh(1)` has a gain of `1/tanh(1) ≈ 1.31` at the origin.\n */\n drive?: number;\n /**\n * Dry/wet mix, 0..1. The dry level is automatically `1 - wet`. Default 1.0.\n */\n wet?: number;\n /**\n * Oversampling mode applied by the `WaveShaperNode`. Higher oversampling\n * reduces aliasing artifacts at the cost of extra CPU. One of `'none'`,\n * `'2x'`, or `'4x'`. Default `'2x'`.\n */\n oversample?: OverSampleType;\n /**\n * Tone control: cutoff of a post-distortion low-pass filter on the wet path.\n * Mapped logarithmically from 100 Hz (tone=0, fully dark) to 20 kHz\n * (tone=1, fully open). Range 0..1, default 1.\n */\n tone?: number;\n}\n\ninterface DistortionEffectSetup {\n readonly inputGain: GainNode;\n readonly outputGain: GainNode;\n readonly dryGain: GainNode;\n readonly wetGain: GainNode;\n readonly waveShaper: WaveShaperNode;\n readonly toneFilter: BiquadFilterNode;\n}\n\n/**\n * Distortion effect built from native Web Audio nodes - no worklet required.\n *\n * The input signal is split into a dry path and a wet path. The wet path runs\n * through a `WaveShaperNode` loaded with a tanh-based soft-clip curve, then\n * through a `BiquadFilterNode` (lowpass) acting as a tone control, before\n * being mixed back with the dry signal. The wave-shaper curve is rebuilt any\n * time `drive` changes. Oversampling is delegated directly to\n * `WaveShaperNode.oversample`.\n *\n * Node graph:\n * ```\n * input(GainNode) ──── dryGain ─────────────────────────── output(GainNode)\n * input ──── waveShaper ── toneFilter ── wetGain ── output\n * ```\n * Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`.\n *\n * @example\n * ```ts\n * const dist = new DistortionEffect({ drive: 0.7, wet: 0.9, tone: 0.6 });\n * bus.addEffect(dist);\n *\n * // Soften the drive at runtime:\n * dist.drive = 0.3;\n *\n * // Open the tone filter fully:\n * dist.tone = 1;\n * ```\n */\nexport class DistortionEffect extends AudioEffect {\n private _setup: DistortionEffectSetup | null = null;\n private _drive: number;\n private _wet: number;\n private _oversample: OverSampleType;\n private _tone: number;\n private readonly _onAudioContextReady = (ctx: AudioContext): void => {\n onAudioContextReady.remove(this._onAudioContextReady);\n this._setupNodes(ctx);\n };\n\n public constructor(options: DistortionEffectOptions = {}) {\n super();\n this._drive = Math.max(0, Math.min(1, options.drive ?? 0.4));\n this._wet = Math.max(0, Math.min(1, options.wet ?? 1));\n this._oversample = options.oversample ?? '2x';\n this._tone = Math.max(0, Math.min(1, options.tone ?? 1));\n if (isAudioContextReady()) {\n this._setupNodes(getAudioContext());\n } else {\n onAudioContextReady.add(this._onAudioContextReady);\n }\n }\n\n /**\n * The node where audio enters this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get inputNode(): AudioNode {\n if (!this._setup) throw new Error('DistortionEffect not yet initialized.');\n return this._setup.inputGain;\n }\n\n /**\n * The node where audio exits this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get outputNode(): AudioNode {\n if (!this._setup) throw new Error('DistortionEffect not yet initialized.');\n return this._setup.outputGain;\n }\n\n /**\n * Drive amount controlling the soft-clip intensity. Range 0..1, default 0.4.\n * Changing this rebuilds the wave-shaper curve immediately (sample-accurate\n * on the next render quantum).\n */\n public get drive(): number {\n return this._drive;\n }\n\n public set drive(value: number) {\n this._drive = Math.max(0, Math.min(1, value));\n if (this._setup) {\n this._setup.waveShaper.curve = DistortionEffect._buildCurve(this._drive);\n }\n }\n\n /**\n * Wet (distorted) mix level, 0..1. The dry level is automatically `1 - wet`.\n * Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.\n */\n public get wet(): number {\n return this._wet;\n }\n\n public set wet(value: number) {\n this._wet = Math.max(0, Math.min(1, value));\n if (this._setup) {\n const ctx = this._setup.wetGain.context;\n this._setup.wetGain.gain.setTargetAtTime(this._wet, ctx.currentTime, 0.01);\n this._setup.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);\n }\n }\n\n /**\n * Oversampling mode for the `WaveShaperNode`. One of `'none'`, `'2x'`, or\n * `'4x'`. Higher values reduce aliasing but increase CPU usage. Default `'2x'`.\n */\n public get oversample(): OverSampleType {\n return this._oversample;\n }\n\n public set oversample(value: OverSampleType) {\n this._oversample = value;\n if (this._setup) {\n this._setup.waveShaper.oversample = this._oversample;\n }\n }\n\n /**\n * Tone control: low-pass cutoff on the wet path. 0 = fully dark (~100 Hz),\n * 1 = fully open (~20 kHz). Mapped logarithmically. Default 1.\n * Changes are ramped smoothly via `setTargetAtTime`.\n */\n public get tone(): number {\n return this._tone;\n }\n\n public set tone(value: number) {\n this._tone = Math.max(0, Math.min(1, value));\n if (this._setup) {\n const ctx = this._setup.toneFilter.context;\n this._setup.toneFilter.frequency.setTargetAtTime(DistortionEffect._toneToFrequency(this._tone), ctx.currentTime, 0.01);\n }\n }\n\n public override destroy(): void {\n onAudioContextReady.remove(this._onAudioContextReady);\n if (this._setup) {\n this._setup.inputGain.disconnect();\n this._setup.waveShaper.disconnect();\n this._setup.toneFilter.disconnect();\n this._setup.dryGain.disconnect();\n this._setup.wetGain.disconnect();\n this._setup.outputGain.disconnect();\n this._setup = null;\n }\n }\n\n /**\n * Build a 256-point tanh-based soft-clip curve for the given drive (0..1).\n *\n * Maps drive to a distortion amount `a = drive * 400 + 1`, then fills the\n * curve with `tanh(x * a) / tanh(a)` so that the output is always bounded\n * to [-1, 1]. At drive=0, `a = 1` and the curve is `tanh(x) / tanh(1)`,\n * which has a small-signal gain of `1/tanh(1) ≈ 1.31` - not perfectly\n * linear. At drive=1, `a = 401` and the curve approximates a hard-clipper.\n */\n private static _buildCurve(drive: number): Float32Array<ArrayBuffer> {\n const n = 256;\n const curve: Float32Array<ArrayBuffer> = new Float32Array(n);\n // drive=0 → amount=1 (mild curve: gain ≈ 1/tanh(1) ≈ 1.31 at origin); drive=1 → amount=401 (near hard-clip)\n const amount = drive * 400 + 1;\n const norm = Math.tanh(amount);\n for (let i = 0; i < n; i++) {\n const x = (i * 2) / (n - 1) - 1;\n curve[i] = Math.tanh(x * amount) / norm;\n }\n return curve;\n }\n\n /**\n * Map tone (0..1) to a low-pass cutoff frequency in Hz.\n * Logarithmic scale: 100 Hz at tone=0, 20 000 Hz at tone=1.\n */\n private static _toneToFrequency(tone: number): number {\n // 100 * 200^tone: 100 Hz at 0, 20 000 Hz at 1\n return 100 * Math.pow(200, tone);\n }\n\n private _setupNodes(ctx: AudioContext): void {\n const inputGain = ctx.createGain();\n const outputGain = ctx.createGain();\n const dryGain = ctx.createGain();\n const wetGain = ctx.createGain();\n const waveShaper = ctx.createWaveShaper();\n const toneFilter = ctx.createBiquadFilter();\n\n inputGain.gain.setValueAtTime(1, ctx.currentTime);\n outputGain.gain.setValueAtTime(1, ctx.currentTime);\n dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);\n wetGain.gain.setValueAtTime(this._wet, ctx.currentTime);\n\n waveShaper.curve = DistortionEffect._buildCurve(this._drive);\n waveShaper.oversample = this._oversample;\n\n toneFilter.type = 'lowpass';\n toneFilter.frequency.setValueAtTime(DistortionEffect._toneToFrequency(this._tone), ctx.currentTime);\n\n // Dry path: input → dryGain → output\n inputGain.connect(dryGain);\n dryGain.connect(outputGain);\n\n // Wet path: input → waveShaper → toneFilter → wetGain → output\n inputGain.connect(waveShaper);\n waveShaper.connect(toneFilter);\n toneFilter.connect(wetGain);\n wetGain.connect(outputGain);\n\n this._setup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AI,GAAG,KAAK;GAC1B,MAAM,IAAK,IAAI,IAAM,MAAS;GAC9B,MAAM,KAAK,KAAK,KAAK,IAAI,MAAM,IAAI;EACrC;EACA,OAAO;CACT;;;;;CAMA,OAAe,iBAAiB,MAAsB;EAEpD,OAAO,MAAM,KAAK,IAAI,KAAK,IAAI;CACjC;CAEA,AAAQ,YAAY,KAAyB;EAC3C,MAAM,YAAY,IAAI,WAAW;EACjC,MAAM,aAAa,IAAI,WAAW;EAClC,MAAM,UAAU,IAAI,WAAW;EAC/B,MAAM,UAAU,IAAI,WAAW;EAC/B,MAAM,aAAa,IAAI,iBAAiB;EACxC,MAAM,aAAa,IAAI,mBAAmB;EAE1C,UAAU,KAAK,eAAe,GAAG,IAAI,WAAW;EAChD,WAAW,KAAK,eAAe,GAAG,IAAI,WAAW;EACjD,QAAQ,KAAK,eAAe,IAAI,KAAK,MAAM,IAAI,WAAW;EAC1D,QAAQ,KAAK,eAAe,KAAK,MAAM,IAAI,WAAW;EAEtD,WAAW,QAAQ,iBAAiB,YAAY,KAAK,MAAM;EAC3D,WAAW,aAAa,KAAK;EAE7B,WAAW,OAAO;EAClB,WAAW,UAAU,eAAe,iBAAiB,iBAAiB,KAAK,KAAK,GAAG,IAAI,WAAW;EAGlG,UAAU,QAAQ,OAAO;EACzB,QAAQ,QAAQ,UAAU;EAG1B,UAAU,QAAQ,UAAU;EAC5B,WAAW,QAAQ,UAAU;EAC7B,WAAW,QAAQ,OAAO;EAC1B,QAAQ,QAAQ,UAAU;EAE1B,KAAK,SAAS;GAAE;GAAW;GAAY;GAAS;GAAS;GAAY;EAAW;CAClF;AACF"}
|
|
1
|
+
{"version":3,"file":"DistortionEffect.js","names":[],"sources":["../../../src/effects/DistortionEffect.ts"],"sourcesContent":["import { AudioEffect } from '@codexo/exojs';\n\n/** Construction options for {@link DistortionEffect}. */\nexport interface DistortionEffectOptions {\n /**\n * Drive amount, 0..1, default 0.4. At 0 the wet path is transparent; higher\n * values blend in a steeper tanh soft-clip, up to a near hard clip at 1.\n */\n drive?: number;\n /**\n * Dry/wet mix, 0..1. The dry level is automatically `1 - wet`. Default 1.0.\n */\n wet?: number;\n /**\n * Oversampling mode applied by the `WaveShaperNode`. Higher oversampling\n * reduces aliasing artifacts at the cost of extra CPU. One of `'none'`,\n * `'2x'`, or `'4x'`. Default `'2x'`.\n */\n oversample?: OverSampleType;\n /**\n * Tone control: cutoff of a post-distortion low-pass filter on the wet path.\n * Mapped logarithmically from 100 Hz (tone=0, fully dark) to 20 kHz\n * (tone=1, fully open). Range 0..1, default 1.\n */\n tone?: number;\n}\n\ninterface DistortionEffectSetup {\n readonly inputGain: GainNode;\n readonly outputGain: GainNode;\n readonly dryGain: GainNode;\n readonly wetGain: GainNode;\n readonly waveShaper: WaveShaperNode;\n readonly toneFilter: BiquadFilterNode;\n}\n\n/**\n * Distortion effect built from native Web Audio nodes - no worklet required.\n *\n * The input signal is split into a dry path and a wet path. The wet path runs\n * through a `WaveShaperNode` loaded with a tanh-based soft-clip curve, then\n * through a `BiquadFilterNode` (lowpass) acting as a tone control, before\n * being mixed back with the dry signal. The wave-shaper curve is rebuilt any\n * time `drive` changes. Oversampling is delegated directly to\n * `WaveShaperNode.oversample`.\n *\n * Node graph:\n * ```\n * input(GainNode) ──── dryGain ─────────────────────────── output(GainNode)\n * input ──── waveShaper ── toneFilter ── wetGain ── output\n * ```\n * Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`.\n *\n * @example\n * ```ts\n * const dist = new DistortionEffect({ drive: 0.7, wet: 0.9, tone: 0.6 });\n * bus.addEffect(dist);\n *\n * // Soften the drive at runtime:\n * dist.drive = 0.3;\n *\n * // Open the tone filter fully:\n * dist.tone = 1;\n * ```\n */\nexport class DistortionEffect extends AudioEffect {\n private _setup: DistortionEffectSetup | null = null;\n private _drive: number;\n private _wet: number;\n private _oversample: OverSampleType;\n private _tone: number;\n\n public constructor(options: DistortionEffectOptions = {}) {\n super();\n this._drive = Math.max(0, Math.min(1, options.drive ?? 0.4));\n this._wet = Math.max(0, Math.min(1, options.wet ?? 1));\n this._oversample = options.oversample ?? '2x';\n this._tone = Math.max(0, Math.min(1, options.tone ?? 1));\n this._deferSetup(context => this._setupNodes(context));\n }\n\n /**\n * The node where audio enters this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get inputNode(): AudioNode {\n if (!this._setup) throw new Error('DistortionEffect not yet initialized.');\n return this._setup.inputGain;\n }\n\n /**\n * The node where audio exits this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get outputNode(): AudioNode {\n if (!this._setup) throw new Error('DistortionEffect not yet initialized.');\n return this._setup.outputGain;\n }\n\n /**\n * Drive amount, 0..1: 0 is transparent, 1 a near hard clip. Default 0.4.\n * Changing this rebuilds the wave-shaper curve immediately (sample-accurate\n * on the next render quantum).\n */\n public get drive(): number {\n return this._drive;\n }\n\n public set drive(value: number) {\n this._drive = Math.max(0, Math.min(1, value));\n if (this._setup) {\n this._setup.waveShaper.curve = DistortionEffect._buildCurve(this._drive);\n }\n }\n\n /**\n * Wet (distorted) mix level, 0..1. The dry level is automatically `1 - wet`.\n * Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.\n */\n public get wet(): number {\n return this._wet;\n }\n\n public set wet(value: number) {\n this._wet = Math.max(0, Math.min(1, value));\n if (this._setup) {\n const ctx = this._setup.wetGain.context;\n this._setup.wetGain.gain.setTargetAtTime(this._wet, ctx.currentTime, 0.01);\n this._setup.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);\n }\n }\n\n /**\n * Oversampling mode for the `WaveShaperNode`. One of `'none'`, `'2x'`, or\n * `'4x'`. Higher values reduce aliasing but increase CPU usage. Default `'2x'`.\n */\n public get oversample(): OverSampleType {\n return this._oversample;\n }\n\n public set oversample(value: OverSampleType) {\n this._oversample = value;\n if (this._setup) {\n this._setup.waveShaper.oversample = this._oversample;\n }\n }\n\n /**\n * Tone control: low-pass cutoff on the wet path. 0 = fully dark (~100 Hz),\n * 1 = fully open (~20 kHz). Mapped logarithmically. Default 1.\n * Changes are ramped smoothly via `setTargetAtTime`.\n */\n public get tone(): number {\n return this._tone;\n }\n\n public set tone(value: number) {\n this._tone = Math.max(0, Math.min(1, value));\n if (this._setup) {\n const ctx = this._setup.toneFilter.context;\n this._setup.toneFilter.frequency.setTargetAtTime(DistortionEffect._toneToFrequency(this._tone), ctx.currentTime, 0.01);\n }\n }\n\n public override destroy(): void {\n this._teardown();\n if (this._setup) {\n this._setup.inputGain.disconnect();\n this._setup.waveShaper.disconnect();\n this._setup.toneFilter.disconnect();\n this._setup.dryGain.disconnect();\n this._setup.wetGain.disconnect();\n this._setup.outputGain.disconnect();\n this._setup = null;\n }\n }\n\n /**\n * Build a 256-point curve for the given drive (0..1): the identity blended\n * with a soft clip `tanh(x * a) / tanh(a)`, `a = drive * 400 + 1`, by `drive`.\n *\n * The blend is what makes drive=0 exactly transparent. A bare soft clip is\n * never linear (at a = 1 its small-signal gain is 1/tanh(1) ≈ 1.31), and\n * special-casing 0 would make the sound jump at the first step above it.\n * Both curves run from -1 to 1 and rise monotonically, so the blend does too.\n */\n private static _buildCurve(drive: number): Float32Array<ArrayBuffer> {\n const n = 256;\n const curve: Float32Array<ArrayBuffer> = new Float32Array(n);\n const amount = drive * 400 + 1;\n const norm = Math.tanh(amount);\n for (let i = 0; i < n; i++) {\n const x = (i * 2) / (n - 1) - 1;\n curve[i] = x * (1 - drive) + (Math.tanh(x * amount) / norm) * drive;\n }\n return curve;\n }\n\n /**\n * Map tone (0..1) to a low-pass cutoff frequency in Hz.\n * Logarithmic scale: 100 Hz at tone=0, 20 000 Hz at tone=1.\n */\n private static _toneToFrequency(tone: number): number {\n // 100 * 200^tone: 100 Hz at 0, 20 000 Hz at 1\n return 100 * Math.pow(200, tone);\n }\n\n private _setupNodes(ctx: AudioContext): void {\n const inputGain = ctx.createGain();\n const outputGain = ctx.createGain();\n const dryGain = ctx.createGain();\n const wetGain = ctx.createGain();\n const waveShaper = ctx.createWaveShaper();\n const toneFilter = ctx.createBiquadFilter();\n\n inputGain.gain.setValueAtTime(1, ctx.currentTime);\n outputGain.gain.setValueAtTime(1, ctx.currentTime);\n dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);\n wetGain.gain.setValueAtTime(this._wet, ctx.currentTime);\n\n waveShaper.curve = DistortionEffect._buildCurve(this._drive);\n waveShaper.oversample = this._oversample;\n\n toneFilter.type = 'lowpass';\n toneFilter.frequency.setValueAtTime(DistortionEffect._toneToFrequency(this._tone), ctx.currentTime);\n\n // Dry path: input → dryGain → output\n inputGain.connect(dryGain);\n dryGain.connect(outputGain);\n\n // Wet path: input → waveShaper → toneFilter → wetGain → output\n inputGain.connect(waveShaper);\n waveShaper.connect(toneFilter);\n toneFilter.connect(wetGain);\n wetGain.connect(outputGain);\n\n this._setup = { inputGain, outputGain, dryGain, wetGain, waveShaper, toneFilter };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,IAAa,mBAAb,MAAa,yBAAyB,YAAY;CAChD,AAAQ,SAAuC;CAC/C,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,AAAO,YAAY,UAAmC,CAAC,GAAG;EACxD,MAAM;EACN,KAAK,SAAS,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,SAAS,EAAG,CAAC;EAC3D,KAAK,OAAO,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,OAAO,CAAC,CAAC;EACrD,KAAK,cAAc,QAAQ,cAAc;EACzC,KAAK,QAAQ,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,QAAQ,CAAC,CAAC;EACvD,KAAK,aAAY,YAAW,KAAK,YAAY,OAAO,CAAC;CACvD;;;;;CAMA,IAAW,YAAuB;EAChC,IAAI,CAAC,KAAK,QAAQ,MAAM,IAAI,MAAM,uCAAuC;EACzE,OAAO,KAAK,OAAO;CACrB;;;;;CAMA,IAAW,aAAwB;EACjC,IAAI,C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B,WAAW,QAAQ,UAAU;EAC7B,WAAW,QAAQ,OAAO;EAC1B,QAAQ,QAAQ,UAAU;EAE1B,KAAK,SAAS;GAAE;GAAW;GAAY;GAAS;GAAS;GAAY;EAAW;CAClF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DuckingEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/DuckingEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAI7D,+EAA+E;AAC/E,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,QAAQ,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,qBAAa,aAAc,SAAQ,aAAa;IAG9C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyB;IACpD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAS;IAE3B,YAAmB,OAAO,EAAE,oBAAoB,EAU/C;IAED,SAAS,KAAK,YAAY,IAAI,MAAM,CAEnC;IAED,SAAS,KAAK,cAAc,IAAI,MAAM,CAErC;IAED,cAAuB,eAAe,IAAI,uBAAuB,CAEhE;IAED,UAAmB,eAAe,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"DuckingEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/DuckingEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAI7D,+EAA+E;AAC/E,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,QAAQ,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,qBAAa,aAAc,SAAQ,aAAa;IAG9C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyB;IACpD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAS;IAE3B,YAAmB,OAAO,EAAE,oBAAoB,EAU/C;IAED,SAAS,KAAK,YAAY,IAAI,MAAM,CAEnC;IAED,SAAS,KAAK,cAAc,IAAI,MAAM,CAErC;IAED,cAAuB,eAAe,IAAI,uBAAuB,CAEhE;IAED,UAAmB,eAAe,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI,CAWnE;IAED,oFAAoF;IACpF,OAAO,CAAC,UAAU;IAKlB,sGAAsG;IACtG,IAAW,SAAS,IAAI,MAAM,CAE7B;IACD,IAAW,SAAS,CAAC,KAAK,EAAE,MAAM,EAGjC;IAED,gGAAgG;IAChG,IAAW,KAAK,IAAI,MAAM,CAEzB;IACD,IAAW,KAAK,CAAC,KAAK,EAAE,MAAM,EAG7B;IAED,gFAAgF;IAChF,IAAW,QAAQ,IAAI,MAAM,CAE5B;IACD,IAAW,QAAQ,CAAC,KAAK,EAAE,MAAM,EAKhC;IAED,qFAAqF;IACrF,IAAW,SAAS,IAAI,MAAM,CAE7B;IACD,IAAW,SAAS,CAAC,KAAK,EAAE,MAAM,EAKjC;CACF"}
|
|
@@ -43,13 +43,7 @@ var DuckingEffect = class extends WorkletEffect {
|
|
|
43
43
|
this._setAudioParam("ratio", this._ratio);
|
|
44
44
|
this._setAudioParam("attack", this._msToCoeff(this._attackMs, audioContext.sampleRate));
|
|
45
45
|
this._setAudioParam("release", this._msToCoeff(this._releaseMs, audioContext.sampleRate));
|
|
46
|
-
|
|
47
|
-
const sidechainOut = sidechain.getOutputNode();
|
|
48
|
-
if (sidechainOut && this._workletNode) sidechainOut.connect(this._workletNode, 0, 1);
|
|
49
|
-
else sidechain.onceSetup(() => {
|
|
50
|
-
const node = sidechain.getOutputNode();
|
|
51
|
-
if (node && this._workletNode) node.connect(this._workletNode, 0, 1);
|
|
52
|
-
});
|
|
46
|
+
this._connectAuxInput(this._sidechain, 1);
|
|
53
47
|
}
|
|
54
48
|
/** Convert ms time-constant to one-pole filter coefficient at given sample rate. */
|
|
55
49
|
_msToCoeff(ms, sampleRate) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DuckingEffect.js","names":["duckingWorkletSource"],"sources":["../../../src/effects/DuckingEffect.ts"],"sourcesContent":["import { type AudioBus, WorkletEffect } from '@codexo/exojs';\n\nimport duckingWorkletSource from '../worklets/ducking.worklet.ts?worklet';\n\n/** Construction options for {@link DuckingEffect}. `sidechain` is required. */\nexport interface DuckingEffectOptions {\n sidechain: AudioBus;\n threshold?: number;\n ratio?: number;\n attackMs?: number;\n releaseMs?: number;\n}\n\n/**\n * Sidechain compressor (ducker) implemented as an {@link WorkletEffect}.\n * Attenuates the main audio signal whenever the `sidechain` {@link AudioBus}\n * exceeds the threshold, then releases when it drops back down. Commonly used\n * to duck background music under dialogue or sound effects. Attack and release\n * times are expressed in milliseconds and converted internally to one-pole\n * filter coefficients.\n */\nexport class DuckingEffect extends WorkletEffect {\n // Declared nullable because super() may trigger _onWorkletReady before the\n // subclass constructor body runs (if construction is aborted by a throw).\n private readonly _sidechain: AudioBus | null = null;\n private _threshold: number;\n private _ratio: number;\n private _attackMs: number;\n private _releaseMs: number;\n\n public constructor(options: DuckingEffectOptions) {\n super();\n if (!options.sidechain) {\n throw new Error('DuckingEffect requires a sidechain AudioBus.');\n }\n this._sidechain = options.sidechain;\n this._threshold = options.threshold ?? -20;\n this._ratio = options.ratio ?? 4;\n this._attackMs = options.attackMs ?? 30;\n this._releaseMs = options.releaseMs ?? 300;\n }\n\n protected get _workletName(): string {\n return 'exojs-ducking';\n }\n\n protected get _workletSource(): string {\n return duckingWorkletSource;\n }\n\n protected override get _workletOptions(): AudioWorkletNodeOptions {\n return { numberOfInputs: 2, numberOfOutputs: 1 };\n }\n\n protected override _onWorkletReady(audioContext: AudioContext): void {\n // Guard against partially-constructed instances (constructor threw after super()).\n if (!this._sidechain) return;\n\n // Apply current parameter values\n this._setAudioParam('threshold', this._threshold);\n this._setAudioParam('ratio', this._ratio);\n this._setAudioParam('attack', this._msToCoeff(this._attackMs, audioContext.sampleRate));\n this._setAudioParam('release', this._msToCoeff(this._releaseMs, audioContext.sampleRate));\n\n
|
|
1
|
+
{"version":3,"file":"DuckingEffect.js","names":["duckingWorkletSource"],"sources":["../../../src/effects/DuckingEffect.ts"],"sourcesContent":["import { type AudioBus, WorkletEffect } from '@codexo/exojs';\n\nimport duckingWorkletSource from '../worklets/ducking.worklet.ts?worklet';\n\n/** Construction options for {@link DuckingEffect}. `sidechain` is required. */\nexport interface DuckingEffectOptions {\n sidechain: AudioBus;\n threshold?: number;\n ratio?: number;\n attackMs?: number;\n releaseMs?: number;\n}\n\n/**\n * Sidechain compressor (ducker) implemented as an {@link WorkletEffect}.\n * Attenuates the main audio signal whenever the `sidechain` {@link AudioBus}\n * exceeds the threshold, then releases when it drops back down. Commonly used\n * to duck background music under dialogue or sound effects. Attack and release\n * times are expressed in milliseconds and converted internally to one-pole\n * filter coefficients.\n */\nexport class DuckingEffect extends WorkletEffect {\n // Declared nullable because super() may trigger _onWorkletReady before the\n // subclass constructor body runs (if construction is aborted by a throw).\n private readonly _sidechain: AudioBus | null = null;\n private _threshold: number;\n private _ratio: number;\n private _attackMs: number;\n private _releaseMs: number;\n\n public constructor(options: DuckingEffectOptions) {\n super();\n if (!options.sidechain) {\n throw new Error('DuckingEffect requires a sidechain AudioBus.');\n }\n this._sidechain = options.sidechain;\n this._threshold = options.threshold ?? -20;\n this._ratio = options.ratio ?? 4;\n this._attackMs = options.attackMs ?? 30;\n this._releaseMs = options.releaseMs ?? 300;\n }\n\n protected get _workletName(): string {\n return 'exojs-ducking';\n }\n\n protected get _workletSource(): string {\n return duckingWorkletSource;\n }\n\n protected override get _workletOptions(): AudioWorkletNodeOptions {\n return { numberOfInputs: 2, numberOfOutputs: 1 };\n }\n\n protected override _onWorkletReady(audioContext: AudioContext): void {\n // Guard against partially-constructed instances (constructor threw after super()).\n if (!this._sidechain) return;\n\n // Apply current parameter values\n this._setAudioParam('threshold', this._threshold);\n this._setAudioParam('ratio', this._ratio);\n this._setAudioParam('attack', this._msToCoeff(this._attackMs, audioContext.sampleRate));\n this._setAudioParam('release', this._msToCoeff(this._releaseMs, audioContext.sampleRate));\n\n this._connectAuxInput(this._sidechain, 1);\n }\n\n /** Convert ms time-constant to one-pole filter coefficient at given sample rate. */\n private _msToCoeff(ms: number, sampleRate: number): number {\n const tau = ms * 0.001 * sampleRate;\n return 1 - Math.exp(-1 / Math.max(1, tau));\n }\n\n /** Sidechain level above which the main signal is attenuated, in dBFS. Range −100..0, default −20. */\n public get threshold(): number {\n return this._threshold;\n }\n public set threshold(value: number) {\n this._threshold = Math.max(-100, Math.min(0, value));\n this._setAudioParam('threshold', this._threshold);\n }\n\n /** Compression ratio applied to the main signal above the threshold. Range 1..20, default 4. */\n public get ratio(): number {\n return this._ratio;\n }\n public set ratio(value: number) {\n this._ratio = Math.max(1, Math.min(20, value));\n this._setAudioParam('ratio', this._ratio);\n }\n\n /** Attack time in milliseconds - how quickly the ducker engages. Default 30. */\n public get attackMs(): number {\n return this._attackMs;\n }\n public set attackMs(value: number) {\n this._attackMs = Math.max(0.001, value);\n if (this._workletNode) {\n this._setAudioParam('attack', this._msToCoeff(this._attackMs, this._workletNode.context.sampleRate));\n }\n }\n\n /** Release time in milliseconds - how quickly the ducker disengages. Default 300. */\n public get releaseMs(): number {\n return this._releaseMs;\n }\n public set releaseMs(value: number) {\n this._releaseMs = Math.max(0.001, value);\n if (this._workletNode) {\n this._setAudioParam('release', this._msToCoeff(this._releaseMs, this._workletNode.context.sampleRate));\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAqBA,IAAa,gBAAb,cAAmC,cAAc;CAG/C,AAAiB,aAA8B;CAC/C,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,AAAO,YAAY,SAA+B;EAChD,MAAM;EACN,IAAI,CAAC,QAAQ,WACX,MAAM,IAAI,MAAM,8CAA8C;EAEhE,KAAK,aAAa,QAAQ;EAC1B,KAAK,aAAa,QAAQ,aAAa;EACvC,KAAK,SAAS,QAAQ,SAAS;EAC/B,KAAK,YAAY,QAAQ,YAAY;EACrC,KAAK,aAAa,QAAQ,aAAa;CACzC;CAEA,IAAc,eAAuB;EACnC,OAAO;CACT;CAEA,IAAc,iBAAyB;EACrC,OAAOA;CACT;CAEA,IAAuB,kBAA2C;EAChE,OAAO;GAAE,gBAAgB;GAAG,iBAAiB;EAAE;CACjD;CAEA,AAAmB,gBAAgB,cAAkC;EAEnE,IAAI,CAAC,KAAK,YAAY;EAGtB,KAAK,eAAe,aAAa,KAAK,UAAU;EAChD,KAAK,eAAe,SAAS,KAAK,MAAM;EACxC,KAAK,eAAe,UAAU,KAAK,WAAW,KAAK,WAAW,aAAa,UAAU,CAAC;EACtF,KAAK,eAAe,WAAW,KAAK,WAAW,KAAK,YAAY,aAAa,UAAU,CAAC;EAExF,KAAK,iBAAiB,KAAK,YAAY,CAAC;CAC1C;;CAGA,AAAQ,WAAW,IAAY,YAA4B;EACzD,MAAM,MAAM,KAAK,OAAQ;EACzB,OAAO,IAAI,KAAK,IAAI,KAAK,KAAK,IAAI,GAAG,GAAG,CAAC;CAC3C;;CAGA,IAAW,YAAoB;EAC7B,OAAO,KAAK;CACd;CACA,IAAW,UAAU,OAAe;EAClC,KAAK,aAAa,KAAK,IAAI,MAAM,KAAK,IAAI,GAAG,KAAK,CAAC;EACnD,KAAK,eAAe,aAAa,KAAK,UAAU;CAClD;;CAGA,IAAW,QAAgB;EACzB,OAAO,KAAK;CACd;CACA,IAAW,MAAM,OAAe;EAC9B,KAAK,SAAS,KAAK,IAAI,GAAG,KAAK,IAAI,IAAI,KAAK,CAAC;EAC7C,KAAK,eAAe,SAAS,KAAK,MAAM;CAC1C;;CAGA,IAAW,WAAmB;EAC5B,OAAO,KAAK;CACd;CACA,IAAW,SAAS,OAAe;EACjC,KAAK,YAAY,KAAK,IAAI,MAAO,KAAK;EACtC,IAAI,KAAK,cACP,KAAK,eAAe,UAAU,KAAK,WAAW,KAAK,WAAW,KAAK,aAAa,QAAQ,UAAU,CAAC;CAEvG;;CAGA,IAAW,YAAoB;EAC7B,OAAO,KAAK;CACd;CACA,IAAW,UAAU,OAAe;EAClC,KAAK,aAAa,KAAK,IAAI,MAAO,KAAK;EACvC,IAAI,KAAK,cACP,KAAK,eAAe,WAAW,KAAK,WAAW,KAAK,YAAY,KAAK,aAAa,QAAQ,UAAU,CAAC;CAEzG;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EqualizerEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/EqualizerEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"EqualizerEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/EqualizerEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,wDAAwD;AACxD,MAAM,WAAW,sBAAsB;IACrC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAQD;;;;;;GAMG;AACH,qBAAa,eAAgB,SAAQ,WAAW;IAC9C,OAAO,CAAC,MAAM,CAAqC;IACnD,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,cAAc,CAAS;IAE/B,YAAmB,OAAO,GAAE,sBAA2B,EAStD;IAED,IAAW,SAAS,IAAI,SAAS,CAGhC;IAED,IAAW,UAAU,IAAI,SAAS,CAGjC;IAED,mFAAmF;IACnF,IAAW,GAAG,IAAI,MAAM,CAEvB;IAED,IAAW,GAAG,CAAC,KAAK,EAAE,MAAM,EAK3B;IAED,wFAAwF;IACxF,IAAW,GAAG,IAAI,MAAM,CAEvB;IAED,IAAW,GAAG,CAAC,KAAK,EAAE,MAAM,EAK3B;IAED,qFAAqF;IACrF,IAAW,IAAI,IAAI,MAAM,CAExB;IAED,IAAW,IAAI,CAAC,KAAK,EAAE,MAAM,EAK5B;IAED,iEAAiE;IACjE,IAAW,YAAY,IAAI,MAAM,CAEhC;IAED,IAAW,YAAY,CAAC,KAAK,EAAE,MAAM,EAKpC;IAED,oEAAoE;IACpE,IAAW,YAAY,IAAI,MAAM,CAEhC;IAED,IAAW,YAAY,CAAC,KAAK,EAAE,MAAM,EAKpC;IAED,mEAAmE;IACnE,IAAW,aAAa,IAAI,MAAM,CAEjC;IAED,IAAW,aAAa,CAAC,KAAK,EAAE,MAAM,EAKrC;IAEe,OAAO,IAAI,IAAI,CAQ9B;IAED,OAAO,CAAC,WAAW;CAsBpB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AudioEffect
|
|
1
|
+
import { AudioEffect } from "@codexo/exojs";
|
|
2
2
|
|
|
3
3
|
//#region src/effects/EqualizerEffect.ts
|
|
4
4
|
/**
|
|
@@ -11,10 +11,6 @@ import { AudioEffect, getAudioContext, isAudioContextReady, onAudioContextReady
|
|
|
11
11
|
var EqualizerEffect = class extends AudioEffect {
|
|
12
12
|
_setup = null;
|
|
13
13
|
_low;
|
|
14
|
-
_onAudioContextReady = (ctx) => {
|
|
15
|
-
onAudioContextReady.remove(this._onAudioContextReady);
|
|
16
|
-
this._setupNodes(ctx);
|
|
17
|
-
};
|
|
18
14
|
_mid;
|
|
19
15
|
_high;
|
|
20
16
|
_lowFrequency;
|
|
@@ -28,8 +24,7 @@ var EqualizerEffect = class extends AudioEffect {
|
|
|
28
24
|
this._lowFrequency = options.lowFrequency ?? 250;
|
|
29
25
|
this._midFrequency = options.midFrequency ?? 1500;
|
|
30
26
|
this._highFrequency = options.highFrequency ?? 6e3;
|
|
31
|
-
|
|
32
|
-
else onAudioContextReady.add(this._onAudioContextReady);
|
|
27
|
+
this._deferSetup((context) => this._setupNodes(context));
|
|
33
28
|
}
|
|
34
29
|
get inputNode() {
|
|
35
30
|
if (!this._setup) throw new Error("EqualizerEffect not yet initialized.");
|
|
@@ -88,7 +83,7 @@ var EqualizerEffect = class extends AudioEffect {
|
|
|
88
83
|
if (this._setup) this._setup.highShelf.frequency.setTargetAtTime(this._highFrequency, this._setup.highShelf.context.currentTime, .01);
|
|
89
84
|
}
|
|
90
85
|
destroy() {
|
|
91
|
-
|
|
86
|
+
this._teardown();
|
|
92
87
|
if (this._setup) {
|
|
93
88
|
this._setup.lowShelf.disconnect();
|
|
94
89
|
this._setup.peaking.disconnect();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EqualizerEffect.js","names":[],"sources":["../../../src/effects/EqualizerEffect.ts"],"sourcesContent":["import { AudioEffect
|
|
1
|
+
{"version":3,"file":"EqualizerEffect.js","names":[],"sources":["../../../src/effects/EqualizerEffect.ts"],"sourcesContent":["import { AudioEffect } from '@codexo/exojs';\n\n/** Construction options for {@link EqualizerEffect}. */\nexport interface EqualizerEffectOptions {\n low?: number;\n mid?: number;\n high?: number;\n lowFrequency?: number;\n midFrequency?: number;\n highFrequency?: number;\n}\n\ninterface EqualizerEffectSetup {\n readonly lowShelf: BiquadFilterNode;\n readonly peaking: BiquadFilterNode;\n readonly highShelf: BiquadFilterNode;\n}\n\n/**\n * Three-band equalizer (low shelf / peaking mid / high shelf) built from a\n * series chain of `BiquadFilterNode` instances. Gain values are in dB and\n * clamped to ±40 dB. All six parameters (gains + frequencies) are\n * adjustable at runtime via the corresponding setters; gain changes use a\n * short exponential ramp to avoid clicks.\n */\nexport class EqualizerEffect extends AudioEffect {\n private _setup: EqualizerEffectSetup | null = null;\n private _low: number;\n private _mid: number;\n private _high: number;\n private _lowFrequency: number;\n private _midFrequency: number;\n private _highFrequency: number;\n\n public constructor(options: EqualizerEffectOptions = {}) {\n super();\n this._low = Math.max(-40, Math.min(40, options.low ?? 0));\n this._mid = Math.max(-40, Math.min(40, options.mid ?? 0));\n this._high = Math.max(-40, Math.min(40, options.high ?? 0));\n this._lowFrequency = options.lowFrequency ?? 250;\n this._midFrequency = options.midFrequency ?? 1500;\n this._highFrequency = options.highFrequency ?? 6000;\n this._deferSetup(context => this._setupNodes(context));\n }\n\n public get inputNode(): AudioNode {\n if (!this._setup) throw new Error('EqualizerEffect not yet initialized.');\n return this._setup.lowShelf;\n }\n\n public get outputNode(): AudioNode {\n if (!this._setup) throw new Error('EqualizerEffect not yet initialized.');\n return this._setup.highShelf;\n }\n\n /** Low-shelf gain in dB applied below `lowFrequency`. Range −40..40, default 0. */\n public get low(): number {\n return this._low;\n }\n\n public set low(value: number) {\n this._low = Math.max(-40, Math.min(40, value));\n if (this._setup) {\n this._setup.lowShelf.gain.setTargetAtTime(this._low, this._setup.lowShelf.context.currentTime, 0.01);\n }\n }\n\n /** Peaking mid-band gain in dB centered at `midFrequency`. Range −40..40, default 0. */\n public get mid(): number {\n return this._mid;\n }\n\n public set mid(value: number) {\n this._mid = Math.max(-40, Math.min(40, value));\n if (this._setup) {\n this._setup.peaking.gain.setTargetAtTime(this._mid, this._setup.peaking.context.currentTime, 0.01);\n }\n }\n\n /** High-shelf gain in dB applied above `highFrequency`. Range −40..40, default 0. */\n public get high(): number {\n return this._high;\n }\n\n public set high(value: number) {\n this._high = Math.max(-40, Math.min(40, value));\n if (this._setup) {\n this._setup.highShelf.gain.setTargetAtTime(this._high, this._setup.highShelf.context.currentTime, 0.01);\n }\n }\n\n /** Cutoff frequency in Hz of the low-shelf band. Default 250. */\n public get lowFrequency(): number {\n return this._lowFrequency;\n }\n\n public set lowFrequency(value: number) {\n this._lowFrequency = Math.max(0, value);\n if (this._setup) {\n this._setup.lowShelf.frequency.setTargetAtTime(this._lowFrequency, this._setup.lowShelf.context.currentTime, 0.01);\n }\n }\n\n /** Center frequency in Hz of the peaking mid band. Default 1500. */\n public get midFrequency(): number {\n return this._midFrequency;\n }\n\n public set midFrequency(value: number) {\n this._midFrequency = Math.max(0, value);\n if (this._setup) {\n this._setup.peaking.frequency.setTargetAtTime(this._midFrequency, this._setup.peaking.context.currentTime, 0.01);\n }\n }\n\n /** Cutoff frequency in Hz of the high-shelf band. Default 6000. */\n public get highFrequency(): number {\n return this._highFrequency;\n }\n\n public set highFrequency(value: number) {\n this._highFrequency = Math.max(0, value);\n if (this._setup) {\n this._setup.highShelf.frequency.setTargetAtTime(this._highFrequency, this._setup.highShelf.context.currentTime, 0.01);\n }\n }\n\n public override destroy(): void {\n this._teardown();\n if (this._setup) {\n this._setup.lowShelf.disconnect();\n this._setup.peaking.disconnect();\n this._setup.highShelf.disconnect();\n this._setup = null;\n }\n }\n\n private _setupNodes(ctx: AudioContext): void {\n const lowShelf = ctx.createBiquadFilter();\n lowShelf.type = 'lowshelf';\n lowShelf.frequency.setValueAtTime(this._lowFrequency, ctx.currentTime);\n lowShelf.gain.setValueAtTime(this._low, ctx.currentTime);\n\n const peaking = ctx.createBiquadFilter();\n peaking.type = 'peaking';\n peaking.frequency.setValueAtTime(this._midFrequency, ctx.currentTime);\n peaking.gain.setValueAtTime(this._mid, ctx.currentTime);\n\n const highShelf = ctx.createBiquadFilter();\n highShelf.type = 'highshelf';\n highShelf.frequency.setValueAtTime(this._highFrequency, ctx.currentTime);\n highShelf.gain.setValueAtTime(this._high, ctx.currentTime);\n\n // Series chain: lowShelf → peaking → highShelf\n lowShelf.connect(peaking);\n peaking.connect(highShelf);\n\n this._setup = { lowShelf, peaking, highShelf };\n }\n}\n"],"mappings":";;;;;;;;;;AAyBA,IAAa,kBAAb,cAAqC,YAAY;CAC/C,AAAQ,SAAsC;CAC9C,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,AAAO,YAAY,UAAkC,CAAC,GAAG;EACvD,MAAM;EACN,KAAK,OAAO,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,QAAQ,OAAO,CAAC,CAAC;EACxD,KAAK,OAAO,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,QAAQ,OAAO,CAAC,CAAC;EACxD,KAAK,QAAQ,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,QAAQ,QAAQ,CAAC,CAAC;EAC1D,KAAK,gBAAgB,QAAQ,gBAAgB;EAC7C,KAAK,gBAAgB,QAAQ,gBAAgB;EAC7C,KAAK,iBAAiB,QAAQ,iBAAiB;EAC/C,KAAK,aAAY,YAAW,KAAK,YAAY,OAAO,CAAC;CACvD;CAEA,IAAW,YAAuB;EAChC,IAAI,CAAC,KAAK,QAAQ,MAAM,IAAI,MAAM,sCAAsC;EACxE,OAAO,KAAK,OAAO;CACrB;CAEA,IAAW,aAAwB;EACjC,IAAI,CAAC,KAAK,QAAQ,MAAM,IAAI,MAAM,sCAAsC;EACxE,OAAO,KAAK,OAAO;CACrB;;CAGA,IAAW,MAAc;EACvB,OAAO,KAAK;CACd;CAEA,IAAW,IAAI,OAAe;EAC5B,KAAK,OAAO,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC;EAC7C,IAAI,KAAK,QACP,KAAK,OAAO,SAAS,KAAK,gBAAgB,KAAK,MAAM,KAAK,OAAO,SAAS,QAAQ,aAAa,GAAI;CAEvG;;CAGA,IAAW,MAAc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,SAAS,WAAW;GAChC,KAAK,OAAO,QAAQ,WAAW;GAC/B,KAAK,OAAO,UAAU,WAAW;GACjC,KAAK,SAAS;EAChB;CACF;CAEA,AAAQ,YAAY,KAAyB;EAC3C,MAAM,WAAW,IAAI,mBAAmB;EACxC,SAAS,OAAO;EAChB,SAAS,UAAU,eAAe,KAAK,eAAe,IAAI,WAAW;EACrE,SAAS,KAAK,eAAe,KAAK,MAAM,IAAI,WAAW;EAEvD,MAAM,UAAU,IAAI,mBAAmB;EACvC,QAAQ,OAAO;EACf,QAAQ,UAAU,eAAe,KAAK,eAAe,IAAI,WAAW;EACpE,QAAQ,KAAK,eAAe,KAAK,MAAM,IAAI,WAAW;EAEtD,MAAM,YAAY,IAAI,mBAAmB;EACzC,UAAU,OAAO;EACjB,UAAU,UAAU,eAAe,KAAK,gBAAgB,IAAI,WAAW;EACvE,UAAU,KAAK,eAAe,KAAK,OAAO,IAAI,WAAW;EAGzD,SAAS,QAAQ,OAAO;EACxB,QAAQ,QAAQ,SAAS;EAEzB,KAAK,SAAS;GAAE;GAAU;GAAS;EAAU;CAC/C;AACF"}
|
|
@@ -8,7 +8,8 @@ export interface FlangerEffectOptions {
|
|
|
8
8
|
delayMs?: number;
|
|
9
9
|
/**
|
|
10
10
|
* LFO modulation depth in milliseconds - the peak deviation added to and
|
|
11
|
-
* subtracted from `delayMs` by the sine LFO. Range 0..10
|
|
11
|
+
* subtracted from `delayMs` by the sine LFO. Range 0..10 and never above
|
|
12
|
+
* `delayMs`, default 2.
|
|
12
13
|
*/
|
|
13
14
|
depthMs?: number;
|
|
14
15
|
/**
|
|
@@ -63,7 +64,6 @@ export declare class FlangerEffect extends AudioEffect {
|
|
|
63
64
|
private _rateHz;
|
|
64
65
|
private _feedback;
|
|
65
66
|
private _wet;
|
|
66
|
-
private readonly _onAudioContextReady;
|
|
67
67
|
constructor(options?: FlangerEffectOptions);
|
|
68
68
|
/**
|
|
69
69
|
* The node where audio enters this effect. Throws if the audio context is
|
|
@@ -75,10 +75,18 @@ export declare class FlangerEffect extends AudioEffect {
|
|
|
75
75
|
* not yet initialized.
|
|
76
76
|
*/
|
|
77
77
|
get outputNode(): AudioNode;
|
|
78
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Base delay time in milliseconds. Range 0.5..20; lowering it below
|
|
80
|
+
* {@link FlangerEffect.depthMs} stops at `depthMs`. Default 3.
|
|
81
|
+
*/
|
|
79
82
|
get delayMs(): number;
|
|
80
83
|
set delayMs(value: number);
|
|
81
|
-
/**
|
|
84
|
+
/**
|
|
85
|
+
* LFO modulation depth in milliseconds (peak deviation from base delay).
|
|
86
|
+
* Range 0..10 and never above {@link FlangerEffect.delayMs}, so the swept
|
|
87
|
+
* delay never reaches below zero, which would flatten one half of the sweep.
|
|
88
|
+
* Default 2.
|
|
89
|
+
*/
|
|
82
90
|
get depthMs(): number;
|
|
83
91
|
set depthMs(value: number);
|
|
84
92
|
/** LFO oscillation rate in Hz. Range 0..10, default 0.25. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FlangerEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/FlangerEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"FlangerEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/FlangerEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,sDAAsD;AACtD,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAaD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,aAAc,SAAQ,WAAW;IAC5C,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,IAAI,CAAS;IAErB,YAAmB,OAAO,GAAE,oBAAyB,EAQpD;IAED;;;OAGG;IACH,IAAW,SAAS,IAAI,SAAS,CAGhC;IAED;;;OAGG;IACH,IAAW,UAAU,IAAI,SAAS,CAGjC;IAMD;;;OAGG;IACH,IAAW,OAAO,IAAI,MAAM,CAE3B;IAED,IAAW,OAAO,CAAC,KAAK,EAAE,MAAM,EAK/B;IAED;;;;;OAKG;IACH,IAAW,OAAO,IAAI,MAAM,CAE3B;IAED,IAAW,OAAO,CAAC,KAAK,EAAE,MAAM,EAK/B;IAED,6DAA6D;IAC7D,IAAW,MAAM,IAAI,MAAM,CAE1B;IAED,IAAW,MAAM,CAAC,KAAK,EAAE,MAAM,EAK9B;IAED;;;OAGG;IACH,IAAW,QAAQ,IAAI,MAAM,CAE5B;IAED,IAAW,QAAQ,CAAC,KAAK,EAAE,MAAM,EAKhC;IAED,4FAA4F;IAC5F,IAAW,GAAG,IAAI,MAAM,CAEvB;IAED,IAAW,GAAG,CAAC,KAAK,EAAE,MAAM,EAO3B;IAMe,OAAO,IAAI,IAAI,CAc9B;IAMD,OAAO,CAAC,WAAW;CAyDpB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AudioEffect
|
|
1
|
+
import { AudioEffect } from "@codexo/exojs";
|
|
2
2
|
|
|
3
3
|
//#region src/effects/FlangerEffect.ts
|
|
4
4
|
/**
|
|
@@ -37,19 +37,14 @@ var FlangerEffect = class extends AudioEffect {
|
|
|
37
37
|
_rateHz;
|
|
38
38
|
_feedback;
|
|
39
39
|
_wet;
|
|
40
|
-
_onAudioContextReady = (ctx) => {
|
|
41
|
-
onAudioContextReady.remove(this._onAudioContextReady);
|
|
42
|
-
this._setupNodes(ctx);
|
|
43
|
-
};
|
|
44
40
|
constructor(options = {}) {
|
|
45
41
|
super();
|
|
46
42
|
this._delayMs = Math.max(.5, Math.min(20, options.delayMs ?? 3));
|
|
47
|
-
this._depthMs = Math.max(0, Math.min(10, options.depthMs ?? 2));
|
|
43
|
+
this._depthMs = Math.max(0, Math.min(10, this._delayMs, options.depthMs ?? 2));
|
|
48
44
|
this._rateHz = Math.max(0, Math.min(10, options.rateHz ?? .25));
|
|
49
45
|
this._feedback = Math.max(0, Math.min(.95, options.feedback ?? .5));
|
|
50
46
|
this._wet = Math.max(0, Math.min(1, options.wet ?? .5));
|
|
51
|
-
|
|
52
|
-
else onAudioContextReady.add(this._onAudioContextReady);
|
|
47
|
+
this._deferSetup((context) => this._setupNodes(context));
|
|
53
48
|
}
|
|
54
49
|
/**
|
|
55
50
|
* The node where audio enters this effect. Throws if the audio context is
|
|
@@ -67,20 +62,28 @@ var FlangerEffect = class extends AudioEffect {
|
|
|
67
62
|
if (!this._nodes) throw new Error("FlangerEffect not yet initialized.");
|
|
68
63
|
return this._nodes.outputGain;
|
|
69
64
|
}
|
|
70
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* Base delay time in milliseconds. Range 0.5..20; lowering it below
|
|
67
|
+
* {@link FlangerEffect.depthMs} stops at `depthMs`. Default 3.
|
|
68
|
+
*/
|
|
71
69
|
get delayMs() {
|
|
72
70
|
return this._delayMs;
|
|
73
71
|
}
|
|
74
72
|
set delayMs(value) {
|
|
75
|
-
this._delayMs = Math.max(.5, Math.min(20, value));
|
|
73
|
+
this._delayMs = Math.max(.5, this._depthMs, Math.min(20, value));
|
|
76
74
|
if (this._nodes) this._nodes.delayNode.delayTime.setTargetAtTime(this._delayMs / 1e3, this._nodes.delayNode.context.currentTime, .01);
|
|
77
75
|
}
|
|
78
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* LFO modulation depth in milliseconds (peak deviation from base delay).
|
|
78
|
+
* Range 0..10 and never above {@link FlangerEffect.delayMs}, so the swept
|
|
79
|
+
* delay never reaches below zero, which would flatten one half of the sweep.
|
|
80
|
+
* Default 2.
|
|
81
|
+
*/
|
|
79
82
|
get depthMs() {
|
|
80
83
|
return this._depthMs;
|
|
81
84
|
}
|
|
82
85
|
set depthMs(value) {
|
|
83
|
-
this._depthMs = Math.max(0, Math.min(10, value));
|
|
86
|
+
this._depthMs = Math.max(0, Math.min(10, this._delayMs, value));
|
|
84
87
|
if (this._nodes) this._nodes.lfoGain.gain.setTargetAtTime(this._depthMs / 1e3, this._nodes.lfoGain.context.currentTime, .01);
|
|
85
88
|
}
|
|
86
89
|
/** LFO oscillation rate in Hz. Range 0..10, default 0.25. */
|
|
@@ -115,7 +118,7 @@ var FlangerEffect = class extends AudioEffect {
|
|
|
115
118
|
}
|
|
116
119
|
}
|
|
117
120
|
destroy() {
|
|
118
|
-
|
|
121
|
+
this._teardown();
|
|
119
122
|
if (this._nodes) {
|
|
120
123
|
this._nodes.lfoOscillator.stop();
|
|
121
124
|
this._nodes.lfoOscillator.disconnect();
|
|
@@ -136,8 +139,7 @@ var FlangerEffect = class extends AudioEffect {
|
|
|
136
139
|
const wetGain = ctx.createGain();
|
|
137
140
|
const feedbackGain = ctx.createGain();
|
|
138
141
|
const lfoGain = ctx.createGain();
|
|
139
|
-
const
|
|
140
|
-
const delayNode = ctx.createDelay(maxDelay);
|
|
142
|
+
const delayNode = ctx.createDelay(.08);
|
|
141
143
|
inputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
142
144
|
outputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
143
145
|
dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FlangerEffect.js","names":[],"sources":["../../../src/effects/FlangerEffect.ts"],"sourcesContent":["import { AudioEffect, getAudioContext, isAudioContextReady, onAudioContextReady } from '@codexo/exojs';\n\n/** Construction options for {@link FlangerEffect}. */\nexport interface FlangerEffectOptions {\n /**\n * Base delay time in milliseconds. The LFO modulates the delay around this\n * centre value. Range 0.5..20, default 3.\n */\n delayMs?: number;\n /**\n * LFO modulation depth in milliseconds - the peak deviation added to and\n * subtracted from `delayMs` by the sine LFO. Range 0..10, default 2.\n */\n depthMs?: number;\n /**\n * LFO oscillation rate in Hz. Lower rates produce a slow, sweeping flange;\n * higher rates create a faster jet-plane effect. Range 0..10, default 0.25.\n */\n rateHz?: number;\n /**\n * Feedback amount - fraction of the delay output fed back to the delay input.\n * Higher values intensify the comb-filter resonance. Range 0..0.95, default 0.5.\n */\n feedback?: number;\n /**\n * Dry/wet mix, 0..1. The dry level is automatically `1 - wet`.\n * Default 0.5 (equal mix).\n */\n wet?: number;\n}\n\ninterface FlangerEffectSetup {\n readonly inputGain: GainNode;\n readonly outputGain: GainNode;\n readonly dryGain: GainNode;\n readonly wetGain: GainNode;\n readonly feedbackGain: GainNode;\n readonly lfoGain: GainNode;\n readonly delayNode: DelayNode;\n readonly lfoOscillator: OscillatorNode;\n}\n\n/**\n * Flanger effect using native WebAudio nodes (no worklet required).\n *\n * A flanger is a very short LFO-modulated delay (1-10 ms) with a feedback\n * loop. Unlike a chorus - which uses longer delays without feedback to produce\n * thickening - the flanger's feedback reinforces phase cancellation, creating\n * the characteristic sweeping comb-filter \"jet-plane\" sound as notches and\n * peaks slide up and down the spectrum.\n *\n * Node graph:\n * ```\n * inputGain ┬── dryGain ─────────────────────────┐\n * │ ├── outputGain\n * └──▶ delayNode ──▶ wetGain ───────────┘\n * ↑ │\n * feedbackGain ◀─┘\n *\n * lfoOscillator ── lfoGain (depth in seconds) ──▶ delayNode.delayTime\n * ```\n *\n * Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`;\n * `feedbackGain.gain = feedback`; `lfoGain.gain = depthMs / 1000`.\n *\n * @example\n * ```ts\n * const flanger = new FlangerEffect({ delayMs: 3, depthMs: 2, rateHz: 0.25, feedback: 0.5, wet: 0.5 });\n * bus.addEffect(flanger);\n * ```\n */\nexport class FlangerEffect extends AudioEffect {\n private _nodes: FlangerEffectSetup | null = null;\n private _delayMs: number;\n private _depthMs: number;\n private _rateHz: number;\n private _feedback: number;\n private _wet: number;\n private readonly _onAudioContextReady = (ctx: AudioContext): void => {\n onAudioContextReady.remove(this._onAudioContextReady);\n this._setupNodes(ctx);\n };\n\n public constructor(options: FlangerEffectOptions = {}) {\n super();\n this._delayMs = Math.max(0.5, Math.min(20, options.delayMs ?? 3));\n this._depthMs = Math.max(0, Math.min(10, options.depthMs ?? 2));\n this._rateHz = Math.max(0, Math.min(10, options.rateHz ?? 0.25));\n this._feedback = Math.max(0, Math.min(0.95, options.feedback ?? 0.5));\n this._wet = Math.max(0, Math.min(1, options.wet ?? 0.5));\n if (isAudioContextReady()) {\n this._setupNodes(getAudioContext());\n } else {\n onAudioContextReady.add(this._onAudioContextReady);\n }\n }\n\n /**\n * The node where audio enters this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get inputNode(): AudioNode {\n if (!this._nodes) throw new Error('FlangerEffect not yet initialized.');\n return this._nodes.inputGain;\n }\n\n /**\n * The node where audio exits this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get outputNode(): AudioNode {\n if (!this._nodes) throw new Error('FlangerEffect not yet initialized.');\n return this._nodes.outputGain;\n }\n\n // ---------------------------------------------------------------------------\n // Getters / setters\n // ---------------------------------------------------------------------------\n\n /** Base delay time in milliseconds. Range 0.5..20, default 3. */\n public get delayMs(): number {\n return this._delayMs;\n }\n\n public set delayMs(value: number) {\n this._delayMs = Math.max(0.5, Math.min(20, value));\n if (this._nodes) {\n this._nodes.delayNode.delayTime.setTargetAtTime(this._delayMs / 1000, this._nodes.delayNode.context.currentTime, 0.01);\n }\n }\n\n /** LFO modulation depth in milliseconds (peak deviation from base delay). Range 0..10, default 2. */\n public get depthMs(): number {\n return this._depthMs;\n }\n\n public set depthMs(value: number) {\n this._depthMs = Math.max(0, Math.min(10, value));\n if (this._nodes) {\n this._nodes.lfoGain.gain.setTargetAtTime(this._depthMs / 1000, this._nodes.lfoGain.context.currentTime, 0.01);\n }\n }\n\n /** LFO oscillation rate in Hz. Range 0..10, default 0.25. */\n public get rateHz(): number {\n return this._rateHz;\n }\n\n public set rateHz(value: number) {\n this._rateHz = Math.max(0, Math.min(10, value));\n if (this._nodes) {\n this._nodes.lfoOscillator.frequency.setTargetAtTime(this._rateHz, this._nodes.inputGain.context.currentTime, 0.01);\n }\n }\n\n /**\n * Feedback amount - fraction of delay output fed back to delay input.\n * Range 0..0.95, default 0.5.\n */\n public get feedback(): number {\n return this._feedback;\n }\n\n public set feedback(value: number) {\n this._feedback = Math.max(0, Math.min(0.95, value));\n if (this._nodes) {\n this._nodes.feedbackGain.gain.setTargetAtTime(this._feedback, this._nodes.feedbackGain.context.currentTime, 0.01);\n }\n }\n\n /** Wet (flanged) mix level, 0..1. The dry level is automatically `1 - wet`. Default 0.5. */\n public get wet(): number {\n return this._wet;\n }\n\n public set wet(value: number) {\n this._wet = Math.max(0, Math.min(1, value));\n if (this._nodes) {\n const ctx = this._nodes.wetGain.context;\n this._nodes.wetGain.gain.setTargetAtTime(this._wet, ctx.currentTime, 0.01);\n this._nodes.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);\n }\n }\n\n // ---------------------------------------------------------------------------\n // Lifecycle\n // ---------------------------------------------------------------------------\n\n public override destroy(): void {\n onAudioContextReady.remove(this._onAudioContextReady);\n if (this._nodes) {\n this._nodes.lfoOscillator.stop();\n this._nodes.lfoOscillator.disconnect();\n this._nodes.lfoGain.disconnect();\n this._nodes.feedbackGain.disconnect();\n this._nodes.delayNode.disconnect();\n this._nodes.dryGain.disconnect();\n this._nodes.wetGain.disconnect();\n this._nodes.inputGain.disconnect();\n this._nodes.outputGain.disconnect();\n this._nodes = null;\n }\n }\n\n // ---------------------------------------------------------------------------\n // Private\n // ---------------------------------------------------------------------------\n\n private _setupNodes(ctx: AudioContext): void {\n const inputGain = ctx.createGain();\n const outputGain = ctx.createGain();\n const dryGain = ctx.createGain();\n const wetGain = ctx.createGain();\n const feedbackGain = ctx.createGain();\n const lfoGain = ctx.createGain();\n\n // maxDelayTime must accommodate base delay plus the full modulation depth,\n // with a safety headroom of 50 ms so the DelayNode never clips.\n const maxDelay = (this._delayMs + this._depthMs) / 1000 + 0.05;\n const delayNode = ctx.createDelay(maxDelay);\n\n // Initial parameter values\n inputGain.gain.setValueAtTime(1, ctx.currentTime);\n outputGain.gain.setValueAtTime(1, ctx.currentTime);\n dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);\n wetGain.gain.setValueAtTime(this._wet, ctx.currentTime);\n feedbackGain.gain.setValueAtTime(this._feedback, ctx.currentTime);\n lfoGain.gain.setValueAtTime(this._depthMs / 1000, ctx.currentTime);\n delayNode.delayTime.setValueAtTime(this._delayMs / 1000, ctx.currentTime);\n\n // LFO\n const lfoOscillator = ctx.createOscillator();\n lfoOscillator.type = 'sine';\n lfoOscillator.frequency.value = this._rateHz;\n\n // LFO path: lfoOscillator → lfoGain → delayNode.delayTime (AudioParam)\n lfoOscillator.connect(lfoGain);\n lfoGain.connect(delayNode.delayTime as unknown as AudioNode);\n\n // Dry path: inputGain → dryGain → outputGain\n inputGain.connect(dryGain);\n dryGain.connect(outputGain);\n\n // Wet path: inputGain → delayNode → wetGain → outputGain\n inputGain.connect(delayNode);\n delayNode.connect(wetGain);\n wetGain.connect(outputGain);\n\n // Feedback loop: delayNode → feedbackGain → delayNode\n delayNode.connect(feedbackGain);\n feedbackGain.connect(delayNode);\n\n lfoOscillator.start();\n\n this._nodes = {\n inputGain,\n outputGain,\n dryGain,\n wetGain,\n feedbackGain,\n lfoGain,\n delayNode,\n lfoOscillator,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,IAAa,gBAAb,cAAmC,YAAY;CAC7C,AAAQ,SAAoC;CAC5C,AAAQ;CACR,AAAQ;CACR,AAAQ;CAC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ACF;AACF"}
|
|
1
|
+
{"version":3,"file":"FlangerEffect.js","names":[],"sources":["../../../src/effects/FlangerEffect.ts"],"sourcesContent":["import { AudioEffect } from '@codexo/exojs';\n\n/** Construction options for {@link FlangerEffect}. */\nexport interface FlangerEffectOptions {\n /**\n * Base delay time in milliseconds. The LFO modulates the delay around this\n * centre value. Range 0.5..20, default 3.\n */\n delayMs?: number;\n /**\n * LFO modulation depth in milliseconds - the peak deviation added to and\n * subtracted from `delayMs` by the sine LFO. Range 0..10 and never above\n * `delayMs`, default 2.\n */\n depthMs?: number;\n /**\n * LFO oscillation rate in Hz. Lower rates produce a slow, sweeping flange;\n * higher rates create a faster jet-plane effect. Range 0..10, default 0.25.\n */\n rateHz?: number;\n /**\n * Feedback amount - fraction of the delay output fed back to the delay input.\n * Higher values intensify the comb-filter resonance. Range 0..0.95, default 0.5.\n */\n feedback?: number;\n /**\n * Dry/wet mix, 0..1. The dry level is automatically `1 - wet`.\n * Default 0.5 (equal mix).\n */\n wet?: number;\n}\n\ninterface FlangerEffectSetup {\n readonly inputGain: GainNode;\n readonly outputGain: GainNode;\n readonly dryGain: GainNode;\n readonly wetGain: GainNode;\n readonly feedbackGain: GainNode;\n readonly lfoGain: GainNode;\n readonly delayNode: DelayNode;\n readonly lfoOscillator: OscillatorNode;\n}\n\n/**\n * Flanger effect using native WebAudio nodes (no worklet required).\n *\n * A flanger is a very short LFO-modulated delay (1-10 ms) with a feedback\n * loop. Unlike a chorus - which uses longer delays without feedback to produce\n * thickening - the flanger's feedback reinforces phase cancellation, creating\n * the characteristic sweeping comb-filter \"jet-plane\" sound as notches and\n * peaks slide up and down the spectrum.\n *\n * Node graph:\n * ```\n * inputGain ┬── dryGain ─────────────────────────┐\n * │ ├── outputGain\n * └──▶ delayNode ──▶ wetGain ───────────┘\n * ↑ │\n * feedbackGain ◀─┘\n *\n * lfoOscillator ── lfoGain (depth in seconds) ──▶ delayNode.delayTime\n * ```\n *\n * Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`;\n * `feedbackGain.gain = feedback`; `lfoGain.gain = depthMs / 1000`.\n *\n * @example\n * ```ts\n * const flanger = new FlangerEffect({ delayMs: 3, depthMs: 2, rateHz: 0.25, feedback: 0.5, wet: 0.5 });\n * bus.addEffect(flanger);\n * ```\n */\nexport class FlangerEffect extends AudioEffect {\n private _nodes: FlangerEffectSetup | null = null;\n private _delayMs: number;\n private _depthMs: number;\n private _rateHz: number;\n private _feedback: number;\n private _wet: number;\n\n public constructor(options: FlangerEffectOptions = {}) {\n super();\n this._delayMs = Math.max(0.5, Math.min(20, options.delayMs ?? 3));\n this._depthMs = Math.max(0, Math.min(10, this._delayMs, options.depthMs ?? 2));\n this._rateHz = Math.max(0, Math.min(10, options.rateHz ?? 0.25));\n this._feedback = Math.max(0, Math.min(0.95, options.feedback ?? 0.5));\n this._wet = Math.max(0, Math.min(1, options.wet ?? 0.5));\n this._deferSetup(context => this._setupNodes(context));\n }\n\n /**\n * The node where audio enters this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get inputNode(): AudioNode {\n if (!this._nodes) throw new Error('FlangerEffect not yet initialized.');\n return this._nodes.inputGain;\n }\n\n /**\n * The node where audio exits this effect. Throws if the audio context is\n * not yet initialized.\n */\n public get outputNode(): AudioNode {\n if (!this._nodes) throw new Error('FlangerEffect not yet initialized.');\n return this._nodes.outputGain;\n }\n\n // ---------------------------------------------------------------------------\n // Getters / setters\n // ---------------------------------------------------------------------------\n\n /**\n * Base delay time in milliseconds. Range 0.5..20; lowering it below\n * {@link FlangerEffect.depthMs} stops at `depthMs`. Default 3.\n */\n public get delayMs(): number {\n return this._delayMs;\n }\n\n public set delayMs(value: number) {\n this._delayMs = Math.max(0.5, this._depthMs, Math.min(20, value));\n if (this._nodes) {\n this._nodes.delayNode.delayTime.setTargetAtTime(this._delayMs / 1000, this._nodes.delayNode.context.currentTime, 0.01);\n }\n }\n\n /**\n * LFO modulation depth in milliseconds (peak deviation from base delay).\n * Range 0..10 and never above {@link FlangerEffect.delayMs}, so the swept\n * delay never reaches below zero, which would flatten one half of the sweep.\n * Default 2.\n */\n public get depthMs(): number {\n return this._depthMs;\n }\n\n public set depthMs(value: number) {\n this._depthMs = Math.max(0, Math.min(10, this._delayMs, value));\n if (this._nodes) {\n this._nodes.lfoGain.gain.setTargetAtTime(this._depthMs / 1000, this._nodes.lfoGain.context.currentTime, 0.01);\n }\n }\n\n /** LFO oscillation rate in Hz. Range 0..10, default 0.25. */\n public get rateHz(): number {\n return this._rateHz;\n }\n\n public set rateHz(value: number) {\n this._rateHz = Math.max(0, Math.min(10, value));\n if (this._nodes) {\n this._nodes.lfoOscillator.frequency.setTargetAtTime(this._rateHz, this._nodes.inputGain.context.currentTime, 0.01);\n }\n }\n\n /**\n * Feedback amount - fraction of delay output fed back to delay input.\n * Range 0..0.95, default 0.5.\n */\n public get feedback(): number {\n return this._feedback;\n }\n\n public set feedback(value: number) {\n this._feedback = Math.max(0, Math.min(0.95, value));\n if (this._nodes) {\n this._nodes.feedbackGain.gain.setTargetAtTime(this._feedback, this._nodes.feedbackGain.context.currentTime, 0.01);\n }\n }\n\n /** Wet (flanged) mix level, 0..1. The dry level is automatically `1 - wet`. Default 0.5. */\n public get wet(): number {\n return this._wet;\n }\n\n public set wet(value: number) {\n this._wet = Math.max(0, Math.min(1, value));\n if (this._nodes) {\n const ctx = this._nodes.wetGain.context;\n this._nodes.wetGain.gain.setTargetAtTime(this._wet, ctx.currentTime, 0.01);\n this._nodes.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);\n }\n }\n\n // ---------------------------------------------------------------------------\n // Lifecycle\n // ---------------------------------------------------------------------------\n\n public override destroy(): void {\n this._teardown();\n if (this._nodes) {\n this._nodes.lfoOscillator.stop();\n this._nodes.lfoOscillator.disconnect();\n this._nodes.lfoGain.disconnect();\n this._nodes.feedbackGain.disconnect();\n this._nodes.delayNode.disconnect();\n this._nodes.dryGain.disconnect();\n this._nodes.wetGain.disconnect();\n this._nodes.inputGain.disconnect();\n this._nodes.outputGain.disconnect();\n this._nodes = null;\n }\n }\n\n // ---------------------------------------------------------------------------\n // Private\n // ---------------------------------------------------------------------------\n\n private _setupNodes(ctx: AudioContext): void {\n const inputGain = ctx.createGain();\n const outputGain = ctx.createGain();\n const dryGain = ctx.createGain();\n const wetGain = ctx.createGain();\n const feedbackGain = ctx.createGain();\n const lfoGain = ctx.createGain();\n\n // Sized for the largest reachable base delay plus depth (20 + 10 ms), not\n // the construction-time values: a DelayNode silently clamps delayTime to\n // the maxDelayTime it was created with.\n const delayNode = ctx.createDelay(0.03 + 0.05);\n\n // Initial parameter values\n inputGain.gain.setValueAtTime(1, ctx.currentTime);\n outputGain.gain.setValueAtTime(1, ctx.currentTime);\n dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);\n wetGain.gain.setValueAtTime(this._wet, ctx.currentTime);\n feedbackGain.gain.setValueAtTime(this._feedback, ctx.currentTime);\n lfoGain.gain.setValueAtTime(this._depthMs / 1000, ctx.currentTime);\n delayNode.delayTime.setValueAtTime(this._delayMs / 1000, ctx.currentTime);\n\n // LFO\n const lfoOscillator = ctx.createOscillator();\n lfoOscillator.type = 'sine';\n lfoOscillator.frequency.value = this._rateHz;\n\n // LFO path: lfoOscillator → lfoGain → delayNode.delayTime (AudioParam)\n lfoOscillator.connect(lfoGain);\n lfoGain.connect(delayNode.delayTime as unknown as AudioNode);\n\n // Dry path: inputGain → dryGain → outputGain\n inputGain.connect(dryGain);\n dryGain.connect(outputGain);\n\n // Wet path: inputGain → delayNode → wetGain → outputGain\n inputGain.connect(delayNode);\n delayNode.connect(wetGain);\n wetGain.connect(outputGain);\n\n // Feedback loop: delayNode → feedbackGain → delayNode\n delayNode.connect(feedbackGain);\n feedbackGain.connect(delayNode);\n\n lfoOscillator.start();\n\n this._nodes = {\n inputGain,\n outputGain,\n dryGain,\n wetGain,\n feedbackGain,\n lfoGain,\n delayNode,\n lfoOscillator,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;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export interface LimiterEffectOptions {
|
|
|
22
22
|
*/
|
|
23
23
|
wet?: number;
|
|
24
24
|
/**
|
|
25
|
-
* Input-to-output dB ratio above the threshold. Range 1..20, default 20
|
|
26
|
-
*
|
|
25
|
+
* Input-to-output dB ratio above the threshold. Range 1..20, default 20, the
|
|
26
|
+
* steepest a `DynamicsCompressorNode` allows. Lower it for a softer, more
|
|
27
|
+
* compressor-like limiter.
|
|
27
28
|
*/
|
|
28
29
|
ratio?: number;
|
|
29
30
|
/**
|
|
@@ -34,12 +35,16 @@ export interface LimiterEffectOptions {
|
|
|
34
35
|
knee?: number;
|
|
35
36
|
}
|
|
36
37
|
/**
|
|
37
|
-
*
|
|
38
|
-
* for hard limiting:
|
|
39
|
-
* fast attack. Use it
|
|
40
|
-
*
|
|
38
|
+
* Limiter-style compressor backed by a Web Audio `DynamicsCompressorNode` set
|
|
39
|
+
* for hard limiting: the highest ratio the node allows (20), zero knee (hard
|
|
40
|
+
* knee), and a fast attack. Use it at the end of a chain to tame peaks.
|
|
41
|
+
*
|
|
42
|
+
* It is not a brick-wall peak limiter: the node has no lookahead, so a
|
|
43
|
+
* transient faster than `attack` passes before gain reduction engages, and
|
|
44
|
+
* above the threshold the level still rises by 1 dB per 20 dB of input.
|
|
45
|
+
* Leave headroom below 0 dBFS rather than relying on it to stop every clip.
|
|
41
46
|
*
|
|
42
|
-
* The compressor `ratio` and `knee` default to
|
|
47
|
+
* The compressor `ratio` and `knee` default to the hardest values (20 and 0
|
|
43
48
|
* respectively) but remain configurable, unlike a truly fixed limiter,
|
|
44
49
|
* because callers occasionally want a softer, more compressor-like limiting
|
|
45
50
|
* curve (lower ratio) or a gentler transition into limiting (larger knee)
|
|
@@ -69,7 +74,6 @@ export declare class LimiterEffect extends AudioEffect {
|
|
|
69
74
|
private _wet;
|
|
70
75
|
private _ratio;
|
|
71
76
|
private _knee;
|
|
72
|
-
private readonly _onAudioContextReady;
|
|
73
77
|
constructor(options?: LimiterEffectOptions);
|
|
74
78
|
/**
|
|
75
79
|
* The node where audio enters this effect. Throws if the audio context is
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LimiterEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/LimiterEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"LimiterEffect.d.ts","sourceRoot":"","sources":["../../../src/effects/LimiterEffect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,sDAAsD;AACtD,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAUD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,aAAc,SAAQ,WAAW;IAC5C,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAS;IAEtB,YAAmB,OAAO,GAAE,oBAAyB,EASpD;IAED;;;OAGG;IACH,IAAW,SAAS,IAAI,SAAS,CAGhC;IAED;;;OAGG;IACH,IAAW,UAAU,IAAI,SAAS,CAGjC;IAED;;;OAGG;IACH,IAAW,SAAS,IAAI,MAAM,CAE7B;IAED,IAAW,SAAS,CAAC,KAAK,EAAE,MAAM,EAKjC;IAED;;;OAGG;IACH,IAAW,MAAM,IA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