/
Clock.ts
317 lines (281 loc) · 8.92 KB
/
Clock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { ToneWithContext, ToneWithContextOptions } from "../context/ToneWithContext";
import { Frequency, Hertz, Seconds, Ticks, Time } from "../type/Units";
import { optionsFromArguments } from "../util/Defaults";
import { Emitter } from "../util/Emitter";
import { noOp, readOnly } from "../util/Interface";
import { PlaybackState, StateTimeline } from "../util/StateTimeline";
import { TickSignal } from "./TickSignal";
import { TickSource } from "./TickSource";
import { assertContextRunning } from "../util/Debug";
type ClockCallback = (time: Seconds, ticks?: Ticks) => void;
interface ClockOptions extends ToneWithContextOptions {
frequency: Hertz;
callback: ClockCallback;
units: "hertz" | "bpm";
}
type ClockEvent = "start" | "stop" | "pause";
/**
* A sample accurate clock which provides a callback at the given rate.
* While the callback is not sample-accurate (it is still susceptible to
* loose JS timing), the time passed in as the argument to the callback
* is precise. For most applications, it is better to use Tone.Transport
* instead of the Clock by itself since you can synchronize multiple callbacks.
* @example
* // the callback will be invoked approximately once a second
* // and will print the time exactly once a second apart.
* const clock = new Tone.Clock(time => {
* console.log(time);
* }, 1);
* clock.start();
* @category Core
*/
export class Clock<TypeName extends "bpm" | "hertz" = "hertz">
extends ToneWithContext<ClockOptions> implements Emitter<ClockEvent> {
readonly name: string = "Clock";
/**
* The callback function to invoke at the scheduled tick.
*/
callback: ClockCallback = noOp;
/**
* The tick counter
*/
private _tickSource: TickSource<TypeName>;
/**
* The last time the loop callback was invoked
*/
private _lastUpdate = 0;
/**
* Keep track of the playback state
*/
private _state: StateTimeline = new StateTimeline("stopped");
/**
* Context bound reference to the _loop method
* This is necessary to remove the event in the end.
*/
private _boundLoop: () => void = this._loop.bind(this);
/**
* The rate the callback function should be invoked.
*/
frequency: TickSignal<TypeName>;
/**
* @param callback The callback to be invoked with the time of the audio event
* @param frequency The rate of the callback
*/
constructor(callback?: ClockCallback, frequency?: Frequency);
constructor(options: Partial<ClockOptions>);
constructor() {
super(optionsFromArguments(Clock.getDefaults(), arguments, ["callback", "frequency"]));
const options = optionsFromArguments(Clock.getDefaults(), arguments, ["callback", "frequency"]);
this.callback = options.callback;
this._tickSource = new TickSource({
context: this.context,
frequency: options.frequency,
units: options.units,
});
this._lastUpdate = 0;
this.frequency = this._tickSource.frequency;
readOnly(this, "frequency");
// add an initial state
this._state.setStateAtTime("stopped", 0);
// bind a callback to the worker thread
this.context.on("tick", this._boundLoop);
}
static getDefaults(): ClockOptions {
return Object.assign(ToneWithContext.getDefaults(), {
callback: noOp as ClockCallback,
frequency: 1,
units: "hertz",
}) as ClockOptions;
}
/**
* Returns the playback state of the source, either "started", "stopped" or "paused".
*/
get state(): PlaybackState {
return this._state.getValueAtTime(this.now());
}
/**
* Start the clock at the given time. Optionally pass in an offset
* of where to start the tick counter from.
* @param time The time the clock should start
* @param offset Where the tick counter starts counting from.
*/
start(time?: Time, offset?: Ticks): this {
// make sure the context is running
assertContextRunning(this.context);
// start the loop
const computedTime = this.toSeconds(time);
this.log("start", computedTime);
if (this._state.getValueAtTime(computedTime) !== "started") {
this._state.setStateAtTime("started", computedTime);
this._tickSource.start(computedTime, offset);
if (computedTime < this._lastUpdate) {
this.emit("start", computedTime, offset);
}
}
return this;
}
/**
* Stop the clock. Stopping the clock resets the tick counter to 0.
* @param time The time when the clock should stop.
* @example
* const clock = new Tone.Clock(time => {
* console.log(time);
* }, 1);
* clock.start();
* // stop the clock after 10 seconds
* clock.stop("+10");
*/
stop(time?: Time): this {
const computedTime = this.toSeconds(time);
this.log("stop", computedTime);
this._state.cancel(computedTime);
this._state.setStateAtTime("stopped", computedTime);
this._tickSource.stop(computedTime);
if (computedTime < this._lastUpdate) {
this.emit("stop", computedTime);
}
return this;
}
/**
* Pause the clock. Pausing does not reset the tick counter.
* @param time The time when the clock should stop.
*/
pause(time?: Time): this {
const computedTime = this.toSeconds(time);
if (this._state.getValueAtTime(computedTime) === "started") {
this._state.setStateAtTime("paused", computedTime);
this._tickSource.pause(computedTime);
if (computedTime < this._lastUpdate) {
this.emit("pause", computedTime);
}
}
return this;
}
/**
* The number of times the callback was invoked. Starts counting at 0
* and increments after the callback was invoked.
*/
get ticks(): Ticks {
return Math.ceil(this.getTicksAtTime(this.now()));
}
set ticks(t: Ticks) {
this._tickSource.ticks = t;
}
/**
* The time since ticks=0 that the Clock has been running. Accounts for tempo curves
*/
get seconds(): Seconds {
return this._tickSource.seconds;
}
set seconds(s: Seconds) {
this._tickSource.seconds = s;
}
/**
* Return the elapsed seconds at the given time.
* @param time When to get the elapsed seconds
* @return The number of elapsed seconds
*/
getSecondsAtTime(time: Time): Seconds {
return this._tickSource.getSecondsAtTime(time);
}
/**
* Set the clock's ticks at the given time.
* @param ticks The tick value to set
* @param time When to set the tick value
*/
setTicksAtTime(ticks: Ticks, time: Time): this {
this._tickSource.setTicksAtTime(ticks, time);
return this;
}
/**
* Get the time of the given tick. The second argument
* is when to test before. Since ticks can be set (with setTicksAtTime)
* there may be multiple times for a given tick value.
* @param tick The tick number.
* @param before When to measure the tick value from.
* @return The time of the tick
*/
getTimeOfTick(tick: Ticks, before = this.now()): Seconds {
return this._tickSource.getTimeOfTick(tick, before);
}
/**
* Get the clock's ticks at the given time.
* @param time When to get the tick value
* @return The tick value at the given time.
*/
getTicksAtTime(time?: Time): Ticks {
return this._tickSource.getTicksAtTime(time);
}
/**
* Get the time of the next tick
* @param offset The tick number.
*/
nextTickTime(offset: Ticks, when: Time): Seconds {
const computedTime = this.toSeconds(when);
const currentTick = this.getTicksAtTime(computedTime);
return this._tickSource.getTimeOfTick(currentTick + offset, computedTime);
}
/**
* The scheduling loop.
*/
private _loop(): void {
const startTime = this._lastUpdate;
const endTime = this.now();
this._lastUpdate = endTime;
this.log("loop", startTime, endTime);
if (startTime !== endTime) {
// the state change events
this._state.forEachBetween(startTime, endTime, e => {
switch (e.state) {
case "started":
const offset = this._tickSource.getTicksAtTime(e.time);
this.emit("start", e.time, offset);
break;
case "stopped":
if (e.time !== 0) {
this.emit("stop", e.time);
}
break;
case "paused":
this.emit("pause", e.time);
break;
}
});
// the tick callbacks
this._tickSource.forEachTickBetween(startTime, endTime, (time, ticks) => {
this.callback(time, ticks);
});
}
}
/**
* Returns the scheduled state at the given time.
* @param time The time to query.
* @return The name of the state input in setStateAtTime.
* @example
* const clock = new Tone.Clock();
* clock.start("+0.1");
* clock.getStateAtTime("+0.1"); // returns "started"
*/
getStateAtTime(time: Time): PlaybackState {
const computedTime = this.toSeconds(time);
return this._state.getValueAtTime(computedTime);
}
/**
* Clean up
*/
dispose(): this {
super.dispose();
this.context.off("tick", this._boundLoop);
this._tickSource.dispose();
this._state.dispose();
return this;
}
//-------------------------------------
// EMITTER MIXIN TO SATISFY COMPILER
//-------------------------------------
on!: (event: ClockEvent, callback: (...args: any[]) => void) => this;
once!: (event: ClockEvent, callback: (...args: any[]) => void) => this;
off!: (event: ClockEvent, callback?: ((...args: any[]) => void) | undefined) => this;
emit!: (event: any, ...args: any[]) => this;
}
Emitter.mixin(Clock);