@codexo/exojs-audio-fx 0.14.0 → 0.15.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/dist/esm/BeatDetector.d.ts +55 -9
- package/dist/esm/BeatDetector.js +19 -9
- package/dist/esm/BeatDetector.js.map +1 -1
- package/dist/esm/dsp/tempogram.d.ts +122 -21
- package/dist/esm/effects/AutoWahEffect.d.ts +127 -0
- package/dist/esm/effects/AutoWahEffect.js +241 -0
- package/dist/esm/effects/AutoWahEffect.js.map +1 -0
- package/dist/esm/effects/BitCrusherEffect.d.ts +63 -0
- package/dist/esm/effects/BitCrusherEffect.js +74 -0
- package/dist/esm/effects/BitCrusherEffect.js.map +1 -0
- package/dist/esm/effects/ConvolutionEffect.d.ts +114 -0
- package/dist/esm/effects/ConvolutionEffect.js +193 -0
- package/dist/esm/effects/ConvolutionEffect.js.map +1 -0
- package/dist/esm/effects/DistortionEffect.d.ts +119 -0
- package/dist/esm/effects/DistortionEffect.js +199 -0
- package/dist/esm/effects/DistortionEffect.js.map +1 -0
- package/dist/esm/effects/DuckingEffect.js +6 -2
- package/dist/esm/effects/DuckingEffect.js.map +1 -1
- package/dist/esm/effects/FlangerEffect.d.ts +98 -0
- package/dist/esm/effects/FlangerEffect.js +205 -0
- package/dist/esm/effects/FlangerEffect.js.map +1 -0
- package/dist/esm/effects/GranularEffect.d.ts +8 -4
- package/dist/esm/effects/GranularEffect.js +4 -12
- package/dist/esm/effects/GranularEffect.js.map +1 -1
- package/dist/esm/effects/LimiterEffect.d.ts +124 -0
- package/dist/esm/effects/LimiterEffect.js +198 -0
- package/dist/esm/effects/LimiterEffect.js.map +1 -0
- package/dist/esm/effects/PhaserEffect.d.ts +126 -0
- package/dist/esm/effects/PhaserEffect.js +258 -0
- package/dist/esm/effects/PhaserEffect.js.map +1 -0
- package/dist/esm/effects/PingPongDelayEffect.d.ts +83 -0
- package/dist/esm/effects/PingPongDelayEffect.js +189 -0
- package/dist/esm/effects/PingPongDelayEffect.js.map +1 -0
- package/dist/esm/effects/PitchShiftEffect.d.ts +14 -13
- package/dist/esm/effects/PitchShiftEffect.js +16 -18
- package/dist/esm/effects/PitchShiftEffect.js.map +1 -1
- package/dist/esm/effects/RingModulatorEffect.d.ts +95 -0
- package/dist/esm/effects/RingModulatorEffect.js +165 -0
- package/dist/esm/effects/RingModulatorEffect.js.map +1 -0
- package/dist/esm/effects/TremoloEffect.d.ts +110 -0
- package/dist/esm/effects/TremoloEffect.js +205 -0
- package/dist/esm/effects/TremoloEffect.js.map +1 -0
- package/dist/esm/effects/VocoderEffect.d.ts +0 -4
- package/dist/esm/effects/VocoderEffect.js +1 -11
- package/dist/esm/effects/VocoderEffect.js.map +1 -1
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/worklets/beat-detector.worklet.d.ts +1 -1
- package/dist/esm/worklets/beat-detector.worklet.js +628 -112
- package/dist/esm/worklets/beat-detector.worklet.js.map +1 -1
- package/dist/esm/worklets/bit-crusher.worklet.d.ts +1 -0
- package/dist/esm/worklets/bit-crusher.worklet.js +45 -0
- package/dist/esm/worklets/bit-crusher.worklet.js.map +1 -0
- package/dist/esm/worklets/granular.worklet.d.ts +1 -1
- package/dist/esm/worklets/granular.worklet.js +13 -4
- package/dist/esm/worklets/granular.worklet.js.map +1 -1
- package/dist/esm/worklets/pitch-shift.worklet.d.ts +1 -1
- package/dist/esm/worklets/pitch-shift.worklet.js +87 -32
- package/dist/esm/worklets/pitch-shift.worklet.js.map +1 -1
- package/dist/esm/worklets/vocoder.worklet.d.ts +1 -1
- package/dist/esm/worklets/vocoder.worklet.js +5 -3
- package/dist/esm/worklets/vocoder.worklet.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { AudioEffect, onAudioContextReady, isAudioContextReady, getAudioContext, Sound } from '@codexo/exojs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convolution effect that convolves the input signal with a user-supplied
|
|
5
|
+
* impulse response (IR). Unlike {@link ReverbEffect}, which generates a
|
|
6
|
+
* procedural noise-based IR, `ConvolutionEffect` accepts a *real* decoded IR —
|
|
7
|
+
* enabling convolution reverb from acoustic-space captures, cabinet/speaker
|
|
8
|
+
* simulations, telephone/pipe character filtering, and any other linear
|
|
9
|
+
* time-invariant transformation.
|
|
10
|
+
*
|
|
11
|
+
* Pass the IR as an `AudioBuffer` (typically decoded via
|
|
12
|
+
* `AudioContext.decodeAudioData`) or as a `Sound` (its `audioBuffer` is used).
|
|
13
|
+
* Omitting the IR leaves `convolver.buffer` as `null`, making the wet path
|
|
14
|
+
* silent and the effect act as a passthrough until {@link setImpulse} is
|
|
15
|
+
* called — safe to add to a bus before an IR is available.
|
|
16
|
+
*
|
|
17
|
+
* Node graph:
|
|
18
|
+
* ```
|
|
19
|
+
* input(GainNode) → dryGain → output(GainNode)
|
|
20
|
+
* input → convolver → wetGain → output
|
|
21
|
+
* ```
|
|
22
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet × gain`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* await loader.load(Sound, { hall: 'hall.wav' });
|
|
27
|
+
* const reverb = new ConvolutionEffect({ impulse: loader.get(Sound, 'hall'), wet: 0.6 });
|
|
28
|
+
* bus.addEffect(reverb);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
class ConvolutionEffect extends AudioEffect {
|
|
32
|
+
_setup = null;
|
|
33
|
+
_wet;
|
|
34
|
+
_normalize;
|
|
35
|
+
_gain;
|
|
36
|
+
_pendingImpulse;
|
|
37
|
+
_onAudioContextReady = (ctx) => {
|
|
38
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
39
|
+
this._setupNodes(ctx);
|
|
40
|
+
};
|
|
41
|
+
constructor(options = {}) {
|
|
42
|
+
super();
|
|
43
|
+
this._wet = Math.max(0, Math.min(1, options.wet ?? 1));
|
|
44
|
+
this._normalize = options.normalize ?? true;
|
|
45
|
+
this._gain = Math.max(0, Math.min(4, options.gain ?? 1));
|
|
46
|
+
this._pendingImpulse = options.impulse ?? null;
|
|
47
|
+
if (isAudioContextReady()) {
|
|
48
|
+
this._setupNodes(getAudioContext());
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
onAudioContextReady.add(this._onAudioContextReady);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The node where audio enters this effect. Throws if the audio context is
|
|
56
|
+
* not yet initialized.
|
|
57
|
+
*/
|
|
58
|
+
get inputNode() {
|
|
59
|
+
if (!this._setup)
|
|
60
|
+
throw new Error('ConvolutionEffect not yet initialized.');
|
|
61
|
+
return this._setup.inputGain;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The node where audio exits this effect. Throws if the audio context is
|
|
65
|
+
* not yet initialized.
|
|
66
|
+
*/
|
|
67
|
+
get outputNode() {
|
|
68
|
+
if (!this._setup)
|
|
69
|
+
throw new Error('ConvolutionEffect not yet initialized.');
|
|
70
|
+
return this._setup.outputGain;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Wet (convolved) mix level, 0..1. The dry level is automatically `1 - wet`.
|
|
74
|
+
* Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.
|
|
75
|
+
*/
|
|
76
|
+
get wet() {
|
|
77
|
+
return this._wet;
|
|
78
|
+
}
|
|
79
|
+
set wet(value) {
|
|
80
|
+
this._wet = Math.max(0, Math.min(1, value));
|
|
81
|
+
if (this._setup) {
|
|
82
|
+
const ctx = this._setup.wetGain.context;
|
|
83
|
+
this._setup.wetGain.gain.setTargetAtTime(this._wet * this._gain, ctx.currentTime, 0.01);
|
|
84
|
+
this._setup.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Whether the browser normalizes the IR for equal-power loudness. Mirrors
|
|
89
|
+
* `ConvolverNode.normalize`. Default `true`.
|
|
90
|
+
*
|
|
91
|
+
* Changing this at runtime re-assigns the current buffer so the new value
|
|
92
|
+
* takes effect (a Web Audio requirement: `normalize` is only applied on
|
|
93
|
+
* buffer assignment).
|
|
94
|
+
*/
|
|
95
|
+
get normalize() {
|
|
96
|
+
return this._normalize;
|
|
97
|
+
}
|
|
98
|
+
set normalize(value) {
|
|
99
|
+
this._normalize = value;
|
|
100
|
+
if (this._setup) {
|
|
101
|
+
const { convolver } = this._setup;
|
|
102
|
+
convolver.normalize = this._normalize;
|
|
103
|
+
if (convolver.buffer !== null) {
|
|
104
|
+
// Re-assign so the new normalize value takes effect immediately.
|
|
105
|
+
// (The Web Audio spec only applies normalize on buffer assignment.)
|
|
106
|
+
const current = convolver.buffer;
|
|
107
|
+
convolver.buffer = current;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Makeup gain multiplier applied to the wet path. Range 0..4, default 1.
|
|
113
|
+
* Adjusting this changes the wet-path amplitude without altering the
|
|
114
|
+
* dry/wet balance ratio.
|
|
115
|
+
*/
|
|
116
|
+
get gain() {
|
|
117
|
+
return this._gain;
|
|
118
|
+
}
|
|
119
|
+
set gain(value) {
|
|
120
|
+
this._gain = Math.max(0, Math.min(4, value));
|
|
121
|
+
if (this._setup) {
|
|
122
|
+
const ctx = this._setup.wetGain.context;
|
|
123
|
+
this._setup.wetGain.gain.setTargetAtTime(this._wet * this._gain, ctx.currentTime, 0.01);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Set or replace the impulse response. Accepts a decoded `AudioBuffer`, a
|
|
128
|
+
* `Sound` (its `audioBuffer` is used), or `null` to clear the IR (wet path
|
|
129
|
+
* goes silent). Safe to call before the audio context is ready — the impulse
|
|
130
|
+
* is stored and applied once nodes are created.
|
|
131
|
+
*
|
|
132
|
+
* **Web Audio note:** `normalize` is written to the convolver *before*
|
|
133
|
+
* `buffer` is assigned, as required by the spec for the value to take effect.
|
|
134
|
+
*/
|
|
135
|
+
setImpulse(ir) {
|
|
136
|
+
const resolved = this._resolveBuffer(ir);
|
|
137
|
+
if (this._setup) {
|
|
138
|
+
const { convolver } = this._setup;
|
|
139
|
+
convolver.normalize = this._normalize;
|
|
140
|
+
convolver.buffer = resolved;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
// Store for application in _setupNodes once the context is ready.
|
|
144
|
+
this._pendingImpulse = ir;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
destroy() {
|
|
148
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
149
|
+
if (this._setup) {
|
|
150
|
+
this._setup.inputGain.disconnect();
|
|
151
|
+
this._setup.convolver.disconnect();
|
|
152
|
+
this._setup.dryGain.disconnect();
|
|
153
|
+
this._setup.wetGain.disconnect();
|
|
154
|
+
this._setup.outputGain.disconnect();
|
|
155
|
+
this._setup = null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
_resolveBuffer(ir) {
|
|
159
|
+
if (ir === null)
|
|
160
|
+
return null;
|
|
161
|
+
if (ir instanceof Sound)
|
|
162
|
+
return ir.audioBuffer;
|
|
163
|
+
return ir;
|
|
164
|
+
}
|
|
165
|
+
_setupNodes(ctx) {
|
|
166
|
+
const inputGain = ctx.createGain();
|
|
167
|
+
const outputGain = ctx.createGain();
|
|
168
|
+
const convolver = ctx.createConvolver();
|
|
169
|
+
const dryGain = ctx.createGain();
|
|
170
|
+
const wetGain = ctx.createGain();
|
|
171
|
+
inputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
172
|
+
outputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
173
|
+
dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);
|
|
174
|
+
wetGain.gain.setValueAtTime(this._wet * this._gain, ctx.currentTime);
|
|
175
|
+
// Web Audio requirement: set normalize BEFORE assigning buffer.
|
|
176
|
+
convolver.normalize = this._normalize;
|
|
177
|
+
if (this._pendingImpulse !== null) {
|
|
178
|
+
convolver.buffer = this._resolveBuffer(this._pendingImpulse);
|
|
179
|
+
this._pendingImpulse = null;
|
|
180
|
+
}
|
|
181
|
+
// Dry path: input → dryGain → output
|
|
182
|
+
inputGain.connect(dryGain);
|
|
183
|
+
dryGain.connect(outputGain);
|
|
184
|
+
// Wet path: input → convolver → wetGain → output
|
|
185
|
+
inputGain.connect(convolver);
|
|
186
|
+
convolver.connect(wetGain);
|
|
187
|
+
wetGain.connect(outputGain);
|
|
188
|
+
this._setup = { inputGain, outputGain, convolver, dryGain, wetGain };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export { ConvolutionEffect };
|
|
193
|
+
//# sourceMappingURL=ConvolutionEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConvolutionEffect.js","sources":["../../../../src/effects/ConvolutionEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAyCA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;AACG,MAAO,iBAAkB,SAAQ,WAAW,CAAA;IACxC,MAAM,GAAkC,IAAI;AAC5C,IAAA,IAAI;AACJ,IAAA,UAAU;AACV,IAAA,KAAK;AACL,IAAA,eAAe;AACN,IAAA,oBAAoB,GAAG,CAAC,GAAiB,KAAU;AAClE,QAAA,mBAAmB,CAAC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC;AACrD,QAAA,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC;AACvB,IAAA,CAAC;AAED,IAAA,WAAA,CAAmB,UAAoC,EAAE,EAAA;AACvD,QAAA,KAAK,EAAE;QACP,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,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3
|
+
export interface DistortionEffectOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Drive amount: controls the intensity of the tanh soft-clip curve.
|
|
6
|
+
* Range 0..1, default 0.4. Higher values produce heavier, more saturated
|
|
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.
|
|
10
|
+
*/
|
|
11
|
+
drive?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Dry/wet mix, 0..1. The dry level is automatically `1 - wet`. Default 1.0.
|
|
14
|
+
*/
|
|
15
|
+
wet?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Oversampling mode applied by the `WaveShaperNode`. Higher oversampling
|
|
18
|
+
* reduces aliasing artifacts at the cost of extra CPU. One of `'none'`,
|
|
19
|
+
* `'2x'`, or `'4x'`. Default `'2x'`.
|
|
20
|
+
*/
|
|
21
|
+
oversample?: OverSampleType;
|
|
22
|
+
/**
|
|
23
|
+
* Tone control: cutoff of a post-distortion low-pass filter on the wet path.
|
|
24
|
+
* Mapped logarithmically from 100 Hz (tone=0, fully dark) to 20 kHz
|
|
25
|
+
* (tone=1, fully open). Range 0..1, default 1.
|
|
26
|
+
*/
|
|
27
|
+
tone?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Distortion effect built from native Web Audio nodes — no worklet required.
|
|
31
|
+
*
|
|
32
|
+
* The input signal is split into a dry path and a wet path. The wet path runs
|
|
33
|
+
* through a `WaveShaperNode` loaded with a tanh-based soft-clip curve, then
|
|
34
|
+
* through a `BiquadFilterNode` (lowpass) acting as a tone control, before
|
|
35
|
+
* being mixed back with the dry signal. The wave-shaper curve is rebuilt any
|
|
36
|
+
* time `drive` changes. Oversampling is delegated directly to
|
|
37
|
+
* `WaveShaperNode.oversample`.
|
|
38
|
+
*
|
|
39
|
+
* Node graph:
|
|
40
|
+
* ```
|
|
41
|
+
* input(GainNode) ──── dryGain ─────────────────────────── output(GainNode)
|
|
42
|
+
* input ──── waveShaper ── toneFilter ── wetGain ── output
|
|
43
|
+
* ```
|
|
44
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const dist = new DistortionEffect({ drive: 0.7, wet: 0.9, tone: 0.6 });
|
|
49
|
+
* bus.addEffect(dist);
|
|
50
|
+
*
|
|
51
|
+
* // Soften the drive at runtime:
|
|
52
|
+
* dist.drive = 0.3;
|
|
53
|
+
*
|
|
54
|
+
* // Open the tone filter fully:
|
|
55
|
+
* dist.tone = 1;
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare class DistortionEffect extends AudioEffect {
|
|
59
|
+
private _setup;
|
|
60
|
+
private _drive;
|
|
61
|
+
private _wet;
|
|
62
|
+
private _oversample;
|
|
63
|
+
private _tone;
|
|
64
|
+
private readonly _onAudioContextReady;
|
|
65
|
+
constructor(options?: DistortionEffectOptions);
|
|
66
|
+
/**
|
|
67
|
+
* The node where audio enters this effect. Throws if the audio context is
|
|
68
|
+
* not yet initialized.
|
|
69
|
+
*/
|
|
70
|
+
get inputNode(): AudioNode;
|
|
71
|
+
/**
|
|
72
|
+
* The node where audio exits this effect. Throws if the audio context is
|
|
73
|
+
* not yet initialized.
|
|
74
|
+
*/
|
|
75
|
+
get outputNode(): AudioNode;
|
|
76
|
+
/**
|
|
77
|
+
* Drive amount controlling the soft-clip intensity. Range 0..1, default 0.4.
|
|
78
|
+
* Changing this rebuilds the wave-shaper curve immediately (sample-accurate
|
|
79
|
+
* on the next render quantum).
|
|
80
|
+
*/
|
|
81
|
+
get drive(): number;
|
|
82
|
+
set drive(value: number);
|
|
83
|
+
/**
|
|
84
|
+
* Wet (distorted) mix level, 0..1. The dry level is automatically `1 - wet`.
|
|
85
|
+
* Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.
|
|
86
|
+
*/
|
|
87
|
+
get wet(): number;
|
|
88
|
+
set wet(value: number);
|
|
89
|
+
/**
|
|
90
|
+
* Oversampling mode for the `WaveShaperNode`. One of `'none'`, `'2x'`, or
|
|
91
|
+
* `'4x'`. Higher values reduce aliasing but increase CPU usage. Default `'2x'`.
|
|
92
|
+
*/
|
|
93
|
+
get oversample(): OverSampleType;
|
|
94
|
+
set oversample(value: OverSampleType);
|
|
95
|
+
/**
|
|
96
|
+
* Tone control: low-pass cutoff on the wet path. 0 = fully dark (~100 Hz),
|
|
97
|
+
* 1 = fully open (~20 kHz). Mapped logarithmically. Default 1.
|
|
98
|
+
* Changes are ramped smoothly via `setTargetAtTime`.
|
|
99
|
+
*/
|
|
100
|
+
get tone(): number;
|
|
101
|
+
set tone(value: number);
|
|
102
|
+
destroy(): void;
|
|
103
|
+
/**
|
|
104
|
+
* Build a 256-point tanh-based soft-clip curve for the given drive (0..1).
|
|
105
|
+
*
|
|
106
|
+
* Maps drive to a distortion amount `a = drive * 400 + 1`, then fills the
|
|
107
|
+
* curve with `tanh(x * a) / tanh(a)` so that the output is always bounded
|
|
108
|
+
* to [-1, 1]. At drive=0, `a = 1` and the curve is `tanh(x) / tanh(1)`,
|
|
109
|
+
* which has a small-signal gain of `1/tanh(1) ≈ 1.31` — not perfectly
|
|
110
|
+
* linear. At drive=1, `a = 401` and the curve approximates a hard-clipper.
|
|
111
|
+
*/
|
|
112
|
+
private static _buildCurve;
|
|
113
|
+
/**
|
|
114
|
+
* Map tone (0..1) to a low-pass cutoff frequency in Hz.
|
|
115
|
+
* Logarithmic scale: 100 Hz at tone=0, 20 000 Hz at tone=1.
|
|
116
|
+
*/
|
|
117
|
+
private static _toneToFrequency;
|
|
118
|
+
private _setupNodes;
|
|
119
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { AudioEffect, onAudioContextReady, isAudioContextReady, getAudioContext } from '@codexo/exojs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Distortion effect built from native Web Audio nodes — no worklet required.
|
|
5
|
+
*
|
|
6
|
+
* The input signal is split into a dry path and a wet path. The wet path runs
|
|
7
|
+
* through a `WaveShaperNode` loaded with a tanh-based soft-clip curve, then
|
|
8
|
+
* through a `BiquadFilterNode` (lowpass) acting as a tone control, before
|
|
9
|
+
* being mixed back with the dry signal. The wave-shaper curve is rebuilt any
|
|
10
|
+
* time `drive` changes. Oversampling is delegated directly to
|
|
11
|
+
* `WaveShaperNode.oversample`.
|
|
12
|
+
*
|
|
13
|
+
* Node graph:
|
|
14
|
+
* ```
|
|
15
|
+
* input(GainNode) ──── dryGain ─────────────────────────── output(GainNode)
|
|
16
|
+
* input ──── waveShaper ── toneFilter ── wetGain ── output
|
|
17
|
+
* ```
|
|
18
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const dist = new DistortionEffect({ drive: 0.7, wet: 0.9, tone: 0.6 });
|
|
23
|
+
* bus.addEffect(dist);
|
|
24
|
+
*
|
|
25
|
+
* // Soften the drive at runtime:
|
|
26
|
+
* dist.drive = 0.3;
|
|
27
|
+
*
|
|
28
|
+
* // Open the tone filter fully:
|
|
29
|
+
* dist.tone = 1;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
class DistortionEffect extends AudioEffect {
|
|
33
|
+
_setup = null;
|
|
34
|
+
_drive;
|
|
35
|
+
_wet;
|
|
36
|
+
_oversample;
|
|
37
|
+
_tone;
|
|
38
|
+
_onAudioContextReady = (ctx) => {
|
|
39
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
40
|
+
this._setupNodes(ctx);
|
|
41
|
+
};
|
|
42
|
+
constructor(options = {}) {
|
|
43
|
+
super();
|
|
44
|
+
this._drive = Math.max(0, Math.min(1, options.drive ?? 0.4));
|
|
45
|
+
this._wet = Math.max(0, Math.min(1, options.wet ?? 1));
|
|
46
|
+
this._oversample = options.oversample ?? '2x';
|
|
47
|
+
this._tone = Math.max(0, Math.min(1, options.tone ?? 1));
|
|
48
|
+
if (isAudioContextReady()) {
|
|
49
|
+
this._setupNodes(getAudioContext());
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
onAudioContextReady.add(this._onAudioContextReady);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The node where audio enters this effect. Throws if the audio context is
|
|
57
|
+
* not yet initialized.
|
|
58
|
+
*/
|
|
59
|
+
get inputNode() {
|
|
60
|
+
if (!this._setup)
|
|
61
|
+
throw new Error('DistortionEffect not yet initialized.');
|
|
62
|
+
return this._setup.inputGain;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The node where audio exits this effect. Throws if the audio context is
|
|
66
|
+
* not yet initialized.
|
|
67
|
+
*/
|
|
68
|
+
get outputNode() {
|
|
69
|
+
if (!this._setup)
|
|
70
|
+
throw new Error('DistortionEffect not yet initialized.');
|
|
71
|
+
return this._setup.outputGain;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Drive amount controlling the soft-clip intensity. Range 0..1, default 0.4.
|
|
75
|
+
* Changing this rebuilds the wave-shaper curve immediately (sample-accurate
|
|
76
|
+
* on the next render quantum).
|
|
77
|
+
*/
|
|
78
|
+
get drive() {
|
|
79
|
+
return this._drive;
|
|
80
|
+
}
|
|
81
|
+
set drive(value) {
|
|
82
|
+
this._drive = Math.max(0, Math.min(1, value));
|
|
83
|
+
if (this._setup) {
|
|
84
|
+
this._setup.waveShaper.curve = DistortionEffect._buildCurve(this._drive);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Wet (distorted) mix level, 0..1. The dry level is automatically `1 - wet`.
|
|
89
|
+
* Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.
|
|
90
|
+
*/
|
|
91
|
+
get wet() {
|
|
92
|
+
return this._wet;
|
|
93
|
+
}
|
|
94
|
+
set wet(value) {
|
|
95
|
+
this._wet = Math.max(0, Math.min(1, value));
|
|
96
|
+
if (this._setup) {
|
|
97
|
+
const ctx = this._setup.wetGain.context;
|
|
98
|
+
this._setup.wetGain.gain.setTargetAtTime(this._wet, ctx.currentTime, 0.01);
|
|
99
|
+
this._setup.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Oversampling mode for the `WaveShaperNode`. One of `'none'`, `'2x'`, or
|
|
104
|
+
* `'4x'`. Higher values reduce aliasing but increase CPU usage. Default `'2x'`.
|
|
105
|
+
*/
|
|
106
|
+
get oversample() {
|
|
107
|
+
return this._oversample;
|
|
108
|
+
}
|
|
109
|
+
set oversample(value) {
|
|
110
|
+
this._oversample = value;
|
|
111
|
+
if (this._setup) {
|
|
112
|
+
this._setup.waveShaper.oversample = this._oversample;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Tone control: low-pass cutoff on the wet path. 0 = fully dark (~100 Hz),
|
|
117
|
+
* 1 = fully open (~20 kHz). Mapped logarithmically. Default 1.
|
|
118
|
+
* Changes are ramped smoothly via `setTargetAtTime`.
|
|
119
|
+
*/
|
|
120
|
+
get tone() {
|
|
121
|
+
return this._tone;
|
|
122
|
+
}
|
|
123
|
+
set tone(value) {
|
|
124
|
+
this._tone = Math.max(0, Math.min(1, value));
|
|
125
|
+
if (this._setup) {
|
|
126
|
+
const ctx = this._setup.toneFilter.context;
|
|
127
|
+
this._setup.toneFilter.frequency.setTargetAtTime(DistortionEffect._toneToFrequency(this._tone), ctx.currentTime, 0.01);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
destroy() {
|
|
131
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
132
|
+
if (this._setup) {
|
|
133
|
+
this._setup.inputGain.disconnect();
|
|
134
|
+
this._setup.waveShaper.disconnect();
|
|
135
|
+
this._setup.toneFilter.disconnect();
|
|
136
|
+
this._setup.dryGain.disconnect();
|
|
137
|
+
this._setup.wetGain.disconnect();
|
|
138
|
+
this._setup.outputGain.disconnect();
|
|
139
|
+
this._setup = null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Build a 256-point tanh-based soft-clip curve for the given drive (0..1).
|
|
144
|
+
*
|
|
145
|
+
* Maps drive to a distortion amount `a = drive * 400 + 1`, then fills the
|
|
146
|
+
* curve with `tanh(x * a) / tanh(a)` so that the output is always bounded
|
|
147
|
+
* to [-1, 1]. At drive=0, `a = 1` and the curve is `tanh(x) / tanh(1)`,
|
|
148
|
+
* which has a small-signal gain of `1/tanh(1) ≈ 1.31` — not perfectly
|
|
149
|
+
* linear. At drive=1, `a = 401` and the curve approximates a hard-clipper.
|
|
150
|
+
*/
|
|
151
|
+
static _buildCurve(drive) {
|
|
152
|
+
const n = 256;
|
|
153
|
+
const curve = new Float32Array(n);
|
|
154
|
+
// drive=0 → amount=1 (mild curve: gain ≈ 1/tanh(1) ≈ 1.31 at origin); drive=1 → amount=401 (near hard-clip)
|
|
155
|
+
const amount = drive * 400 + 1;
|
|
156
|
+
const norm = Math.tanh(amount);
|
|
157
|
+
for (let i = 0; i < n; i++) {
|
|
158
|
+
const x = (i * 2) / (n - 1) - 1;
|
|
159
|
+
curve[i] = Math.tanh(x * amount) / norm;
|
|
160
|
+
}
|
|
161
|
+
return curve;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Map tone (0..1) to a low-pass cutoff frequency in Hz.
|
|
165
|
+
* Logarithmic scale: 100 Hz at tone=0, 20 000 Hz at tone=1.
|
|
166
|
+
*/
|
|
167
|
+
static _toneToFrequency(tone) {
|
|
168
|
+
// 100 * 200^tone: 100 Hz at 0, 20 000 Hz at 1
|
|
169
|
+
return 100 * Math.pow(200, tone);
|
|
170
|
+
}
|
|
171
|
+
_setupNodes(ctx) {
|
|
172
|
+
const inputGain = ctx.createGain();
|
|
173
|
+
const outputGain = ctx.createGain();
|
|
174
|
+
const dryGain = ctx.createGain();
|
|
175
|
+
const wetGain = ctx.createGain();
|
|
176
|
+
const waveShaper = ctx.createWaveShaper();
|
|
177
|
+
const toneFilter = ctx.createBiquadFilter();
|
|
178
|
+
inputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
179
|
+
outputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
180
|
+
dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);
|
|
181
|
+
wetGain.gain.setValueAtTime(this._wet, ctx.currentTime);
|
|
182
|
+
waveShaper.curve = DistortionEffect._buildCurve(this._drive);
|
|
183
|
+
waveShaper.oversample = this._oversample;
|
|
184
|
+
toneFilter.type = 'lowpass';
|
|
185
|
+
toneFilter.frequency.setValueAtTime(DistortionEffect._toneToFrequency(this._tone), ctx.currentTime);
|
|
186
|
+
// Dry path: input → dryGain → output
|
|
187
|
+
inputGain.connect(dryGain);
|
|
188
|
+
dryGain.connect(outputGain);
|
|
189
|
+
// Wet path: input → waveShaper → toneFilter → wetGain → output
|
|
190
|
+
inputGain.connect(waveShaper);
|
|
191
|
+
waveShaper.connect(toneFilter);
|
|
192
|
+
toneFilter.connect(wetGain);
|
|
193
|
+
wetGain.connect(outputGain);
|
|
194
|
+
this._setup = { inputGain, outputGain, dryGain, wetGain, waveShaper, toneFilter };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export { DistortionEffect };
|
|
199
|
+
//# sourceMappingURL=DistortionEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DistortionEffect.js","sources":["../../../../src/effects/DistortionEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAuCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BG;AACG,MAAO,gBAAiB,SAAQ,WAAW,CAAA;IACvC,MAAM,GAAiC,IAAI;AAC3C,IAAA,MAAM;AACN,IAAA,IAAI;AAC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AAE,GAAG,CAAC,WAAW,CAAC;AAClD,QAAA,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC;AAC3D,QAAA,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC;QAEvD,UAAU,CAAC,KAAK,GAAG,gBAAgB,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC;AAC5D,QAAA,UAAU,CAAC,UAAU,GAAG,IAAI,CAAC,WAAW;AAExC,QAAA,UAAU,CAAC,IAAI,GAAG,SAAS;AAC3B,QAAA,UAAU,CAAC,SAAS,CAAC,cAAc,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC;;AAGnG,QAAA,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC;AAC1B,QAAA,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC;;AAG3B,QAAA,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC;AAC7B,QAAA,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC;AAC9B,QAAA,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC;AAC3B,QAAA,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC;AAE3B,QAAA,IAAI,CAAC,MAAM,GAAG,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE;IACnF;AACD;;;;"}
|
|
@@ -6,8 +6,12 @@ class DuckingProcessor extends AudioWorkletProcessor {
|
|
|
6
6
|
return [
|
|
7
7
|
{ name: 'threshold', defaultValue: -20, minValue: -100, maxValue: 0, automationRate: 'k-rate' },
|
|
8
8
|
{ name: 'ratio', defaultValue: 4, minValue: 1, maxValue: 20, automationRate: 'k-rate' },
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
// attack/release are one-pole smoothing coefficients (1 - exp(-1/tau)),
|
|
10
|
+
// not times — their value lives in [0, 1]. A 0.001 floor clipped the
|
|
11
|
+
// small coefficients of typical attack/release times (e.g. 30 ms at
|
|
12
|
+
// 48 kHz ≈ 0.0007), so the browser clamped them and logged a warning.
|
|
13
|
+
{ name: 'attack', defaultValue: 0.03, minValue: 0, maxValue: 1, automationRate: 'k-rate' },
|
|
14
|
+
{ name: 'release', defaultValue: 0.3, minValue: 0, maxValue: 1, automationRate: 'k-rate' },
|
|
11
15
|
];
|
|
12
16
|
}
|
|
13
17
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DuckingEffect.js","sources":["../../../../src/effects/DuckingEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,MAAM,oBAAoB,GAAG
|
|
1
|
+
{"version":3,"file":"DuckingEffect.js","sources":["../../../../src/effects/DuckingEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoE5B;AAWD;;;;;;;AAOG;AACG,MAAO,aAAc,SAAQ,aAAa,CAAA;;;IAG7B,UAAU,GAAoB,IAAI;AAC3C,IAAA,UAAU;AACV,IAAA,MAAM;AACN,IAAA,SAAS;AACT,IAAA,UAAU;AAElB,IAAA,WAAA,CAAmB,OAA6B,EAAA;AAC9C,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE;AACtB,YAAA,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC;QACjE;AACA,QAAA,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS;QACnC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,IAAI,GAAG;QAC1C,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC;QAChC,IAAI,CAAC,SAAS,GAAG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AAC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACtG;IACF;;AAGA,IAAA,IAAW,SAAS,GAAA;QAClB,OAAO,IAAI,CAAC,UAAU;IACxB;IACA,IAAW,SAAS,CAAC,KAAa,EAAA;QAChC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC;AACxC,QAAA,IAAI,IAAI,CAAC,YAAY,EAAE;YACrB,IAAI,CAAC,cAAc,CAAC,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACxG;IACF;AACD;;;;"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { AudioEffect } from '@codexo/exojs';
|
|
2
|
+
/** Construction options for {@link FlangerEffect}. */
|
|
3
|
+
export interface FlangerEffectOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Base delay time in milliseconds. The LFO modulates the delay around this
|
|
6
|
+
* centre value. Range 0.5..20, default 3.
|
|
7
|
+
*/
|
|
8
|
+
delayMs?: number;
|
|
9
|
+
/**
|
|
10
|
+
* LFO modulation depth in milliseconds — the peak deviation added to and
|
|
11
|
+
* subtracted from `delayMs` by the sine LFO. Range 0..10, default 2.
|
|
12
|
+
*/
|
|
13
|
+
depthMs?: number;
|
|
14
|
+
/**
|
|
15
|
+
* LFO oscillation rate in Hz. Lower rates produce a slow, sweeping flange;
|
|
16
|
+
* higher rates create a faster jet-plane effect. Range 0..10, default 0.25.
|
|
17
|
+
*/
|
|
18
|
+
rateHz?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Feedback amount — fraction of the delay output fed back to the delay input.
|
|
21
|
+
* Higher values intensify the comb-filter resonance. Range 0..0.95, default 0.5.
|
|
22
|
+
*/
|
|
23
|
+
feedback?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Dry/wet mix, 0..1. The dry level is automatically `1 - wet`.
|
|
26
|
+
* Default 0.5 (equal mix).
|
|
27
|
+
*/
|
|
28
|
+
wet?: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Flanger effect using native WebAudio nodes (no worklet required).
|
|
32
|
+
*
|
|
33
|
+
* A flanger is a very short LFO-modulated delay (1–10 ms) with a feedback
|
|
34
|
+
* loop. Unlike a chorus — which uses longer delays without feedback to produce
|
|
35
|
+
* thickening — the flanger's feedback reinforces phase cancellation, creating
|
|
36
|
+
* the characteristic sweeping comb-filter "jet-plane" sound as notches and
|
|
37
|
+
* peaks slide up and down the spectrum.
|
|
38
|
+
*
|
|
39
|
+
* Node graph:
|
|
40
|
+
* ```
|
|
41
|
+
* inputGain ┬── dryGain ─────────────────────────┐
|
|
42
|
+
* │ ├── outputGain
|
|
43
|
+
* └──▶ delayNode ──▶ wetGain ───────────┘
|
|
44
|
+
* ↑ │
|
|
45
|
+
* feedbackGain ◀─┘
|
|
46
|
+
*
|
|
47
|
+
* lfoOscillator ── lfoGain (depth in seconds) ──▶ delayNode.delayTime
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`;
|
|
51
|
+
* `feedbackGain.gain = feedback`; `lfoGain.gain = depthMs / 1000`.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const flanger = new FlangerEffect({ delayMs: 3, depthMs: 2, rateHz: 0.25, feedback: 0.5, wet: 0.5 });
|
|
56
|
+
* bus.addEffect(flanger);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare class FlangerEffect extends AudioEffect {
|
|
60
|
+
private _nodes;
|
|
61
|
+
private _delayMs;
|
|
62
|
+
private _depthMs;
|
|
63
|
+
private _rateHz;
|
|
64
|
+
private _feedback;
|
|
65
|
+
private _wet;
|
|
66
|
+
private readonly _onAudioContextReady;
|
|
67
|
+
constructor(options?: FlangerEffectOptions);
|
|
68
|
+
/**
|
|
69
|
+
* The node where audio enters this effect. Throws if the audio context is
|
|
70
|
+
* not yet initialized.
|
|
71
|
+
*/
|
|
72
|
+
get inputNode(): AudioNode;
|
|
73
|
+
/**
|
|
74
|
+
* The node where audio exits this effect. Throws if the audio context is
|
|
75
|
+
* not yet initialized.
|
|
76
|
+
*/
|
|
77
|
+
get outputNode(): AudioNode;
|
|
78
|
+
/** Base delay time in milliseconds. Range 0.5..20, default 3. */
|
|
79
|
+
get delayMs(): number;
|
|
80
|
+
set delayMs(value: number);
|
|
81
|
+
/** LFO modulation depth in milliseconds (peak deviation from base delay). Range 0..10, default 2. */
|
|
82
|
+
get depthMs(): number;
|
|
83
|
+
set depthMs(value: number);
|
|
84
|
+
/** LFO oscillation rate in Hz. Range 0..10, default 0.25. */
|
|
85
|
+
get rateHz(): number;
|
|
86
|
+
set rateHz(value: number);
|
|
87
|
+
/**
|
|
88
|
+
* Feedback amount — fraction of delay output fed back to delay input.
|
|
89
|
+
* Range 0..0.95, default 0.5.
|
|
90
|
+
*/
|
|
91
|
+
get feedback(): number;
|
|
92
|
+
set feedback(value: number);
|
|
93
|
+
/** Wet (flanged) mix level, 0..1. The dry level is automatically `1 - wet`. Default 0.5. */
|
|
94
|
+
get wet(): number;
|
|
95
|
+
set wet(value: number);
|
|
96
|
+
destroy(): void;
|
|
97
|
+
private _setupNodes;
|
|
98
|
+
}
|