UNPKG

9.7 kBTypeScriptView Raw
1import { InputNode, OutputNode } from "../../core/context/ToneAudioNode.js";
2import { ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode.js";
3import { NormalRange, Time } from "../../core/type/Units.js";
4import { Signal } from "../../signal/Signal.js";
5type BasicEnvelopeCurve = "linear" | "exponential";
6export type EnvelopeCurve = EnvelopeCurveName | number[];
7export interface EnvelopeOptions extends ToneAudioNodeOptions {
8 attack: Time;
9 decay: Time;
10 sustain: NormalRange;
11 release: Time;
12 attackCurve: EnvelopeCurve;
13 releaseCurve: EnvelopeCurve;
14 decayCurve: BasicEnvelopeCurve;
15}
16/**
17 * Envelope is an [ADSR](https://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope)
18 * envelope generator. Envelope outputs a signal which
19 * can be connected to an AudioParam or Tone.Signal.
20 * ```
21 * /\
22 * / \
23 * / \
24 * / \
25 * / \___________
26 * / \
27 * / \
28 * / \
29 * / \
30 * ```
31 * @example
32 * return Tone.Offline(() => {
33 * const env = new Tone.Envelope({
34 * attack: 0.1,
35 * decay: 0.2,
36 * sustain: 0.5,
37 * release: 0.8,
38 * }).toDestination();
39 * env.triggerAttackRelease(0.5);
40 * }, 1.5, 1);
41 * @category Component
42 */
43export declare class Envelope extends ToneAudioNode<EnvelopeOptions> {
44 readonly name: string;
45 /**
46 * When triggerAttack is called, the attack time is the amount of
47 * time it takes for the envelope to reach it's maximum value.
48 * ```
49 * /\
50 * /X \
51 * /XX \
52 * /XXX \
53 * /XXXX \___________
54 * /XXXXX \
55 * /XXXXXX \
56 * /XXXXXXX \
57 * /XXXXXXXX \
58 * ```
59 * @min 0
60 * @max 2
61 */
62 attack: Time;
63 /**
64 * After the attack portion of the envelope, the value will fall
65 * over the duration of the decay time to it's sustain value.
66 * ```
67 * /\
68 * / X\
69 * / XX\
70 * / XXX\
71 * / XXXX\___________
72 * / XXXXX \
73 * / XXXXX \
74 * / XXXXX \
75 * / XXXXX \
76 * ```
77 * @min 0
78 * @max 2
79 */
80 decay: Time;
81 /**
82 * The sustain value is the value
83 * which the envelope rests at after triggerAttack is
84 * called, but before triggerRelease is invoked.
85 * ```
86 * /\
87 * / \
88 * / \
89 * / \
90 * / \___________
91 * / XXXXXXXXXXX\
92 * / XXXXXXXXXXX \
93 * / XXXXXXXXXXX \
94 * / XXXXXXXXXXX \
95 * ```
96 */
97 sustain: NormalRange;
98 /**
99 * After triggerRelease is called, the envelope's
100 * value will fall to it's miminum value over the
101 * duration of the release time.
102 * ```
103 * /\
104 * / \
105 * / \
106 * / \
107 * / \___________
108 * / X\
109 * / XX\
110 * / XXX\
111 * / XXXX\
112 * ```
113 * @min 0
114 * @max 5
115 */
116 release: Time;
117 /**
118 * The automation curve type for the attack
119 */
120 private _attackCurve;
121 /**
122 * The automation curve type for the decay
123 */
124 private _decayCurve;
125 /**
126 * The automation curve type for the release
127 */
128 private _releaseCurve;
129 /**
130 * the signal which is output.
131 */
132 protected _sig: Signal<"normalRange">;
133 /**
134 * The output signal of the envelope
135 */
136 output: OutputNode;
137 /**
138 * Envelope has no input
139 */
140 input: InputNode | undefined;
141 /**
142 * @param attack The amount of time it takes for the envelope to go from
143 * 0 to it's maximum value.
144 * @param decay The period of time after the attack that it takes for the envelope
145 * to fall to the sustain value. Value must be greater than 0.
146 * @param sustain The percent of the maximum value that the envelope rests at until
147 * the release is triggered.
148 * @param release The amount of time after the release is triggered it takes to reach 0.
149 * Value must be greater than 0.
150 */
151 constructor(attack?: Time, decay?: Time, sustain?: NormalRange, release?: Time);
152 constructor(options?: Partial<EnvelopeOptions>);
153 static getDefaults(): EnvelopeOptions;
154 /**
155 * Read the current value of the envelope. Useful for
156 * synchronizing visual output to the envelope.
157 */
158 get value(): NormalRange;
159 /**
160 * Get the curve
161 * @param curve
162 * @param direction In/Out
163 * @return The curve name
164 */
165 private _getCurve;
166 /**
167 * Assign a the curve to the given name using the direction
168 * @param name
169 * @param direction In/Out
170 * @param curve
171 */
172 private _setCurve;
173 /**
174 * The shape of the attack.
175 * Can be any of these strings:
176 * * "linear"
177 * * "exponential"
178 * * "sine"
179 * * "cosine"
180 * * "bounce"
181 * * "ripple"
182 * * "step"
183 *
184 * Can also be an array which describes the curve. Values
185 * in the array are evenly subdivided and linearly
186 * interpolated over the duration of the attack.
187 * @example
188 * return Tone.Offline(() => {
189 * const env = new Tone.Envelope(0.4).toDestination();
190 * env.attackCurve = "linear";
191 * env.triggerAttack();
192 * }, 1, 1);
193 */
194 get attackCurve(): EnvelopeCurve;
195 set attackCurve(curve: EnvelopeCurve);
196 /**
197 * The shape of the release. See the attack curve types.
198 * @example
199 * return Tone.Offline(() => {
200 * const env = new Tone.Envelope({
201 * release: 0.8
202 * }).toDestination();
203 * env.triggerAttack();
204 * // release curve could also be defined by an array
205 * env.releaseCurve = [1, 0.3, 0.4, 0.2, 0.7, 0];
206 * env.triggerRelease(0.2);
207 * }, 1, 1);
208 */
209 get releaseCurve(): EnvelopeCurve;
210 set releaseCurve(curve: EnvelopeCurve);
211 /**
212 * The shape of the decay either "linear" or "exponential"
213 * @example
214 * return Tone.Offline(() => {
215 * const env = new Tone.Envelope({
216 * sustain: 0.1,
217 * decay: 0.5
218 * }).toDestination();
219 * env.decayCurve = "linear";
220 * env.triggerAttack();
221 * }, 1, 1);
222 */
223 get decayCurve(): EnvelopeCurve;
224 set decayCurve(curve: EnvelopeCurve);
225 /**
226 * Trigger the attack/decay portion of the ADSR envelope.
227 * @param time When the attack should start.
228 * @param velocity The velocity of the envelope scales the vales.
229 * number between 0-1
230 * @example
231 * const env = new Tone.AmplitudeEnvelope().toDestination();
232 * const osc = new Tone.Oscillator().connect(env).start();
233 * // trigger the attack 0.5 seconds from now with a velocity of 0.2
234 * env.triggerAttack("+0.5", 0.2);
235 */
236 triggerAttack(time?: Time, velocity?: NormalRange): this;
237 /**
238 * Triggers the release of the envelope.
239 * @param time When the release portion of the envelope should start.
240 * @example
241 * const env = new Tone.AmplitudeEnvelope().toDestination();
242 * const osc = new Tone.Oscillator({
243 * type: "sawtooth"
244 * }).connect(env).start();
245 * env.triggerAttack();
246 * // trigger the release half a second after the attack
247 * env.triggerRelease("+0.5");
248 */
249 triggerRelease(time?: Time): this;
250 /**
251 * Get the scheduled value at the given time. This will
252 * return the unconverted (raw) value.
253 * @example
254 * const env = new Tone.Envelope(0.5, 1, 0.4, 2);
255 * env.triggerAttackRelease(2);
256 * setInterval(() => console.log(env.getValueAtTime(Tone.now())), 100);
257 */
258 getValueAtTime(time: Time): NormalRange;
259 /**
260 * triggerAttackRelease is shorthand for triggerAttack, then waiting
261 * some duration, then triggerRelease.
262 * @param duration The duration of the sustain.
263 * @param time When the attack should be triggered.
264 * @param velocity The velocity of the envelope.
265 * @example
266 * const env = new Tone.AmplitudeEnvelope().toDestination();
267 * const osc = new Tone.Oscillator().connect(env).start();
268 * // trigger the release 0.5 seconds after the attack
269 * env.triggerAttackRelease(0.5);
270 */
271 triggerAttackRelease(duration: Time, time?: Time, velocity?: NormalRange): this;
272 /**
273 * Cancels all scheduled envelope changes after the given time.
274 */
275 cancel(after?: Time): this;
276 /**
277 * Connect the envelope to a destination node.
278 */
279 connect(destination: InputNode, outputNumber?: number, inputNumber?: number): this;
280 /**
281 * Render the envelope curve to an array of the given length.
282 * Good for visualizing the envelope curve. Rescales the duration of the
283 * envelope to fit the length.
284 */
285 asArray(length?: number): Promise<Float32Array>;
286 dispose(): this;
287}
288interface EnvelopeCurveObject {
289 In: number[];
290 Out: number[];
291}
292interface EnvelopeCurveMap {
293 linear: "linear";
294 exponential: "exponential";
295 bounce: EnvelopeCurveObject;
296 cosine: EnvelopeCurveObject;
297 sine: EnvelopeCurveObject;
298 ripple: EnvelopeCurveObject;
299 step: EnvelopeCurveObject;
300}
301type EnvelopeCurveName = keyof EnvelopeCurveMap;
302export {};