Initial import.
[Faustine.git] / interpretor / faust-0.9.47mr3 / tools / faust2pd / examples / synth / karplus.syn
1
2 declare name "karplus -- Karplus-Strong string synth";
3 declare author "Yann Orlarey";
4 declare version "1.0";
5
6 import("music.lib");
7
8 // control variables
9
10 // master volume and pan
11 vol = hslider("vol", -10, -96, 96, 0.1); // dB
12 pan = hslider("pan", 0, -1, 1, 0.01); // %
13
14 // excitator and resonator parameters
15 size = hslider("samples", 512, 1, 1024, 1); // #samples
16 dtime = hslider("decay time", 4, 0, 10, 0.01);// -60db decay time
17
18 // voice parameters
19 freq = nentry("freq", 440, 20, 20000, 1); // Hz
20 gain = nentry("gain", 1, 0, 10, 0.01); // %
21 gate = button("gate"); // 0/1
22 bend = hslider("pitch bend", 0, -2, 2, 0.01);// semitones
23
24 /* The excitator: */
25
26 upfront(x) = (x-x') > 0.0;
27 decay(n,x) = x - (x>0)/n;
28 release(n) = + ~ decay(n);
29 trigger(n) = upfront : release(n) : >(0.0) : +(leak);
30 leak = 1.0/65536.0; // avoid denormals on Pentium
31 excitator = trigger(size);
32
33 /* The resonator: */
34
35 average(x) = (x+x')/2;
36 att(d,t) = 1-1/pow(db2linear(60), d/(SR*t));
37 comb(d,a) = (+ : fdelay(4096, d-1.5)) ~ (average : *(1.0-a));
38 resonator(d) = comb(d,att(d,dtime));
39
40 /* DC blocker (see http://ccrma.stanford.edu/~jos/filters/DC_Blocker.html): */
41
42 dcblocker(x) = (x-x') : (+ ~ *(0.995));
43
44 /* Karplus-Strong string synthesizer: */
45
46 process = vgroup("1-excitator", noise*gain : *(gate : excitator))
47 : vgroup("2-resonator", resonator(SR/(freq*pow(2,bend/12))))
48 : dcblocker
49 : vgroup("3-master", *(db2linear(vol)) : panner((pan+1)/2));