2 summary:: Specification for a segmented envelope
3 related:: Classes/EnvGen, Classes/IEnvGen, Classes/Pseg
4 categories:: Control, Envelopes
7 An Env is a specification for a segmented envelope. Envs can be used both server-side, by an link::Classes/EnvGen:: or an link::Classes/IEnvGen:: within a link::Classes/SynthDef::, and clientside, with methods such as link::#-at:: and link::#-asStream::, below.
9 An Env can have any number of segments which can stop at a particular value or loop several segments when sustaining. It can have several shapes for its segments.
12 Env.new([0, 1, 0.9, 0], [0.1, 0.5, 1],[-5, 0, -5]).plot;
15 The envelope is conceived as a sequence of emphasis::nodes:: (not to be confused with a synthesis-Node) each of which has three parameters: a target level, a time duration from the previous node, and a shape. The three parameters for each node are kept in separate arrays as explained below.
18 In some situations we deal with control points or breakpoints. If these control points have associated x positions (say in an envelope GUI, see link::Classes/EnvelopeView::) they must be converted to time differences between points to be used as nodes in a Env object. The methods link::#*xyc:: and link::#*pairs:: can be used to specify an envelope in terms of points.
22 // an envelope in a synth
25 var env = Env([0, 1, 0.5, 1, 0], [0.01, 0.5, 0.02, 0.5]);
26 SinOsc.ar(470) * EnvGen.kr(env, doneAction: 2)
29 // an envelope to control a parameter in a pattern
32 \note, Env([0, 12, 6, 13, 0], [1, 5, 2, 10]),
43 Create a new envelope specification.
46 an array of levels. The first level is the initial value of the envelope. When the envelope is used with an EnvGen, levels can be any UGen (new level values are updated only when the envelope has reached that point).
47 When the array of levels contains itself an array, the envelope returns a multichannel output (for a discussion, see link::#multichannel expansion::)
50 an array of durations of segments in seconds. There should be one fewer duration than there are levels, but if shorter, the array is extended by wrapping around the given values.
53 a link::Classes/Symbol::, link::Classes/Float::, or an link::Classes/Array:: of those. Determines the shape of the envelope segments.
55 The possible values are:
57 ## code::\step:: || || flat segments
58 ## code::\linear:: || code::\lin:: || linear segments, the default
59 ## code::\exponential:: || code::\exp:: || natural exponential growth and decay. In this case, the levels must all be nonzero and have the same sign.
60 ## code::\sine:: || code::\sin:: || sinusoidal S shaped segments.
61 ## code::\welch:: || code::\wel:: || sinusoidal segments shaped like the sides of a Welch window.
62 ## code::\squared:: || code::\sqr:: || squared segment
63 ## code::\cubed:: || code::\cub:: || cubed segment
64 ## a link::Classes/Float:: || || a curvature value for all segments. 0 means linear, positive and negative numbers curve the segment up and down.
65 ## an link::Classes/Array:: of symbols or floats || || curvature values for each segment.
69 an link::Classes/Integer:: or nil. The envelope will sustain at the release node until released.
72 an link::Classes/Integer:: or nil. If not nil the output will loop through those nodes startign at the loop node to the node immediately preceeding the release node, before back to the loop node, and so on. Note that the envelope only transitions to the release node when released. Examples are below. The loop is escaped when a gate signal is sent, when the output transitions to the release node, as described below.
75 an offset to all time values (only applies in link::Classes/IEnvGen::).
81 var env = Env([0.0, 0.5, 0.0, 1.0, 0.9, 0.0], [0.05, 0.1, 0.01, 1.0, 1.5], -4);
82 var envgen = EnvGen.ar(env, doneAction: 2);
94 Creates a new envelope specification with strong::numSegments:: for filling in later.
96 This can be useful when passing Env parameters as args to a link::Classes/Synth::. Note that the maximum number of segments is fixed and cannot be changed once embedded in a link::Classes/SynthDef::. Trying to set an Env with more segments than then this may result in other args being unexpectedly set. See link::#newClear:: example below.
98 subsection::Standard Shape Envelope Creation Methods
99 The following class methods create some frequently used envelope shapes based on supplied durations.
102 Creates a new envelope specification which has a trapezoidal shape.
105 the duration of the attack portion.
107 argument::sustainTime
108 the duration of the sustain portion.
110 argument::releaseTime
111 the duration of the release portion.
114 the level of the sustain portion.
117 the curvature of the envelope.
122 Env.linen(0.1, 0.2, 0.1, 0.6).test.plot;
123 Env.linen(1, 2, 3, 0.6).test.plot;
124 Env.linen(1, 2, 3, 0.6, \sine).test.plot;
125 Env.linen(1, 2, 3, 0.6, \welch).test.plot;
126 Env.linen(1, 2, 3, 0.6, -3).test.plot;
127 Env.linen(1, 2, 3, 0.6, -3).test.plot;
128 Env.linen(1, 2, 3, 0.6, [[\sine, \welch, \lin, \exp]]).plot;
132 Creates a new envelope specification which has a triangle shape.
135 the duration of the envelope.
138 the peak level of the envelope.
142 Env.triangle(1, 1).test.plot;
146 Creates a new envelope specification which has a hanning window shape.
149 the duration of the envelope.
152 the peak level of the envelope.
156 Env.sine(1, 1).test.plot;
160 Creates a new envelope specification which (usually) has a percussive shape.
163 the duration of the attack portion.
165 argument::releaseTime
166 the duration of the release portion.
169 the peak level of the envelope.
172 the curvature of the envelope.
176 Env.perc(0.05, 1, 1, -4).test.plot;
177 Env.perc(0.001, 1, 1, -4).test.plot; // sharper attack
178 Env.perc(0.001, 1, 1, -8).test.plot; // change curvature
179 Env.perc(1, 0.01, 1, 4).test.plot; // reverse envelope
184 Creates a new envelope specification from coordinates / control points
187 an array of pairs [[time, level], ...]
190 the curvature of the envelope.
194 Env.pairs([[0, 1], [2.1, 0.5], [3, 1.4]], \exp).plot;
195 Env.pairs([[0, 1], [3, 1.4], [2.1, 0.5], [3, 4]], \exp).plot; // pairs are sorted according to time
196 Env.pairs({ { 1.0.rand } ! 2 } ! 16, \exp).plot;
201 Creates a new envelope specification from coordinates / control points with curvature.
204 an array of triplets [[time, level, curve], ...]
208 Env.xyc([[0, 1, \sin], [2.1, 0.5, \lin], [3, 1.4, \lin]]).plot;
209 Env.xyc([[2.1, 0.5, \lin], [0, 1, \sin], [3, 1.4, \lin]]).plot; // pairs are sorted according to time
210 Env.xyc({ [1.0.rand, 10.0.rand, -4.rand2] } ! 16, \exp).plot;
214 subsection::Sustained Envelope Creation Methods
215 The following methods create some frequently used envelope shapes which have a sustain segment. They are typically used in SynthDefs in situations where at the time of starting the synth it is not known when it will end. Typical cases are external interfaces, midi input, or quickly varying TempoClock.
219 SynthDef(\env_help, { |out, gate = 1, amp = 0.1, release = 0.1|
220 var env = Env.adsr(0.02, release, amp);
221 var gen = EnvGen.kr(env, gate, doneAction: 2);
222 Out.ar(out, PinkNoise.ar(1 ! 2) * gen)
226 a = Synth(\env_help);
227 b = Synth(\env_help, [\release, 2]);
228 a.set(\gate, 0); // alternatively, you can write a.release;
235 Creates a new envelope specification which is shaped like traditional analog attack-decay-sustain-release (adsr) envelopes.
238 the duration of the attack portion.
241 the duration of the decay portion.
243 argument::sustainLevel
244 the level of the sustain portion as a ratio of the peak level.
246 argument::releaseTime
247 the duration of the release portion.
250 the peak level of the envelope.
253 the curvature of the envelope.
260 Env.adsr(0.02, 0.2, 0.25, 1, 1, -4).test(2).plot;
261 Env.adsr(0.001, 0.2, 0.25, 1, 1, -4).test(2).plot;
262 Env.adsr(0.001, 0.2, 0.25, 1, 1, -4).test(0.45).plot; // release after 0.45 sec
266 As link::#*adsr:: above, but with its onset delayed by strong::delayTime:: in seconds. The default delay is 0.1.
269 Creates a new envelope specification which is shaped like traditional analog attack-sustain-release (asr) envelopes.
272 the duration of the attack portion.
274 argument::sustainLevel
275 the level of the sustain portion as a ratio of the peak level.
277 argument::releaseTime
278 the duration of the release portion.
281 the curvature of the envelope.
285 Env.asr(0.02, 0.5, 1, -4).test(2).plot;
286 Env.asr(0.001, 0.5, 1, -4).test(2).plot; // sharper attack
287 Env.asr(0.02, 0.5, 1, 'linear').test(2).plot; // linear segments
292 Creates a new envelope specification which has no attack segment. It simply sustains at the peak level until released. Useful if you only need a fadeout, and more versatile than link::Classes/Line::.
294 argument::releaseTime
295 the duration of the release portion.
298 the peak level of the envelope.
301 the curvature of the envelope.
305 Env.cutoff(1, 1).test(2).plot;
306 Env.cutoff(1, 1, 4).test(2).plot;
307 Env.cutoff(1, 1, \sine).test(2).plot;
311 Creates a new envelope specification which cycles through its values. For making a given envelope cyclic, you can use the instance method link::#-circle::
314 The levels through which the envelope passes.
317 The time between subsequent points in the envelope, which may be a single value (number), or an array of them. If too short, the array is extended. In difference to the *new method, the size of the times array is the same as that of the levels, because it includes the loop time.
320 The curvature of the envelope, which may be a single value (number or symbol), or an array of them. If too short, the array is extended. In difference to the *new method, the size of the curve array is the same as that of the levels, because it includes the loop time.
325 { SinOsc.ar(EnvGen.kr(Env.circle([0, 1, 0], [0.01, 0.5, 0.2])) * 440 + 200) * 0.2 }.play;
326 { SinOsc.ar(EnvGen.kr(Env.circle([0, 1, 0, 2, 0, 1, 0], [0.01, 0.3])) * 440 + 200) * 0.2 }.play;
327 { SinOsc.ar(EnvGen.kr(Env.circle([0, 1, 0, (2..4), 0, (1..3), 0], [0.01, 0.3])) * 440 + 200).sum * 0.2 }.play; // multichannel expanded levels
332 subsection::Multichannel expansion
333 If one of the values within either levels, times, or curves is itself an array, the envelope expands to multiple channels wherever appropriate. This means that when such an envelope is passed to an EnvGen, this EnvGen will expand, and when the envelope is queried via the methods link::#-at:: or link::#-asSignal::, it will return an array of values.
338 var env = Env([0.0, 0.5, 0.0, [1.0, 1.25, 1.5], 0.9, 0.0], [0.05, 0.1, 0.01, 1.0, 1.5], -4);
339 var envgen = EnvGen.ar(env, doneAction: 2);
348 var env = Env([1, [1, 2, 3], 0.5, 0.5, [3, 2, 1], 2], [1, 1, 0.5, 1], [[\exp, \sin]]);
350 Splay.ar(SinOsc.ar(EnvGen.kr(env) * 400 + 600)) * 0.1
357 var levels = (1..30);
358 var env = Env([1, levels, 0.5, levels / 2.5, 2], [1, 0.15, 1, 0.25, 0.1], \exp);
359 Splay.ar(SinOsc.ar(EnvGen.kr(env) * 400 + 600)) * 0.1
364 // accessing the envlope by indexing
366 e = Env([1, [1, 2, 3], 1], [1, 1], \exp);
371 e = Env([1, 1, 1], [1, [1, 2, 3]], \exp);
376 // multichannel levels
378 Env([0.1, 1, 0.1], [1, [1, 2, 3]], \exp).plot;
379 Env([0.1, 1, 0.1], [1, [1, 2, 3]], [\lin, [\lin, \exp, \sin]]).plot;
381 Env([1, 1, 0.5, 3, 2], [1, 0.5, 1, 0.25], \exp).plot;
382 Env([0, 1, 0, 2, 0] * [[1, 2, 3]], [1, 0.5, 1, 0.25], \lin).plot;
385 // multichannel curves
387 Env([0.01, 5, 1, 0.5] + 1, [1, 0.5, 1, 0.25], [[\lin, \sqr]]).plot;
389 Env([0.01, 5, 1, 0.5, 0.001] + 1, [1, 0.5, 1, 0.25, 1], [[\lin, \cub, \sin, \cubed, \welch, \step, \exp]]).plot(bounds: Rect(30, 100, 500, 700));
391 Env([0.01, 5, 1, 0.5, 0.001] + 1, [1, 0.5, 1, 0.25, 1], [(-4..4)]).plot(bounds: Rect(30, 100, 500, 700));
392 Env([0.01, 5, 1, 0.5] + 1, [1, 0.5, 1, 0.25], [(-4..4)]).plot(bounds: Rect(30, 100, 500, 700));
395 Env([[0, 0.01], 1, 0], [0.5, 0.5], [[\lin, \exp], \step]).plot;
396 Env([[0, 0.01], 1, [0, 0.01]], [0.5, 1], [[\lin, \exp]]).plot;
398 // multichannel times
400 Env([[2, 1], 0], [[1, 2]], \lin).plot;
401 Env([0, 1], [1/(1..5)], [(-4..4)]).plot(bounds: Rect(30, 100, 300, 700));
402 Env([0, 1], [1/(1..5)], \lin).plot(bounds: Rect(30, 100, 300, 700));
407 Env([1, [ 1, 2, 3, 4, 5 ], 0.5, [3, 2, 1], 2], [1, 0.5, 1, 0.25], [[\exp, \lin]]).plot;
408 Env([1, [ 1, 2, 3, 4, 5 ], 0.5, 4, 2], [1, 0.5, 1, 0.25], \exp).plot;
411 // expanding control point envelopes
413 Env.xyc([[2, 0.5, [\lin, \exp]], [0, 1, \lin], [3, 1.4, \lin]]).plot;
414 Env.xyc({ [1.0.rand, 1.0.rand, {[\lin, \exp, \step].choose} ! 3] } ! 8).plot
416 Env.xyc([[[2.0, 2.3], 0.5, \lin], [0, 1, \lin], [3, 1.4, \lin]]).plot; // multiple times
427 Instead of using an link::Classes/EnvGen:: inside a UGen graph, this message does the same implicitly for convenience. Its argument order corresponds to the most common arguments.
431 An integer representing an action to be executed when the env is
432 finished playing. This can be used to free the enclosing synth,
433 etc. See link::Reference/UGen-doneActions:: for more detail.
437 This triggers the envelope and holds it open while > 0. If the
438 Env is fixed-length (e.g. Env.linen, Env.perc), the gate argument
439 is used as a simple trigger. If it is an sustaining envelope
440 (e.g. Env.adsr, Env.asr), the envelope is held open until the
441 gate becomes 0, at which point is released.
443 If strong::gate:: < 0, force release with time code:: -1.0 - gate ::, see link::#forced_release:: below.
448 Scales the durations of the segments.
452 { Blip.ar(50, 200, Env.perc(1, 0.1, 0.2).kr(2)) }.play;
456 Env({ exprand(3, 2000.0) } ! 18, 0.2, \exp).kr,
458 Env({ rrand(0.1, 0.2) } ! 18 ++ 0, 0.2).kr(2))
464 Blend two envelopes. Returns a new Env. See link::#blend:: example below.
466 argument::argAnotherEnv
469 argument::argBlendFrac
470 a number from zero to one.
473 Returns a new Env based on the receiver in which the start value will be held for strong::delay:: number of seconds.
476 The amount of time to delay the start of the envelope.
480 a = Env.perc(0.05, 1, 1, -4);
485 a = Env([0.5, 1, 0], [1, 1]).plot;
490 Set the total duration of times, by stretching them.
493 e = Env([0, 1, 0], [1, 2]);
500 circle from end to beginning over the time specified, with the curve specified. See also the class method link::#*circle::
507 Env([6000, 700, 100], [1, 1], ['exp', 'lin']).circle.postcs)
509 + Impulse.ar(1) }.play;
515 Env([6000, 700, 100], [1, 1], ['exp', 'lin']).circle(1).postcs,
518 + Impulse.ar(1) }.play;
523 Test the envelope on the default link::Classes/Server:: with a link::Classes/SinOsc::.
525 argument::releaseTime
526 If this is a sustaining envelope, it will be released after this much time in seconds. The default is 3 seconds.
529 Plot this envelope's shape in a window.
532 The size of the plot. The default is 400.
535 the size of the plot window.
538 the minimum value in the plot. Defaults to the lowest value in the data.
541 the maximum value in the plot. Defaults to the highest value in the data.
544 a window to place the plot in. If nil, one will be created for you.
547 Returns a link::Classes/Signal:: of size strong::length:: created by sampling this Env at strong::length:: number of intervals. If the envelope has multiple channels (see link::#multichannel expansion::), this method returns an array of signals.
550 Converts the Env to an link::Classes/Array:: in a specially ordered format. This allows for Env parameters to be settable arguments in a SynthDef. See example below under link::#newClear::.
552 method::asMultichannelArray
553 Converts the Env to an link::Classes/Array:: in a specially ordered format, like link::#asArray::, however it always returns an array of these data sets, corresponding to the number of channels of the envelope.
556 Returns true if this is a sustaining envelope, false otherwise.
558 method::range, exprange
559 Returns a copy of the Env whose levels have been mapped onto the given linear or exponential range.
564 a.range(42, 45).levels;
565 a.exprange(42, 45).levels;
568 // Mapping an Env to an exponential frequency range:
570 SinOsc.ar(EnvGen.ar(Env.perc(0.01, 0.7).exprange(40, 10000), doneAction: 2)) * 0.2;
575 subsection::Client-side Access and Stream Support
576 Sustain and loop settings have no effect in the methods below.
579 Returns the value of the Env at strong::time::. If the envelope has multiple channels, this method returns an array of levels.
582 A number or an array of numbers to specify a cut in the envelope. If time is an array, it returns the corresponding levels of each time value, and if the envelope has multiple channels, it returns an array of values. A combination of both returns a two-dimensional array.
586 e = Env.triangle(1, 1);
590 e = Env([1, [1, 2, 3], 1], [1, 1], \exp);
596 e = Env([1, 100, 1], [1, [1, 2, 3]], \exp);
604 method::embedInStream
605 Embeds this Env within an enclosing link::Classes/Stream::. Timing is derived from code::thisThread.beats::.
608 Creates a Routine and embeds the Env in it. This allows the Env to function as a link::Classes/Stream::.
613 e = Env.sine.asStream;
624 s.boot; //.test below will run a synthesis example
625 // to demonstrate the envelope, so the Server must be on
627 // different shaped segments: .plot graphs the Env
628 Env.new([0,1, 0.3, 0.8, 0], [2, 3, 1, 4],'linear').test.plot;
629 Env.new([0.001, 1, 0.3, 0.8, 0.001], [2, 3, 1, 4],'exponential').test.plot;
630 Env.new([0, 1, 0.3, 0.8, 0], [2, 3, 1, 4],\sine).test.plot;
631 Env.new([0.001, 1, 0.3, 0.8, 0.001],[2,3,1,4],\welch).test.plot;
632 Env.new([0, 1, 0.3, 0.8, 0], [2, 3, 1, 4],'step').test.plot;
633 Env.new([0, 1, 0.3, 0.8, 0], [2, 3, 1, 4], -2).test.plot;
634 Env.new([0, 1, 0.3, 0.8, 0], [2, 3, 1, 4], 2).test.plot;
635 Env.new([0, 1, 0.3, 0.8, 0], [2, 3, 1, 4], [0, 3, -3, -1]).test.plot;
638 If a release node is given, and the gate input of the EnvGen is set to zero, it outputs the nodes after the release node:
641 // release node is node 1; takes 0.5 seconds to go from 0 to 1,
642 // sustains at level of 1, then released after three seconds
643 // (test causes the release after three seconds, given the argument 3),
644 // taking 2 seconds to finish
645 Env.new([0,1,0],[0.5,2],'linear',1).test(3).plot
647 // more complex examples
648 // release node is node 2; releases after 5 sec
649 Env.new([0.001,1,0.3,0.8,0.001],[2,3,1,4] * 0.2, 2, 2).test(5).plot;
650 Env.new([0.001,1,0.3,0.8,0.5,0.8,0],[2,3,1,2,2,1] * 0.2, 2, 2).test(5).plot;
652 // early release: goes straight onto the release node after 0.1 seconds
653 Env.new([0.001,1,0.3,0.8,0.5,0.8,0],[2,3,1,2,2,1] * 0.2, 2, 2).test(0.1).plot;
656 If a loop node is given, the EnvGen outputs the nodes between the loop node and the release node (not including the release node itself) until it is released:
659 // release node is node 2, loop node is node 0: so loops around nodes 0 (lvl 1, dur 0.5)
660 // and 1 (lvl 0.1, dur 0.5) //until released after 3.5 seconds
661 Env.new([0,1,0.1,0],[0.5,0.5,2], 'lin', 2, 0).test(3.5).plot;
663 // this just sustains at node 0, because there is no other node to loop around!
664 Env.new([0,1,0],[0.5,2], 'lin', 1, 0).test(3.5).plot;
666 // more complex example: release node is node 3, loop node is node 1
667 Env.new([0.001,1,0.3,0.8,0.5,0.8,0],[2,1,1,2,3,1] * 0.1, 'lin', 3, 1).test(3).plot;
671 The starting level for an envelope segment is always the level you are at right now. For example when the gate is released and you jump to the release segment, the level does not jump to the level at the beginning of the release segment, it changes from whatever the current level is to the goal level of the release segment over the specified duration of the release segment.
673 There is an extra level at the beginning of the envelope to set the initial level. After that each node is a goal level and a duration, so node zero has duration equal to times[0] and goal level equal to levels[1].
675 The loop jumps back to the loop node. The endpoint of that segment is the goal level for that segment and the duration of that segment will be the time over which the level changed from the current level to the goal level.
681 SynthDef(\help_Env_newClear, { |out = 0|
683 // make an empty 4 segment envelope
684 env = Env.newClear(4);
685 // create a control argument array
686 envctl = \env.kr(env.asArray);
687 Out.ar(out, SinOsc.ar(EnvGen.kr(envctl, \gate.tr), 0, 0.3)); // the gate control is a trigger
691 Synth(\help_Env_newClear, [\gate, 1, \env, Env([700,900,900,800], [1,1,1], \exp)]); // 3 segments
693 // reset then play again:
694 Synth(\help_Env_newClear, [\gate, 1, \env, Env({ rrand(60, 70).midicps } ! 4, [1,1,1], \exp)]);
696 // the same written as an event:
697 (instrument: \help_Env_newClear, gate: 1, env: Env({ rrand(60, 70).midicps } ! 4, [1,1,1], \exp)).play;
701 note:: The above technique currently doesn't work with multichannel expanded envelopes (See: link::#multichannel expansion::)::
707 a = Env([0, 0.2, 1, 0.2, 0.2, 0], [0.5, 0.01, 0.01, 0.3, 0.2]);
710 b = Env([0, 0.4, 1, 0.2, 0.5, 0], [0.05, 0.4, [0.01, 0.1], 0.1, 0.4]);
717 blend(a, b, u).test.plot;
719 Window.allWindows.pop.close; // close last opened window
724 // blend in a SynthDef
726 SynthDef(\help_EnvBlend, { | factor = 0 |
727 Out.ar(0, EnvGen.kr(blend(Env.perc, Env.sine, factor), 1.0, doneAction: 2)
728 * SinOsc.ar(440,0,0.1)
735 var factors = (0, 0.1..1);
736 factors.do {|f| Synth(\help_EnvBlend, [\factor, f.postln]); 1.wait };