-
Notifications
You must be signed in to change notification settings - Fork 63
/
envelopes.lib
666 lines (571 loc) · 21.4 KB
/
envelopes.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//################################ envelopes.lib ##########################################
// This library contains a collection of envelope generators. Its official prefix is `en`.
//
// #### References
// * <https://github.com/grame-cncm/faustlibraries/blob/master/envelopes.lib>
//########################################################################################
/************************************************************************
************************************************************************
FAUST library file, GRAME section
Except where noted otherwise, Copyright (C) 2003-2017 by GRAME,
Centre National de Creation Musicale.
----------------------------------------------------------------------
GRAME LICENSE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
EXCEPTION TO THE LGPL LICENSE : As a special exception, you may create a
larger FAUST program which directly or indirectly imports this library
file and still distribute the compiled code generated by the FAUST
compiler, or a modified version of this compiled code, under your own
copyright and license. This EXCEPTION TO THE LGPL LICENSE explicitly
grants you the right to freely choose the license for the resulting
compiled code. In particular the resulting compiled code has no obligation
to be LGPL or GPL. For example you are free to choose a commercial or
closed source license or any other license if you decide so.
************************************************************************
************************************************************************/
aa = library("aanl.lib");
ma = library("maths.lib");
ba = library("basics.lib");
si = library("signals.lib");
it = library("interpolators.lib");
declare name "Faust Envelope Library";
declare version "1.3.0";
declare author "GRAME";
declare copyright "GRAME";
declare license "LGPL with exception";
//=============================Functions Reference========================================
//========================================================================================
//-----------------------`(en.)ar`--------------------------
// AR (Attack, Release) envelope generator (useful to create percussion envelopes).
// `ar` is a standard Faust function.
//
// #### Usage
//
// ```
// ar(at,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `rt`: release (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare ar author "Yann Orlarey, Stéphane Letz";
ar(at,rt,gate) = AR : max(0)
with {
// Durations in samples
an = max(1, at*ma.SR);
rn = max(1, rt*ma.SR);
// Deltas per samples
adelta = 1/an;
rdelta = 1/rn;
// Attack time (starts at gate upfront and raises infinitely)
atime = (raise*reset + upfront) ~ _
with {
upfront = gate > gate';
reset = gate <= gate';
raise(x) = (x + (x > 0));
};
// Attack curve
A = atime * adelta;
// Release curve
D0 = 1 + an * rdelta;
D = D0 - atime * rdelta;
// AR part
AR = min(A, D);
};
//------------------------`(en.)asr`----------------------
// ASR (Attack, Sustain, Release) envelope generator.
// `asr` is a standard Faust function.
//
// #### Usage
//
// ```
// asr(at,sl,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare asr author "Yann Orlarey, Stéphane Letz";
asr(at,sl,rt,gate) = AS - R : max(0)
with {
// Durations in samples
an = max(1, at*ma.SR);
rn = max(1, rt*ma.SR);
// Deltas per samples
adelta = sl/an;
rdelta = sl/rn;
// Attack time (starts when gate changes and raises until gate == 0)
atime = +(gate) ~ *(gate' >= gate);
// Attack curve
A = atime * adelta;
// AS part
AS = min(A, sl);
// Release time starts when gate is 0
rtime = (+(1) : *(gate == 0)) ~ _;
// Release curve starts when gate is 0 with the current value of the envelope
R = rtime * rdelta;
};
//------------------------`(en.)adsr`----------------------
// ADSR (Attack, Decay, Sustain, Release) envelope generator.
// `adsr` is a standard Faust function.
//
// #### Usage
//
// ```
// adsr(at,dt,sl,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack time (sec)
// * `dt`: decay time (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release time (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare adsr author "Yann Orlarey and Andrey Bundin";
adsr(at,dt,sl,rt,gate) = ADS : *(1-R) : max(0)
with {
// Durations in samples
an = max(1, at*ma.SR);
dn = max(1, dt*ma.SR);
rn = max(1, rt*ma.SR);
// Deltas per samples
adelta = 1/an;
ddelta = (1-sl)/dn;
// Attack time (starts when gate changes and raises until gate == 0)
atime = +(gate) ~ *(gate' >= gate);
// Attack curve
A = atime * adelta;
// Decay curve
D0 = 1 + an * ddelta;
D = D0 - atime * ddelta;
// ADS part
ADS = min(A, max(D, sl));
// Release time starts when gate is 0
rtime = (+(1) : *(gate == 0)) ~ _;
// Release curve starts when gate is 0 with the current value of the envelope
R = rtime/rn;
};
adsr_bias_env = environment
{
// In the functions below, we use the equation `y=bias_curve(b,x)`.
// `b`: bias between 0 and 1. Bias of 0.5 results in `y=x`. Bias above 0.5 pulls y upward.
// `x`: input between 0 and 1 that needs to be remapped/biased into `y`
// `y`: `x` after it has been biased. Note that `(y==0 iff x==0) AND (y==1 iff x==1)`.
bias_curve(b,x) = (x / ((((1.0/b) - 2.0)*(1.0 - x))+1.0));
// d/dx of bias_curve(b,x)
bias_curve_d_dx(b, x) = 0-(-1+b)*b/((1-x+b*(-1+2*x))^2);
// Solve for x in y=bias_curve(b,x)
bias_curve_inverse(b,y) = (b-1)*y / (2*b*y-b-y);
// We don't allow the bias to be too close to 0 or 1 because it leads to slopes too
// close to positive or negative infinity.
bias_clip = aa.clip(.03, .97);
adsr_bias(att, dec, sus, rel, final, bias_att, bias_dec, bias_rel, legato, gate) = envelope
with {
ugate = gate>0;
fb(_state, _y) = nextState, nextY
with {
// Legato control
onset = ba.impulsify(ugate);
state = select2(onset, _state, 0), _state : select2(legato);
// two conditions in which we want to hard-reset the `y` to `final`
// instead of using the previous `_y`.
y = ba.if((onset & (legato<0.5)) | (ba.time==0), final, _y);
// note that if final is a constant zero then this could simply be
// y = ba.if(onset & (legato<0.5), 0, _y);
// State 0: release
// State 1: attack
// State 2: decay
y_at_release = ba.if(ba.time==0,1,y) : ba.latch(gate==0);
// note that if final is a constant zero then this could simply be
// y_at_release = y : ba.latch(gate==0);
// Slope is a y-distance divided by a number of samples
att_slope = (1-final) / max(1,(att*ma.SR));
dec_slope = (sus-1) / max(1,(dec*ma.SR));
rel_slope = (final-y_at_release) / max(1,(rel*ma.SR));
// Get bias based on the state, then clip for safety.
b = bias_rel, bias_att, bias_dec : select3(state) : bias_clip;
// We will remap from an input domain to [0..1] based on the current state.
from1 = ba.if(state==2, sus, final);
from2 = ba.if(state==0, y_at_release, 1);
// Prevent divide-by-zero in it.remap
pct = it.remap(from1, from2, 0, 1, y), 0.5 : select2(from1==from2) : bias_curve_inverse(b);
slope = rel_slope, att_slope, dec_slope : select3(state) : _*bias_curve_d_dx(b, pct);
nextY = y + slope : aa.clip(from1, 1);
nextState = select2(ugate,
0,
select3(state,
1,
select2(y < 1.0, 2, 1),
2
)
);
};
envelope = fb ~ (_,_) : !, _;
};
ahdsr_bias(att, hol, dec, sus, rel, final, bias_att, bias_dec, bias_rel, legato, gate) = envelope
with {
ugate = gate>0;
fb(_state, _y) = nextState, nextY
with {
// Legato control
onset = ba.impulsify(ugate);
state = select2(onset, _state, 0), _state : select2(legato);
// two conditions in which we want to hard-reset the `y` to `final`
// instead of using the previous `_y`.
y = ba.if((onset & (legato<0.5)) | (ba.time==0), final, _y);
// note that if final is a constant zero then this could simply be
// y = ba.if(onset & (legato<0.5), 0, _y);
// State 0: release
// State 1: attack
// State 2: hold
// State 3: decay
y_at_release = ba.if(ba.time==0,1,y) : ba.latch(gate==0);
// note that if final is a constant zero then this could simply be
// y_at_release = y : ba.latch(gate==0);
// Slope is a y-distance divided by a number of samples
att_slope = (1-final) / max(1,(att*ma.SR));
hold_slope = ba.if(gate, att_slope, rel_slope);
dec_slope = (sus-1) / max(1,(dec*ma.SR));
rel_slope = (final-y_at_release) / max(1,(rel*ma.SR));
// Get bias based on the state, then clip for safety.
// Note that for the hold state, we choose a bias of 0.5 (the middle value).
b = bias_rel, bias_att, .5, bias_dec : ba.selectn(4, state) : bias_clip;
from1 = ba.if(state==3, sus, final);
from2 = ba.if(state==0, y_at_release, 1);
// Prevent divide-by-zero in it.remap
pct = it.remap(from1, from2, 0, 1, y), 0.5 : select2(from1==from2) : bias_curve_inverse(b);
slope = rel_slope, att_slope, hold_slope, dec_slope : ba.selectn(4, state) : _*bias_curve_d_dx(b, pct);
hold_time = ma.SR * hol;
hold_timer = ugate : +~(*(ugate * (state != 1)));
nextY = y + slope : aa.clip(from1, 1);
nextState = select2(ugate,
0,
ba.selectn(4, state,
1,
select2(y < 1.0, 2, 1),
select2(hold_timer < hold_time, 3, 2),
3
)
);
};
envelope = fb ~ (_,_) : !, _;
};
};
//------------------------`(en.)adsrf_bias`------------------------------
// ADSR (Attack, Decay, Sustain, Release, Final) envelope generator with
// control over bias on each segment, and toggle for legato.
//
// #### Usage
//
// ```
// adsrf_bias(at,dt,sl,rt,final,b_att,b_dec,b_rel,legato,t) : _
// ```
//
// Where:
//
// * `at`: attack time (sec)
// * `dt`: decay time (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release time (sec)
// * `final`: final level (between 0..1) but less than or equal to `sl`
// * `b_att`: bias during attack (between 0..1) where 0.5 is no bias.
// * `b_dec`: bias during decay (between 0..1) where 0.5 is no bias.
// * `b_rel`: bias during release (between 0..1) where 0.5 is no bias.
// * `legato`: toggle for legato. If disabled, envelopes "re-trigger" from zero.
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-------------------------------------------------------------------------------
declare adsrf_bias author "Andrew John March and David Braun";
declare adsrf_bias licence "STK-4.3";
adsrf_bias(att, dec, sus, rel, final, bias_att, bias_dec, bias_rel, legato, gate) = adsr_bias_env.adsr_bias(att, dec, sus, rel, final, bias_att, bias_dec, bias_rel, legato, gate);
//------------------------`(en.)adsr_bias`------------------------------
// ADSR (Attack, Decay, Sustain, Release) envelope generator with
// control over bias on each segment, and toggle for legato.
//
// #### Usage
//
// ```
// adsr_bias(at,dt,sl,rt,b_att,b_dec,b_rel,legato,t) : _
// ```
//
// Where:
//
// * `at`: attack time (sec)
// * `dt`: decay time (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release time (sec)
// * `b_att`: bias during attack (between 0..1) where 0.5 is no bias.
// * `b_dec`: bias during decay (between 0..1) where 0.5 is no bias.
// * `b_rel`: bias during release (between 0..1) where 0.5 is no bias.
// * `legato`: toggle for legato. If disabled, envelopes "re-trigger" from zero.
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-------------------------------------------------------------------------------
declare adsr_bias author "Andrew John March and David Braun";
declare adsr_bias licence "STK-4.3";
adsr_bias(att, dec, sus, rel, bias_att, bias_dec, bias_rel, legato, gate) = adsr_bias_env.adsr_bias(att, dec, sus, rel, 0, bias_att, bias_dec, bias_rel, legato, gate);
//------------------------`(en.)ahdsrf_bias`---------------------------
// AHDSR (Attack, Hold, Decay, Sustain, Release, Final) envelope generator
// with control over bias on each segment, and toggle for legato.
//
// #### Usage
//
// ```
// ahdsrf_bias(at,ht,dt,sl,rt,final,b_att,b_dec,b_rel,legato,t) : _
// ```
//
// Where:
//
// * `at`: attack time (sec)
// * `ht`: hold time (sec)
// * `dt`: decay time (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release time (sec)
// * `final`: final level (between 0..1) but less than or equal to `sl`
// * `b_att`: bias during attack (between 0..1) where 0.5 is no bias.
// * `b_dec`: bias during decay (between 0..1) where 0.5 is no bias.
// * `b_rel`: bias during release (between 0..1) where 0.5 is no bias.
// * `legato`: toggle for legato. If disabled, envelopes "re-trigger" from zero.
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//---------------------------------------------------------------------
declare ahdsrf_bias author "Andrew John March and David Braun";
declare ahdsrf_bias licence "STK-4.3";
ahdsrf_bias(att, hol, dec, sus, rel, final, bias_att, bias_dec, bias_rel, legato, gate) = adsr_bias_env.ahdsr_bias(att, hol, dec, sus, rel, final, bias_att, bias_dec, bias_rel, legato, gate);
//------------------------`(en.)ahdsr_bias`---------------------------
// AHDSR (Attack, Hold, Decay, Sustain, Release) envelope generator
// with control over bias on each segment, and toggle for legato.
//
// #### Usage
//
// ```
// ahdsr_bias(at,ht,dt,sl,rt,final,b_att,b_dec,b_rel,legato,t) : _
// ```
//
// Where:
//
// * `at`: attack time (sec)
// * `ht`: hold time (sec)
// * `dt`: decay time (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release time (sec)
// * `final`: final level (between 0..1) but less than or equal to `sl`
// * `b_att`: bias during attack (between 0..1) where 0.5 is no bias.
// * `b_dec`: bias during decay (between 0..1) where 0.5 is no bias.
// * `b_rel`: bias during release (between 0..1) where 0.5 is no bias.
// * `legato`: toggle for legato. If disabled, envelopes "re-trigger" from zero.
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//---------------------------------------------------------------------
declare ahdsr_bias author "Andrew John March and David Braun";
declare ahdsr_bias licence "STK-4.3";
ahdsr_bias(att, hol, dec, sus, rel, bias_att, bias_dec, bias_rel, legato, gate) = adsr_bias_env.ahdsr_bias(att, hol, dec, sus, rel, 0, bias_att, bias_dec, bias_rel, legato, gate);
//------------------------`(en.)smoothEnvelope`------------------------
// An envelope with an exponential attack and release.
// `smoothEnvelope` is a standard Faust function.
//
// #### Usage
//
// ```
// smoothEnvelope(ar,t) : _
// ```
//
// * `ar`: attack and release duration (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//----------------------------------------------------------------
declare smoothEnvelope author "Romain Michon";
smoothEnvelope(ar,t) = t : si.smooth(ba.tau2pole(ar));
// Generic form to be specialized
// Author: JOS, revised by Stephane Letz
asrfe(attT60,susLvl,relT60,finLvl,gate) = envelope
with {
ugate = gate>0;
target = select2(ugate, finLvl, susLvl*float(ugate));
t60 = select2(ugate, relT60, attT60);
pole = ba.tau2pole(t60/6.91);
envelope = target : si.smooth(pole);
};
//------------------------`(en.)arfe`----------------------
// ARFE (Attack and Release-to-Final-value Exponentially) envelope generator.
// Approximately equal to `smoothEnvelope(Attack/6.91)` when Attack == Release.
//
// #### Usage
//
// ```
// arfe(at,rt,fl,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `rt`: release (sec)
// * `fl`: final level to approach upon release (such as 0)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare arfe author "Julius O. Smith III, revised by Stephane Letz";
declare arfe licence "STK-4.3";
arfe(attT60,relT60,fv,gate) = asrfe(attT60,1.0,relT60,fv,gate);
//------------------------`(en.)are`----------------------
// ARE (Attack, Release) envelope generator with Exponential segments.
// Approximately equal to `smoothEnvelope(Attack/6.91)` when Attack == Release.
//
// #### Usage
//
// ```
// are(at,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `rt`: release (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare are author "Julius O. Smith III, revised by Stephane Letz";
declare are licence "STK-4.3";
are(attT60,relT60,gate) = asrfe(attT60,1.0,relT60,0.0,gate);
//------------------------`(en.)asre`----------------------
// ASRE (Attack, Sustain, Release) envelope generator with Exponential segments.
//
// #### Usage
//
// ```
// asre(at,sl,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare asre author "Julius O. Smith III, revised by Stephane Letz";
declare asre licence "STK-4.3";
asre(attT60,susLvl,relT60,gate) = asrfe(attT60,susLvl,relT60,0.0,gate);
//------------------------`(en.)adsre`----------------------
// ADSRE (Attack, Decay, Sustain, Release) envelope generator with Exponential
// segments.
//
// #### Usage
//
// ```
// adsre(at,dt,sl,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `dt`: decay (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare adsre author "Julius O. Smith III";
declare adsre licence "STK-4.3";
adsre(attT60,decT60,susLvl,relT60,gate) = envelope
with {
ugate = gate>0;
samps = ugate : +~(*(ugate)); // ramp time in samples
attSamps = int(attT60 * ma.SR);
// if attSamps==0, go straight into the decay phase
attPhase = (samps<attSamps) | (ugate:ba.impulsify);
target = select2(ugate, 0.0,
select2(attPhase, susLvl*float(ugate), ugate));
t60 = select2(ugate, relT60, select2(attPhase, decT60, attT60));
pole = ba.tau2pole(t60/6.91);
envelope = target : si.smooth(pole);
};
//------------------------`(en.)ahdsre`----------------------
// AHDSRE (Attack, Hold, Decay, Sustain, Release) envelope generator with Exponential
// segments.
//
// #### Usage
//
// ```
// ahdsre(at,ht,dt,sl,rt,t) : _
// ```
//
// Where:
//
// * `at`: attack (sec)
// * `ht`: hold (sec)
// * `dt`: decay (sec)
// * `sl`: sustain level (between 0..1)
// * `rt`: release (sec)
// * `t`: trigger signal (attack is triggered when `t>0`, release is triggered
// when `t=0`)
//-----------------------------------------------------
declare ahdsre author "Julius O. Smith III, David Braun";
declare ahdsre licence "STK-4.3";
ahdsre(attT60,htT60,decT60,susLvl,relT60,gate) = envelope
with {
ugate = gate>0;
samps = ugate : +~(*(ugate)); // ramp time in samples
attSamps = int(attT60 * ma.SR);
holdSamps = int((attT60+htT60)*ma.SR);
attPhase = (samps<attSamps) | (ugate:ba.impulsify);
holdPhase = (samps<holdSamps) & ugate;
target = select2(ugate, 0.0,
select2(holdPhase, susLvl*float(ugate), ugate));
t60 = select2(ugate, relT60, select2(holdPhase, decT60, attT60));
pole = ba.tau2pole(t60/6.91);
envelope = target : si.smooth(pole);
};
//----------------------`(en.)dx7envelope`----------------------
// DX7 operator envelope generator with 4 independent rates and levels. It is
// essentially a 4 points BPF.
//
// #### Usage
//
// ```
// dx7_envelope(R1,R2,R3,R4,L1,L2,L3,L4,t) : _
// ```
//
// Where:
//
// * `RN`: rates in seconds
// * `LN`: levels (0-1)
// * `t`: trigger signal
//-----------------------------------------------------
declare dx7envelope author "Romain Michon";
dx7envelope(R1,R2,R3,R4,L1,L2,L3,L4,t) = up*on : ba.sAndH(on) : down
with {
on = t>0;
off = t==0;
rs1 = R1*ma.SR;
rs2 = R2*ma.SR;
rs3 = R3*ma.SR;
rs4 = R4*ma.SR;
up = ba.countup(rs1+rs2+rs3,off) : ba.bpf.start(0,L4) : ba.bpf.point(rs1,L1) :
ba.bpf.point(rs1+rs2,L2) : ba.bpf.end(rs1+rs2+rs3,L3);
down = _,ba.countup(rs4,on) : ba.bpf.start(0) : ba.bpf.end(rs4,L4);
};