Skip to content
Tone.js

Clock

class Core ↳ extends ToneWithContext<ClockOptions>

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.

// 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();
new Clock(callback?: ClockCallback, frequency?: Frequency)

Parameters

  • callback ClockCallback optional

    The callback to be invoked with the time of the audio event

  • frequency Frequency optional

    The rate of the callback

new Clock(options: Partial<ClockOptions>)

Parameters

callback: ClockCallback

The callback function to invoke at the scheduled tick.

Default: noOp

emit: (event: any, ...args: any[]) => this

Invoke all of the callbacks bound to the event with any arguments passed in.

frequency: TickSignal<TypeName>

The rate the callback function should be invoked.

read-only

name: string

The name of the class

Default: "Clock"

off: (event: ClockEvent, callback?: (...args: any[]) => void) => this

Remove the event listener.

on: (event: ClockEvent, callback: (...args: any[]) => void) => this

Bind a callback to a specific event.

once: (event: ClockEvent, callback: (...args: any[]) => void) => this

Bind a callback which is only invoked once

seconds: number

The time since ticks=0 that the Clock has been running. Accounts for tempo curves

read-only

Returns the playback state of the source, either “started”, “stopped” or “paused”.

ticks: number

The number of times the callback was invoked. Starts counting at 0 and increments after the callback was invoked.

dispose(): this

Clean up

Returns this

getSecondsAtTime(time: Time): number

Return the elapsed seconds at the given time.

Parameters

  • time Time

    When to get the elapsed seconds

Returns number The number of elapsed seconds

getStateAtTime(time: Time): PlaybackState

Returns the scheduled state at the given time.

const clock = new Tone.Clock();
clock.start("+0.1");
clock.getStateAtTime("+0.1"); // returns "started"

Parameters

  • time Time

    The time to query.

Returns PlaybackState The name of the state input in setStateAtTime.

getTicksAtTime(time?: Time): number

Get the clock’s ticks at the given time.

Parameters

  • time Time optional

    When to get the tick value

Returns number The tick value at the given time.

getTimeOfTick(tick: number, before?: number): number

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.

Parameters

  • tick number

    The tick number.

  • before number optional

    When to measure the tick value from.

Returns number The time of the tick

nextTickTime(offset: number, when: Time): number

Get the time of the next tick

Parameters

  • offset number

    The tick number.

  • when Time

Returns number

pause(time?: Time): this

Pause the clock. Pausing does not reset the tick counter.

Parameters

  • time Time optional

    The time when the clock should stop.

Returns this

setTicksAtTime(ticks: number, time: Time): this

Set the clock’s ticks at the given time.

Parameters

  • ticks number

    The tick value to set

  • time Time

    When to set the tick value

Returns this

start(time?: Time, offset?: number): this

Start the clock at the given time. Optionally pass in an offset of where to start the tick counter from.

Parameters

  • time Time optional

    The time the clock should start

  • offset number optional

    Where the tick counter starts counting from.

Returns this

stop(time?: Time): this

Stop 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 Time optional

    The time when the clock should stop.

Returns this

static

getDefaults(): ClockOptions

Returns all of the default options belonging to the class.

Returns ClockOptions