Add /usr/local/lib/faustine and /usr/lib/faustine to preprocessor import path.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / examples / rewriting / fold.dsp
1
2 // How to emulate Faust's seq, par, sum.
3 // x(k) is assumed to yield the kth signal.
4
5 xseq(1,x) = x(0);
6 xseq(n,x) = xseq(n-1,x) : x(n-1);
7
8 xpar(1,x) = x(0);
9 xpar(n,x) = xpar(n-1,x) , x(n-1);
10
11 xsum(1,x) = x(0);
12 xsum(n,x) = xsum(n-1,x) + x(n-1);
13
14 // These are all very similar. Abstracting
15 // on the binary "accumulator" function, we
16 // get the familiar fold(-left) function:
17
18 fold(1,f,x) = x(0);
19 fold(n,f,x) = f(fold(n-1,f,x),x(n-1));
20
21 // Now seq, par, sum can be defined as:
22
23 fseq(n) = fold(n,\(x,y).(x:y));
24 fpar(n) = fold(n,\(x,y).(x,y));
25 fsum(n) = fold(n,+);
26 fprod(n) = fold(n,*);
27
28 // Often it is more convenient to specify
29 // parameters as a Faust tuple. We can match
30 // against the (xs,x) pattern to decompose
31 // these.
32
33 vfold(f,(xs,x)) = f(vfold(f,xs),x);
34 vfold(f,x) = x;
35
36 // Tuple version of seq, par, sum:
37
38 vseq = vfold(\(x,y).(x:y));
39 vpar = vfold(\(x,y).(x,y));
40 vsum = vfold(+);
41 vprod = vfold(*);
42
43 // Examples:
44
45 import("music.lib");
46 f0 = 440;
47 a(0) = 1;
48 a(1) = 0.5;
49 a(2) = 0.3;
50 h(i) = a(i)*osc((i+1)*f0);
51
52 // sums
53 //process = xsum(3,h);
54 //process = fsum(3,h);
55 //process = vsum((h(0),h(1),h(2)));
56
57 reverse (x:y) = reverse(y):reverse(x);
58 reverse(x) = x;
59
60 // sequences from tuples (parallel -> serial)
61 process = reverse(vseq((sin,cos,tan)));