1 | import { TicksClass } from "../core/type/Ticks.js";
|
2 | import { TransportTimeClass } from "../core/type/TransportTime.js";
|
3 | import { defaultArg, optionsFromArguments } from "../core/util/Defaults.js";
|
4 | import { StateTimeline } from "../core/util/StateTimeline.js";
|
5 | import { isArray, isDefined, isObject, isUndef, } from "../core/util/TypeCheck.js";
|
6 | import { ToneEvent } from "./ToneEvent.js";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export class Part extends ToneEvent {
|
31 | constructor() {
|
32 | const options = optionsFromArguments(Part.getDefaults(), arguments, [
|
33 | "callback",
|
34 | "events",
|
35 | ]);
|
36 | super(options);
|
37 | this.name = "Part";
|
38 | |
39 |
|
40 |
|
41 | this._state = new StateTimeline("stopped");
|
42 | |
43 |
|
44 |
|
45 | this._events = new Set();
|
46 |
|
47 | this._state.increasing = true;
|
48 |
|
49 | options.events.forEach((event) => {
|
50 | if (isArray(event)) {
|
51 | this.add(event[0], event[1]);
|
52 | }
|
53 | else {
|
54 | this.add(event);
|
55 | }
|
56 | });
|
57 | }
|
58 | static getDefaults() {
|
59 | return Object.assign(ToneEvent.getDefaults(), {
|
60 | events: [],
|
61 | });
|
62 | }
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | start(time, offset) {
|
69 | const ticks = this.toTicks(time);
|
70 | if (this._state.getValueAtTime(ticks) !== "started") {
|
71 | offset = defaultArg(offset, this._loop ? this._loopStart : 0);
|
72 | if (this._loop) {
|
73 | offset = defaultArg(offset, this._loopStart);
|
74 | }
|
75 | else {
|
76 | offset = defaultArg(offset, 0);
|
77 | }
|
78 | const computedOffset = this.toTicks(offset);
|
79 | this._state.add({
|
80 | id: -1,
|
81 | offset: computedOffset,
|
82 | state: "started",
|
83 | time: ticks,
|
84 | });
|
85 | this._forEach((event) => {
|
86 | this._startNote(event, ticks, computedOffset);
|
87 | });
|
88 | }
|
89 | return this;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | _startNote(event, ticks, offset) {
|
99 | ticks -= offset;
|
100 | if (this._loop) {
|
101 | if (event.startOffset >= this._loopStart &&
|
102 | event.startOffset < this._loopEnd) {
|
103 | if (event.startOffset < offset) {
|
104 |
|
105 | ticks += this._getLoopDuration();
|
106 | }
|
107 | event.start(new TicksClass(this.context, ticks));
|
108 | }
|
109 | else if (event.startOffset < this._loopStart &&
|
110 | event.startOffset >= offset) {
|
111 | event.loop = false;
|
112 | event.start(new TicksClass(this.context, ticks));
|
113 | }
|
114 | }
|
115 | else if (event.startOffset >= offset) {
|
116 | event.start(new TicksClass(this.context, ticks));
|
117 | }
|
118 | }
|
119 | get startOffset() {
|
120 | return this._startOffset;
|
121 | }
|
122 | set startOffset(offset) {
|
123 | this._startOffset = offset;
|
124 | this._forEach((event) => {
|
125 | event.startOffset += this._startOffset;
|
126 | });
|
127 | }
|
128 | |
129 |
|
130 |
|
131 |
|
132 | stop(time) {
|
133 | const ticks = this.toTicks(time);
|
134 | this._state.cancel(ticks);
|
135 | this._state.setStateAtTime("stopped", ticks);
|
136 | this._forEach((event) => {
|
137 | event.stop(time);
|
138 | });
|
139 | return this;
|
140 | }
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | at(time, value) {
|
156 | const timeInTicks = new TransportTimeClass(this.context, time).toTicks();
|
157 | const tickTime = new TicksClass(this.context, 1).toSeconds();
|
158 | const iterator = this._events.values();
|
159 | let result = iterator.next();
|
160 | while (!result.done) {
|
161 | const event = result.value;
|
162 | if (Math.abs(timeInTicks - event.startOffset) < tickTime) {
|
163 | if (isDefined(value)) {
|
164 | event.value = value;
|
165 | }
|
166 | return event;
|
167 | }
|
168 | result = iterator.next();
|
169 | }
|
170 |
|
171 | if (isDefined(value)) {
|
172 | this.add(time, value);
|
173 |
|
174 | return this.at(time);
|
175 | }
|
176 | else {
|
177 | return null;
|
178 | }
|
179 | }
|
180 | add(time, value) {
|
181 |
|
182 | if (time instanceof Object && Reflect.has(time, "time")) {
|
183 | value = time;
|
184 | time = value.time;
|
185 | }
|
186 | const ticks = this.toTicks(time);
|
187 | let event;
|
188 | if (value instanceof ToneEvent) {
|
189 | event = value;
|
190 | event.callback = this._tick.bind(this);
|
191 | }
|
192 | else {
|
193 | event = new ToneEvent({
|
194 | callback: this._tick.bind(this),
|
195 | context: this.context,
|
196 | value,
|
197 | });
|
198 | }
|
199 |
|
200 | event.startOffset = ticks;
|
201 |
|
202 | event.set({
|
203 | humanize: this.humanize,
|
204 | loop: this.loop,
|
205 | loopEnd: this.loopEnd,
|
206 | loopStart: this.loopStart,
|
207 | playbackRate: this.playbackRate,
|
208 | probability: this.probability,
|
209 | });
|
210 | this._events.add(event);
|
211 |
|
212 | this._restartEvent(event);
|
213 | return this;
|
214 | }
|
215 | |
216 |
|
217 |
|
218 | _restartEvent(event) {
|
219 | this._state.forEach((stateEvent) => {
|
220 | if (stateEvent.state === "started") {
|
221 | this._startNote(event, stateEvent.time, stateEvent.offset);
|
222 | }
|
223 | else {
|
224 |
|
225 | event.stop(new TicksClass(this.context, stateEvent.time));
|
226 | }
|
227 | });
|
228 | }
|
229 | remove(time, value) {
|
230 |
|
231 | if (isObject(time) && time.hasOwnProperty("time")) {
|
232 | value = time;
|
233 | time = value.time;
|
234 | }
|
235 | time = this.toTicks(time);
|
236 | this._events.forEach((event) => {
|
237 | if (event.startOffset === time) {
|
238 | if (isUndef(value) ||
|
239 | (isDefined(value) && event.value === value)) {
|
240 | this._events.delete(event);
|
241 | event.dispose();
|
242 | }
|
243 | }
|
244 | });
|
245 | return this;
|
246 | }
|
247 | |
248 |
|
249 |
|
250 | clear() {
|
251 | this._forEach((event) => event.dispose());
|
252 | this._events.clear();
|
253 | return this;
|
254 | }
|
255 | |
256 |
|
257 |
|
258 |
|
259 | cancel(after) {
|
260 | this._forEach((event) => event.cancel(after));
|
261 | this._state.cancel(this.toTicks(after));
|
262 | return this;
|
263 | }
|
264 | |
265 |
|
266 |
|
267 | _forEach(callback) {
|
268 | if (this._events) {
|
269 | this._events.forEach((event) => {
|
270 | if (event instanceof Part) {
|
271 | event._forEach(callback);
|
272 | }
|
273 | else {
|
274 | callback(event);
|
275 | }
|
276 | });
|
277 | }
|
278 | return this;
|
279 | }
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 | _setAll(attr, value) {
|
286 | this._forEach((event) => {
|
287 | event[attr] = value;
|
288 | });
|
289 | }
|
290 | |
291 |
|
292 |
|
293 |
|
294 | _tick(time, value) {
|
295 | if (!this.mute) {
|
296 | this.callback(time, value);
|
297 | }
|
298 | }
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 | _testLoopBoundries(event) {
|
305 | if (this._loop &&
|
306 | (event.startOffset < this._loopStart ||
|
307 | event.startOffset >= this._loopEnd)) {
|
308 | event.cancel(0);
|
309 | }
|
310 | else if (event.state === "stopped") {
|
311 |
|
312 | this._restartEvent(event);
|
313 | }
|
314 | }
|
315 | get probability() {
|
316 | return this._probability;
|
317 | }
|
318 | set probability(prob) {
|
319 | this._probability = prob;
|
320 | this._setAll("probability", prob);
|
321 | }
|
322 | get humanize() {
|
323 | return this._humanize;
|
324 | }
|
325 | set humanize(variation) {
|
326 | this._humanize = variation;
|
327 | this._setAll("humanize", variation);
|
328 | }
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | get loop() {
|
344 | return this._loop;
|
345 | }
|
346 | set loop(loop) {
|
347 | this._loop = loop;
|
348 | this._forEach((event) => {
|
349 | event.loopStart = this.loopStart;
|
350 | event.loopEnd = this.loopEnd;
|
351 | event.loop = loop;
|
352 | this._testLoopBoundries(event);
|
353 | });
|
354 | }
|
355 | |
356 |
|
357 |
|
358 |
|
359 | get loopEnd() {
|
360 | return new TicksClass(this.context, this._loopEnd).toSeconds();
|
361 | }
|
362 | set loopEnd(loopEnd) {
|
363 | this._loopEnd = this.toTicks(loopEnd);
|
364 | if (this._loop) {
|
365 | this._forEach((event) => {
|
366 | event.loopEnd = loopEnd;
|
367 | this._testLoopBoundries(event);
|
368 | });
|
369 | }
|
370 | }
|
371 | |
372 |
|
373 |
|
374 |
|
375 | get loopStart() {
|
376 | return new TicksClass(this.context, this._loopStart).toSeconds();
|
377 | }
|
378 | set loopStart(loopStart) {
|
379 | this._loopStart = this.toTicks(loopStart);
|
380 | if (this._loop) {
|
381 | this._forEach((event) => {
|
382 | event.loopStart = this.loopStart;
|
383 | this._testLoopBoundries(event);
|
384 | });
|
385 | }
|
386 | }
|
387 | |
388 |
|
389 |
|
390 | get playbackRate() {
|
391 | return this._playbackRate;
|
392 | }
|
393 | set playbackRate(rate) {
|
394 | this._playbackRate = rate;
|
395 | this._setAll("playbackRate", rate);
|
396 | }
|
397 | |
398 |
|
399 |
|
400 | get length() {
|
401 | return this._events.size;
|
402 | }
|
403 | dispose() {
|
404 | super.dispose();
|
405 | this.clear();
|
406 | return this;
|
407 | }
|
408 | }
|
409 |
|
\ | No newline at end of file |