1 | import { Volume } from "../component/channel/Volume.js";
|
2 | import { Param } from "../core/context/Param.js";
|
3 | import {
|
4 | OutputNode,
|
5 | ToneAudioNode,
|
6 | ToneAudioNodeOptions,
|
7 | } from "../core/context/ToneAudioNode.js";
|
8 | import { Decibels, Frequency, NormalRange, Time } from "../core/type/Units.js";
|
9 | import { optionsFromArguments } from "../core/util/Defaults.js";
|
10 | import { readOnly } from "../core/util/Interface.js";
|
11 |
|
12 | export interface InstrumentOptions extends ToneAudioNodeOptions {
|
13 | volume: Decibels;
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export abstract class Instrument<
|
20 | Options extends InstrumentOptions,
|
21 | > extends ToneAudioNode<Options> {
|
22 | |
23 |
|
24 |
|
25 | private _volume: Volume;
|
26 | output: OutputNode;
|
27 |
|
28 | |
29 |
|
30 |
|
31 | input: undefined;
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | volume: Param<"decibels">;
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 | private _scheduledEvents: number[] = [];
|
47 |
|
48 | |
49 |
|
50 |
|
51 | private _synced = false;
|
52 |
|
53 | constructor(options?: Partial<InstrumentOptions>);
|
54 | constructor() {
|
55 | const options = optionsFromArguments(
|
56 | Instrument.getDefaults(),
|
57 | arguments
|
58 | );
|
59 | super(options);
|
60 |
|
61 | this._volume = this.output = new Volume({
|
62 | context: this.context,
|
63 | volume: options.volume,
|
64 | });
|
65 | this.volume = this._volume.volume;
|
66 | readOnly(this, "volume");
|
67 | }
|
68 |
|
69 | static getDefaults(): InstrumentOptions {
|
70 | return Object.assign(ToneAudioNode.getDefaults(), {
|
71 | volume: 0,
|
72 | });
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | sync(): this {
|
90 | if (this._syncState()) {
|
91 | this._syncMethod("triggerAttack", 1);
|
92 | this._syncMethod("triggerRelease", 0);
|
93 |
|
94 | this.context.transport.on("stop", this._syncedRelease);
|
95 | this.context.transport.on("pause", this._syncedRelease);
|
96 | this.context.transport.on("loopEnd", this._syncedRelease);
|
97 | }
|
98 | return this;
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 | protected _syncState(): boolean {
|
105 | let changed = false;
|
106 | if (!this._synced) {
|
107 | this._synced = true;
|
108 | changed = true;
|
109 | }
|
110 | return changed;
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 | protected _syncMethod(method: string, timePosition: number): void {
|
119 | const originalMethod = (this["_original_" + method] = this[method]);
|
120 | this[method] = (...args: any[]) => {
|
121 | const time = args[timePosition];
|
122 | const id = this.context.transport.schedule((t) => {
|
123 | args[timePosition] = t;
|
124 | originalMethod.apply(this, args);
|
125 | }, time);
|
126 | this._scheduledEvents.push(id);
|
127 | };
|
128 | }
|
129 |
|
130 | |
131 |
|
132 |
|
133 | unsync(): this {
|
134 | this._scheduledEvents.forEach((id) => this.context.transport.clear(id));
|
135 | this._scheduledEvents = [];
|
136 | if (this._synced) {
|
137 | this._synced = false;
|
138 | this.triggerAttack = this._original_triggerAttack;
|
139 | this.triggerRelease = this._original_triggerRelease;
|
140 |
|
141 | this.context.transport.off("stop", this._syncedRelease);
|
142 | this.context.transport.off("pause", this._syncedRelease);
|
143 | this.context.transport.off("loopEnd", this._syncedRelease);
|
144 | }
|
145 | return this;
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | triggerAttackRelease(
|
161 | note: Frequency,
|
162 | duration: Time,
|
163 | time?: Time,
|
164 | velocity?: NormalRange
|
165 | ): this {
|
166 | const computedTime = this.toSeconds(time);
|
167 | const computedDuration = this.toSeconds(duration);
|
168 | this.triggerAttack(note, computedTime, velocity);
|
169 | this.triggerRelease(computedTime + computedDuration);
|
170 | return this;
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | abstract triggerAttack(
|
180 | note: Frequency,
|
181 | time?: Time,
|
182 | velocity?: NormalRange
|
183 | ): this;
|
184 | private _original_triggerAttack = this.triggerAttack;
|
185 |
|
186 | |
187 |
|
188 |
|
189 |
|
190 | abstract triggerRelease(...args: any[]): this;
|
191 | private _original_triggerRelease = this.triggerRelease;
|
192 |
|
193 | |
194 |
|
195 |
|
196 | protected _syncedRelease = (time: number) =>
|
197 | this._original_triggerRelease(time);
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 | dispose(): this {
|
204 | super.dispose();
|
205 | this._volume.dispose();
|
206 | this.unsync();
|
207 | this._scheduledEvents = [];
|
208 | return this;
|
209 | }
|
210 | }
|