UNPKG

4 kBJavaScriptView Raw
1import { StereoEffect } from "./StereoEffect.js";
2import { LFO } from "../source/oscillator/LFO.js";
3import { Gain } from "../core/context/Gain.js";
4import { Signal } from "../signal/Signal.js";
5import { optionsFromArguments } from "../core/util/Defaults.js";
6import { readOnly } from "../core/util/Interface.js";
7/**
8 * Tremolo modulates the amplitude of an incoming signal using an {@link LFO}.
9 * The effect is a stereo effect where the modulation phase is inverted in each channel.
10 *
11 * @example
12 * // create a tremolo and start it's LFO
13 * const tremolo = new Tone.Tremolo(9, 0.75).toDestination().start();
14 * // route an oscillator through the tremolo and start it
15 * const oscillator = new Tone.Oscillator().connect(tremolo).start();
16 *
17 * @category Effect
18 */
19export class Tremolo extends StereoEffect {
20 constructor() {
21 const options = optionsFromArguments(Tremolo.getDefaults(), arguments, [
22 "frequency",
23 "depth",
24 ]);
25 super(options);
26 this.name = "Tremolo";
27 this._lfoL = new LFO({
28 context: this.context,
29 type: options.type,
30 min: 1,
31 max: 0,
32 });
33 this._lfoR = new LFO({
34 context: this.context,
35 type: options.type,
36 min: 1,
37 max: 0,
38 });
39 this._amplitudeL = new Gain({ context: this.context });
40 this._amplitudeR = new Gain({ context: this.context });
41 this.frequency = new Signal({
42 context: this.context,
43 value: options.frequency,
44 units: "frequency",
45 });
46 this.depth = new Signal({
47 context: this.context,
48 value: options.depth,
49 units: "normalRange",
50 });
51 readOnly(this, ["frequency", "depth"]);
52 this.connectEffectLeft(this._amplitudeL);
53 this.connectEffectRight(this._amplitudeR);
54 this._lfoL.connect(this._amplitudeL.gain);
55 this._lfoR.connect(this._amplitudeR.gain);
56 this.frequency.fan(this._lfoL.frequency, this._lfoR.frequency);
57 this.depth.fan(this._lfoR.amplitude, this._lfoL.amplitude);
58 this.spread = options.spread;
59 }
60 static getDefaults() {
61 return Object.assign(StereoEffect.getDefaults(), {
62 frequency: 10,
63 type: "sine",
64 depth: 0.5,
65 spread: 180,
66 });
67 }
68 /**
69 * Start the tremolo.
70 */
71 start(time) {
72 this._lfoL.start(time);
73 this._lfoR.start(time);
74 return this;
75 }
76 /**
77 * Stop the tremolo.
78 */
79 stop(time) {
80 this._lfoL.stop(time);
81 this._lfoR.stop(time);
82 return this;
83 }
84 /**
85 * Sync the effect to the transport.
86 */
87 sync() {
88 this._lfoL.sync();
89 this._lfoR.sync();
90 this.context.transport.syncSignal(this.frequency);
91 return this;
92 }
93 /**
94 * Unsync the filter from the transport
95 */
96 unsync() {
97 this._lfoL.unsync();
98 this._lfoR.unsync();
99 this.context.transport.unsyncSignal(this.frequency);
100 return this;
101 }
102 /**
103 * The oscillator type.
104 */
105 get type() {
106 return this._lfoL.type;
107 }
108 set type(type) {
109 this._lfoL.type = type;
110 this._lfoR.type = type;
111 }
112 /**
113 * Amount of stereo spread. When set to 0, both LFO's will be panned centrally.
114 * When set to 180, LFO's will be panned hard left and right respectively.
115 */
116 get spread() {
117 return this._lfoR.phase - this._lfoL.phase; // 180
118 }
119 set spread(spread) {
120 this._lfoL.phase = 90 - spread / 2;
121 this._lfoR.phase = spread / 2 + 90;
122 }
123 dispose() {
124 super.dispose();
125 this._lfoL.dispose();
126 this._lfoR.dispose();
127 this._amplitudeL.dispose();
128 this._amplitudeR.dispose();
129 this.frequency.dispose();
130 this.depth.dispose();
131 return this;
132 }
133}
134//# sourceMappingURL=Tremolo.js.map
\No newline at end of file