-
Notifications
You must be signed in to change notification settings - Fork 63
/
maxmsp.lib
237 lines (198 loc) · 7.98 KB
/
maxmsp.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//#################################### maxmsp.lib ########################################
// MaxMSP compatibility Library.
//
// #### References
// * <https://github.com/grame-cncm/faustlibraries/blob/master/maxmsp.lib>
//########################################################################################
/************************************************************************
************************************************************************
FAUST library file
Copyright (C) 2019-2020 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
EXCEPTION TO THE LGPL LICENSE : As a special exception, you may create a
larger FAUST program which directly or indirectly imports this library
file and still distribute the compiled code generated by the FAUST
compiler, or a modified version of this compiled code, under your own
copyright and license. This EXCEPTION TO THE LGPL LICENSE explicitly
grants you the right to freely choose the license for the resulting
compiled code. In particular the resulting compiled code has no obligation
to be LGPL or GPL. For example you are free to choose a commerci
************************************************************************
************************************************************************/
ba = library("basics.lib");
ma = library("maths.lib");
declare name "MaxMSP compatibility Library";
declare author "GRAME";
declare copyright "GRAME";
declare version "1.1.0";
declare license "LGPL with exception";
atodb = db2lin;
//-------------------------------------------------------------------------
//
// Implementation of MaxMSP filtercoeff
//
// from : Cookbook formulae for audio EQ biquad filter coefficients
// by : Robert Bristow-Johnson <[email protected]>
// URL : <http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt>
//
//-------------------------------------------------------------------------
filtercoeff(f0, dBgain, Q) = environment
{
//----------------------------------------
// biquad coeffs for various filters
// usage : filtercoeff(f0, dBgain, Q).LPF
//----------------------------------------
LPF = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = (1 - cos(w0))/2;
b1 = 1 - cos(w0);
b2 = (1 - cos(w0))/2;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
};
HPF = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = (1 + cos(w0))/2;
b1 = -1 - cos(w0);
b2 = (1 + cos(w0))/2;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
};
BPF = rbjcoef(a0, a1, a2, b0, b1, b2) // constant 0 dB peak gain
with {
b0 = alpha;
b1 = 0;
b2 = -alpha;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
};
notch = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = 1;
b1 = -2*cos(w0);
b2 = 1;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
};
APF = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = 1 - alpha;
b1 = -2*cos(w0);
b2 = 1 + alpha;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
};
peakingEQ = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = 1 + alpha*A;
b1 = -2*cos(w0);
b2 = 1 - alpha*A;
a0 = 1 + alpha/A;
a1 = -2*cos(w0);
a2 = 1 - alpha/A;
};
peakNotch = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = 1 + alpha*G;
b1 = -2*cos(w0);
b2 = 1 - alpha*G;
a0 = 1 + alpha/G;
a1 = -2*cos(w0);
a2 = 1 - alpha/G;
};
lowShelf = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = A*((A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha);
b1 = 2*A*((A-1) - (A+1)*cos(w0));
b2 = A*((A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha);
a0 = (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha;
a1 = -2*((A-1) + (A+1)*cos(w0));
a2 = (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha;
};
highShelf = rbjcoef(a0, a1, a2, b0, b1, b2)
with {
b0 = A*((A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha);
b1 = -2*A*((A-1) + (A+1)*cos(w0));
b2 = A*((A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha);
a0 = (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha;
a1 = 2*((A-1) - (A+1)*cos(w0));
a2 = (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha;
};
// --------------------- implementation ------------------------------
// convert rbj coeffs to biquad coeffs
rbjcoef(a0,a1,a2,b0,b1,b2) = (b0/a0, b1/a0, b2/a0, a1/a0, a2/a0);
// common values
alpha = sin(w0)/(2*max(0.001,Q));
w0 = 2*ma.PI*max(0,f0)/Fs;
Fs = ma.SR;
A = 10^(dBgain/40); // (for peaking and shelving EQ filters only)
G = sqrt(max(0.00001, dBgain)); // When gain is a linear values (i.e. not in dB)
};
//-------------------------------------------------------------------------
// Implementation of MaxMSP biquad~
// y[n] = a0 * x[n] + a1 * x[n-1] + a2 * x[n-2] - b1 * y[n-1] - b2 * y[n-2]
//-------------------------------------------------------------------------
biquad(x,a0,a1,a2,b1,b2) = x : + ~ ((-1)*conv2(b1, b2)) : conv3(a0, a1, a2)
with {
conv2(c0,c1,x) = c0*x+c1*x';
conv3(c0,c1,c2,x) = c0*x+c1*x'+c2*x'';
};
//-------------------------------------------------------------------------
//
// Filters using filtercoeff and biquad
//
//-------------------------------------------------------------------------
// Low Pass Filter
LPF(x, f0, gain, Q)= x , filtercoeff(f0,gain,Q).LPF : biquad;
// High Pass Filter
HPF(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).HPF : biquad;
// Band Pass Filter
BPF(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).BPF : biquad;
// notch Filter
notch(x, f0, gain, Q)= x , filtercoeff(f0,gain,Q).notch : biquad;
// All Pass Filter
APF(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).APF : biquad;
// ????
peakingEQ(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).peakingEQ : biquad;
// Max peakNotch is like peakingEQ but with a linear gain
peakNotch(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).peakNotch : biquad;
// ????
lowShelf(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).lowShelf : biquad;
// ????
highShelf(x, f0, gain, Q) = x , filtercoeff(f0,gain,Q).highShelf : biquad;
//-------------------------------------------------------------------------
// Implementation of Max/MSP line~. Generate signal ramp or envelop
//
// USAGE : line(value, time)
// value : the desired output value
// time : the interpolation time to reach this value (in milliseconds)
//
// NOTE : the interpolation process is restarted every time the desired
// output value changes. The interpolation time is sampled only then.
//-------------------------------------------------------------------------
line(value, time) = state~(_,_):!,_
with {
state(t, c) = nt,nc
with {
nt = ba.if(value != value', samples, t-1);
nc = ba.if(nt > 0, c + (value - c)/nt, value);
samples = time*ma.SR/1000.0;
};
};