1 | import { CrossFade } from "../component/channel/CrossFade.js";
|
2 | import { Gain } from "../core/context/Gain.js";
|
3 | import {
|
4 | ToneAudioNode,
|
5 | ToneAudioNodeOptions,
|
6 | } from "../core/context/ToneAudioNode.js";
|
7 | import { NormalRange } from "../core/type/Units.js";
|
8 | import { readOnly } from "../core/util/Interface.js";
|
9 | import { Signal } from "../signal/Signal.js";
|
10 |
|
11 | export interface EffectOptions extends ToneAudioNodeOptions {
|
12 | wet: NormalRange;
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export abstract class Effect<
|
20 | Options extends EffectOptions,
|
21 | > extends ToneAudioNode<Options> {
|
22 | readonly name: string = "Effect";
|
23 |
|
24 | |
25 |
|
26 |
|
27 | private _dryWet: CrossFade = new CrossFade({ context: this.context });
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | wet: Signal<"normalRange"> = this._dryWet.fade;
|
35 |
|
36 | |
37 |
|
38 |
|
39 | protected effectSend: Gain = new Gain({ context: this.context });
|
40 |
|
41 | |
42 |
|
43 |
|
44 | protected effectReturn: Gain = new Gain({ context: this.context });
|
45 |
|
46 | |
47 |
|
48 |
|
49 | input: Gain = new Gain({ context: this.context });
|
50 |
|
51 | |
52 |
|
53 |
|
54 | output = this._dryWet;
|
55 |
|
56 | constructor(options: EffectOptions) {
|
57 | super(options);
|
58 |
|
59 |
|
60 | this.input.fan(this._dryWet.a, this.effectSend);
|
61 | this.effectReturn.connect(this._dryWet.b);
|
62 | this.wet.setValueAtTime(options.wet, 0);
|
63 | this._internalChannels = [this.effectReturn, this.effectSend];
|
64 | readOnly(this, "wet");
|
65 | }
|
66 |
|
67 | static getDefaults(): EffectOptions {
|
68 | return Object.assign(ToneAudioNode.getDefaults(), {
|
69 | wet: 1,
|
70 | });
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 | protected connectEffect(effect: ToneAudioNode | AudioNode): this {
|
77 |
|
78 | this._internalChannels.push(effect);
|
79 | this.effectSend.chain(effect, this.effectReturn);
|
80 | return this;
|
81 | }
|
82 |
|
83 | dispose(): this {
|
84 | super.dispose();
|
85 | this._dryWet.dispose();
|
86 | this.effectSend.dispose();
|
87 | this.effectReturn.dispose();
|
88 | this.wet.dispose();
|
89 | return this;
|
90 | }
|
91 | }
|