mirror of
https://github.com/Tonejs/Tone.js
synced 2024-12-30 21:48:45 +00:00
f17249691d
now in the form Tone.Something instead of using `import { Something } from "tone"`. It makes the example runner on the docs page work much faster
115 lines
2.4 KiB
TypeScript
115 lines
2.4 KiB
TypeScript
import { Effect, EffectOptions } from "../effect/Effect";
|
|
import { Frequency, NormalRange, Time } from "../core/type/Units";
|
|
import { LFO } from "../source/oscillator/LFO";
|
|
import { ToneOscillatorType } from "../source/oscillator/OscillatorInterface";
|
|
import { Signal } from "../signal/Signal";
|
|
import { readOnly } from "../core/util/Interface";
|
|
import { Param } from "../core/context/Param";
|
|
|
|
export interface LFOEffectOptions extends EffectOptions {
|
|
frequency: Frequency;
|
|
type: ToneOscillatorType;
|
|
depth: NormalRange;
|
|
}
|
|
|
|
/**
|
|
* Base class for LFO-based effects.
|
|
*/
|
|
export abstract class LFOEffect<Options extends LFOEffectOptions> extends Effect<Options> {
|
|
|
|
readonly name: string = "LFOEffect";
|
|
|
|
/**
|
|
* the lfo which drives the filter cutoff
|
|
*/
|
|
protected _lfo: LFO;
|
|
|
|
/**
|
|
* The range of the filter modulating between the min and max frequency.
|
|
* 0 = no modulation. 1 = full modulation.
|
|
*/
|
|
readonly depth: Param<"normalRange">;
|
|
|
|
/**
|
|
* How fast the filter modulates between min and max.
|
|
*/
|
|
readonly frequency: Signal<"frequency">;
|
|
|
|
constructor(options: LFOEffectOptions) {
|
|
|
|
super(options);
|
|
|
|
this._lfo = new LFO({
|
|
context: this.context,
|
|
frequency: options.frequency,
|
|
amplitude: options.depth,
|
|
});
|
|
this.depth = this._lfo.amplitude;
|
|
this.frequency = this._lfo.frequency;
|
|
|
|
this.type = options.type;
|
|
readOnly(this, ["frequency", "depth"]);
|
|
}
|
|
|
|
static getDefaults(): LFOEffectOptions {
|
|
return Object.assign(Effect.getDefaults(), {
|
|
frequency: 1,
|
|
type: "sine" as ToneOscillatorType,
|
|
depth: 1,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Start the effect.
|
|
*/
|
|
start(time?: Time): this {
|
|
this._lfo.start(time);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Stop the lfo
|
|
*/
|
|
stop(time?: Time): this {
|
|
this._lfo.stop(time);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Sync the filter to the transport. See [[LFO.sync]]
|
|
*/
|
|
sync(): this {
|
|
this._lfo.sync();
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Unsync the filter from the transport.
|
|
*/
|
|
unsync(): this {
|
|
this._lfo.unsync();
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* The type of the LFO's oscillator: See [[Oscillator.type]]
|
|
* @example
|
|
* const autoFilter = new Tone.AutoFilter().start().toDestination();
|
|
* const noise = new Tone.Noise().start().connect(autoFilter);
|
|
* autoFilter.type = "square";
|
|
*/
|
|
get type() {
|
|
return this._lfo.type;
|
|
}
|
|
set type(type) {
|
|
this._lfo.type = type;
|
|
}
|
|
|
|
dispose(): this {
|
|
super.dispose();
|
|
this._lfo.dispose();
|
|
this.frequency.dispose();
|
|
this.depth.dispose();
|
|
return this;
|
|
}
|
|
}
|