2 declare name "subtractive -- saw wave filtered with resonant lowpass";
3 declare author "Albert Graef";
10 // master volume and pan
11 vol = hslider("vol", 0.3, 0, 10, 0.01); // %
12 pan = hslider("pan", 0.5, 0, 1, 0.01); // %
15 attack = hslider("attack", 0.01, 0, 1, 0.001); // sec
16 decay = hslider("decay", 0.3, 0, 1, 0.001); // sec
17 sustain = hslider("sustain", 0.5, 0, 1, 0.01); // %
18 release = hslider("release", 0.2, 0, 1, 0.001); // sec
21 res = hslider("resonance (dB)", 3, 0, 20, 0.1);
22 cutoff = hslider("cutoff (harmonic)", 6, 1, 20, 0.1);
25 freq = nentry("freq", 440, 20, 20000, 1); // Hz
26 gain = nentry("gain", 1, 0, 10, 0.01); // %
27 gate = button("gate"); // 0/1
29 // generic table-driven oscillator with phase modulation
31 // n = the size of the table, must be a power of 2
32 // f = the wave function, must be defined on the range [0,2*PI]
33 // freq = the desired frequency in Hz
34 // mod = the phase modulation signal, in radians
36 tblosc(n,f,freq,mod) = (1-d)*rdtable(n,waveform,i&(n-1)) +
37 d*rdtable(n,waveform,(i+1)&(n-1))
39 waveform = time*(2.0*PI)/n : f;
40 phase = freq/SR : (+ : decimal) ~ _;
41 modphase = decimal(phase+mod/(2*PI))*n;
42 i = int(floor(modphase));
43 d = decimal(modphase);
48 // This is a tweaked Butterworth filter by David Werner and Patrice Tarrabia,
49 // see http://www.musicdsp.org and http://www.experimentalscene.com for
52 // res = resonance in dB above DC gain
53 // freq = cutoff frequency
55 lowpass(res,freq) = f : (+ ~ g) : *(a)
57 f(x) = a0*x+a1*x'+a2*x'';
59 a = 1/db2linear(0.5*res);
61 c = 1.0/tan(PI*(freq/SR));
63 r = 1/db2linear(2.0*res);
65 a0 = 1.0/(1.0+(q*c)+(c2));
72 // subtractive synth (saw wave passed through resonant lowpass)
76 smooth(c) = *(1-c) : +~*(c);
78 process = tblosc(1<<16, saw, freq, 0) : ((env,freq,_) : filter) :
79 *(env * (gain/*:smooth(0.999)*/))
80 : vgroup("3-master", *(vol) : panner(pan))
82 env = gate : vgroup("1-adsr", adsr(attack, decay, sustain, release));
84 = vgroup("2-filter", lowpass(env*res, fmax(1/cutoff, env)*freq*cutoff));