Tone.js
    Preparing search index...

    Class Clock<TypeName>

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

    Type Parameters

    • TypeName extends "bpm" | "hertz" = "hertz"

    Hierarchy

    • ToneWithContext<ClockOptions>
      • Clock

    Implements

    Index

    Constructors

    • Type Parameters

      • TypeName extends "bpm" | "hertz" = "hertz"

      Parameters

      • Optionalcallback: ClockCallback

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

      • Optionalfrequency: Unit.Frequency

        The rate of the callback

      Returns Clock<TypeName>

    • Type Parameters

      • TypeName extends "bpm" | "hertz" = "hertz"

      Parameters

      • options: Partial<ClockOptions>

      Returns Clock<TypeName>

    Properties

    callback: ClockCallback = noOp

    The callback function to invoke at the scheduled tick.

    context: BaseContext

    The context belonging to the node.

    debug: boolean = false

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

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

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

    Type Declaration

      • (event: any, ...args: any[]): this
      • Parameters

        • event: any

          The name of the event.

        • ...args: any[]

          The arguments to pass to the functions listening.

        Returns this

    frequency: TickSignal<TypeName>

    The rate the callback function should be invoked.

    name: string = "Clock"

    The name of the class

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

    Remove the event listener.

    Type Declaration

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

        • event: ClockEvent

          The event to stop listening to.

        • Optionalcallback: (...args: any[]) => void

          The callback which was bound to the event with Emitter.on. If no callback is given, all callbacks events are removed.

        Returns this

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

    Bind a callback to a specific event.

    Type Declaration

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

        • event: ClockEvent

          The name of the event to listen for.

        • callback: (...args: any[]) => void

          The callback to invoke when the event is emitted

        Returns this

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

    Bind a callback which is only invoked once

    Type Declaration

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

        • event: ClockEvent

          The name of the event to listen for.

        • callback: (...args: any[]) => void

          The callback to invoke when the event is emitted

        Returns this

    version: string = version

    The version number semver

    Accessors

    • get blockTime(): number

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

      Returns number

      console.log(Tone.Destination.blockTime);
      
    • get disposed(): 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.

      Returns boolean

    • get ticks(): number

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

      Returns number

    • set ticks(t: number): void

      Parameters

      • t: number

      Returns void

    Methods

    • Get the object's attributes.

      Returns ClockOptions

      const osc = new Tone.Oscillator();
      console.log(osc.get());
    • Return the elapsed seconds at the given time.

      Parameters

      • time: Unit.Time

        When to get the elapsed seconds

      Returns number

      The number of elapsed seconds

    • Returns the scheduled state at the given time.

      Parameters

      Returns PlaybackState

      The name of the state input in setStateAtTime.

      const clock = new Tone.Clock();
      clock.start("+0.1");
      clock.getStateAtTime("+0.1"); // returns "started"
    • Get the clock's ticks at the given time.

      Parameters

      • Optionaltime: Unit.Time

        When to get the tick value

      Returns number

      The tick value at the given time.

    • 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 = ...

        When to measure the tick value from.

      Returns number

      The time of the tick

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

      Returns number

      setInterval(() => {
      console.log(Tone.immediate());
      }, 100);
    • Return the current time of the Context clock plus the lookAhead.

      Returns number

      setInterval(() => {
      console.log(Tone.now());
      }, 100);
    • Set multiple properties at once with an object.

      Parameters

      • props: RecursivePartial<Options>

      Returns this

      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 the clock's ticks at the given time.

      Parameters

      • ticks: number

        The tick value to set

      • time: Unit.Time

        When to set the tick value

      Returns this

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

      Parameters

      • Optionaltime: Unit.Time

        The time the clock should start

      • Optionaloffset: number

        Where the tick counter starts counting from.

      Returns this

    • Stop the clock. Stopping the clock resets the tick counter to 0.

      Parameters

      • Optionaltime: Unit.Time

        The time when the clock should stop.

      Returns this

      const clock = new Tone.Clock(time => {
      console.log(time);
      }, 1);
      clock.start();
      // stop the clock after 10 seconds
      clock.stop("+10");
    • Convert the incoming time to seconds. This is calculated against the current TransportInstance bpm

      Parameters

      Returns number

      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);
    • Convert the class to a string

      Returns string

      const osc = new Tone.Oscillator();
      console.log(osc.toString());
    • Returns all of the default options belonging to the class.

      Returns ClockOptions