Debian Codegraph @

audio/system_compressor.dsp

raw
1 /* Copyright 2013 Kjetil S. Matheussen
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License
5 as published by the Free Software Foundation; either version 2
6 of the License, or (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17
18 // The functions compression_gain_mono, compressor_stereo, and compressor_stereo_demo
19 // are copied from effect.lib in the faust distribution.
20 //
21 // All three functions are written by Julius O. Smith III.
22 //
23 // They have been slightly modified and optimized by me.
24
25
26 import("filter.lib");
27 import("fast_log_exp.dsp");
28
29 effect = library("effect.lib");
30
31 def_ratio = 2.0;
32 def_threshold = -20.0;
33
34 def_attack = 50.148;
35 def_release = 100.237;
36
37 def_ingain = 0.0;
38 def_outgain = 0.0;
39
40 // ratio
41 ratio = hslider("[0] Ratio [style:slider] [tooltip: A compression Ratio of N means that for each N dB increase in input signal level above Threshold, the output level goes up 1 dB]",
42 def_ratio, 1, 20, 0.1);
43
44 // threshold
45 threshold = hslider("[1] Threshold [unit:dB] [style:slider] [tooltip: When the signal level exceeds the Threshold (in dB), its level is compressed according to the Ratio]",
46 def_threshold, -20, 20, 0.1);
47
48 // attack
49 attack = hslider("[2] Attack [unit:ms] [style:slider] [tooltip: Time constant in ms (1/e smoothing time) for the compression gain to approach (exponentially) a new lower target level (the compression `kicking in')]",
50 def_attack, 0, 500, 0.1)
51 : *(0.001)
52 : max(1/SR);
53
54 // release
55 release = hslider("[3] Release [unit:ms] [style: slider] [tooltip: Time constant in ms (1/e smoothing time) for the compression gain to approach (exponentially) a new higher target level (the compression 'releasing')]",
56 def_release, 0, 1000, 0.1)
57 : *(0.001)
58 : max(1/SR);
59
60 // ingain
61 ingain = hslider("[4] Input Gain [unit:dB] [tooltip: The compressed-signal output level is increased by this amount (in dB) to make up for the level lost due to compression]",
62 def_ingain, -40, 40, 0.1)
63 : db2linear;
64
65 // outgain
66 outgain = hslider("[5] Output Gain [unit:dB] [tooltip: The compressed-signal output level is increased by this amount (in dB) to make up for the level lost due to compression]",
67 def_outgain, -40, 40, 0.1)
68 : db2linear;
69
70 gainview2(i) = _ <: _,hbargraph("[7] [%i] Gakk",-50,10) : attach;
71
72
73
74 compression_gain_mono(ratio,thresh,att,rel) = _
75 : effect.amp_follower_ud(att,rel)
76 //: linear2db : outminusin_db(ratio,thresh) : kneesmooth(att) : db2linear
77 //: linear2db : outminusin_db(ratio,thresh) : db2linear
78 //: fasterlinear2db : outminusin_db(ratio,thresh) : kneesmooth(att) : fasterdb2linear
79 //: ll2_linear2db : outminusin_db(ratio,thresh) : kneesmooth(att) : ll2_db2linear
80
81 //: fasterlinear2db : gainview2(0) : outminusin_db(ratio,thresh) : gainview2(1) : kneesmooth(att) : fasterdb2linear
82 : ll2_linear2db : gainview2(0) : outminusin_db(ratio,thresh) : gainview2(1) : kneesmooth(att) : ll2_db2linear
83
84 //: quitefastlinear2db : outminusin_db(ratio,thresh) : kneesmooth(att) : quitefastdb2linear
85 //: linear2db_from_table : outminusin_db(ratio,thresh) : kneesmooth(att) : db2linear_from_table
86 //: outminusin_linear(ratio,thresh) : kneesmooth(att)
87 with {
88 // kneesmooth(att) installs a "knee" in the dynamic-range compression,
89 // where knee smoothness is set equal to half that of the compression-attack.
90 // A general 'knee' parameter could be used instead of tying it to att/2:
91 kneesmooth(att) = smooth(tau2pole(att/2.0));
92 // compression gain in dB:
93 outminusin_linear(ratio,thresh,level) = level*db2linear(thresh) * (1.0/float(ratio)-1.0);
94 outminusin_db(ratio,thresh,level) = max(level-thresh,0.0) * (1.0/float(ratio)-1.0);
95 // Note: "float(ratio)" REQUIRED when ratio is an integer > 1!
96 };
97
98 compressor_stereo(ratio,thresh,att,rel,x,y) = cgm*x, cgm*y with {
99 cgm = compression_gain_mono(ratio,thresh,att,rel,abs(x)+abs(y));
100 };
101
102 compressor_stereo_demo = compressor with{
103 compressor = (_,_)
104 //: *(ingain), *(ingain)
105 // : displaygain(0)
106 : compressor_stereo(ratio,threshold,attack,release)
107 // : displaygain(1)
108 : *(outgain), *(outgain)
109 // : displaygain(2)
110 ;
111 };
112
113 process = compressor_stereo_demo; //(x,0) : (_,!);
114