UNPKG

4.61 kBPlain TextView Raw
1import { StereoEffect, StereoEffectOptions } from "./StereoEffect.js";
2import { LFO } from "../source/oscillator/LFO.js";
3import { Gain } from "../core/context/Gain.js";
4import { Signal } from "../signal/Signal.js";
5import { Degrees, Frequency, NormalRange, Time } from "../core/type/Units.js";
6import { ToneOscillatorType } from "../source/oscillator/OscillatorInterface.js";
7import { optionsFromArguments } from "../core/util/Defaults.js";
8import { readOnly } from "../core/util/Interface.js";
9
10export interface TremoloOptions extends StereoEffectOptions {
11 frequency: Frequency;
12 type: ToneOscillatorType;
13 depth: NormalRange;
14 spread: Degrees;
15}
16
17/**
18 * Tremolo modulates the amplitude of an incoming signal using an {@link LFO}.
19 * The effect is a stereo effect where the modulation phase is inverted in each channel.
20 *
21 * @example
22 * // create a tremolo and start it's LFO
23 * const tremolo = new Tone.Tremolo(9, 0.75).toDestination().start();
24 * // route an oscillator through the tremolo and start it
25 * const oscillator = new Tone.Oscillator().connect(tremolo).start();
26 *
27 * @category Effect
28 */
29export class Tremolo extends StereoEffect<TremoloOptions> {
30 readonly name: string = "Tremolo";
31
32 /**
33 * The tremolo LFO in the left channel
34 */
35 private _lfoL: LFO;
36
37 /**
38 * The tremolo LFO in the left channel
39 */
40 private _lfoR: LFO;
41
42 /**
43 * Where the gain is multiplied
44 */
45 private _amplitudeL: Gain;
46
47 /**
48 * Where the gain is multiplied
49 */
50 private _amplitudeR: Gain;
51
52 /**
53 * The frequency of the tremolo.
54 */
55 readonly frequency: Signal<"frequency">;
56
57 /**
58 * The depth of the effect. A depth of 0, has no effect
59 * on the amplitude, and a depth of 1 makes the amplitude
60 * modulate fully between 0 and 1.
61 */
62 readonly depth: Signal<"normalRange">;
63
64 /**
65 * @param frequency The rate of the effect.
66 * @param depth The depth of the effect.
67 */
68 constructor(frequency?: Frequency, depth?: NormalRange);
69 constructor(options?: Partial<TremoloOptions>);
70 constructor() {
71 const options = optionsFromArguments(Tremolo.getDefaults(), arguments, [
72 "frequency",
73 "depth",
74 ]);
75 super(options);
76
77 this._lfoL = new LFO({
78 context: this.context,
79 type: options.type,
80 min: 1,
81 max: 0,
82 });
83 this._lfoR = new LFO({
84 context: this.context,
85 type: options.type,
86 min: 1,
87 max: 0,
88 });
89 this._amplitudeL = new Gain({ context: this.context });
90 this._amplitudeR = new Gain({ context: this.context });
91 this.frequency = new Signal({
92 context: this.context,
93 value: options.frequency,
94 units: "frequency",
95 });
96 this.depth = new Signal({
97 context: this.context,
98 value: options.depth,
99 units: "normalRange",
100 });
101
102 readOnly(this, ["frequency", "depth"]);
103 this.connectEffectLeft(this._amplitudeL);
104 this.connectEffectRight(this._amplitudeR);
105 this._lfoL.connect(this._amplitudeL.gain);
106 this._lfoR.connect(this._amplitudeR.gain);
107 this.frequency.fan(this._lfoL.frequency, this._lfoR.frequency);
108 this.depth.fan(this._lfoR.amplitude, this._lfoL.amplitude);
109 this.spread = options.spread;
110 }
111
112 static getDefaults(): TremoloOptions {
113 return Object.assign(StereoEffect.getDefaults(), {
114 frequency: 10,
115 type: "sine" as const,
116 depth: 0.5,
117 spread: 180,
118 });
119 }
120
121 /**
122 * Start the tremolo.
123 */
124 start(time?: Time): this {
125 this._lfoL.start(time);
126 this._lfoR.start(time);
127 return this;
128 }
129
130 /**
131 * Stop the tremolo.
132 */
133 stop(time?: Time): this {
134 this._lfoL.stop(time);
135 this._lfoR.stop(time);
136 return this;
137 }
138
139 /**
140 * Sync the effect to the transport.
141 */
142 sync(): this {
143 this._lfoL.sync();
144 this._lfoR.sync();
145 this.context.transport.syncSignal(this.frequency);
146 return this;
147 }
148
149 /**
150 * Unsync the filter from the transport
151 */
152 unsync(): this {
153 this._lfoL.unsync();
154 this._lfoR.unsync();
155 this.context.transport.unsyncSignal(this.frequency);
156 return this;
157 }
158
159 /**
160 * The oscillator type.
161 */
162 get type(): ToneOscillatorType {
163 return this._lfoL.type;
164 }
165 set type(type) {
166 this._lfoL.type = type;
167 this._lfoR.type = type;
168 }
169
170 /**
171 * Amount of stereo spread. When set to 0, both LFO's will be panned centrally.
172 * When set to 180, LFO's will be panned hard left and right respectively.
173 */
174 get spread(): Degrees {
175 return this._lfoR.phase - this._lfoL.phase; // 180
176 }
177 set spread(spread) {
178 this._lfoL.phase = 90 - spread / 2;
179 this._lfoR.phase = spread / 2 + 90;
180 }
181
182 dispose(): this {
183 super.dispose();
184 this._lfoL.dispose();
185 this._lfoR.dispose();
186 this._amplitudeL.dispose();
187 this._amplitudeR.dispose();
188 this.frequency.dispose();
189 this.depth.dispose();
190 return this;
191 }
192}