Clock
class Core ↳ extends ToneWithContext<ClockOptions>
A sample-accurate clock that provides a callback at a given rate.
While the callback is not sample-accurate (it is susceptible to loose JavaScript timing), the time passed to the callback is precise.
For most applications, it is better to use Transport instead of the Clock by itself, since you can synchronize multiple callbacks.
// The callback will be invoked approximately once a second,
// and it will print the time exactly one second apart.
const clock = new Tone.Clock(time => {
console.log(time);
}, 1);
clock.start();Constructor
Section titled “Constructor”new Clock(callback?: ClockCallback, frequency?: Frequency)Parameters
-
callback
ClockCallbackoptionalThe callback to be invoked with the time of the audio event
-
frequency
FrequencyoptionalThe rate of the callback
new Clock(options: Partial<ClockOptions>)Parameters
- options
Partial<ClockOptions>
Properties
Section titled “Properties”callback
Section titled “callback”callback: ClockCallbackThe callback function to invoke at the scheduled tick.
Default: noOp
emit: (event: any, ...args: any[]) => thisInvoke all of the callbacks bound to the event with any arguments passed in.
frequency
Section titled “frequency”frequency: TickSignal<TypeName>The rate the callback function should be invoked.
off: (event: ClockEvent, callback?: (...args: any[]) => void) => thisRemove the event listener.
on: (event: ClockEvent, callback: (...args: any[]) => void) => thisBind a callback to a specific event.
once: (event: ClockEvent, callback: (...args: any[]) => void) => thisBind a callback which is only invoked once
seconds
Section titled “seconds”seconds: numberThe time since ticks=0 that the clock has been running.
Accounts for tempo curves.
read-only
state: PlaybackStateThe playback state of the clock, either “started”, “stopped”, or “paused”.
ticks: numberThe number of times the callback has been invoked.
Starts counting at 0 and increments after the callback is invoked.
Methods
Section titled “Methods”getSecondsAtTime()
Section titled “getSecondsAtTime()”getSecondsAtTime(time: Time): numberReturn the elapsed seconds at the given time.
Parameters
-
time
TimeWhen to get the elapsed seconds.
Returns number The number of elapsed seconds.
getStateAtTime()
Section titled “getStateAtTime()”getStateAtTime(time: Time): PlaybackStateReturns the scheduled state at the given time.
const clock = new Tone.Clock();
clock.start("+0.1");
clock.getStateAtTime("+0.1"); // returns "started"Parameters
-
time
TimeThe time to query.
Returns PlaybackState The name of the state input in setStateAtTime.
getTicksAtTime()
Section titled “getTicksAtTime()”getTicksAtTime(time?: Time): numberGet the clock’s ticks at the given time.
Parameters
-
time
TimeoptionalWhen to get the tick value.
Returns number The tick value at the given time.
getTimeOfTick()
Section titled “getTimeOfTick()”getTimeOfTick(tick: number, before?: number): numberGet 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.
Parameters
-
tick
numberThe tick number.
-
before
numberoptionalWhen to measure the tick value from.
Returns number The time of the tick
nextTickTime()
Section titled “nextTickTime()”nextTickTime(offset: number, when: Time): numberGet the time of the next tick.
Parameters
-
offset
numberThe tick number.
-
when
Time
Returns number
pause()
Section titled “pause()”pause(time?: Time): thisPause the clock. Pausing does not reset the tick counter.
Parameters
-
time
TimeoptionalThe time when the clock should pause.
Returns this
setTicksAtTime()
Section titled “setTicksAtTime()”setTicksAtTime(ticks: number, time: Time): thisSet the clock’s ticks at the given time.
Parameters
-
ticks
numberThe tick value to set.
-
time
TimeWhen to set the tick value.
Returns this
start()
Section titled “start()”start(time?: Time, offset?: number): thisStart the clock at the given time.
Parameters
-
time
TimeoptionalThe time the clock should start.
-
offset
numberoptionalThe number of ticks to start the clock from.
Returns this
stop()
Section titled “stop()”stop(time?: Time): thisStop the clock. Stopping the clock resets the tick counter to 0.
const clock = new Tone.Clock(time => {
console.log(time);
}, 1);
clock.start();
// Stop the clock after 10 seconds.
clock.stop("+10");Parameters
-
time
TimeoptionalThe time when the clock should stop.
Returns this
getDefaults()
Section titled “getDefaults()”static
getDefaults(): ClockOptionsReturns all of the default options belonging to the class.
Returns ClockOptions
Inherited members
Section titled “Inherited members”- From
ToneWithContext: context, blockTime, sampleTime, get(), immediate(), now(), set(), toFrequency(), toSeconds(), toTicks() - From
Tone: debug, version, disposed, toString()