Skip to content
Tone.js

Part

class Event ↳ extends ToneEvent<ValueType>

Part is a collection ToneEvents which can be started/stopped and looped as a single unit.

const synth = new Tone.Synth().toDestination();
const part = new Tone.Part(((time, note) => {
	// the notes given as the second element in the array
	// will be passed in as the second argument
	synth.triggerAttackRelease(note, "8n", time);
}), [[0, "C2"], ["0:2", "C3"], ["0:3:2", "G2"]]).start(0);
Tone.Transport.start();
const synth = new Tone.Synth().toDestination();
// use an array of objects as long as the object has a "time" attribute
const part = new Tone.Part(((time, value) => {
	// the value is an object which contains both the note and the velocity
	synth.triggerAttackRelease(value.note, "8n", time, value.velocity);
}), [{ time: 0, note: "C3", velocity: 0.9 },
	{ time: "0:2", note: "C4", velocity: 0.5 }
]).start(0);
Tone.Transport.start();
new Part(callback?: ToneEventCallback<CallbackType<ValueType>>, value?: ValueType[])

Parameters

  • callback ToneEventCallback<CallbackType<ValueType>> optional

    The callback to invoke on each event

  • value ValueType[] optional

    the array of events

new Part(options?: Partial<PartOptions<ValueType>>)

Parameters

↳ inherited from ToneEvent

callback: ToneEventCallback<ValueType>

The callback to invoke.

↳ inherited from ToneEvent

mute: boolean

If mute is true, the callback won’t be invoked.

read-only

name: string

The name of the class

Default: "Part"

↳ inherited from ToneEvent

value: ValueType

The value which is passed to the callback function.

humanize: boolean | Time

If set to true, will apply small random variation to the callback time. If the value is given as a time, it will randomize by that amount.

const event = new Tone.ToneEvent();
event.humanize = true;

read-only

length: number

The number of scheduled notes in the part.

loop: number | boolean

If the part should loop or not between Part.loopStart and Part.loopEnd. If set to true, the part will loop indefinitely, if set to a number greater than 1 it will play a specific number of times, if set to false, 0 or 1, the part will only play once.

const part = new Tone.Part();
// loop the part 8 times
part.loop = 8;
loopEnd: Time

The loopEnd point determines when it will loop if Part.loop is true.

loopStart: Time

The loopStart point determines when it will loop if Part.loop is true.

playbackRate: number

The playback rate of the part

probability: number

The probability of the notes being triggered.

read-only ↳ inherited from ToneEvent

progress: number

The current progress of the loop interval. Returns 0 if the event is not started yet or it is not set to loop.

startOffset: number

The start from the scheduled start time.

read-only ↳ inherited from ToneEvent

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

add(obj: { [key: string]: any; time: Time }): this

Add a an event to the part.

const part = new Tone.Part();
part.add("1m", "C#+11");

Parameters

  • obj { [key: string]: any; time: Time }

Returns this

add(time: Time, value?: any): this

Add a an event to the part.

const part = new Tone.Part();
part.add("1m", "C#+11");

Parameters

  • time Time

    The time the note should start. If an object is passed in, it should have a ‘time’ attribute and the rest of the object will be used as the ‘value’.

  • value any optional

    Any value to add to the timeline

Returns this

at(time: Time, value?: any): ToneEvent<any> | null

Get/Set an Event’s value at the given time. If a value is passed in and no event exists at the given time, one will be created with that value. If two events are at the same time, the first one will be returned.

const part = new Tone.Part();
part.at("1m"); // returns the part at the first measure
part.at("2m", "C2"); // set the value at "2m" to C2.
// if an event didn't exist at that time, it will be created.

Parameters

  • time Time

    The time of the event to get or set.

  • value any optional

    If a value is passed in, the value of the event at the given time will be set to it.

Returns ToneEvent<any> | null

cancel(after?: Time | TransportTimeClass<number>): this

Cancel scheduled state change events: i.e. “start” and “stop”.

Parameters

Returns this

clear(): this

Remove all of the notes from the group.

Returns this

dispose(): this

Dispose and disconnect

Returns this

remove(obj: { [key: string]: any; time: Time }): this

Remove an event from the part. If the event at that time is a Part, it will remove the entire part.

Parameters

  • obj { [key: string]: any; time: Time }

Returns this

remove(time: Time, value?: any): this

Remove an event from the part. If the event at that time is a Part, it will remove the entire part.

Parameters

  • time Time

    The time of the event

  • value any optional

    Optionally select only a specific event value

Returns this

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

Start the part at the given time.

Parameters

  • time Time optional

    When to start the part.

  • offset Time optional

    The offset from the start of the part to begin playing at.

Returns this

stop(time?: Time): this

Stop the part at the given time.

Parameters

  • time Time optional

    When to stop the part.

Returns this

static

getDefaults(): PartOptions<any>

Returns all of the default options belonging to the class.

Returns PartOptions<any>