59668844b576a74b0001bd2418e8028f0d664ba5
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / tools / faust2pd / examples / synth / expander.dsp
1
2 /* Expander unit. */
3
4 /* This is pretty much the same as compressor.dsp, but here the given ratio is
5 applied to *attenuate* levels *below* the threshold. */
6
7 declare name "expander -- expander/noise gate unit";
8 declare author "Albert Graef";
9 declare version "1.0";
10
11 import("music.lib");
12
13 /* Controls. */
14
15 exp_group(x) = hgroup("1-expander", x);
16 env_group(x) = vgroup("2-envelop", x);
17 gain_group(x) = vgroup("3-gain", x);
18
19 ratio = exp_group(nentry("ratio", 2, 1, 20, 0.1));
20 threshold = exp_group(nentry("threshold", -40, -96, 10, 0.1));
21 knee = exp_group(nentry("knee", 3, 0, 20, 0.1));
22
23 attack = env_group(hslider("attack", 0.001, 0, 1, 0.001)) : max(1/SR);
24 release = env_group(hslider("release", 0.1, 0, 10, 0.01)) : max(1/SR);
25
26 gain(x) = attach(x, x : gain_group(hbargraph("gain", -96, 0)));
27
28 t = 0.1;
29 g = exp(-1/(SR*t));
30 env = abs : *(1-g) : + ~ *(g);
31 rms = sqr : *(1-g) : + ~ *(g) : sqrt;
32 sqr(x) = x*x;
33
34 env2(x,y) = max(env(x),env(y));
35
36 expand(env) = level*(1-r)
37 with {
38 level = env : h ~ _ : linear2db : (threshold+knee-_) : max(0)
39 with {
40 h(x,y) = f*x+(1-f)*y with { f = (x<y)*ga+(x>=y)*gr; };
41 ga = exp(-1/(SR*attack));
42 gr = exp(-1/(SR*release));
43 };
44 p = level/(knee+eps) : max(0) : min(1) with { eps = 0.001; };
45 r = 1-p+p*ratio;
46 };
47
48 process(x,y) = (g*x,g*y)
49 with {
50 g = env2(x,y) : expand : gain : db2linear;
51 };