UNPKG

6.58 kBTypeScriptView Raw
1import { TransportTimeClass } from "../core/type/TransportTime.js";
2import { NormalRange, Positive, Seconds, Ticks, Time, TransportTime } from "../core/type/Units.js";
3import { StateTimeline } from "../core/util/StateTimeline.js";
4import { ToneEvent, ToneEventCallback, ToneEventOptions } from "./ToneEvent.js";
5type CallbackType<T> = T extends {
6 time: Time;
7 [key: string]: any;
8} ? T : T extends ArrayLike<any> ? T[1] : T extends Time ? null : never;
9interface PartOptions<T> extends Omit<ToneEventOptions<CallbackType<T>>, "value"> {
10 events: T[];
11}
12/**
13 * Part is a collection ToneEvents which can be started/stopped and looped as a single unit.
14 *
15 * @example
16 * const synth = new Tone.Synth().toDestination();
17 * const part = new Tone.Part(((time, note) => {
18 * // the notes given as the second element in the array
19 * // will be passed in as the second argument
20 * synth.triggerAttackRelease(note, "8n", time);
21 * }), [[0, "C2"], ["0:2", "C3"], ["0:3:2", "G2"]]).start(0);
22 * Tone.Transport.start();
23 * @example
24 * const synth = new Tone.Synth().toDestination();
25 * // use an array of objects as long as the object has a "time" attribute
26 * const part = new Tone.Part(((time, value) => {
27 * // the value is an object which contains both the note and the velocity
28 * synth.triggerAttackRelease(value.note, "8n", time, value.velocity);
29 * }), [{ time: 0, note: "C3", velocity: 0.9 },
30 * { time: "0:2", note: "C4", velocity: 0.5 }
31 * ]).start(0);
32 * Tone.Transport.start();
33 * @category Event
34 */
35export declare class Part<ValueType = any> extends ToneEvent<ValueType> {
36 readonly name: string;
37 /**
38 * Tracks the scheduled events
39 */
40 protected _state: StateTimeline<{
41 id: number;
42 offset: number;
43 }>;
44 /**
45 * The events that belong to this part
46 */
47 private _events;
48 /**
49 * @param callback The callback to invoke on each event
50 * @param value the array of events
51 */
52 constructor(callback?: ToneEventCallback<CallbackType<ValueType>>, value?: ValueType[]);
53 constructor(options?: Partial<PartOptions<ValueType>>);
54 static getDefaults(): PartOptions<any>;
55 /**
56 * Start the part at the given time.
57 * @param time When to start the part.
58 * @param offset The offset from the start of the part to begin playing at.
59 */
60 start(time?: TransportTime, offset?: Time): this;
61 /**
62 * Start the event in the given event at the correct time given
63 * the ticks and offset and looping.
64 * @param event
65 * @param ticks
66 * @param offset
67 */
68 private _startNote;
69 get startOffset(): Ticks;
70 set startOffset(offset: Ticks);
71 /**
72 * Stop the part at the given time.
73 * @param time When to stop the part.
74 */
75 stop(time?: TransportTime): this;
76 /**
77 * Get/Set an Event's value at the given time.
78 * If a value is passed in and no event exists at
79 * the given time, one will be created with that value.
80 * If two events are at the same time, the first one will
81 * be returned.
82 * @example
83 * const part = new Tone.Part();
84 * part.at("1m"); // returns the part at the first measure
85 * part.at("2m", "C2"); // set the value at "2m" to C2.
86 * // if an event didn't exist at that time, it will be created.
87 * @param time The time of the event to get or set.
88 * @param value If a value is passed in, the value of the event at the given time will be set to it.
89 */
90 at(time: Time, value?: any): ToneEvent | null;
91 /**
92 * Add a an event to the part.
93 * @param time The time the note should start. If an object is passed in, it should
94 * have a 'time' attribute and the rest of the object will be used as the 'value'.
95 * @param value Any value to add to the timeline
96 * @example
97 * const part = new Tone.Part();
98 * part.add("1m", "C#+11");
99 */
100 add(obj: {
101 time: Time;
102 [key: string]: any;
103 }): this;
104 add(time: Time, value?: any): this;
105 /**
106 * Restart the given event
107 */
108 private _restartEvent;
109 /**
110 * Remove an event from the part. If the event at that time is a Part,
111 * it will remove the entire part.
112 * @param time The time of the event
113 * @param value Optionally select only a specific event value
114 */
115 remove(obj: {
116 time: Time;
117 [key: string]: any;
118 }): this;
119 remove(time: Time, value?: any): this;
120 /**
121 * Remove all of the notes from the group.
122 */
123 clear(): this;
124 /**
125 * Cancel scheduled state change events: i.e. "start" and "stop".
126 * @param after The time after which to cancel the scheduled events.
127 */
128 cancel(after?: TransportTime | TransportTimeClass): this;
129 /**
130 * Iterate over all of the events
131 */
132 private _forEach;
133 /**
134 * Set the attribute of all of the events
135 * @param attr the attribute to set
136 * @param value The value to set it to
137 */
138 private _setAll;
139 /**
140 * Internal tick method
141 * @param time The time of the event in seconds
142 */
143 protected _tick(time: Seconds, value?: any): void;
144 /**
145 * Determine if the event should be currently looping
146 * given the loop boundries of this Part.
147 * @param event The event to test
148 */
149 private _testLoopBoundries;
150 get probability(): NormalRange;
151 set probability(prob: NormalRange);
152 get humanize(): boolean | Time;
153 set humanize(variation: boolean | Time);
154 /**
155 * If the part should loop or not
156 * between Part.loopStart and
157 * Part.loopEnd. If set to true,
158 * the part will loop indefinitely,
159 * if set to a number greater than 1
160 * it will play a specific number of
161 * times, if set to false, 0 or 1, the
162 * part will only play once.
163 * @example
164 * const part = new Tone.Part();
165 * // loop the part 8 times
166 * part.loop = 8;
167 */
168 get loop(): boolean | number;
169 set loop(loop: boolean | number);
170 /**
171 * The loopEnd point determines when it will
172 * loop if Part.loop is true.
173 */
174 get loopEnd(): Time;
175 set loopEnd(loopEnd: Time);
176 /**
177 * The loopStart point determines when it will
178 * loop if Part.loop is true.
179 */
180 get loopStart(): Time;
181 set loopStart(loopStart: Time);
182 /**
183 * The playback rate of the part
184 */
185 get playbackRate(): Positive;
186 set playbackRate(rate: Positive);
187 /**
188 * The number of scheduled notes in the part.
189 */
190 get length(): number;
191 dispose(): this;
192}
193export {};