Draw

Draw is useful for synchronizing visuals and audio events. Callbacks from Tone.Transport or any of the Tone.Event classes always happen before the scheduled time and are not synchronized to the animation frame so they are not good for triggering tightly synchronized visuals and sound. Draw makes it easy to schedule callbacks using the AudioContext time and uses requestAnimationFrame. Draw is used to synchronize the draw frame with the Transport's callbacks. See Draw


Tone.Transport.schedule((time) => {
	// use the time argument to schedule a callback with Draw
	Tone.Draw.schedule(() => {
		// do drawing or DOM manipulation here
		console.log(time);
	}, time);
}, "+0.5");
Tone.Transport.start();

Constructor

Pass in a constructor as the first argument

new Draw (
context?:BaseContext
) => Draw

Pass in a constructor as the first argument

new Draw (
options?:Partial<ToneWithContextOptions >
) => Draw

Properties

anticipation #

Seconds

The amount of time before the scheduled time that the callback can be invoked. Default is half the time of an animation frame (0.008 seconds).

blockTime #

readonly Seconds

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


console.log(Tone.Destination.blockTime);

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.

expiration #

Seconds

The duration after which events are not invoked.

name #

string

sampleTime #

readonly Seconds

The duration in seconds of one sample.


console.log(Tone.Transport.sampleTime);

static version #

string

The version number semver

Methods

cancel #

Cancel events scheduled after the given time

cancel (
after?:Time

Time after which scheduled events will be removed from the scheduling timeline.

) => this

dispose #

disconnect and dispose.

dispose ( ) => this

get #

Get the object's attributes.


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

static getDefaults #

Returns all of the default options belonging to the class.

getDefaults ( ) => ToneWithContextOptions

immediate #

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


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

now #

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


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

schedule #

Schedule a function at the given time to be invoked on the nearest animation frame.


Tone.Transport.scheduleRepeat(time => {
	Tone.Draw.schedule(() => console.log(time), time);
}, 1);
Tone.Transport.start();
schedule (
callback:() => void ,

Callback is invoked at the given time.

time:Time

The time relative to the AudioContext time to invoke the callback.

) => this

set #

Set multiple properties at once with an object.


const filter = new Tone.Filter();
// set values using an object
filter.set({
	frequency: 300,
	type: "highpass"
});
set ( ) => 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


const gain = new Tone.Gain();
console.log(gain.toSeconds("4n"));
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