Clock < TypeName >

  • TypeName: "bpm" | "hertz"

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();

Hierarchy

Constructor

new Clock (
callback?:ClockCallback ,

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

frequency?:Frequency

The rate of the callback

) => Clock
new Clock (
options:Partial<ClockOptions >
) => Clock

Properties

blockTime #

readonly Seconds

The number of seconds of 1 processing block (128 samples)


console.log(Tone.Destination.blockTime);

callback #

ClockCallback

The callback function to invoke at the scheduled tick.

context #

BaseContext

The context belonging to the node.

debug #

boolean

Set this debug flag to log all events that happen in this class.

disposed #

readonly boolean

Indicates if the instance was disposed. 'Disposing' an instance means that all of the Web Audio nodes that were created for the instance are disconnected and freed for garbage collection.

emit #

(event,args) => this

frequency #

TickSignal<TypeName >

The rate the callback function should be invoked.

name #

string

off #

(event,callback) => this

on #

(event,callback) => this

once #

(event,callback) => this

sampleTime #

readonly Seconds

The duration in seconds of one sample.


console.log(Tone.Transport.sampleTime);

seconds #

Seconds

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

state #

readonly PlaybackState

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

ticks #

Ticks

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

static version #

string

The version number semver

Methods

dispose #

Clean up

dispose ( ) => this

get #

Get the object's attributes.


const osc = new Tone.Oscillator();
console.log(osc.get());
get ( ) => ClockOptions

static getDefaults #

Returns all of the default options belonging to the class.

getDefaults ( ) => ClockOptions

getSecondsAtTime #

Return the elapsed seconds at the given time.

getSecondsAtTime (
time:Time

When to get the elapsed seconds

) => Seconds
The number of elapsed seconds

getStateAtTime #

Returns the scheduled state at the given time.


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

The time to query.

) => PlaybackState
The name of the state input in setStateAtTime.

getTicksAtTime #

Get the clock's ticks at the given time.

getTicksAtTime (
time?:Time

When to get the tick value

) => Ticks
The tick value at the given time.

getTimeOfTick #

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.

getTimeOfTick (
tick:Ticks ,

The tick number.

before= this.now():number

When to measure the tick value from.

) => Seconds
The time of the tick

immediate #

Return the current time of the Context clock without any lookAhead.


setInterval(() => {
	console.log(Tone.immediate());
}, 100);
immediate ( ) => Seconds

nextTickTime #

Get the time of the next tick

nextTickTime (
offset:Ticks ,

The tick number.

when:Time
) => Seconds

now #

Return the current time of the Context clock plus the lookAhead.


setInterval(() => {
	console.log(Tone.now());
}, 100);
now ( ) => Seconds

pause #

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

pause (
time?:Time

The time when the clock should stop.

) => this

set #

Set multiple properties at once with an object.


const filter = new Tone.Filter().toDestination();
// set values using an object
filter.set({
	frequency: "C6",
	type: "highpass"
});
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/Analogsynth_octaves_highmid.mp3").connect(filter);
player.autostart = true;
set ( ) => this

setTicksAtTime #

Set the clock's ticks at the given time.

setTicksAtTime (
ticks:Ticks ,

The tick value to set

time:Time

When to set the tick value

) => this

start #

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

start (
time?:Time ,

The time the clock should start

offset?:Ticks

Where the tick counter starts counting from.

) => this

stop #

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");
stop (
time?:Time

The time when the clock should stop.

) => this

toFrequency #

Convert the input to a frequency number


const gain = new Tone.Gain();
console.log(gain.toFrequency("4n"));
toFrequency (
freq:Frequency
) => Hertz

toSeconds #

Convert the incoming time to seconds. This is calculated against the current Tone.Transport bpm


const gain = new Tone.Gain();
setInterval(() => console.log(gain.toSeconds("4n")), 100);
// ramp the tempo to 60 bpm over 30 seconds
Tone.getTransport().bpm.rampTo(60, 30);
toSeconds (
time?:Time
) => Seconds

toString #

Convert the class to a string


const osc = new Tone.Oscillator();
console.log(osc.toString());
toString ( ) => string

toTicks #

Convert the input time into ticks


const gain = new Tone.Gain();
console.log(gain.toTicks("4n"));
toTicks (
time?:Time | TimeClass
) => Ticks