UNPKG

6.03 kBTypeScriptView Raw
1import "../core/clock/Transport.js";
2import { ToneWithContext, ToneWithContextOptions } from "../core/context/ToneWithContext.js";
3import { TransportTimeClass } from "../core/type/TransportTime.js";
4import { NormalRange, Positive, Seconds, Ticks, Time, TransportTime } from "../core/type/Units.js";
5import { BasicPlaybackState, StateTimeline } from "../core/util/StateTimeline.js";
6export type ToneEventCallback<T> = (time: Seconds, value: T) => void;
7export interface ToneEventOptions<T> extends ToneWithContextOptions {
8 callback: ToneEventCallback<T>;
9 loop: boolean | number;
10 loopEnd: Time;
11 loopStart: Time;
12 playbackRate: Positive;
13 value?: T;
14 probability: NormalRange;
15 mute: boolean;
16 humanize: boolean | Time;
17}
18/**
19 * ToneEvent abstracts away this.context.transport.schedule and provides a schedulable
20 * callback for a single or repeatable events along the timeline.
21 *
22 * @example
23 * const synth = new Tone.PolySynth().toDestination();
24 * const chordEvent = new Tone.ToneEvent(((time, chord) => {
25 * // the chord as well as the exact time of the event
26 * // are passed in as arguments to the callback function
27 * synth.triggerAttackRelease(chord, 0.5, time);
28 * }), ["D4", "E4", "F4"]);
29 * // start the chord at the beginning of the transport timeline
30 * chordEvent.start();
31 * // loop it every measure for 8 measures
32 * chordEvent.loop = 8;
33 * chordEvent.loopEnd = "1m";
34 * @category Event
35 */
36export declare class ToneEvent<ValueType = any> extends ToneWithContext<ToneEventOptions<ValueType>> {
37 readonly name: string;
38 /**
39 * Loop value
40 */
41 protected _loop: boolean | number;
42 /**
43 * The callback to invoke.
44 */
45 callback: ToneEventCallback<ValueType>;
46 /**
47 * The value which is passed to the
48 * callback function.
49 */
50 value: ValueType;
51 /**
52 * When the note is scheduled to start.
53 */
54 protected _loopStart: Ticks;
55 /**
56 * When the note is scheduled to start.
57 */
58 protected _loopEnd: Ticks;
59 /**
60 * Tracks the scheduled events
61 */
62 protected _state: StateTimeline<{
63 id: number;
64 }>;
65 /**
66 * The playback speed of the note. A speed of 1
67 * is no change.
68 */
69 protected _playbackRate: Positive;
70 /**
71 * A delay time from when the event is scheduled to start
72 */
73 protected _startOffset: Ticks;
74 /**
75 * private holder of probability value
76 */
77 protected _probability: NormalRange;
78 /**
79 * the amount of variation from the given time.
80 */
81 protected _humanize: boolean | Time;
82 /**
83 * If mute is true, the callback won't be invoked.
84 */
85 mute: boolean;
86 /**
87 * @param callback The callback to invoke at the time.
88 * @param value The value or values which should be passed to the callback function on invocation.
89 */
90 constructor(callback?: ToneEventCallback<ValueType>, value?: ValueType);
91 constructor(options?: Partial<ToneEventOptions<ValueType>>);
92 static getDefaults(): ToneEventOptions<any>;
93 /**
94 * Reschedule all of the events along the timeline
95 * with the updated values.
96 * @param after Only reschedules events after the given time.
97 */
98 private _rescheduleEvents;
99 /**
100 * Returns the playback state of the note, either "started" or "stopped".
101 */
102 get state(): BasicPlaybackState;
103 /**
104 * The start from the scheduled start time.
105 */
106 get startOffset(): Ticks;
107 set startOffset(offset: Ticks);
108 /**
109 * The probability of the notes being triggered.
110 */
111 get probability(): NormalRange;
112 set probability(prob: NormalRange);
113 /**
114 * If set to true, will apply small random variation
115 * to the callback time. If the value is given as a time, it will randomize
116 * by that amount.
117 * @example
118 * const event = new Tone.ToneEvent();
119 * event.humanize = true;
120 */
121 get humanize(): Time | boolean;
122 set humanize(variation: Time | boolean);
123 /**
124 * Start the note at the given time.
125 * @param time When the event should start.
126 */
127 start(time?: TransportTime | TransportTimeClass): this;
128 /**
129 * Stop the Event at the given time.
130 * @param time When the event should stop.
131 */
132 stop(time?: TransportTime | TransportTimeClass): this;
133 /**
134 * Cancel all scheduled events greater than or equal to the given time
135 * @param time The time after which events will be cancel.
136 */
137 cancel(time?: TransportTime | TransportTimeClass): this;
138 /**
139 * The callback function invoker. Also
140 * checks if the Event is done playing
141 * @param time The time of the event in seconds
142 */
143 protected _tick(time: Seconds): void;
144 /**
145 * Get the duration of the loop.
146 */
147 protected _getLoopDuration(): Ticks;
148 /**
149 * If the note should loop or not
150 * between ToneEvent.loopStart and
151 * ToneEvent.loopEnd. If set to true,
152 * the event will loop indefinitely,
153 * if set to a number greater than 1
154 * it will play a specific number of
155 * times, if set to false, 0 or 1, the
156 * part will only play once.
157 */
158 get loop(): boolean | number;
159 set loop(loop: boolean | number);
160 /**
161 * The playback rate of the event. Defaults to 1.
162 * @example
163 * const note = new Tone.ToneEvent();
164 * note.loop = true;
165 * // repeat the note twice as fast
166 * note.playbackRate = 2;
167 */
168 get playbackRate(): Positive;
169 set playbackRate(rate: Positive);
170 /**
171 * The loopEnd point is the time the event will loop
172 * if ToneEvent.loop is true.
173 */
174 get loopEnd(): Time;
175 set loopEnd(loopEnd: Time);
176 /**
177 * The time when the loop should start.
178 */
179 get loopStart(): Time;
180 set loopStart(loopStart: Time);
181 /**
182 * The current progress of the loop interval.
183 * Returns 0 if the event is not started yet or
184 * it is not set to loop.
185 */
186 get progress(): NormalRange;
187 dispose(): this;
188}