1 | import { StereoEffect, StereoEffectOptions } from "./StereoEffect.js";
|
2 | import { LFO } from "../source/oscillator/LFO.js";
|
3 | import { Gain } from "../core/context/Gain.js";
|
4 | import { Signal } from "../signal/Signal.js";
|
5 | import { Degrees, Frequency, NormalRange, Time } from "../core/type/Units.js";
|
6 | import { ToneOscillatorType } from "../source/oscillator/OscillatorInterface.js";
|
7 | import { optionsFromArguments } from "../core/util/Defaults.js";
|
8 | import { readOnly } from "../core/util/Interface.js";
|
9 |
|
10 | export interface TremoloOptions extends StereoEffectOptions {
|
11 | frequency: Frequency;
|
12 | type: ToneOscillatorType;
|
13 | depth: NormalRange;
|
14 | spread: Degrees;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export class Tremolo extends StereoEffect<TremoloOptions> {
|
30 | readonly name: string = "Tremolo";
|
31 |
|
32 | |
33 |
|
34 |
|
35 | private _lfoL: LFO;
|
36 |
|
37 | |
38 |
|
39 |
|
40 | private _lfoR: LFO;
|
41 |
|
42 | |
43 |
|
44 |
|
45 | private _amplitudeL: Gain;
|
46 |
|
47 | |
48 |
|
49 |
|
50 | private _amplitudeR: Gain;
|
51 |
|
52 | |
53 |
|
54 |
|
55 | readonly frequency: Signal<"frequency">;
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | readonly depth: Signal<"normalRange">;
|
63 |
|
64 | |
65 |
|
66 |
|
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 |
|
132 |
|
133 | stop(time?: Time): this {
|
134 | this._lfoL.stop(time);
|
135 | this._lfoR.stop(time);
|
136 | return this;
|
137 | }
|
138 |
|
139 | |
140 |
|
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 |
|
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 |
|
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 |
|
172 |
|
173 |
|
174 | get spread(): Degrees {
|
175 | return this._lfoR.phase - this._lfoL.phase;
|
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 | }
|