1 | import { StereoEffect } 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 { optionsFromArguments } from "../core/util/Defaults.js";
|
6 | import { readOnly } from "../core/util/Interface.js";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export 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 |
|
70 |
|
71 | start(time) {
|
72 | this._lfoL.start(time);
|
73 | this._lfoR.start(time);
|
74 | return this;
|
75 | }
|
76 | |
77 |
|
78 |
|
79 | stop(time) {
|
80 | this._lfoL.stop(time);
|
81 | this._lfoR.stop(time);
|
82 | return this;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | sync() {
|
88 | this._lfoL.sync();
|
89 | this._lfoR.sync();
|
90 | this.context.transport.syncSignal(this.frequency);
|
91 | return this;
|
92 | }
|
93 | |
94 |
|
95 |
|
96 | unsync() {
|
97 | this._lfoL.unsync();
|
98 | this._lfoR.unsync();
|
99 | this.context.transport.unsyncSignal(this.frequency);
|
100 | return this;
|
101 | }
|
102 | |
103 |
|
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 |
|
114 |
|
115 |
|
116 | get spread() {
|
117 | return this._lfoR.phase - this._lfoL.phase;
|
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 |
|
\ | No newline at end of file |