2
/* Stereo amplifier stage with bass, treble, gain and balance controls and a
5
declare name "amp -- stereo amplifier stage";
6
declare author "Albert Graef";
12
/* Fixed bass and treble frequencies. You might want to tune these for your
18
/* Bass and treble gain controls in dB. The range of +/-20 corresponds to a
19
boost/cut factor of 10. */
21
bass_gain = nentry("bass", 0, -20, 20, 0.1);
22
treble_gain = nentry("treble", 0, -20, 20, 0.1);
24
/* Gain and balance controls. */
26
gain = db2linear(nentry("gain", 0, -96, 96, 0.1));
27
bal = hslider("balance", 0, -1, 1, 0.001);
29
/* Balance a stereo signal by attenuating the left channel if balance is on
30
the right and vice versa. I found that a linear control works best here. */
32
balance = *(1-max(0,bal)), *(1-max(0,0-bal));
34
/* Generic biquad filter. */
36
filter(b0,b1,b2,a0,a1,a2) = f : (+ ~ g)
38
f(x) = (b0/a0)*x+(b1/a0)*x'+(b2/a0)*x'';
39
g(y) = 0-(a1/a0)*y-(a2/a0)*y';
42
/* Low and high shelf filters, straight from Robert Bristow-Johnson's "Audio
43
EQ Cookbook", see http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt. f0
44
is the shelf midpoint frequency, g the desired gain in dB. S is the shelf
45
slope parameter, we always set that to 1 here. */
47
low_shelf(f0,g) = filter(b0,b1,b2,a0,a1,a2)
52
alpha = sin(w0)/2 * sqrt( (A + 1/A)*(1/S - 1) + 2 );
54
b0 = A*( (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha );
55
b1 = 2*A*( (A-1) - (A+1)*cos(w0) );
56
b2 = A*( (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha );
57
a0 = (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha;
58
a1 = -2*( (A-1) + (A+1)*cos(w0) );
59
a2 = (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha;
62
high_shelf(f0,g) = filter(b0,b1,b2,a0,a1,a2)
67
alpha = sin(w0)/2 * sqrt( (A + 1/A)*(1/S - 1) + 2 );
69
b0 = A*( (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha );
70
b1 = -2*A*( (A-1) + (A+1)*cos(w0) );
71
b2 = A*( (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha );
72
a0 = (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha;
73
a1 = 2*( (A-1) - (A+1)*cos(w0) );
74
a2 = (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha;
77
/* The tone control. We simply run a low and a high shelf in series here. */
79
tone = low_shelf(bass_freq,bass_gain)
80
: high_shelf(treble_freq,treble_gain);
82
/* Envelop follower. This is basically a 1 pole LP with configurable attack/
83
release time. The result is converted to dB. You have to set the desired
84
attack/release time in seconds using the t parameter below. */
86
t = 0.1; // attack/release time in seconds
87
g = exp(-1/(SR*t)); // corresponding gain factor
89
env = abs : *(1-g) : + ~ *(g) : linear2db;
91
/* Use this if you want the RMS instead. Note that this doesn't really
92
calculate an RMS value (you'd need an FIR for that), but in practice our
93
simple 1 pole IIR filter works just as well. */
95
rms = sqr : *(1-g) : + ~ *(g) : sqrt : linear2db;
98
/* The dB meters for left and right channel. These are passive controls. */
100
left_meter(x) = attach(x, env(x) : hbargraph("left", -96, 10));
101
right_meter(x) = attach(x, env(x) : hbargraph("right", -96, 10));
103
/* The main program. */
105
process = hgroup("0-amp", hgroup("1-tone", tone, tone) :
106
hgroup("2-gain", (_*gain, _*gain)))
107
: vgroup("3-balance", balance)
108
: vgroup("4-meter", (left_meter, right_meter));