~ubuntu-branches/ubuntu/saucy/faust/saucy

« back to all changes in this revision

Viewing changes to tools/faust2pd-1.0.2/examples/basic/music.lib

  • Committer: Package Import Robot
  • Author(s): Mario Lang
  • Date: 2012-04-04 13:52:01 UTC
  • mfrom: (1.1.6) (3.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20120404135201-hpsrk87x3hga94tc
Tags: 0.9.46-2
* Fix "ftbfs with GCC-4.7":
  - debian/patches/unistd: Include <unistd.h> where necessary.
    (Closes: #667163)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import("math.lib");
2
 
 
3
 
//-----------------------------------------------
4
 
//                                      DELAY LINE
5
 
//-----------------------------------------------
6
 
frac(n)                 = n-int(n);
7
 
index(n)                = &(n-1) ~ +(1);                // n = 2**i
8
 
delay(n,d,x)    = rwtable(n, 0.0, index(n), x, (index(n)-int(d)) & (n-1));
9
 
fdelay(n,d,x)   = delay(n,int(d),x)*(1 - frac(d)) + delay(n,int(d)+1,x)*frac(d);
10
 
 
11
 
 
12
 
delay1s(d)              = delay(65536,d);
13
 
delay2s(d)              = delay(131072,d);
14
 
delay5s(d)              = delay(262144,d);
15
 
delay10s(d)             = delay(524288,d);
16
 
delay21s(d)             = delay(1048576,d);
17
 
delay43s(d)             = delay(2097152,d);
18
 
 
19
 
fdelay1s(d)             = fdelay(65536,d);
20
 
fdelay2s(d)             = fdelay(131072,d);
21
 
fdelay5s(d)             = fdelay(262144,d);
22
 
fdelay10s(d)    = fdelay(524288,d);
23
 
fdelay21s(d)    = fdelay(1048576,d);
24
 
fdelay43s(d)    = fdelay(2097152,d);
25
 
 
26
 
millisec        = SR/1000.0;
27
 
 
28
 
time1s  = hslider("time", 0, 0,  1000, 0.1)*millisec;
29
 
time2s  = hslider("time", 0, 0,  2000, 0.1)*millisec;
30
 
time5s  = hslider("time", 0, 0,  5000, 0.1)*millisec;
31
 
time10s = hslider("time", 0, 0, 10000, 0.1)*millisec;
32
 
time21s = hslider("time", 0, 0, 21000, 0.1)*millisec;
33
 
time43s = hslider("time", 0, 0, 43000, 0.1)*millisec;
34
 
 
35
 
 
36
 
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)));
37
 
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)));
38
 
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)));
39
 
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)));
40
 
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)));
41
 
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)));
42
 
 
43
 
 
44
 
 
45
 
//-----------------------------------------------
46
 
//                      Tempo, beats and pulses
47
 
//-----------------------------------------------
48
 
 
49
 
tempo(t)        = (60*SR)/t;                    // tempo(t) -> samples
50
 
 
51
 
period(p)       = %(int(p))~+(1);               // signal en dent de scie de periode p
52
 
pulse(t)        = period(t)==0;                 // pulse (10000...) de periode p
53
 
beat(t)         = pulse(tempo(t));              // pulse au tempo t
54
 
 
55
 
 
56
 
 
57
 
//-----------------------------------------------
58
 
//      conversions between db and linear values
59
 
//-----------------------------------------------
60
 
 
61
 
db2linear(x)    = pow(10, x/20.0);
62
 
linear2db(x)    = 20*log10(x);
63
 
 
64
 
 
65
 
//-----------------------------------------------
66
 
//                      noise : Noise generator
67
 
//-----------------------------------------------
68
 
 
69
 
random          = +(12345) ~ *(1103515245);
70
 
RANDMAX         = 2147483647.0;
71
 
 
72
 
noise           = random / RANDMAX;
73
 
 
74
 
 
75
 
 
76
 
//-----------------------------------------------
77
 
//                      osc(freq) : Sinusoidal Oscillator
78
 
//-----------------------------------------------
79
 
 
80
 
tablesize       = 1 << 16;
81
 
samplingfreq    = SR;
82
 
 
83
 
time            = (+(1)~_ ) - 1;                        // 0,1,2,3,...
84
 
sinwaveform     = float(time)*(2.0*PI)/float(tablesize) : sin;
85
 
 
86
 
decimal(x)      = x - floor(x);
87
 
phase(freq)     = freq/float(samplingfreq) : (+ : decimal) ~ _ : *(float(tablesize));
88
 
osc(freq)       = rdtable(tablesize, sinwaveform, int(phase(freq)) );
89
 
osci(freq)      = s1 + d * (s2 - s1)
90
 
                with {
91
 
                        i = int(phase(freq));
92
 
                        d = decimal(phase(freq));
93
 
                        s1 = rdtable(tablesize+1,sinwaveform,i);
94
 
                        s2 = rdtable(tablesize+1,sinwaveform,i+1);};
95
 
 
96
 
 
97
 
//-----------------------------------------------
98
 
//                      ADSR envelop
99
 
//-----------------------------------------------
100
 
 
101
 
// a,d,s,r = attack (#samples), decay (sec), sustain (percentage), release (sec)
102
 
// t       = trigger signal
103
 
 
104
 
adsr(a,d,s,r,t) = env ~ (_,_) : ! // the 2 'state' signals are fed back
105
 
with {
106
 
    env (p2,y) =
107
 
        (t>0) & (p2|(y>=1)),          // p2 = decay-sustain phase
108
 
        (y + p1*u - (p2&(y>s))*v*y - p3*w*y)    // y  = envelop signal
109
 
        *((p3==0)|(y>=eps)) // cut off tails to prevent denormals
110
 
    with {
111
 
        p1 = (p2==0) & (t>0) & (y<1);         // p1 = attack phase
112
 
        p3 = (t<=0) & (y>0);                  // p3 = release phase
113
 
        // #samples in attack, decay, release, must be >0
114
 
        na = SR*a+(a==0.0); nd = SR*d+(d==0.0); nr = SR*r+(r==0.0);
115
 
        // correct zero sustain level
116
 
        z = s+(s==0.0)*db2linear(-60);
117
 
        // attack, decay and (-60dB) release rates
118
 
        u = 1/na; v = 1-pow(z, 1/nd); w = 1-1/pow(z*db2linear(60), 1/nr);
119
 
        // values below this threshold are considered zero in the release phase
120
 
        eps = db2linear(-120);
121
 
    };
122
 
};
123
 
 
124
 
 
125
 
//-----------------------------------------------
126
 
//                      Spatialisation
127
 
//-----------------------------------------------
128
 
 
129
 
panner(c) = _ <: *(1-c), *(c);
130
 
 
131
 
bus2 = _,_;
132
 
bus3 = _,_,_;
133
 
bus4 = _,_,_,_;
134
 
bus5 = _,_,_,_,_;
135
 
bus6 = _,_,_,_,_,_;
136
 
bus7 = _,_,_,_,_,_,_;
137
 
bus8 = _,_,_,_,_,_,_,_;
138
 
 
139
 
gain2(g) = *(g),*(g);
140
 
gain3(g) = *(g),*(g),*(g);
141
 
gain4(g) = *(g),*(g),*(g),*(g);
142
 
gain5(g) = *(g),*(g),*(g),*(g),*(g);
143
 
gain6(g) = *(g),*(g),*(g),*(g),*(g),*(g);
144
 
gain7(g) = *(g),*(g),*(g),*(g),*(g),*(g),*(g);
145
 
gain8(g) = *(g),*(g),*(g),*(g),*(g),*(g),*(g),*(g);
146
 
 
147
 
 
148
 
 
149
 
//--------------- Second Order Generic Transfert Function -------------------------
150
 
// TF2(b0,b1,b2,a1,a2)
151
 
//
152
 
//---------------------------------------------------------------------------------
153
 
 
154
 
TF2(b0,b1,b2,a1,a2) = sub ~ conv2(a1,a2) : conv3(b0,b1,b2)
155
 
        with {
156
 
                conv3(k0,k1,k2,x)       = k0*x + k1*x' + k2*x'';
157
 
                conv2(k0,k1,x)          = k0*x + k1*x';
158
 
                sub(x,y)                        = y-x;
159
 
        };
160
 
 
161
 
 
162
 
 
163