2 // How to emulate Faust's seq, par, sum.
3 // x(k) is assumed to yield the kth signal.
5 xseq(1,x) = x(0);
6 xseq(n,x) = xseq(n-1,x) : x(n-1);
8 xpar(1,x) = x(0);
9 xpar(n,x) = xpar(n-1,x) , x(n-1);
11 xsum(1,x) = x(0);
12 xsum(n,x) = xsum(n-1,x) + x(n-1);
14 // These are all very similar. Abstracting
15 // on the binary "accumulator" function, we
16 // get the familiar fold(-left) function:
18 fold(1,f,x) = x(0);
19 fold(n,f,x) = f(fold(n-1,f,x),x(n-1));
21 // Now seq, par, sum can be defined as:
23 fseq(n) = fold(n,\(x,y).(x:y));
24 fpar(n) = fold(n,\(x,y).(x,y));
25 fsum(n) = fold(n,+);
27 // Often it is more convenient to specify
28 // parameters as a Faust tuple. We can match
29 // against the (x,y) pattern to decompose
30 // these.
32 vfold(f,(x,y)) = f(vfold(f,x),y);
33 vfold(f,x) = x;
35 // Tuple version of seq, par, sum:
37 vseq = vfold(\(x,y).(x:y));
38 vpar = vfold(\(x,y).(x,y));
39 vsum = vfold(+);
41 // Example: sum of sinusoids.
43 import("music.lib");
45 f0 = 440;
46 a(0) = 1;
47 a(1) = 0.5;
48 a(2) = 0.3;
49 h(i) = a(i)*osc((i+1)*f0);
51 v = hslider("vol", 0.3, 0, 1, 0.01);
53 process = v*fsum(3,h);
54 //process = v*xsum(3,h);
55 //process = v*vsum((h(0),h(1),h(2)));