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();Constructor
Section titled “Constructor”new Part(callback?: ToneEventCallback<CallbackType<ValueType>>, value?: ValueType[])Parameters
-
callback
ToneEventCallback<CallbackType<ValueType>>optionalThe callback to invoke on each event
-
value
ValueType[]optionalthe array of events
new Part(options?: Partial<PartOptions<ValueType>>)Parameters
- options
Partial<PartOptions<ValueType>>optional
Properties
Section titled “Properties”callback
Section titled “callback”↳ inherited from ToneEvent
callback: ToneEventCallback<ValueType>The callback to invoke.
humanize
Section titled “humanize”humanize: boolean | TimeIf 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;
loop: number | booleanIf 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
Section titled “loopEnd”loopEnd: TimeThe loopEnd point determines when it will loop if Part.loop is true.
loopStart
Section titled “loopStart”loopStart: TimeThe loopStart point determines when it will loop if Part.loop is true.
probability
Section titled “probability”probability: numberThe probability of the notes being triggered.
progress
Section titled “progress”read-only ↳ inherited from ToneEvent
progress: numberThe current progress of the loop interval. Returns 0 if the event is not started yet or it is not set to loop.
startOffset
Section titled “startOffset”startOffset: numberThe start from the scheduled start time.
read-only ↳ inherited from ToneEvent
state: BasicPlaybackStateReturns the playback state of the note, either “started” or “stopped”.
Methods
Section titled “Methods”add(obj: { [key: string]: any; time: Time }): thisAdd 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): thisAdd a an event to the part.
const part = new Tone.Part();
part.add("1m", "C#+11");Parameters
-
time
TimeThe 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
anyoptionalAny value to add to the timeline
Returns this
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
TimeThe time of the event to get or set.
-
value
anyoptionalIf a value is passed in, the value of the event at the given time will be set to it.
Returns ToneEvent<any> | null
cancel()
Section titled “cancel()”cancel(after?: Time | TransportTimeClass<number>): thisCancel scheduled state change events: i.e. “start” and “stop”.
Parameters
-
after
Time | TransportTimeClass<number>optionalThe time after which to cancel the scheduled events.
Returns this
remove()
Section titled “remove()”remove(obj: { [key: string]: any; time: Time }): thisRemove 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): thisRemove an event from the part. If the event at that time is a Part, it will remove the entire part.
Parameters
-
time
TimeThe time of the event
-
value
anyoptionalOptionally select only a specific event value
Returns this
stop()
Section titled “stop()”stop(time?: Time): thisStop the part at the given time.
Parameters
-
time
TimeoptionalWhen to stop the part.
Returns this
getDefaults()
Section titled “getDefaults()”static
getDefaults(): PartOptions<any>Returns all of the default options belonging to the class.
Returns PartOptions<any>
Inherited members
Section titled “Inherited members”- From
ToneWithContext: context, blockTime, sampleTime, get(), immediate(), now(), set(), toFrequency(), toSeconds(), toTicks() - From
Tone: debug, version, disposed, toString()