UNPKG

4.57 kBJavaScriptView Raw
1import { ToneEvent } from "./ToneEvent.js";
2import { ToneWithContext, } from "../core/context/ToneWithContext.js";
3import { optionsFromArguments } from "../core/util/Defaults.js";
4import { noOp } from "../core/util/Interface.js";
5/**
6 * Loop creates a looped callback at the
7 * specified interval. The callback can be
8 * started, stopped and scheduled along
9 * the Transport's timeline.
10 * @example
11 * const loop = new Tone.Loop((time) => {
12 * // triggered every eighth note.
13 * console.log(time);
14 * }, "8n").start(0);
15 * Tone.Transport.start();
16 * @category Event
17 */
18export class Loop extends ToneWithContext {
19 constructor() {
20 const options = optionsFromArguments(Loop.getDefaults(), arguments, [
21 "callback",
22 "interval",
23 ]);
24 super(options);
25 this.name = "Loop";
26 this._event = new ToneEvent({
27 context: this.context,
28 callback: this._tick.bind(this),
29 loop: true,
30 loopEnd: options.interval,
31 playbackRate: options.playbackRate,
32 probability: options.probability,
33 humanize: options.humanize,
34 });
35 this.callback = options.callback;
36 // set the iterations
37 this.iterations = options.iterations;
38 }
39 static getDefaults() {
40 return Object.assign(ToneWithContext.getDefaults(), {
41 interval: "4n",
42 callback: noOp,
43 playbackRate: 1,
44 iterations: Infinity,
45 probability: 1,
46 mute: false,
47 humanize: false,
48 });
49 }
50 /**
51 * Start the loop at the specified time along the Transport's timeline.
52 * @param time When to start the Loop.
53 */
54 start(time) {
55 this._event.start(time);
56 return this;
57 }
58 /**
59 * Stop the loop at the given time.
60 * @param time When to stop the Loop.
61 */
62 stop(time) {
63 this._event.stop(time);
64 return this;
65 }
66 /**
67 * Cancel all scheduled events greater than or equal to the given time
68 * @param time The time after which events will be cancel.
69 */
70 cancel(time) {
71 this._event.cancel(time);
72 return this;
73 }
74 /**
75 * Internal function called when the notes should be called
76 * @param time The time the event occurs
77 */
78 _tick(time) {
79 this.callback(time);
80 }
81 /**
82 * The state of the Loop, either started or stopped.
83 */
84 get state() {
85 return this._event.state;
86 }
87 /**
88 * The progress of the loop as a value between 0-1. 0, when the loop is stopped or done iterating.
89 */
90 get progress() {
91 return this._event.progress;
92 }
93 /**
94 * The time between successive callbacks.
95 * @example
96 * const loop = new Tone.Loop();
97 * loop.interval = "8n"; // loop every 8n
98 */
99 get interval() {
100 return this._event.loopEnd;
101 }
102 set interval(interval) {
103 this._event.loopEnd = interval;
104 }
105 /**
106 * The playback rate of the loop. The normal playback rate is 1 (no change).
107 * A `playbackRate` of 2 would be twice as fast.
108 */
109 get playbackRate() {
110 return this._event.playbackRate;
111 }
112 set playbackRate(rate) {
113 this._event.playbackRate = rate;
114 }
115 /**
116 * Random variation +/-0.01s to the scheduled time.
117 * Or give it a time value which it will randomize by.
118 */
119 get humanize() {
120 return this._event.humanize;
121 }
122 set humanize(variation) {
123 this._event.humanize = variation;
124 }
125 /**
126 * The probably of the callback being invoked.
127 */
128 get probability() {
129 return this._event.probability;
130 }
131 set probability(prob) {
132 this._event.probability = prob;
133 }
134 /**
135 * Muting the Loop means that no callbacks are invoked.
136 */
137 get mute() {
138 return this._event.mute;
139 }
140 set mute(mute) {
141 this._event.mute = mute;
142 }
143 /**
144 * The number of iterations of the loop. The default value is `Infinity` (loop forever).
145 */
146 get iterations() {
147 if (this._event.loop === true) {
148 return Infinity;
149 }
150 else {
151 return this._event.loop;
152 }
153 }
154 set iterations(iters) {
155 if (iters === Infinity) {
156 this._event.loop = true;
157 }
158 else {
159 this._event.loop = iters;
160 }
161 }
162 dispose() {
163 super.dispose();
164 this._event.dispose();
165 return this;
166 }
167}
168//# sourceMappingURL=Loop.js.map
\No newline at end of file