Cleaning the examples' Makefiles and octaves scripts.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / architecture / music.lib
1 /************************************************************************
2 ************************************************************************
3 FAUST library file
4 Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20 ************************************************************************
21 ************************************************************************/
22
23 declare name "Music Library";
24 declare author "GRAME";
25 declare copyright "GRAME";
26 declare version "1.0";
27 declare license "LGPL";
28
29 import("math.lib");
30
31 //-----------------------------------------------
32 // DELAY LINE
33 //-----------------------------------------------
34 frac(n) = n-int(n);
35 index(n) = &(n-1) ~ +(1); // n = 2**i
36 //delay(n,d,x) = rwtable(n, 0.0, index(n), x, (index(n)-int(d)) & (n-1));
37 delay(n,d,x) = x@(int(d)&(n-1));
38 fdelay(n,d,x) = delay(n,int(d),x)*(1 - frac(d)) + delay(n,int(d)+1,x)*frac(d);
39
40
41 delay1s(d) = delay(65536,d);
42 delay2s(d) = delay(131072,d);
43 delay5s(d) = delay(262144,d);
44 delay10s(d) = delay(524288,d);
45 delay21s(d) = delay(1048576,d);
46 delay43s(d) = delay(2097152,d);
47
48 fdelay1s(d) = fdelay(65536,d);
49 fdelay2s(d) = fdelay(131072,d);
50 fdelay5s(d) = fdelay(262144,d);
51 fdelay10s(d) = fdelay(524288,d);
52 fdelay21s(d) = fdelay(1048576,d);
53 fdelay43s(d) = fdelay(2097152,d);
54
55 millisec = SR/1000.0;
56
57 time1s = hslider("time", 0, 0, 1000, 0.1)*millisec;
58 time2s = hslider("time", 0, 0, 2000, 0.1)*millisec;
59 time5s = hslider("time", 0, 0, 5000, 0.1)*millisec;
60 time10s = hslider("time", 0, 0, 10000, 0.1)*millisec;
61 time21s = hslider("time", 0, 0, 21000, 0.1)*millisec;
62 time43s = hslider("time", 0, 0, 43000, 0.1)*millisec;
63
64
65 echo1s = vgroup("echo 1000", +~(delay(65536, int(hslider("millisecond", 0, 0, 1000, 0.10)*millisec)-1) * (hslider("feedback", 0, 0, 100, 0.1)/100.0)));
66 echo2s = vgroup("echo 2000", +~(delay(131072, int(hslider("millisecond", 0, 0, 2000, 0.25)*millisec)-1) * (hslider("feedback", 0, 0, 100, 0.1)/100.0)));
67 echo5s = vgroup("echo 5000", +~(delay(262144, int(hslider("millisecond", 0, 0, 5000, 0.50)*millisec)-1) * (hslider("feedback", 0, 0, 100, 0.1)/100.0)));
68 echo10s = vgroup("echo 10000", +~(delay(524288, int(hslider("millisecond", 0, 0, 10000, 1.00)*millisec)-1) * (hslider("feedback", 0, 0, 100, 0.1)/100.0)));
69 echo21s = vgroup("echo 21000", +~(delay(1048576, int(hslider("millisecond", 0, 0, 21000, 1.00)*millisec)-1) * (hslider("feedback", 0, 0, 100, 0.1)/100.0)));
70 echo43s = vgroup("echo 43000", +~(delay(2097152, int(hslider("millisecond", 0, 0, 43000, 1.00)*millisec)-1) * (hslider("feedback", 0, 0, 100, 0.1)/100.0)));
71
72
73 //--------------------------sdelay(N,it,dt)----------------------------
74 // s(mooth)delay : a mono delay that doesn't click and doesn't
75 // transpose when the delay time is changed. It takes 4 input signals
76 // and produces a delayed output signal
77 //
78 // USAGE : ... : sdelay(N,it,dt) : ...
79 //
80 // Where :
81 // <N> = maximal delay in samples (must be a constant power of 2, for example 65536)
82 // <it> = interpolation time (in samples) for example 1024
83 // <dt> = delay time (in samples)
84 // < > = input signal we want to delay
85 //--------------------------------------------------------------------------
86
87 sdelay(N, it, dt) = ctrl(it,dt),_ : ddi(N)
88
89 with {
90
91 //---------------------------ddi(N,i,d0,d1)-------------------------------
92 // DDI (Double Delay with Interpolation) : the input signal is sent to two
93 // delay lines. The outputs of these delay lines are crossfaded with
94 // an interpolation stage. By acting on this interpolation value one
95 // can move smoothly from one delay to another. When <i> is 0 we can
96 // freely change the delay time <d1> of line 1, when it is 1 we can freely change
97 // the delay time <d0> of line 0.
98 //
99 // <N> = maximal delay in samples (must be a power of 2, for example 65536)
100 // <i> = interpolation value between 0 and 1 used to crossfade the outputs of the
101 // two delay lines (0.0: first delay line, 1.0: second delay line)
102 // <d0> = delay time of delay line 0 in samples between 0 and <N>-1
103 // <d1> = delay time of delay line 1 in samples between 0 and <N>-1
104 // < > = the input signal we want to delay
105 //-------------------------------------------------------------------------
106 ddi(N, i, d0, d1) = _ <: delay(N,d0), delay(N,d1) : interpolate(i);
107
108
109 //----------------------------ctrl(it,dt)------------------------------------
110 // Control logic for a Double Delay with Interpolation according to two
111 //
112 // USAGE : ctrl(it,dt)
113 // where :
114 // <it> an interpolation time (in samples, for example 256)
115 // <dt> a delay time (in samples)
116 //
117 // ctrl produces 3 outputs : an interpolation value <i> and two delay
118 // times <d0> and <d1>. These signals are used to control a ddi (Double Delay with Interpolation).
119 // The principle is to detect changes in the input delay time dt, then to
120 // change the delay time of the delay line currently unused and then by a
121 // smooth crossfade to remove the first delay line and activate the second one.
122 //
123 // The control logic has an internal state controlled by 4 elements
124 // <v> : the interpolation variation (0, 1/it, -1/it)
125 // <i> : the interpolation value (between 0 and 1)
126 // <d0>: the delay time of line 0
127 // <d1>: the delay time of line 1
128 //
129 // Please note that the last stage (!,_,_,_) cut <v> because it is only
130 // used internally.
131 //-------------------------------------------------------------------------
132 ctrl(it, dt) = \(v,ip,d0,d1).( (nv, nip, nd0, nd1)
133 with {
134
135 // interpolation variation
136 nv = if (v!=0.0, // if variation we are interpolating
137 if( (ip>0.0) & (ip<1.0), v , 0), // should we continue or not ?
138 if ((ip==0.0) & (dt!=d0), 1.0/it, // if true xfade from dl0 to dl1
139 if ((ip==1.0) & (dt!=d1), -1.0/it, // if true xfade from dl1 to dl0
140 0))); // nothing to change
141 // interpolation value
142 nip = ip+nv : min(1.0) : max(0.0);
143
144 // update delay time of line 0 if needed
145 nd0 = if ((ip >= 1.0) & (d1!=dt), dt, d0);
146
147 // update delay time of line 0 if needed
148 nd1 = if ((ip <= 0.0) & (d0!=dt), dt, d1);
149
150 } ) ~ (_,_,_,_) : (!,_,_,_);
151 };
152
153
154
155
156 //-----------------------------------------------
157 // Tempo, beats and pulses
158 //-----------------------------------------------
159
160 tempo(t) = (60*SR)/t; // tempo(t) -> samples
161
162 period(p) = %(int(p))~+(1); // signal en dent de scie de periode p
163 pulse(t) = period(t)==0; // pulse (10000...) de periode p
164 pulsen(n,t) = period(t)<n; // pulse (1110000...) de taille n et de periode p
165 beat(t) = pulse(tempo(t)); // pulse au tempo t
166
167
168
169 //-----------------------------------------------
170 // conversions between db and linear values
171 //-----------------------------------------------
172
173 db2linear(x) = pow(10, x/20.0);
174 linear2db(x) = 20*log10(x);
175
176
177
178
179
180 //===============================================
181 // Random and Noise generators
182 //===============================================
183
184
185 //-----------------------------------------------
186 // noise : Noise generator
187 //-----------------------------------------------
188
189 random = +(12345) ~ *(1103515245);
190 RANDMAX = 2147483647.0;
191
192 noise = random / RANDMAX;
193
194
195 //-----------------------------------------------
196 // Generates multiple decorrelated random numbers
197 // in parallel. Expects n>0.
198 //-----------------------------------------------
199
200 multirandom(n) = randomize(n) ~_
201 with {
202 randomize (1) = +(12345) : *(1103515245);
203 randomize (n) = randomize(1) <: randomize(n-1),_;
204 };
205
206
207 //-----------------------------------------------
208 // Generates multiple decorrelated noises
209 // in parallel. Expects n>0.
210 //-----------------------------------------------
211
212 multinoise(n) = multirandom(n) : par(i,n,/(RANDMAX))
213 with {
214 RANDMAX = 2147483647.0;
215 };
216
217
218 //------------------------------------------------
219
220 noises(N,i) = multinoise(N) : selector(i,N);
221
222
223 //-----------------------------------------------
224 // osc(freq) : Sinusoidal Oscillator
225 //-----------------------------------------------
226
227 tablesize = 1 << 16;
228 samplingfreq = SR;
229
230 time = (+(1)~_ ) - 1; // 0,1,2,3,...
231 sinwaveform = float(time)*(2.0*PI)/float(tablesize) : sin;
232
233 decimal(x) = x - floor(x);
234 phase(freq) = freq/float(samplingfreq) : (+ : decimal) ~ _ : *(float(tablesize));
235 osc(freq) = rdtable(tablesize, sinwaveform, int(phase(freq)) );
236 osci(freq) = s1 + d * (s2 - s1)
237 with {
238 i = int(phase(freq));
239 d = decimal(phase(freq));
240 s1 = rdtable(tablesize+1,sinwaveform,i);
241 s2 = rdtable(tablesize+1,sinwaveform,i+1);};
242
243
244 //-----------------------------------------------
245 // ADSR envelop
246 //-----------------------------------------------
247
248 // a,d,s,r = attack (#samples), decay (sec), sustain (percentage), release (sec)
249 // t = trigger signal
250
251 adsr(a,d,s,r,t) = env ~ (_,_) : (!,_) // the 2 'state' signals are fed back
252 with {
253 env (p2,y) =
254 (t>0) & (p2|(y>=1)), // p2 = decay-sustain phase
255 (y + p1*u - (p2&(y>s))*v*y - p3*w*y) // y = envelop signal
256 *((p3==0)|(y>=eps)) // cut off tails to prevent denormals
257 with {
258 p1 = (p2==0) & (t>0) & (y<1); // p1 = attack phase
259 p3 = (t<=0) & (y>0); // p3 = release phase
260 // #samples in attack, decay, release, must be >0
261 na = SR*a+(a==0.0); nd = SR*d+(d==0.0); nr = SR*r+(r==0.0);
262 // correct zero sustain level
263 z = s+(s==0.0)*db2linear(-60);
264 // attack, decay and (-60dB) release rates
265 u = 1/na; v = 1-pow(z, 1/nd); w = 1-1/pow(z*db2linear(60), 1/nr);
266 // values below this threshold are considered zero in the release phase
267 eps = db2linear(-120);
268 };
269 };
270
271
272 //-----------------------------------------------
273 // Spatialisation
274 //-----------------------------------------------
275
276 panner(c) = _ <: *(1-c), *(c);
277
278 bus2 = _,_;
279 bus3 = _,_,_;
280 bus4 = _,_,_,_;
281 bus5 = _,_,_,_,_;
282 bus6 = _,_,_,_,_,_;
283 bus7 = _,_,_,_,_,_,_;
284 bus8 = _,_,_,_,_,_,_,_;
285
286 gain2(g) = *(g),*(g);
287 gain3(g) = *(g),*(g),*(g);
288 gain4(g) = *(g),*(g),*(g),*(g);
289 gain5(g) = *(g),*(g),*(g),*(g),*(g);
290 gain6(g) = *(g),*(g),*(g),*(g),*(g),*(g);
291 gain7(g) = *(g),*(g),*(g),*(g),*(g),*(g),*(g);
292 gain8(g) = *(g),*(g),*(g),*(g),*(g),*(g),*(g),*(g);
293
294
295 //------------------------------------------------------
296 //
297 // GMEM SPAT
298 // n-outputs spatializer
299 // implementation of L. Pottier
300 //
301 //------------------------------------------------------
302 //
303 // n = number of outputs
304 // r = rotation (between 0 et 1)
305 // d = distance of the source (between 0 et 1)
306 //
307 //------------------------------------------------------
308 spat(n,a,d) = _ <: par(i, n, *( scaler(i, n, a, d) : smooth(0.9999) ))
309 with {
310 scaler(i,n,a,d) = (d/2.0+0.5)
311 * sqrt( max(0.0, 1.0 - abs(fmod(a+0.5+float(n-i)/n, 1.0) - 0.5) * n * d) );
312 smooth(c) = *(1-c) : +~*(c);
313 };
314
315
316
317 //--------------- Second Order Generic Transfert Function -------------------------
318 // TF2(b0,b1,b2,a1,a2)
319 //
320 //---------------------------------------------------------------------------------
321
322 TF2(b0,b1,b2,a1,a2) = sub ~ conv2(a1,a2) : conv3(b0,b1,b2)
323 with {
324 conv3(k0,k1,k2,x) = k0*x + k1*x' + k2*x'';
325 conv2(k0,k1,x) = k0*x + k1*x';
326 sub(x,y) = y-x;
327 };
328
329
330 /*************************** Break Point Functions ***************************
331
332 bpf is an environment (a group of related definitions) tha can be used to
333 create break-point functions. It contains three functions :
334 - start(x,y) to start a break-point function
335 - end(x,y) to end a break-point function
336 - point(x,y) to add intermediate points to a break-point function
337
338 A minimal break-point function must contain at least a start and an end point :
339
340 f = bpf.start(x0,y0) : bpf.end(x1,y1);
341
342 A more involved break-point function can contains any number of intermediate
343 points
344
345 f = bpf.start(x0,y0) : bpf.point(x1,y1) : bpf.point(x2,y2) : bpf.end(x3,y3);
346
347 In any case the x_{i} must be in increasing order (for all i, x_{i} < x_{i+1})
348
349 For example the following definition :
350
351 f = bpf.start(x0,y0) : ... : bpf.point(xi,yi) : ... : bpf.end(xn,yn);
352
353 implements a break-point function f such that :
354
355 f(x) = y_{0} when x < x_{0}
356 f(x) = y_{n} when x > x_{n}
357 f(x) = y_{i} + (y_{i+1}-y_{i})*(x-x_{i})/(x_{i+1}-x_{i}) when x_{i} <= x and x < x_{i+1}
358
359 ******************************************************************************/
360
361 bpf = environment
362 {
363 // Start a break-point function
364 start(x0,y0) = \(x).(x0,y0,x,y0);
365
366 // Add a break-point
367 point(x1,y1) = \(x0,y0,x,y).(x1, y1, x , if (x < x0, y, if (x < x1, y0 + (x-x0)*(y1-y0)/(x1-x0), y1)));
368
369 // End a break-point function
370 end (x1,y1) = \(x0,y0,x,y).(if (x < x0, y, if (x < x1, y0 + (x-x0)*(y1-y0)/(x1-x0), y1)));
371
372 // definition of if
373 if (c,t,e) = select2(c,e,t);
374 };
375
376
377