Completing basic operations in basic.ml for primitives float, And, Or, Xor, fmod...
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / tools / faust2pd / examples / synth / chimes.syn
1
2 /* A simple "chimes" patch, using a bank of resonz filters (cf. Steiglitz, DSP
3 Primer, pp. 89f.). */
4
5 declare name "chimes -- chimes synth using a bank of resonz filters";
6 declare author "Albert Graef";
7 declare version "2.0";
8
9 import("music.lib");
10
11 /* Control variables: */
12
13 // master volume, pan
14 vol = hslider("vol", 0.5, 0, 10, 0.01); // %
15 pan = hslider("pan", 0.5, 0, 1, 0.01); // %
16
17 // excitator and resonator parameters
18
19 // excitator decay time [sec]
20 xdecay = nentry("decay", 0.01, 0, 1, 0.001);
21
22 // resonator #0
23 hrm0 = nentry("harmonic0", 1, 0, 50, 0.001); // harmonic
24 amp0 = nentry("amplitude0", 0.167, 0, 1, 0.001); // amplitude
25 decay0 = nentry("decay0", 3.693, 0, 10, 0.001); // decay time
26 rq0 = nentry("rq0", 0.002, 0, 1, 0.0001); // filter 1/Q
27 // resonator #1
28 hrm1 = nentry("harmonic1", 3.007, 0, 50, 0.001); // harmonic
29 amp1 = nentry("amplitude1", 0.083, 0, 1, 0.001); // amplitude
30 decay1 = nentry("decay1", 2.248, 0, 10, 0.001); // decay time
31 rq1 = nentry("rq1", 0.002, 0, 1, 0.0001); // filter 1/Q
32 // resonator #2
33 hrm2 = nentry("harmonic2", 4.968, 0, 50, 0.001); // harmonic
34 amp2 = nentry("amplitude2", 0.087, 0, 1, 0.001); // amplitude
35 decay2 = nentry("decay2", 2.828, 0, 10, 0.001); // decay time
36 rq2 = nentry("rq2", 0.002, 0, 1, 0.0001); // filter 1/Q
37 // resonator #3
38 hrm3 = nentry("harmonic3", 8.994, 0, 50, 0.001); // harmonic
39 amp3 = nentry("amplitude3", 0.053, 0, 1, 0.001); // amplitude
40 decay3 = nentry("decay3", 3.364, 0, 10, 0.001); // decay time
41 rq3 = nentry("rq3", 0.002, 0, 1, 0.0001); // filter 1/Q
42 // resonator #4
43 hrm4 = nentry("harmonic4", 12.006, 0, 50, 0.001); // harmonic
44 amp4 = nentry("amplitude4", 0.053, 0, 1, 0.001); // amplitude
45 decay4 = nentry("decay4", 2.488, 0, 10, 0.001); // decay time
46 rq4 = nentry("rq4", 0.002, 0, 1, 0.0001); // filter 1/Q
47
48 // frequency, gain, gate
49 freq = nentry("freq", 440, 20, 20000, 1); // Hz
50 gain = nentry("gain", 1, 0, 10, 0.01); // %
51 gate = button("gate"); // 0/1
52
53 /* Definition of the resonz filter. This is basically a biquad filter with
54 pairs of poles near the desired resonance frequency and zeroes at -1 and
55 +1. See Steiglitz for details. */
56
57 resonz(R,freq) = f : (+ ~ g)
58 with {
59 f(x) = a*(x-x'); // feedforward function (two zeros)
60 g(y) = 2*R*c*y - R*R*y'; // feedback function (two poles)
61 w = 2*PI*freq/SR; // freq in rad per sample period
62 c = 2*R/(1+R*R)*cos(w); // cosine of pole angle
63 s = sqrt (1-c*c); // sine of pole angle
64 a = (1-R*R)*s; // factor to normalize resonance
65 };
66
67 /* The excitator, a short burst of noise. */
68
69 excitator(t) = t : hgroup("1-excitator", adsr(0, xdecay, 0, 0) : *(noise));
70
71 /* Bank of 5 resonators. */
72
73 resonator(f,t,i,hrm,amp,decay,rq)
74 = (f,t,_) : hgroup("2-resonator-%i", g)
75 with {
76 g(f,t) = resonz(R,h)*(amp*b*env)
77 with {
78 h = hrm*f; // harmonic
79 B = rq*f/SR; // bandwidth, as fraction of sample rate
80 R = 1-PI*B; // resonance (pole radius)
81 b = 1/(2*B); // boost factor = Nyquist/bandwidth
82 env = adsr(0, decay, 0, 0, t); // envelop
83 };
84 };
85
86 resonators(f,t) = resonator(f,t,0,hrm0,amp0,decay0,rq0)
87 + resonator(f,t,1,hrm1,amp1,decay1,rq1)
88 + resonator(f,t,2,hrm2,amp2,decay2,rq2)
89 + resonator(f,t,3,hrm3,amp3,decay3,rq3)
90 + resonator(f,t,4,hrm4,amp4,decay4,rq4);
91
92 /* The synth. */
93
94 process = excitator(gate)*gain <: resonators(freq, gate)
95 : vgroup("3-master", *(vol) : panner(pan));