1 | import { Gain } from "../core/context/Gain.js";
|
2 | import {
|
3 | ToneAudioNode,
|
4 | ToneAudioNodeOptions,
|
5 | } from "../core/context/ToneAudioNode.js";
|
6 | import { GainFactor, Seconds, Time } from "../core/type/Units.js";
|
7 | import { noOp } from "../core/util/Interface.js";
|
8 | import { assert } from "../core/util/Debug.js";
|
9 | import { BasicPlaybackState } from "../core/util/StateTimeline.js";
|
10 |
|
11 | export type OneShotSourceCurve = "linear" | "exponential";
|
12 |
|
13 | type onEndedCallback = (source: OneShotSource<any>) => void;
|
14 |
|
15 | export interface OneShotSourceOptions extends ToneAudioNodeOptions {
|
16 | onended: onEndedCallback;
|
17 | fadeIn: Time;
|
18 | fadeOut: Time;
|
19 | curve: OneShotSourceCurve;
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export abstract class OneShotSource<
|
26 | Options extends ToneAudioNodeOptions,
|
27 | > extends ToneAudioNode<Options> {
|
28 | |
29 |
|
30 |
|
31 |
|
32 | onended: onEndedCallback = noOp;
|
33 |
|
34 | |
35 |
|
36 |
|
37 | input: undefined;
|
38 |
|
39 | |
40 |
|
41 |
|
42 | protected _startTime = -1;
|
43 |
|
44 | |
45 |
|
46 |
|
47 | protected _stopTime = -1;
|
48 |
|
49 | |
50 |
|
51 |
|
52 | private _timeout = -1;
|
53 |
|
54 | |
55 |
|
56 |
|
57 | output: Gain = new Gain({
|
58 | context: this.context,
|
59 | gain: 0,
|
60 | });
|
61 |
|
62 | |
63 |
|
64 |
|
65 | protected _gainNode = this.output;
|
66 |
|
67 | |
68 |
|
69 |
|
70 | protected _fadeIn: Time;
|
71 |
|
72 | |
73 |
|
74 |
|
75 | protected _fadeOut: Time;
|
76 |
|
77 | |
78 |
|
79 |
|
80 | protected _curve: OneShotSourceCurve;
|
81 |
|
82 | constructor(options: OneShotSourceOptions) {
|
83 | super(options);
|
84 |
|
85 | this._fadeIn = options.fadeIn;
|
86 | this._fadeOut = options.fadeOut;
|
87 | this._curve = options.curve;
|
88 | this.onended = options.onended;
|
89 | }
|
90 |
|
91 | static getDefaults(): OneShotSourceOptions {
|
92 | return Object.assign(ToneAudioNode.getDefaults(), {
|
93 | curve: "linear" as OneShotSourceCurve,
|
94 | fadeIn: 0,
|
95 | fadeOut: 0,
|
96 | onended: noOp,
|
97 | });
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 | protected abstract _stopSource(time: Seconds): void;
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 | protected abstract start(time?: Time): this;
|
110 | |
111 |
|
112 |
|
113 |
|
114 | protected _startGain(time: Seconds, gain: GainFactor = 1): this {
|
115 | assert(
|
116 | this._startTime === -1,
|
117 | "Source cannot be started more than once"
|
118 | );
|
119 |
|
120 | const fadeInTime = this.toSeconds(this._fadeIn);
|
121 |
|
122 |
|
123 | this._startTime = time + fadeInTime;
|
124 | this._startTime = Math.max(this._startTime, this.context.currentTime);
|
125 |
|
126 |
|
127 | if (fadeInTime > 0) {
|
128 | this._gainNode.gain.setValueAtTime(0, time);
|
129 | if (this._curve === "linear") {
|
130 | this._gainNode.gain.linearRampToValueAtTime(
|
131 | gain,
|
132 | time + fadeInTime
|
133 | );
|
134 | } else {
|
135 | this._gainNode.gain.exponentialApproachValueAtTime(
|
136 | gain,
|
137 | time,
|
138 | fadeInTime
|
139 | );
|
140 | }
|
141 | } else {
|
142 | this._gainNode.gain.setValueAtTime(gain, time);
|
143 | }
|
144 | return this;
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 | stop(time?: Time): this {
|
152 | this.log("stop", time);
|
153 | this._stopGain(this.toSeconds(time));
|
154 | return this;
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 | protected _stopGain(time: Seconds): this {
|
162 | assert(this._startTime !== -1, "'start' must be called before 'stop'");
|
163 |
|
164 | this.cancelStop();
|
165 |
|
166 |
|
167 | const fadeOutTime = this.toSeconds(this._fadeOut);
|
168 |
|
169 |
|
170 | this._stopTime = this.toSeconds(time) + fadeOutTime;
|
171 | this._stopTime = Math.max(this._stopTime, this.now());
|
172 | if (fadeOutTime > 0) {
|
173 |
|
174 | if (this._curve === "linear") {
|
175 | this._gainNode.gain.linearRampTo(0, fadeOutTime, time);
|
176 | } else {
|
177 | this._gainNode.gain.targetRampTo(0, fadeOutTime, time);
|
178 | }
|
179 | } else {
|
180 |
|
181 | this._gainNode.gain.cancelAndHoldAtTime(time);
|
182 | this._gainNode.gain.setValueAtTime(0, time);
|
183 | }
|
184 | this.context.clearTimeout(this._timeout);
|
185 | this._timeout = this.context.setTimeout(() => {
|
186 |
|
187 | const additionalTail =
|
188 | this._curve === "exponential" ? fadeOutTime * 2 : 0;
|
189 | this._stopSource(this.now() + additionalTail);
|
190 | this._onended();
|
191 | }, this._stopTime - this.context.currentTime);
|
192 | return this;
|
193 | }
|
194 |
|
195 | |
196 |
|
197 |
|
198 | protected _onended(): void {
|
199 | if (this.onended !== noOp) {
|
200 | this.onended(this);
|
201 |
|
202 | this.onended = noOp;
|
203 |
|
204 | if (!this.context.isOffline) {
|
205 | const disposeCallback = () => this.dispose();
|
206 |
|
207 | if (typeof window.requestIdleCallback !== "undefined") {
|
208 |
|
209 | window.requestIdleCallback(disposeCallback);
|
210 | } else {
|
211 | setTimeout(disposeCallback, 1000);
|
212 | }
|
213 | }
|
214 | }
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 | getStateAtTime = function (time: Time): BasicPlaybackState {
|
221 | const computedTime = this.toSeconds(time);
|
222 | if (
|
223 | this._startTime !== -1 &&
|
224 | computedTime >= this._startTime &&
|
225 | (this._stopTime === -1 || computedTime <= this._stopTime)
|
226 | ) {
|
227 | return "started";
|
228 | } else {
|
229 | return "stopped";
|
230 | }
|
231 | };
|
232 |
|
233 | |
234 |
|
235 |
|
236 | get state(): BasicPlaybackState {
|
237 | return this.getStateAtTime(this.now());
|
238 | }
|
239 |
|
240 | |
241 |
|
242 |
|
243 | cancelStop(): this {
|
244 | this.log("cancelStop");
|
245 | assert(this._startTime !== -1, "Source is not started");
|
246 |
|
247 | this._gainNode.gain.cancelScheduledValues(
|
248 | this._startTime + this.sampleTime
|
249 | );
|
250 | this.context.clearTimeout(this._timeout);
|
251 | this._stopTime = -1;
|
252 | return this;
|
253 | }
|
254 |
|
255 | dispose(): this {
|
256 | super.dispose();
|
257 | this._gainNode.dispose();
|
258 | this.onended = noOp;
|
259 | return this;
|
260 | }
|
261 | }
|