ez-web-audio
Version:
Making the Web Audio API super EZ since 2024.
98 lines • 3.44 kB
TypeScript
import { Effect } from './index';
/**
* Minimal interface for external effects that can be wrapped.
* Any effect with a connect() method can be wrapped to provide bypass/mix controls.
*/
export interface ExternalEffect {
/**
* Connect the effect's output to a destination node.
* This is the minimum requirement for wrapping an external effect.
*/
connect(destination: AudioNode): void;
}
/**
* EffectWrapper - Wraps external effects (like Tuna.js, custom WaveShaperNode, etc.)
* to provide a standard Effect interface with bypass and wet/dry mix controls.
*
* External effects only need a connect() method to be wrapped. The wrapper creates
* the necessary infrastructure for wet/dry mixing and bypass functionality.
*
* Routing:
* - Dry path: input -> dryGain -> output
* - Wet path: input -> externalEffect -> wetGain -> output
*
* @example
* ```typescript
* // Wrap a Tuna.js effect
* const tuna = new Tuna(audioContext)
* const chorus = tuna.Chorus({ rate: 1.5 })
* const wrapped = wrapEffect(audioContext, chorus)
*
* // Now use standard Effect interface
* wrapped.bypass = true // Bypass the effect
* wrapped.mix = 0.5 // 50% wet/dry blend
*
* // Access original effect
* wrapped.effect.rate = 2.0
* ```
*/
export declare class EffectWrapper implements Effect {
private readonly _effect;
private readonly inputNode;
private readonly outputNode;
private readonly dryGain;
private readonly wetGain;
private _bypass;
private _mix;
constructor(audioContext: AudioContext, externalEffect: ExternalEffect);
/** The input AudioNode (receives signal from chain) */
get input(): AudioNode;
/** The output AudioNode (sends signal to next in chain) */
get output(): AudioNode;
/**
* When true, signal bypasses the effect entirely (100% dry).
*/
get bypass(): boolean;
set bypass(v: boolean);
/**
* Wet/dry mix: 0 = fully dry (no effect), 1 = fully wet (all through effect).
* Uses equal-power crossfade for natural mixing.
*/
get mix(): number;
set mix(v: number);
/**
* Access the wrapped external effect for configuration.
* This allows direct manipulation of the effect's native properties.
*/
get effect(): ExternalEffect;
/**
* Apply wet/dry mix using equal-power crossfade.
* cos(angle) for dry, sin(angle) for wet where angle = mix * PI/2
*/
private applyMix;
}
/**
* Factory function to wrap an external effect with the Effect interface.
*
* Use this for effects from libraries like Tuna.js, or custom AudioNodes like
* WaveShaperNode that only have a connect() method.
*
* @param audioContext - The AudioContext to use
* @param externalEffect - The external effect object with a connect() method
* @returns A new EffectWrapper instance implementing the Effect interface
*
* @example
* ```typescript
* // Wrap a WaveShaperNode
* const distortion = audioContext.createWaveShaper()
* distortion.curve = makeDistortionCurve(400)
* const wrapped = wrapEffect(audioContext, distortion)
*
* // Wrap a Tuna.js effect
* const tuna = new Tuna(audioContext)
* const delay = tuna.Delay({ delayTime: 300 })
* const wrappedDelay = wrapEffect(audioContext, delay)
* ```
*/
export declare function wrapEffect(audioContext: AudioContext, externalEffect: ExternalEffect): EffectWrapper;
//# sourceMappingURL=effect-wrapper.d.ts.map