Tone.js/Tone/core/clock/Clock.ts

318 lines
8.9 KiB
TypeScript
Raw Normal View History

2019-05-22 03:37:03 +00:00
import { ToneWithContext, ToneWithContextOptions } from "../context/ToneWithContext";
2019-10-31 19:43:16 +00:00
import { Frequency, Hertz, Seconds, Ticks, Time } from "../type/Units";
2019-05-22 03:37:03 +00:00
import { optionsFromArguments } from "../util/Defaults";
2019-09-07 21:57:45 +00:00
import { Emitter } from "../util/Emitter";
2019-05-22 03:37:03 +00:00
import { noOp, readOnly } from "../util/Interface";
import { PlaybackState, StateTimeline } from "../util/StateTimeline";
import { TickSignal } from "./TickSignal";
import { TickSource } from "./TickSource";
2019-11-12 21:55:30 +00:00
import { assertContextRunning } from "../util/Debug";
2019-05-22 03:37:03 +00:00
2020-04-08 01:52:07 +00:00
type ClockCallback = (time: Seconds, ticks?: Ticks) => void;
2019-05-22 03:37:03 +00:00
interface ClockOptions extends ToneWithContextOptions {
2019-09-19 20:53:53 +00:00
frequency: Hertz;
2019-05-22 03:37:03 +00:00
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
2019-10-23 03:04:52 +00:00
* // 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 => {
2019-09-14 21:47:07 +00:00
* console.log(time);
2019-05-22 03:37:03 +00:00
* }, 1);
2019-10-23 20:30:07 +00:00
* clock.start();
2019-08-21 20:59:01 +00:00
* @category Core
2019-05-22 03:37:03 +00:00
*/
export class Clock<TypeName extends "bpm" | "hertz" = "hertz">
2019-09-14 21:47:07 +00:00
extends ToneWithContext<ClockOptions> implements Emitter<ClockEvent> {
2019-05-22 03:37:03 +00:00
2019-09-04 22:38:04 +00:00
readonly name: string = "Clock";
2019-05-22 03:37:03 +00:00
/**
2019-09-14 20:39:18 +00:00
* The callback function to invoke at the scheduled tick.
2019-05-22 03:37:03 +00:00
*/
callback: ClockCallback = noOp;
/**
2019-09-14 20:39:18 +00:00
* The tick counter
2019-05-22 03:37:03 +00:00
*/
private _tickSource: TickSource<TypeName>;
2019-05-22 03:37:03 +00:00
/**
2019-09-14 20:39:18 +00:00
* The last time the loop callback was invoked
2019-05-22 03:37:03 +00:00
*/
2019-11-17 18:09:19 +00:00
private _lastUpdate = 0;
2019-05-22 03:37:03 +00:00
/**
2019-09-14 20:39:18 +00:00
* Keep track of the playback state
2019-05-22 03:37:03 +00:00
*/
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);
/**
2019-09-14 20:39:18 +00:00
* The rate the callback function should be invoked.
2019-05-22 03:37:03 +00:00
*/
frequency: TickSignal<TypeName>;
2019-05-22 03:37:03 +00:00
2019-08-21 20:59:01 +00:00
/**
* @param callback The callback to be invoked with the time of the audio event
* @param frequency The rate of the callback
*/
2019-05-22 03:37:03 +00:00
constructor(callback?: ClockCallback, frequency?: Frequency);
2019-08-21 20:59:01 +00:00
constructor(options: Partial<ClockOptions>);
2019-05-22 03:37:03 +00:00
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;
}
/**
2019-09-14 20:39:18 +00:00
* Returns the playback state of the source, either "started", "stopped" or "paused".
2019-05-22 03:37:03 +00:00
*/
get state(): PlaybackState {
return this._state.getValueAtTime(this.now());
}
/**
2019-09-14 20:39:18 +00:00
* 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.
2019-05-22 03:37:03 +00:00
*/
start(time?: Time, offset?: Ticks): this {
2019-11-12 21:55:30 +00:00
// make sure the context is running
assertContextRunning(this.context);
2019-05-22 03:37:03 +00:00
// start the loop
const computedTime = this.toSeconds(time);
2019-09-19 20:53:53 +00:00
this.log("start", computedTime);
2019-05-22 03:37:03 +00:00
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;
}
/**
2019-09-14 20:39:18 +00:00
* 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 => {
2019-10-23 20:30:07 +00:00
* console.log(time);
* }, 1);
* clock.start();
* // stop the clock after 10 seconds
* clock.stop("+10");
2019-05-22 03:37:03 +00:00
*/
2019-07-30 14:25:17 +00:00
stop(time?: Time): this {
2019-05-22 03:37:03 +00:00
const computedTime = this.toSeconds(time);
2019-09-19 20:53:53 +00:00
this.log("stop", computedTime);
2019-05-22 03:37:03 +00:00
this._state.cancel(computedTime);
this._state.setStateAtTime("stopped", computedTime);
this._tickSource.stop(computedTime);
if (computedTime < this._lastUpdate) {
this.emit("stop", computedTime);
}
return this;
}
/**
2019-09-14 20:39:18 +00:00
* Pause the clock. Pausing does not reset the tick counter.
* @param time The time when the clock should stop.
2019-05-22 03:37:03 +00:00
*/
2019-07-30 14:25:17 +00:00
pause(time?: Time): this {
2019-05-22 03:37:03 +00:00
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;
}
/**
2019-09-14 20:39:18 +00:00
* The number of times the callback was invoked. Starts counting at 0
* and increments after the callback was invoked.
2019-05-22 03:37:03 +00:00
*/
get ticks(): Ticks {
return Math.ceil(this.getTicksAtTime(this.now()));
}
set ticks(t: Ticks) {
this._tickSource.ticks = t;
}
/**
2019-09-14 20:39:18 +00:00
* The time since ticks=0 that the Clock has been running. Accounts for tempo curves
2019-05-22 03:37:03 +00:00
*/
get seconds(): Seconds {
return this._tickSource.seconds;
}
set seconds(s: Seconds) {
this._tickSource.seconds = s;
}
/**
2019-09-14 20:39:18 +00:00
* Return the elapsed seconds at the given time.
* @param time When to get the elapsed seconds
* @return The number of elapsed seconds
2019-05-22 03:37:03 +00:00
*/
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);
}
2019-05-22 03:37:03 +00:00
/**
* 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.
*/
2019-07-30 14:25:17 +00:00
getTicksAtTime(time?: Time): Ticks {
2019-05-22 03:37:03 +00:00
return this._tickSource.getTicksAtTime(time);
}
/**
* Get the time of the next tick
2019-10-23 03:04:52 +00:00
* @param offset The tick number.
2019-05-22 03:37:03 +00:00
*/
nextTickTime(offset: Ticks, when: Time): Seconds {
const computedTime = this.toSeconds(when);
const currentTick = this.getTicksAtTime(computedTime);
return this._tickSource.getTimeOfTick(currentTick + offset, computedTime);
}
/**
2019-09-14 20:39:18 +00:00
* The scheduling loop.
2019-05-22 03:37:03 +00:00
*/
private _loop(): void {
const startTime = this._lastUpdate;
const endTime = this.now();
this._lastUpdate = endTime;
2019-09-19 20:53:53 +00:00
this.log("loop", startTime, endTime);
2019-05-22 03:37:03 +00:00
if (startTime !== endTime) {
// the state change events
this._state.forEachBetween(startTime, endTime, e => {
switch (e.state) {
2020-04-08 01:52:07 +00:00
case "started":
2019-05-22 03:37:03 +00:00
const offset = this._tickSource.getTicksAtTime(e.time);
this.emit("start", e.time, offset);
break;
2020-04-08 01:52:07 +00:00
case "stopped":
2019-05-22 03:37:03 +00:00
if (e.time !== 0) {
this.emit("stop", e.time);
}
break;
2020-04-08 01:52:07 +00:00
case "paused":
2019-05-22 03:37:03 +00:00
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();
2019-05-22 03:37:03 +00:00
* clock.start("+0.1");
2019-10-23 03:04:52 +00:00
* clock.getStateAtTime("+0.1"); // returns "started"
2019-05-22 03:37:03 +00:00
*/
getStateAtTime(time: Time): PlaybackState {
const computedTime = this.toSeconds(time);
return this._state.getValueAtTime(computedTime);
}
/**
2019-09-14 20:39:18 +00:00
* Clean up
2019-05-22 03:37:03 +00:00
*/
dispose(): this {
super.dispose();
2019-05-22 03:37:03 +00:00
this.context.off("tick", this._boundLoop);
this._tickSource.dispose();
this._state.dispose();
return this;
}
2019-09-14 21:47:07 +00:00
//-------------------------------------
2019-05-22 03:37:03 +00:00
// EMITTER MIXIN TO SATISFY COMPILER
2019-09-14 21:47:07 +00:00
//-------------------------------------
2019-05-22 03:37:03 +00:00
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);