@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
|
@@ -5,21 +5,25 @@ export interface PitchShiftEffectOptions {
|
|
|
5
5
|
/** Dry/wet mix, 0..1. Default 1.0 (full wet). */
|
|
6
6
|
wet?: number;
|
|
7
7
|
/**
|
|
8
|
-
* Internal
|
|
9
|
-
* Larger = more
|
|
8
|
+
* Internal SOLA frame size in samples. Default 1024 (~21ms at 48kHz).
|
|
9
|
+
* Larger = more latency but better low-frequency alignment; smaller = lower
|
|
10
|
+
* latency but coarser correlation. The correlation search radius scales with it.
|
|
10
11
|
*/
|
|
11
12
|
grainSize?: number;
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
14
|
-
* Real-time pitch shifter
|
|
15
|
+
* Real-time pitch shifter (WorkletEffect) using SOLA — synchronized
|
|
16
|
+
* overlap-add. Each synthesis grain is realigned by waveform cross-correlation,
|
|
17
|
+
* so grain restarts stay phase coherent and the output pitch tracks the `pitch`
|
|
18
|
+
* ratio exactly across the whole 0.25x–4.0x range (a naive granular delay drifts
|
|
19
|
+
* the pitch instead).
|
|
15
20
|
*
|
|
16
|
-
* Quality:
|
|
17
|
-
*
|
|
18
|
-
*
|
|
21
|
+
* Quality: clean, in-tune shifting for tonal material. As with any time-domain
|
|
22
|
+
* method, sharp transients can smear or double slightly; for those a phase
|
|
23
|
+
* vocoder would do better at the cost of much more CPU and latency.
|
|
19
24
|
*
|
|
20
|
-
* Latency: ~
|
|
21
|
-
* (at 48kHz
|
|
22
|
-
* games / playback.
|
|
25
|
+
* Latency: ~one frame + search ≈ 30ms at the default 1024-sample grain
|
|
26
|
+
* (at 48kHz). Not suitable for live monitoring; fine for games / playback.
|
|
23
27
|
*
|
|
24
28
|
* Use cases:
|
|
25
29
|
* - Sound variation: random ±200 cent pitch on each footstep / bullet
|
|
@@ -28,17 +32,14 @@ export interface PitchShiftEffectOptions {
|
|
|
28
32
|
*/
|
|
29
33
|
export declare class PitchShiftEffect extends WorkletEffect {
|
|
30
34
|
private _pitch;
|
|
31
|
-
private _wet;
|
|
32
35
|
private readonly _grainSize;
|
|
33
36
|
constructor(options?: PitchShiftEffectOptions);
|
|
34
37
|
protected get _workletName(): string;
|
|
35
38
|
protected get _workletSource(): string;
|
|
39
|
+
protected get _dryLatencySeconds(): number;
|
|
36
40
|
protected get _workletOptions(): AudioWorkletNodeOptions;
|
|
37
41
|
protected _onWorkletReady(): void;
|
|
38
42
|
/** Pitch ratio relative to the original. 1.0 = no change, 0.5 = one octave down, 2.0 = one octave up. Range 0.25..4.0, default 1.0. */
|
|
39
43
|
get pitch(): number;
|
|
40
44
|
set pitch(value: number);
|
|
41
|
-
/** Wet (pitch-shifted) mix level, 0..1. Default 1.0 (full wet). */
|
|
42
|
-
get wet(): number;
|
|
43
|
-
set wet(value: number);
|
|
44
45
|
}
|
|
@@ -2,15 +2,18 @@ import { WorkletEffect } from '@codexo/exojs';
|
|
|
2
2
|
import { pitchShiftWorkletSource } from '../worklets/pitch-shift.worklet.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Real-time pitch shifter
|
|
5
|
+
* Real-time pitch shifter (WorkletEffect) using SOLA — synchronized
|
|
6
|
+
* overlap-add. Each synthesis grain is realigned by waveform cross-correlation,
|
|
7
|
+
* so grain restarts stay phase coherent and the output pitch tracks the `pitch`
|
|
8
|
+
* ratio exactly across the whole 0.25x–4.0x range (a naive granular delay drifts
|
|
9
|
+
* the pitch instead).
|
|
6
10
|
*
|
|
7
|
-
* Quality:
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* Quality: clean, in-tune shifting for tonal material. As with any time-domain
|
|
12
|
+
* method, sharp transients can smear or double slightly; for those a phase
|
|
13
|
+
* vocoder would do better at the cost of much more CPU and latency.
|
|
10
14
|
*
|
|
11
|
-
* Latency: ~
|
|
12
|
-
* (at 48kHz
|
|
13
|
-
* games / playback.
|
|
15
|
+
* Latency: ~one frame + search ≈ 30ms at the default 1024-sample grain
|
|
16
|
+
* (at 48kHz). Not suitable for live monitoring; fine for games / playback.
|
|
14
17
|
*
|
|
15
18
|
* Use cases:
|
|
16
19
|
* - Sound variation: random ±200 cent pitch on each footstep / bullet
|
|
@@ -19,13 +22,12 @@ import { pitchShiftWorkletSource } from '../worklets/pitch-shift.worklet.js';
|
|
|
19
22
|
*/
|
|
20
23
|
class PitchShiftEffect extends WorkletEffect {
|
|
21
24
|
_pitch;
|
|
22
|
-
_wet;
|
|
23
25
|
_grainSize;
|
|
24
26
|
constructor(options = {}) {
|
|
25
27
|
super();
|
|
26
28
|
this._pitch = Math.max(0.25, Math.min(4, options.pitch ?? 1));
|
|
27
|
-
this._wet = Math.max(0, Math.min(1, options.wet ?? 1));
|
|
28
29
|
this._grainSize = options.grainSize ?? 1024;
|
|
30
|
+
this.wet = options.wet ?? 1;
|
|
29
31
|
}
|
|
30
32
|
get _workletName() {
|
|
31
33
|
return 'exojs-pitch-shift';
|
|
@@ -33,6 +35,11 @@ class PitchShiftEffect extends WorkletEffect {
|
|
|
33
35
|
get _workletSource() {
|
|
34
36
|
return pitchShiftWorkletSource;
|
|
35
37
|
}
|
|
38
|
+
get _dryLatencySeconds() {
|
|
39
|
+
// SOLA algorithmic latency ≈ one analysis frame + the correlation search
|
|
40
|
+
// radius (grainSize/4). Tuned against the wet=0.5 comb-filter test below.
|
|
41
|
+
return (this._grainSize + (this._grainSize >> 2)) / this._sampleRate;
|
|
42
|
+
}
|
|
36
43
|
get _workletOptions() {
|
|
37
44
|
return {
|
|
38
45
|
numberOfInputs: 1,
|
|
@@ -42,7 +49,6 @@ class PitchShiftEffect extends WorkletEffect {
|
|
|
42
49
|
}
|
|
43
50
|
_onWorkletReady() {
|
|
44
51
|
this._setAudioParam('pitch', this._pitch);
|
|
45
|
-
this._setAudioParam('wet', this._wet);
|
|
46
52
|
}
|
|
47
53
|
/** Pitch ratio relative to the original. 1.0 = no change, 0.5 = one octave down, 2.0 = one octave up. Range 0.25..4.0, default 1.0. */
|
|
48
54
|
get pitch() {
|
|
@@ -52,14 +58,6 @@ class PitchShiftEffect extends WorkletEffect {
|
|
|
52
58
|
this._pitch = Math.max(0.25, Math.min(4, value));
|
|
53
59
|
this._setAudioParam('pitch', this._pitch);
|
|
54
60
|
}
|
|
55
|
-
/** Wet (pitch-shifted) mix level, 0..1. Default 1.0 (full wet). */
|
|
56
|
-
get wet() {
|
|
57
|
-
return this._wet;
|
|
58
|
-
}
|
|
59
|
-
set wet(value) {
|
|
60
|
-
this._wet = Math.max(0, Math.min(1, value));
|
|
61
|
-
this._setAudioParam('wet', this._wet);
|
|
62
|
-
}
|
|
63
61
|
}
|
|
64
62
|
|
|
65
63
|
export { PitchShiftEffect };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PitchShiftEffect.js","sources":["../../../../src/effects/PitchShiftEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"PitchShiftEffect.js","sources":["../../../../src/effects/PitchShiftEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAiBA;;;;;;;;;;;;;;;;;;AAkBG;AACG,MAAO,gBAAiB,SAAQ,aAAa,CAAA;AACzC,IAAA,MAAM;AACG,IAAA,UAAU;AAE3B,IAAA,WAAA,CAAmB,UAAmC,EAAE,EAAA;AACtD,QAAA,KAAK,EAAE;QACP,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC;QAC7D,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI;QAC3C,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC;IAC7B;AAEA,IAAA,IAAc,YAAY,GAAA;AACxB,QAAA,OAAO,mBAAmB;IAC5B;AAEA,IAAA,IAAc,cAAc,GAAA;AAC1B,QAAA,OAAO,uBAAuB;IAChC;AAEA,IAAA,IAAuB,kBAAkB,GAAA;;;AAGvC,QAAA,OAAO,CAAC,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,WAAW;IACtE;AAEA,IAAA,IAAuB,eAAe,GAAA;QACpC,OAAO;AACL,YAAA,cAAc,EAAE,CAAC;AACjB,YAAA,eAAe,EAAE,CAAC;AAClB,YAAA,gBAAgB,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,UAAU,EAAE;SACjD;IACH;IAEmB,eAAe,GAAA;QAChC,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC;IAC3C;;AAGA,IAAA,IAAW,KAAK,GAAA;QACd,OAAO,IAAI,CAAC,MAAM;IACpB;IACA,IAAW,KAAK,CAAC,KAAa,EAAA;AAC5B,QAAA,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;QAChD,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC;IAC3C;AACD;;;;"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { AudioEffect } from '@codexo/exojs';
|
|
2
|
+
/** Construction options for {@link RingModulatorEffect}. */
|
|
3
|
+
export interface RingModulatorEffectOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Carrier oscillator frequency in Hz. Range 0..20000, default 440. Lower
|
|
6
|
+
* frequencies (1–60 Hz) produce tremolo-like pulsing; mid-range values
|
|
7
|
+
* (100–1000 Hz) give the characteristic robotic ring-mod sound; high values
|
|
8
|
+
* shift sidebands above the audible fundamental.
|
|
9
|
+
*/
|
|
10
|
+
frequency?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Carrier oscillator waveform shape. Default `'sine'`. Any
|
|
13
|
+
* `OscillatorType` is accepted (`'sine'`, `'square'`, `'sawtooth'`,
|
|
14
|
+
* `'triangle'`). Non-sine shapes introduce additional harmonic content into
|
|
15
|
+
* both sidebands.
|
|
16
|
+
*/
|
|
17
|
+
waveform?: OscillatorType;
|
|
18
|
+
/**
|
|
19
|
+
* Dry/wet mix, 0..1. The dry level is automatically `1 - wet`. Default 1.0
|
|
20
|
+
* (fully wet / ring-modulated). Set to 0.5 for an equal mix of dry and
|
|
21
|
+
* ring-modulated signal.
|
|
22
|
+
*/
|
|
23
|
+
wet?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Ring modulator effect using native Web Audio nodes (no worklet required).
|
|
27
|
+
*
|
|
28
|
+
* A carrier `OscillatorNode` is connected directly to the `gain` AudioParam of
|
|
29
|
+
* a `GainNode` whose base gain value is 0. The carrier's bipolar output (±1)
|
|
30
|
+
* modulates the gain, multiplying the input signal by the instantaneous carrier
|
|
31
|
+
* amplitude — the mathematical definition of ring modulation. The result
|
|
32
|
+
* contains the sum and difference frequencies of the input and carrier but
|
|
33
|
+
* suppresses the original carrier (unlike amplitude modulation, which retains
|
|
34
|
+
* the carrier). At sub-audio carrier frequencies (< 20 Hz) the effect becomes
|
|
35
|
+
* a tremolo.
|
|
36
|
+
*
|
|
37
|
+
* Node graph:
|
|
38
|
+
* ```
|
|
39
|
+
* input(GainNode) ┬── dryGain ──────────────────────────┐
|
|
40
|
+
* │ ├── output(GainNode)
|
|
41
|
+
* └── ringGain ── wetGain ────────────────┘
|
|
42
|
+
*
|
|
43
|
+
* carrierOsc ── ringGain.gain (AudioParam, base = 0)
|
|
44
|
+
* ```
|
|
45
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`;
|
|
46
|
+
* `ringGain.gain` base = 0 (carrier provides the ±1 modulation).
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const ringMod = new RingModulatorEffect({ frequency: 220, wet: 0.8 });
|
|
51
|
+
* bus.addEffect(ringMod);
|
|
52
|
+
*
|
|
53
|
+
* // Change carrier frequency at runtime:
|
|
54
|
+
* ringMod.frequency = 440;
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare class RingModulatorEffect extends AudioEffect {
|
|
58
|
+
private _setup;
|
|
59
|
+
private _frequency;
|
|
60
|
+
private _waveform;
|
|
61
|
+
private _wet;
|
|
62
|
+
private readonly _onAudioContextReady;
|
|
63
|
+
constructor(options?: RingModulatorEffectOptions);
|
|
64
|
+
/**
|
|
65
|
+
* The node where audio enters this effect. Throws if the audio context is
|
|
66
|
+
* not yet initialized.
|
|
67
|
+
*/
|
|
68
|
+
get inputNode(): AudioNode;
|
|
69
|
+
/**
|
|
70
|
+
* The node where audio exits this effect. Throws if the audio context is
|
|
71
|
+
* not yet initialized.
|
|
72
|
+
*/
|
|
73
|
+
get outputNode(): AudioNode;
|
|
74
|
+
/**
|
|
75
|
+
* Carrier oscillator frequency in Hz. Range 0..20000, default 440.
|
|
76
|
+
* Changes are ramped smoothly via `setTargetAtTime`.
|
|
77
|
+
*/
|
|
78
|
+
get frequency(): number;
|
|
79
|
+
set frequency(value: number);
|
|
80
|
+
/**
|
|
81
|
+
* Carrier oscillator waveform shape. Default `'sine'`. Changes take effect
|
|
82
|
+
* immediately (no smoothing; waveform transitions are instantaneous in Web
|
|
83
|
+
* Audio).
|
|
84
|
+
*/
|
|
85
|
+
get waveform(): OscillatorType;
|
|
86
|
+
set waveform(value: OscillatorType);
|
|
87
|
+
/**
|
|
88
|
+
* Wet (ring-modulated) mix level, 0..1. The dry level is automatically
|
|
89
|
+
* `1 - wet`. Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.
|
|
90
|
+
*/
|
|
91
|
+
get wet(): number;
|
|
92
|
+
set wet(value: number);
|
|
93
|
+
destroy(): void;
|
|
94
|
+
private _setupNodes;
|
|
95
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { AudioEffect, onAudioContextReady, isAudioContextReady, getAudioContext } from '@codexo/exojs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Ring modulator effect using native Web Audio nodes (no worklet required).
|
|
5
|
+
*
|
|
6
|
+
* A carrier `OscillatorNode` is connected directly to the `gain` AudioParam of
|
|
7
|
+
* a `GainNode` whose base gain value is 0. The carrier's bipolar output (±1)
|
|
8
|
+
* modulates the gain, multiplying the input signal by the instantaneous carrier
|
|
9
|
+
* amplitude — the mathematical definition of ring modulation. The result
|
|
10
|
+
* contains the sum and difference frequencies of the input and carrier but
|
|
11
|
+
* suppresses the original carrier (unlike amplitude modulation, which retains
|
|
12
|
+
* the carrier). At sub-audio carrier frequencies (< 20 Hz) the effect becomes
|
|
13
|
+
* a tremolo.
|
|
14
|
+
*
|
|
15
|
+
* Node graph:
|
|
16
|
+
* ```
|
|
17
|
+
* input(GainNode) ┬── dryGain ──────────────────────────┐
|
|
18
|
+
* │ ├── output(GainNode)
|
|
19
|
+
* └── ringGain ── wetGain ────────────────┘
|
|
20
|
+
*
|
|
21
|
+
* carrierOsc ── ringGain.gain (AudioParam, base = 0)
|
|
22
|
+
* ```
|
|
23
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`;
|
|
24
|
+
* `ringGain.gain` base = 0 (carrier provides the ±1 modulation).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const ringMod = new RingModulatorEffect({ frequency: 220, wet: 0.8 });
|
|
29
|
+
* bus.addEffect(ringMod);
|
|
30
|
+
*
|
|
31
|
+
* // Change carrier frequency at runtime:
|
|
32
|
+
* ringMod.frequency = 440;
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
class RingModulatorEffect extends AudioEffect {
|
|
36
|
+
_setup = null;
|
|
37
|
+
_frequency;
|
|
38
|
+
_waveform;
|
|
39
|
+
_wet;
|
|
40
|
+
_onAudioContextReady = (ctx) => {
|
|
41
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
42
|
+
this._setupNodes(ctx);
|
|
43
|
+
};
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
super();
|
|
46
|
+
this._frequency = Math.max(0, Math.min(20000, options.frequency ?? 440));
|
|
47
|
+
this._waveform = options.waveform ?? 'sine';
|
|
48
|
+
this._wet = Math.max(0, Math.min(1, options.wet ?? 1));
|
|
49
|
+
if (isAudioContextReady()) {
|
|
50
|
+
this._setupNodes(getAudioContext());
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
onAudioContextReady.add(this._onAudioContextReady);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The node where audio enters this effect. Throws if the audio context is
|
|
58
|
+
* not yet initialized.
|
|
59
|
+
*/
|
|
60
|
+
get inputNode() {
|
|
61
|
+
if (!this._setup)
|
|
62
|
+
throw new Error('RingModulatorEffect not yet initialized.');
|
|
63
|
+
return this._setup.inputGain;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The node where audio exits this effect. Throws if the audio context is
|
|
67
|
+
* not yet initialized.
|
|
68
|
+
*/
|
|
69
|
+
get outputNode() {
|
|
70
|
+
if (!this._setup)
|
|
71
|
+
throw new Error('RingModulatorEffect not yet initialized.');
|
|
72
|
+
return this._setup.outputGain;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Carrier oscillator frequency in Hz. Range 0..20000, default 440.
|
|
76
|
+
* Changes are ramped smoothly via `setTargetAtTime`.
|
|
77
|
+
*/
|
|
78
|
+
get frequency() {
|
|
79
|
+
return this._frequency;
|
|
80
|
+
}
|
|
81
|
+
set frequency(value) {
|
|
82
|
+
this._frequency = Math.max(0, Math.min(20000, value));
|
|
83
|
+
if (this._setup) {
|
|
84
|
+
const ctx = this._setup.inputGain.context;
|
|
85
|
+
this._setup.carrierOsc.frequency.setTargetAtTime(this._frequency, ctx.currentTime, 0.01);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Carrier oscillator waveform shape. Default `'sine'`. Changes take effect
|
|
90
|
+
* immediately (no smoothing; waveform transitions are instantaneous in Web
|
|
91
|
+
* Audio).
|
|
92
|
+
*/
|
|
93
|
+
get waveform() {
|
|
94
|
+
return this._waveform;
|
|
95
|
+
}
|
|
96
|
+
set waveform(value) {
|
|
97
|
+
this._waveform = value;
|
|
98
|
+
if (this._setup) {
|
|
99
|
+
this._setup.carrierOsc.type = this._waveform;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Wet (ring-modulated) mix level, 0..1. The dry level is automatically
|
|
104
|
+
* `1 - wet`. Changes are ramped smoothly via `setTargetAtTime`. Default 1.0.
|
|
105
|
+
*/
|
|
106
|
+
get wet() {
|
|
107
|
+
return this._wet;
|
|
108
|
+
}
|
|
109
|
+
set wet(value) {
|
|
110
|
+
this._wet = Math.max(0, Math.min(1, value));
|
|
111
|
+
if (this._setup) {
|
|
112
|
+
const ctx = this._setup.wetGain.context;
|
|
113
|
+
this._setup.wetGain.gain.setTargetAtTime(this._wet, ctx.currentTime, 0.01);
|
|
114
|
+
this._setup.dryGain.gain.setTargetAtTime(1 - this._wet, ctx.currentTime, 0.01);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
destroy() {
|
|
118
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
119
|
+
if (this._setup) {
|
|
120
|
+
this._setup.carrierOsc.stop();
|
|
121
|
+
this._setup.carrierOsc.disconnect();
|
|
122
|
+
this._setup.ringGain.disconnect();
|
|
123
|
+
this._setup.dryGain.disconnect();
|
|
124
|
+
this._setup.wetGain.disconnect();
|
|
125
|
+
this._setup.inputGain.disconnect();
|
|
126
|
+
this._setup.outputGain.disconnect();
|
|
127
|
+
this._setup = null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
_setupNodes(ctx) {
|
|
131
|
+
const inputGain = ctx.createGain();
|
|
132
|
+
const outputGain = ctx.createGain();
|
|
133
|
+
const dryGain = ctx.createGain();
|
|
134
|
+
const wetGain = ctx.createGain();
|
|
135
|
+
const ringGain = ctx.createGain();
|
|
136
|
+
inputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
137
|
+
outputGain.gain.setValueAtTime(1, ctx.currentTime);
|
|
138
|
+
dryGain.gain.setValueAtTime(1 - this._wet, ctx.currentTime);
|
|
139
|
+
wetGain.gain.setValueAtTime(this._wet, ctx.currentTime);
|
|
140
|
+
// Base gain is 0; the carrier oscillator provides the ±1 modulation signal
|
|
141
|
+
// via AudioParam connection — the effective gain becomes exactly the carrier
|
|
142
|
+
// waveform, multiplying the input: output = input × carrier(t).
|
|
143
|
+
ringGain.gain.setValueAtTime(0, ctx.currentTime);
|
|
144
|
+
const carrierOsc = ctx.createOscillator();
|
|
145
|
+
carrierOsc.type = this._waveform;
|
|
146
|
+
carrierOsc.frequency.value = this._frequency;
|
|
147
|
+
// Connect carrier to ringGain's gain AudioParam. The carrier (±1) is added
|
|
148
|
+
// to the base value (0), producing an effective gain in [-1, +1] — true
|
|
149
|
+
// bipolar ring modulation. The `as unknown as AudioNode` cast works around
|
|
150
|
+
// a TypeScript overload-resolution quirk (same pattern as ChorusEffect).
|
|
151
|
+
carrierOsc.connect(ringGain.gain);
|
|
152
|
+
// Dry path: inputGain → dryGain → outputGain
|
|
153
|
+
inputGain.connect(dryGain);
|
|
154
|
+
dryGain.connect(outputGain);
|
|
155
|
+
// Wet (ring-modulated) path: inputGain → ringGain → wetGain → outputGain
|
|
156
|
+
inputGain.connect(ringGain);
|
|
157
|
+
ringGain.connect(wetGain);
|
|
158
|
+
wetGain.connect(outputGain);
|
|
159
|
+
carrierOsc.start();
|
|
160
|
+
this._setup = { inputGain, outputGain, dryGain, wetGain, ringGain, carrierOsc };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export { RingModulatorEffect };
|
|
165
|
+
//# sourceMappingURL=RingModulatorEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RingModulatorEffect.js","sources":["../../../../src/effects/RingModulatorEffect.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BG;AACG,MAAO,mBAAoB,SAAQ,WAAW,CAAA;IAC1C,MAAM,GAAoC,IAAI;AAC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,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;;;;QAIvD,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC;AAEhD,QAAA,MAAM,UAAU,GAAG,GAAG,CAAC,gBAAgB,EAAE;AACzC,QAAA,UAAU,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS;QAChC,UAAU,CAAC,SAAS,CAAC,KAAK,GAAG,IAAI,CAAC,UAAU;;;;;AAM5C,QAAA,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,IAA4B,CAAC;;AAGzD,QAAA,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC;AAC1B,QAAA,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC;;AAG3B,QAAA,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC;AAC3B,QAAA,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC;AACzB,QAAA,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC;QAE3B,UAAU,CAAC,KAAK,EAAE;AAElB,QAAA,IAAI,CAAC,MAAM,GAAG,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE;IACjF;AACD;;;;"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { AudioEffect } from '@codexo/exojs';
|
|
2
|
+
/** Construction options for {@link TremoloEffect}. */
|
|
3
|
+
export interface TremoloEffectOptions {
|
|
4
|
+
/**
|
|
5
|
+
* LFO oscillation rate in Hz controlling the speed of the tremolo pulse.
|
|
6
|
+
* Range 0..20, default 5.
|
|
7
|
+
*/
|
|
8
|
+
rateHz?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Modulation depth, 0..1. Determines the amplitude of the LFO that modulates
|
|
11
|
+
* `tremoloGain.gain`. The base gain is `1 - depth`; adding the LFO (peak = `depth`)
|
|
12
|
+
* makes the gain oscillate between `1 - 2 * depth` and `1`. At depth=0 the effect
|
|
13
|
+
* is transparent; at depth=0.5 the gain briefly reaches zero at the trough; at
|
|
14
|
+
* depth>0.5 the gain goes negative (phase inversion at the trough). Default 0.7.
|
|
15
|
+
*/
|
|
16
|
+
depth?: number;
|
|
17
|
+
/**
|
|
18
|
+
* When `true` the same LFO also drives a `StereoPannerNode.pan` (amplitude = `depth`),
|
|
19
|
+
* sweeping the signal left and right in synchrony with the tremolo pulsing. This option
|
|
20
|
+
* is read-only after construction. Default `false`.
|
|
21
|
+
*/
|
|
22
|
+
autoPan?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Dry/wet mix, 0..1. The dry level is automatically `1 - wet`. Default 1.
|
|
25
|
+
*/
|
|
26
|
+
wet?: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Tremolo effect that amplitude-modulates the signal with a sine LFO using native WebAudio nodes.
|
|
30
|
+
*
|
|
31
|
+
* A sine-wave LFO drives a `GainNode`'s gain to produce the characteristic volume pulsing of
|
|
32
|
+
* tremolo. The base gain is `1 - depth` and the LFO peak deviation is `depth`, so the gain
|
|
33
|
+
* oscillates between `1 - 2 * depth` and `1`. Optionally the same LFO can drive a
|
|
34
|
+
* `StereoPannerNode.pan` for a synchronized auto-pan effect.
|
|
35
|
+
*
|
|
36
|
+
* Node graph (`autoPan = false`):
|
|
37
|
+
* ```
|
|
38
|
+
* inputGain ┬── dryGain ──────────────────────────┐
|
|
39
|
+
* │ ├── outputGain
|
|
40
|
+
* └── tremoloGain ── wetGain ─────────────┘
|
|
41
|
+
*
|
|
42
|
+
* lfoOscillator ── lfoGain (depth) ── tremoloGain.gain
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* Node graph (`autoPan = true`):
|
|
46
|
+
* ```
|
|
47
|
+
* inputGain ┬── dryGain ──────────────────────────────────┐
|
|
48
|
+
* │ ├── outputGain
|
|
49
|
+
* └── tremoloGain ── panner ── wetGain ──────────┘
|
|
50
|
+
*
|
|
51
|
+
* lfoOscillator ── lfoGain (depth) ── tremoloGain.gain
|
|
52
|
+
* lfoOscillator ── panGain (depth) ── panner.pan
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* Gains: `dryGain.gain = 1 - wet`; `wetGain.gain = wet`; `tremoloGain.gain` base = `1 - depth`.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* // Classic tremolo at 5 Hz, 70 % depth, full wet
|
|
60
|
+
* const tremolo = new TremoloEffect({ rateHz: 5, depth: 0.7, wet: 1 });
|
|
61
|
+
* bus.addEffect(tremolo);
|
|
62
|
+
*
|
|
63
|
+
* // Slow auto-pan at 0.5 Hz
|
|
64
|
+
* const autopan = new TremoloEffect({ rateHz: 0.5, depth: 0.5, autoPan: true });
|
|
65
|
+
* bus.addEffect(autopan);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare class TremoloEffect extends AudioEffect {
|
|
69
|
+
private _setup;
|
|
70
|
+
private _rateHz;
|
|
71
|
+
private _depth;
|
|
72
|
+
private readonly _autoPan;
|
|
73
|
+
private _wet;
|
|
74
|
+
private readonly _onAudioContextReady;
|
|
75
|
+
constructor(options?: TremoloEffectOptions);
|
|
76
|
+
/**
|
|
77
|
+
* The node where audio enters this effect. Throws if the audio context is not yet initialized.
|
|
78
|
+
*/
|
|
79
|
+
get inputNode(): AudioNode;
|
|
80
|
+
/**
|
|
81
|
+
* The node where audio exits this effect. Throws if the audio context is not yet initialized.
|
|
82
|
+
*/
|
|
83
|
+
get outputNode(): AudioNode;
|
|
84
|
+
/**
|
|
85
|
+
* LFO oscillation rate in Hz. Range 0..20, default 5.
|
|
86
|
+
* Ramped via `setTargetAtTime` when changed at runtime.
|
|
87
|
+
*/
|
|
88
|
+
get rateHz(): number;
|
|
89
|
+
set rateHz(value: number);
|
|
90
|
+
/**
|
|
91
|
+
* Modulation depth, 0..1. At 0 the effect is transparent; at 0.5 the gain briefly
|
|
92
|
+
* reaches zero at the trough; at depth>0.5 the gain goes negative (phase inversion).
|
|
93
|
+
* Default 0.7. Ramped via `setTargetAtTime`.
|
|
94
|
+
*/
|
|
95
|
+
get depth(): number;
|
|
96
|
+
set depth(value: number);
|
|
97
|
+
/**
|
|
98
|
+
* Whether auto-pan is enabled. Set at construction time via {@link TremoloEffectOptions.autoPan};
|
|
99
|
+
* read-only at runtime. Default `false`.
|
|
100
|
+
*/
|
|
101
|
+
get autoPan(): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Wet (tremolo) mix level, 0..1. The dry level is automatically `1 - wet`. Default 1.
|
|
104
|
+
* Ramped via `setTargetAtTime` when changed at runtime.
|
|
105
|
+
*/
|
|
106
|
+
get wet(): number;
|
|
107
|
+
set wet(value: number);
|
|
108
|
+
destroy(): void;
|
|
109
|
+
private _setupNodes;
|
|
110
|
+
}
|