summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/d_filter.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/d_filter.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/d_filter.c1094
1 files changed, 1094 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/d_filter.c b/apps/plugins/pdbox/PDa/src/d_filter.c
new file mode 100644
index 0000000000..05bb7cd58e
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/d_filter.c
@@ -0,0 +1,1094 @@
1/* Copyright (c) 1997-1999 Miller Puckette.
2* For information on usage and redistribution, and for a DISCLAIMER OF ALL
3* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
4
5/* "filters", both linear and nonlinear.
6*/
7#include "m_pd.h"
8#include <math.h>
9
10/* ---------------- hip~ - 1-pole 1-zero hipass filter. ----------------- */
11
12typedef struct hipctl
13{
14 float c_x;
15 float c_coef;
16} t_hipctl;
17
18typedef struct sighip
19{
20 t_object x_obj;
21 float x_sr;
22 float x_hz;
23 t_hipctl x_cspace;
24 t_hipctl *x_ctl;
25 float x_f;
26} t_sighip;
27
28t_class *sighip_class;
29static void sighip_ft1(t_sighip *x, t_floatarg f);
30
31static void *sighip_new(t_floatarg f)
32{
33 t_sighip *x = (t_sighip *)pd_new(sighip_class);
34 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
35 outlet_new(&x->x_obj, gensym("signal"));
36 x->x_sr = 44100;
37 x->x_ctl = &x->x_cspace;
38 x->x_cspace.c_x = 0;
39 sighip_ft1(x, f);
40 x->x_f = 0;
41 return (x);
42}
43
44static void sighip_ft1(t_sighip *x, t_floatarg f)
45{
46 if (f < 0) f = 0;
47 x->x_hz = f;
48 x->x_ctl->c_coef = 1 - f * (2 * 3.14159) / x->x_sr;
49 if (x->x_ctl->c_coef < 0)
50 x->x_ctl->c_coef = 0;
51 else if (x->x_ctl->c_coef > 1)
52 x->x_ctl->c_coef = 1;
53}
54
55static t_int *sighip_perform(t_int *w)
56{
57 float *in = (float *)(w[1]);
58 float *out = (float *)(w[2]);
59 t_hipctl *c = (t_hipctl *)(w[3]);
60 int n = (t_int)(w[4]);
61 int i;
62 float last = c->c_x;
63 float coef = c->c_coef;
64 if (coef < 1)
65 {
66 for (i = 0; i < n; i++)
67 {
68 float new = *in++ + coef * last;
69 *out++ = new - last;
70 last = new;
71 }
72 if (PD_BIGORSMALL(last))
73 last = 0;
74 c->c_x = last;
75 }
76 else
77 {
78 for (i = 0; i < n; i++)
79 *out++ = *in++;
80 c->c_x = 0;
81 }
82 return (w+5);
83}
84
85static void sighip_dsp(t_sighip *x, t_signal **sp)
86{
87 x->x_sr = sp[0]->s_sr;
88 sighip_ft1(x, x->x_hz);
89 dsp_add(sighip_perform, 4,
90 sp[0]->s_vec, sp[1]->s_vec,
91 x->x_ctl, sp[0]->s_n);
92
93}
94
95static void sighip_clear(t_sighip *x, t_floatarg q)
96{
97 x->x_cspace.c_x = 0;
98}
99
100void sighip_setup(void)
101{
102 sighip_class = class_new(gensym("hip~"), (t_newmethod)sighip_new, 0,
103 sizeof(t_sighip), 0, A_DEFFLOAT, 0);
104 CLASS_MAINSIGNALIN(sighip_class, t_sighip, x_f);
105 class_addmethod(sighip_class, (t_method)sighip_dsp, gensym("dsp"), 0);
106 class_addmethod(sighip_class, (t_method)sighip_ft1,
107 gensym("ft1"), A_FLOAT, 0);
108 class_addmethod(sighip_class, (t_method)sighip_clear, gensym("clear"), 0);
109}
110
111/* ---------------- lop~ - 1-pole lopass filter. ----------------- */
112
113typedef struct lopctl
114{
115 float c_x;
116 float c_coef;
117} t_lopctl;
118
119typedef struct siglop
120{
121 t_object x_obj;
122 float x_sr;
123 float x_hz;
124 t_lopctl x_cspace;
125 t_lopctl *x_ctl;
126 float x_f;
127} t_siglop;
128
129t_class *siglop_class;
130
131static void siglop_ft1(t_siglop *x, t_floatarg f);
132
133static void *siglop_new(t_floatarg f)
134{
135 t_siglop *x = (t_siglop *)pd_new(siglop_class);
136 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
137 outlet_new(&x->x_obj, gensym("signal"));
138 x->x_sr = 44100;
139 x->x_ctl = &x->x_cspace;
140 x->x_cspace.c_x = 0;
141 siglop_ft1(x, f);
142 x->x_f = 0;
143 return (x);
144}
145
146static void siglop_ft1(t_siglop *x, t_floatarg f)
147{
148 if (f < 0) f = 0;
149 x->x_hz = f;
150 x->x_ctl->c_coef = f * (2 * 3.14159) / x->x_sr;
151 if (x->x_ctl->c_coef > 1)
152 x->x_ctl->c_coef = 1;
153 else if (x->x_ctl->c_coef < 0)
154 x->x_ctl->c_coef = 0;
155}
156
157static void siglop_clear(t_siglop *x, t_floatarg q)
158{
159 x->x_cspace.c_x = 0;
160}
161
162static t_int *siglop_perform(t_int *w)
163{
164 float *in = (float *)(w[1]);
165 float *out = (float *)(w[2]);
166 t_lopctl *c = (t_lopctl *)(w[3]);
167 int n = (t_int)(w[4]);
168 int i;
169 float last = c->c_x;
170 float coef = c->c_coef;
171 float feedback = 1 - coef;
172 for (i = 0; i < n; i++)
173 last = *out++ = coef * *in++ + feedback * last;
174 if (PD_BIGORSMALL(last))
175 last = 0;
176 c->c_x = last;
177 return (w+5);
178}
179
180static void siglop_dsp(t_siglop *x, t_signal **sp)
181{
182 x->x_sr = sp[0]->s_sr;
183 siglop_ft1(x, x->x_hz);
184 dsp_add(siglop_perform, 4,
185 sp[0]->s_vec, sp[1]->s_vec,
186 x->x_ctl, sp[0]->s_n);
187
188}
189
190void siglop_setup(void)
191{
192 siglop_class = class_new(gensym("lop~"), (t_newmethod)siglop_new, 0,
193 sizeof(t_siglop), 0, A_DEFFLOAT, 0);
194 CLASS_MAINSIGNALIN(siglop_class, t_siglop, x_f);
195 class_addmethod(siglop_class, (t_method)siglop_dsp, gensym("dsp"), 0);
196 class_addmethod(siglop_class, (t_method)siglop_ft1,
197 gensym("ft1"), A_FLOAT, 0);
198 class_addmethod(siglop_class, (t_method)siglop_clear, gensym("clear"), 0);
199}
200
201/* ---------------- bp~ - 2-pole bandpass filter. ----------------- */
202
203typedef struct bpctl
204{
205 float c_x1;
206 float c_x2;
207 float c_coef1;
208 float c_coef2;
209 float c_gain;
210} t_bpctl;
211
212typedef struct sigbp
213{
214 t_object x_obj;
215 float x_sr;
216 float x_freq;
217 float x_q;
218 t_bpctl x_cspace;
219 t_bpctl *x_ctl;
220 float x_f;
221} t_sigbp;
222
223t_class *sigbp_class;
224
225static void sigbp_docoef(t_sigbp *x, t_floatarg f, t_floatarg q);
226
227static void *sigbp_new(t_floatarg f, t_floatarg q)
228{
229 t_sigbp *x = (t_sigbp *)pd_new(sigbp_class);
230 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
231 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft2"));
232 outlet_new(&x->x_obj, gensym("signal"));
233 x->x_sr = 44100;
234 x->x_ctl = &x->x_cspace;
235 x->x_cspace.c_x1 = 0;
236 x->x_cspace.c_x2 = 0;
237 sigbp_docoef(x, f, q);
238 x->x_f = 0;
239 return (x);
240}
241
242static float sigbp_qcos(float f)
243{
244 if (f >= -(0.5f*3.14159f) && f <= 0.5f*3.14159f)
245 {
246 float g = f*f;
247 return (((g*g*g * (-1.0f/720.0f) + g*g*(1.0f/24.0f)) - g*0.5) + 1);
248 }
249 else return (0);
250}
251
252static void sigbp_docoef(t_sigbp *x, t_floatarg f, t_floatarg q)
253{
254 float r, oneminusr, omega;
255 if (f < 0.001) f = 10;
256 if (q < 0) q = 0;
257 x->x_freq = f;
258 x->x_q = q;
259 omega = f * (2.0f * 3.14159f) / x->x_sr;
260 if (q < 0.001) oneminusr = 1.0f;
261 else oneminusr = omega/q;
262 if (oneminusr > 1.0f) oneminusr = 1.0f;
263 r = 1.0f - oneminusr;
264 x->x_ctl->c_coef1 = 2.0f * sigbp_qcos(omega) * r;
265 x->x_ctl->c_coef2 = - r * r;
266 x->x_ctl->c_gain = 2 * oneminusr * (oneminusr + r * omega);
267 /* post("r %f, omega %f, coef1 %f, coef2 %f",
268 r, omega, x->x_ctl->c_coef1, x->x_ctl->c_coef2); */
269}
270
271static void sigbp_ft1(t_sigbp *x, t_floatarg f)
272{
273 sigbp_docoef(x, f, x->x_q);
274}
275
276static void sigbp_ft2(t_sigbp *x, t_floatarg q)
277{
278 sigbp_docoef(x, x->x_freq, q);
279}
280
281static void sigbp_clear(t_sigbp *x, t_floatarg q)
282{
283 x->x_ctl->c_x1 = x->x_ctl->c_x2 = 0;
284}
285
286static t_int *sigbp_perform(t_int *w)
287{
288 float *in = (float *)(w[1]);
289 float *out = (float *)(w[2]);
290 t_bpctl *c = (t_bpctl *)(w[3]);
291 int n = (t_int)(w[4]);
292 int i;
293 float last = c->c_x1;
294 float prev = c->c_x2;
295 float coef1 = c->c_coef1;
296 float coef2 = c->c_coef2;
297 float gain = c->c_gain;
298 for (i = 0; i < n; i++)
299 {
300 float output = *in++ + coef1 * last + coef2 * prev;
301 *out++ = gain * output;
302 prev = last;
303 last = output;
304 }
305 if (PD_BIGORSMALL(last))
306 last = 0;
307 if (PD_BIGORSMALL(prev))
308 prev = 0;
309 c->c_x1 = last;
310 c->c_x2 = prev;
311 return (w+5);
312}
313
314static void sigbp_dsp(t_sigbp *x, t_signal **sp)
315{
316 x->x_sr = sp[0]->s_sr;
317 sigbp_docoef(x, x->x_freq, x->x_q);
318 dsp_add(sigbp_perform, 4,
319 sp[0]->s_vec, sp[1]->s_vec,
320 x->x_ctl, sp[0]->s_n);
321
322}
323
324void sigbp_setup(void)
325{
326 sigbp_class = class_new(gensym("bp~"), (t_newmethod)sigbp_new, 0,
327 sizeof(t_sigbp), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
328 CLASS_MAINSIGNALIN(sigbp_class, t_sigbp, x_f);
329 class_addmethod(sigbp_class, (t_method)sigbp_dsp, gensym("dsp"), 0);
330 class_addmethod(sigbp_class, (t_method)sigbp_ft1,
331 gensym("ft1"), A_FLOAT, 0);
332 class_addmethod(sigbp_class, (t_method)sigbp_ft2,
333 gensym("ft2"), A_FLOAT, 0);
334 class_addmethod(sigbp_class, (t_method)sigbp_clear, gensym("clear"), 0);
335}
336
337/* ---------------- biquad~ - raw biquad filter ----------------- */
338
339typedef struct biquadctl
340{
341 float c_x1;
342 float c_x2;
343 float c_fb1;
344 float c_fb2;
345 float c_ff1;
346 float c_ff2;
347 float c_ff3;
348} t_biquadctl;
349
350typedef struct sigbiquad
351{
352 t_object x_obj;
353 float x_f;
354 t_biquadctl x_cspace;
355 t_biquadctl *x_ctl;
356} t_sigbiquad;
357
358t_class *sigbiquad_class;
359
360static void sigbiquad_list(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv);
361
362static void *sigbiquad_new(t_symbol *s, int argc, t_atom *argv)
363{
364 t_sigbiquad *x = (t_sigbiquad *)pd_new(sigbiquad_class);
365 outlet_new(&x->x_obj, gensym("signal"));
366 x->x_ctl = &x->x_cspace;
367 x->x_cspace.c_x1 = x->x_cspace.c_x2 = 0;
368 sigbiquad_list(x, s, argc, argv);
369 x->x_f = 0;
370 return (x);
371}
372
373static t_int *sigbiquad_perform(t_int *w)
374{
375 float *in = (float *)(w[1]);
376 float *out = (float *)(w[2]);
377 t_biquadctl *c = (t_biquadctl *)(w[3]);
378 int n = (t_int)(w[4]);
379 int i;
380 float last = c->c_x1;
381 float prev = c->c_x2;
382 float fb1 = c->c_fb1;
383 float fb2 = c->c_fb2;
384 float ff1 = c->c_ff1;
385 float ff2 = c->c_ff2;
386 float ff3 = c->c_ff3;
387 for (i = 0; i < n; i++)
388 {
389 float output = *in++ + fb1 * last + fb2 * prev;
390 if (PD_BIGORSMALL(output))
391 output = 0;
392 *out++ = ff1 * output + ff2 * last + ff3 * prev;
393 prev = last;
394 last = output;
395 }
396 c->c_x1 = last;
397 c->c_x2 = prev;
398 return (w+5);
399}
400
401static void sigbiquad_list(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv)
402{
403 float fb1 = atom_getfloatarg(0, argc, argv);
404 float fb2 = atom_getfloatarg(1, argc, argv);
405 float ff1 = atom_getfloatarg(2, argc, argv);
406 float ff2 = atom_getfloatarg(3, argc, argv);
407 float ff3 = atom_getfloatarg(4, argc, argv);
408 float discriminant = fb1 * fb1 + 4 * fb2;
409 t_biquadctl *c = x->x_ctl;
410 if (discriminant < 0) /* imaginary roots -- resonant filter */
411 {
412 /* they're conjugates so we just check that the product
413 is less than one */
414 if (fb2 >= -1.0f) goto stable;
415 }
416 else /* real roots */
417 {
418 /* check that the parabola 1 - fb1 x - fb2 x^2 has a
419 vertex between -1 and 1, and that it's nonnegative
420 at both ends, which implies both roots are in [1-,1]. */
421 if (fb1 <= 2.0f && fb1 >= -2.0f &&
422 1.0f - fb1 -fb2 >= 0 && 1.0f + fb1 - fb2 >= 0)
423 goto stable;
424 }
425 /* if unstable, just bash to zero */
426 fb1 = fb2 = ff1 = ff2 = ff3 = 0;
427stable:
428 c->c_fb1 = fb1;
429 c->c_fb2 = fb2;
430 c->c_ff1 = ff1;
431 c->c_ff2 = ff2;
432 c->c_ff3 = ff3;
433}
434
435static void sigbiquad_set(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv)
436{
437 t_biquadctl *c = x->x_ctl;
438 c->c_x1 = atom_getfloatarg(0, argc, argv);
439 c->c_x2 = atom_getfloatarg(1, argc, argv);
440}
441
442static void sigbiquad_dsp(t_sigbiquad *x, t_signal **sp)
443{
444 dsp_add(sigbiquad_perform, 4,
445 sp[0]->s_vec, sp[1]->s_vec,
446 x->x_ctl, sp[0]->s_n);
447
448}
449
450void sigbiquad_setup(void)
451{
452 sigbiquad_class = class_new(gensym("biquad~"), (t_newmethod)sigbiquad_new,
453 0, sizeof(t_sigbiquad), 0, A_GIMME, 0);
454 CLASS_MAINSIGNALIN(sigbiquad_class, t_sigbiquad, x_f);
455 class_addmethod(sigbiquad_class, (t_method)sigbiquad_dsp, gensym("dsp"), 0);
456 class_addlist(sigbiquad_class, sigbiquad_list);
457 class_addmethod(sigbiquad_class, (t_method)sigbiquad_set, gensym("set"),
458 A_GIMME, 0);
459 class_addmethod(sigbiquad_class, (t_method)sigbiquad_set, gensym("clear"),
460 A_GIMME, 0);
461}
462
463/* ---------------- samphold~ - sample and hold ----------------- */
464
465typedef struct sigsamphold
466{
467 t_object x_obj;
468 float x_f;
469 float x_lastin;
470 float x_lastout;
471} t_sigsamphold;
472
473t_class *sigsamphold_class;
474
475static void *sigsamphold_new(void)
476{
477 t_sigsamphold *x = (t_sigsamphold *)pd_new(sigsamphold_class);
478 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
479 outlet_new(&x->x_obj, gensym("signal"));
480 x->x_lastin = 0;
481 x->x_lastout = 0;
482 x->x_f = 0;
483 return (x);
484}
485
486static t_int *sigsamphold_perform(t_int *w)
487{
488 float *in1 = (float *)(w[1]);
489 float *in2 = (float *)(w[2]);
490 float *out = (float *)(w[3]);
491 t_sigsamphold *x = (t_sigsamphold *)(w[4]);
492 int n = (t_int)(w[5]);
493 int i;
494 float lastin = x->x_lastin;
495 float lastout = x->x_lastout;
496 for (i = 0; i < n; i++, *in1++)
497 {
498 float next = *in2++;
499 if (next < lastin) lastout = *in1;
500 *out++ = lastout;
501 lastin = next;
502 }
503 x->x_lastin = lastin;
504 x->x_lastout = lastout;
505 return (w+6);
506}
507
508static void sigsamphold_dsp(t_sigsamphold *x, t_signal **sp)
509{
510 dsp_add(sigsamphold_perform, 5,
511 sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec,
512 x, sp[0]->s_n);
513}
514
515static void sigsamphold_reset(t_sigsamphold *x)
516{
517 x->x_lastin = 1e20;
518}
519
520static void sigsamphold_set(t_sigsamphold *x, t_float f)
521{
522 x->x_lastout = f;
523}
524
525void sigsamphold_setup(void)
526{
527 sigsamphold_class = class_new(gensym("samphold~"),
528 (t_newmethod)sigsamphold_new, 0, sizeof(t_sigsamphold), 0, 0);
529 CLASS_MAINSIGNALIN(sigsamphold_class, t_sigsamphold, x_f);
530 class_addmethod(sigsamphold_class, (t_method)sigsamphold_set,
531 gensym("set"), A_FLOAT, 0);
532 class_addmethod(sigsamphold_class, (t_method)sigsamphold_reset,
533 gensym("reset"), 0);
534 class_addmethod(sigsamphold_class, (t_method)sigsamphold_dsp,
535 gensym("dsp"), 0);
536}
537
538/* ------------------------ setup routine ------------------------- */
539
540void d_filter_setup(void)
541{
542 sighip_setup();
543 siglop_setup();
544 sigbp_setup();
545 sigbiquad_setup();
546 sigsamphold_setup();
547}
548/* Copyright (c) 1997-1999 Miller Puckette.
549* For information on usage and redistribution, and for a DISCLAIMER OF ALL
550* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
551
552/* "filters", both linear and nonlinear.
553*/
554#include "m_pd.h"
555#include <math.h>
556
557/* ---------------- hip~ - 1-pole 1-zero hipass filter. ----------------- */
558
559typedef struct hipctl
560{
561 float c_x;
562 float c_coef;
563} t_hipctl;
564
565typedef struct sighip
566{
567 t_object x_obj;
568 float x_sr;
569 float x_hz;
570 t_hipctl x_cspace;
571 t_hipctl *x_ctl;
572 float x_f;
573} t_sighip;
574
575t_class *sighip_class;
576static void sighip_ft1(t_sighip *x, t_floatarg f);
577
578static void *sighip_new(t_floatarg f)
579{
580 t_sighip *x = (t_sighip *)pd_new(sighip_class);
581 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
582 outlet_new(&x->x_obj, gensym("signal"));
583 x->x_sr = 44100;
584 x->x_ctl = &x->x_cspace;
585 x->x_cspace.c_x = 0;
586 sighip_ft1(x, f);
587 x->x_f = 0;
588 return (x);
589}
590
591static void sighip_ft1(t_sighip *x, t_floatarg f)
592{
593 if (f < 0) f = 0;
594 x->x_hz = f;
595 x->x_ctl->c_coef = 1 - f * (2 * 3.14159) / x->x_sr;
596 if (x->x_ctl->c_coef < 0)
597 x->x_ctl->c_coef = 0;
598 else if (x->x_ctl->c_coef > 1)
599 x->x_ctl->c_coef = 1;
600}
601
602static t_int *sighip_perform(t_int *w)
603{
604 float *in = (float *)(w[1]);
605 float *out = (float *)(w[2]);
606 t_hipctl *c = (t_hipctl *)(w[3]);
607 int n = (t_int)(w[4]);
608 int i;
609 float last = c->c_x;
610 float coef = c->c_coef;
611 if (coef < 1)
612 {
613 for (i = 0; i < n; i++)
614 {
615 float new = *in++ + coef * last;
616 *out++ = new - last;
617 last = new;
618 }
619 if (PD_BIGORSMALL(last))
620 last = 0;
621 c->c_x = last;
622 }
623 else
624 {
625 for (i = 0; i < n; i++)
626 *out++ = *in++;
627 c->c_x = 0;
628 }
629 return (w+5);
630}
631
632static void sighip_dsp(t_sighip *x, t_signal **sp)
633{
634 x->x_sr = sp[0]->s_sr;
635 sighip_ft1(x, x->x_hz);
636 dsp_add(sighip_perform, 4,
637 sp[0]->s_vec, sp[1]->s_vec,
638 x->x_ctl, sp[0]->s_n);
639
640}
641
642static void sighip_clear(t_sighip *x, t_floatarg q)
643{
644 x->x_cspace.c_x = 0;
645}
646
647void sighip_setup(void)
648{
649 sighip_class = class_new(gensym("hip~"), (t_newmethod)sighip_new, 0,
650 sizeof(t_sighip), 0, A_DEFFLOAT, 0);
651 CLASS_MAINSIGNALIN(sighip_class, t_sighip, x_f);
652 class_addmethod(sighip_class, (t_method)sighip_dsp, gensym("dsp"), 0);
653 class_addmethod(sighip_class, (t_method)sighip_ft1,
654 gensym("ft1"), A_FLOAT, 0);
655 class_addmethod(sighip_class, (t_method)sighip_clear, gensym("clear"), 0);
656}
657
658/* ---------------- lop~ - 1-pole lopass filter. ----------------- */
659
660typedef struct lopctl
661{
662 float c_x;
663 float c_coef;
664} t_lopctl;
665
666typedef struct siglop
667{
668 t_object x_obj;
669 float x_sr;
670 float x_hz;
671 t_lopctl x_cspace;
672 t_lopctl *x_ctl;
673 float x_f;
674} t_siglop;
675
676t_class *siglop_class;
677
678static void siglop_ft1(t_siglop *x, t_floatarg f);
679
680static void *siglop_new(t_floatarg f)
681{
682 t_siglop *x = (t_siglop *)pd_new(siglop_class);
683 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
684 outlet_new(&x->x_obj, gensym("signal"));
685 x->x_sr = 44100;
686 x->x_ctl = &x->x_cspace;
687 x->x_cspace.c_x = 0;
688 siglop_ft1(x, f);
689 x->x_f = 0;
690 return (x);
691}
692
693static void siglop_ft1(t_siglop *x, t_floatarg f)
694{
695 if (f < 0) f = 0;
696 x->x_hz = f;
697 x->x_ctl->c_coef = f * (2 * 3.14159) / x->x_sr;
698 if (x->x_ctl->c_coef > 1)
699 x->x_ctl->c_coef = 1;
700 else if (x->x_ctl->c_coef < 0)
701 x->x_ctl->c_coef = 0;
702}
703
704static void siglop_clear(t_siglop *x, t_floatarg q)
705{
706 x->x_cspace.c_x = 0;
707}
708
709static t_int *siglop_perform(t_int *w)
710{
711 float *in = (float *)(w[1]);
712 float *out = (float *)(w[2]);
713 t_lopctl *c = (t_lopctl *)(w[3]);
714 int n = (t_int)(w[4]);
715 int i;
716 float last = c->c_x;
717 float coef = c->c_coef;
718 float feedback = 1 - coef;
719 for (i = 0; i < n; i++)
720 last = *out++ = coef * *in++ + feedback * last;
721 if (PD_BIGORSMALL(last))
722 last = 0;
723 c->c_x = last;
724 return (w+5);
725}
726
727static void siglop_dsp(t_siglop *x, t_signal **sp)
728{
729 x->x_sr = sp[0]->s_sr;
730 siglop_ft1(x, x->x_hz);
731 dsp_add(siglop_perform, 4,
732 sp[0]->s_vec, sp[1]->s_vec,
733 x->x_ctl, sp[0]->s_n);
734
735}
736
737void siglop_setup(void)
738{
739 siglop_class = class_new(gensym("lop~"), (t_newmethod)siglop_new, 0,
740 sizeof(t_siglop), 0, A_DEFFLOAT, 0);
741 CLASS_MAINSIGNALIN(siglop_class, t_siglop, x_f);
742 class_addmethod(siglop_class, (t_method)siglop_dsp, gensym("dsp"), 0);
743 class_addmethod(siglop_class, (t_method)siglop_ft1,
744 gensym("ft1"), A_FLOAT, 0);
745 class_addmethod(siglop_class, (t_method)siglop_clear, gensym("clear"), 0);
746}
747
748/* ---------------- bp~ - 2-pole bandpass filter. ----------------- */
749
750typedef struct bpctl
751{
752 float c_x1;
753 float c_x2;
754 float c_coef1;
755 float c_coef2;
756 float c_gain;
757} t_bpctl;
758
759typedef struct sigbp
760{
761 t_object x_obj;
762 float x_sr;
763 float x_freq;
764 float x_q;
765 t_bpctl x_cspace;
766 t_bpctl *x_ctl;
767 float x_f;
768} t_sigbp;
769
770t_class *sigbp_class;
771
772static void sigbp_docoef(t_sigbp *x, t_floatarg f, t_floatarg q);
773
774static void *sigbp_new(t_floatarg f, t_floatarg q)
775{
776 t_sigbp *x = (t_sigbp *)pd_new(sigbp_class);
777 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
778 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft2"));
779 outlet_new(&x->x_obj, gensym("signal"));
780 x->x_sr = 44100;
781 x->x_ctl = &x->x_cspace;
782 x->x_cspace.c_x1 = 0;
783 x->x_cspace.c_x2 = 0;
784 sigbp_docoef(x, f, q);
785 x->x_f = 0;
786 return (x);
787}
788
789static float sigbp_qcos(float f)
790{
791 if (f >= -(0.5f*3.14159f) && f <= 0.5f*3.14159f)
792 {
793 float g = f*f;
794 return (((g*g*g * (-1.0f/720.0f) + g*g*(1.0f/24.0f)) - g*0.5) + 1);
795 }
796 else return (0);
797}
798
799static void sigbp_docoef(t_sigbp *x, t_floatarg f, t_floatarg q)
800{
801 float r, oneminusr, omega;
802 if (f < 0.001) f = 10;
803 if (q < 0) q = 0;
804 x->x_freq = f;
805 x->x_q = q;
806 omega = f * (2.0f * 3.14159f) / x->x_sr;
807 if (q < 0.001) oneminusr = 1.0f;
808 else oneminusr = omega/q;
809 if (oneminusr > 1.0f) oneminusr = 1.0f;
810 r = 1.0f - oneminusr;
811 x->x_ctl->c_coef1 = 2.0f * sigbp_qcos(omega) * r;
812 x->x_ctl->c_coef2 = - r * r;
813 x->x_ctl->c_gain = 2 * oneminusr * (oneminusr + r * omega);
814 /* post("r %f, omega %f, coef1 %f, coef2 %f",
815 r, omega, x->x_ctl->c_coef1, x->x_ctl->c_coef2); */
816}
817
818static void sigbp_ft1(t_sigbp *x, t_floatarg f)
819{
820 sigbp_docoef(x, f, x->x_q);
821}
822
823static void sigbp_ft2(t_sigbp *x, t_floatarg q)
824{
825 sigbp_docoef(x, x->x_freq, q);
826}
827
828static void sigbp_clear(t_sigbp *x, t_floatarg q)
829{
830 x->x_ctl->c_x1 = x->x_ctl->c_x2 = 0;
831}
832
833static t_int *sigbp_perform(t_int *w)
834{
835 float *in = (float *)(w[1]);
836 float *out = (float *)(w[2]);
837 t_bpctl *c = (t_bpctl *)(w[3]);
838 int n = (t_int)(w[4]);
839 int i;
840 float last = c->c_x1;
841 float prev = c->c_x2;
842 float coef1 = c->c_coef1;
843 float coef2 = c->c_coef2;
844 float gain = c->c_gain;
845 for (i = 0; i < n; i++)
846 {
847 float output = *in++ + coef1 * last + coef2 * prev;
848 *out++ = gain * output;
849 prev = last;
850 last = output;
851 }
852 if (PD_BIGORSMALL(last))
853 last = 0;
854 if (PD_BIGORSMALL(prev))
855 prev = 0;
856 c->c_x1 = last;
857 c->c_x2 = prev;
858 return (w+5);
859}
860
861static void sigbp_dsp(t_sigbp *x, t_signal **sp)
862{
863 x->x_sr = sp[0]->s_sr;
864 sigbp_docoef(x, x->x_freq, x->x_q);
865 dsp_add(sigbp_perform, 4,
866 sp[0]->s_vec, sp[1]->s_vec,
867 x->x_ctl, sp[0]->s_n);
868
869}
870
871void sigbp_setup(void)
872{
873 sigbp_class = class_new(gensym("bp~"), (t_newmethod)sigbp_new, 0,
874 sizeof(t_sigbp), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
875 CLASS_MAINSIGNALIN(sigbp_class, t_sigbp, x_f);
876 class_addmethod(sigbp_class, (t_method)sigbp_dsp, gensym("dsp"), 0);
877 class_addmethod(sigbp_class, (t_method)sigbp_ft1,
878 gensym("ft1"), A_FLOAT, 0);
879 class_addmethod(sigbp_class, (t_method)sigbp_ft2,
880 gensym("ft2"), A_FLOAT, 0);
881 class_addmethod(sigbp_class, (t_method)sigbp_clear, gensym("clear"), 0);
882}
883
884/* ---------------- biquad~ - raw biquad filter ----------------- */
885
886typedef struct biquadctl
887{
888 float c_x1;
889 float c_x2;
890 float c_fb1;
891 float c_fb2;
892 float c_ff1;
893 float c_ff2;
894 float c_ff3;
895} t_biquadctl;
896
897typedef struct sigbiquad
898{
899 t_object x_obj;
900 float x_f;
901 t_biquadctl x_cspace;
902 t_biquadctl *x_ctl;
903} t_sigbiquad;
904
905t_class *sigbiquad_class;
906
907static void sigbiquad_list(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv);
908
909static void *sigbiquad_new(t_symbol *s, int argc, t_atom *argv)
910{
911 t_sigbiquad *x = (t_sigbiquad *)pd_new(sigbiquad_class);
912 outlet_new(&x->x_obj, gensym("signal"));
913 x->x_ctl = &x->x_cspace;
914 x->x_cspace.c_x1 = x->x_cspace.c_x2 = 0;
915 sigbiquad_list(x, s, argc, argv);
916 x->x_f = 0;
917 return (x);
918}
919
920static t_int *sigbiquad_perform(t_int *w)
921{
922 float *in = (float *)(w[1]);
923 float *out = (float *)(w[2]);
924 t_biquadctl *c = (t_biquadctl *)(w[3]);
925 int n = (t_int)(w[4]);
926 int i;
927 float last = c->c_x1;
928 float prev = c->c_x2;
929 float fb1 = c->c_fb1;
930 float fb2 = c->c_fb2;
931 float ff1 = c->c_ff1;
932 float ff2 = c->c_ff2;
933 float ff3 = c->c_ff3;
934 for (i = 0; i < n; i++)
935 {
936 float output = *in++ + fb1 * last + fb2 * prev;
937 if (PD_BIGORSMALL(output))
938 output = 0;
939 *out++ = ff1 * output + ff2 * last + ff3 * prev;
940 prev = last;
941 last = output;
942 }
943 c->c_x1 = last;
944 c->c_x2 = prev;
945 return (w+5);
946}
947
948static void sigbiquad_list(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv)
949{
950 float fb1 = atom_getfloatarg(0, argc, argv);
951 float fb2 = atom_getfloatarg(1, argc, argv);
952 float ff1 = atom_getfloatarg(2, argc, argv);
953 float ff2 = atom_getfloatarg(3, argc, argv);
954 float ff3 = atom_getfloatarg(4, argc, argv);
955 float discriminant = fb1 * fb1 + 4 * fb2;
956 t_biquadctl *c = x->x_ctl;
957 if (discriminant < 0) /* imaginary roots -- resonant filter */
958 {
959 /* they're conjugates so we just check that the product
960 is less than one */
961 if (fb2 >= -1.0f) goto stable;
962 }
963 else /* real roots */
964 {
965 /* check that the parabola 1 - fb1 x - fb2 x^2 has a
966 vertex between -1 and 1, and that it's nonnegative
967 at both ends, which implies both roots are in [1-,1]. */
968 if (fb1 <= 2.0f && fb1 >= -2.0f &&
969 1.0f - fb1 -fb2 >= 0 && 1.0f + fb1 - fb2 >= 0)
970 goto stable;
971 }
972 /* if unstable, just bash to zero */
973 fb1 = fb2 = ff1 = ff2 = ff3 = 0;
974stable:
975 c->c_fb1 = fb1;
976 c->c_fb2 = fb2;
977 c->c_ff1 = ff1;
978 c->c_ff2 = ff2;
979 c->c_ff3 = ff3;
980}
981
982static void sigbiquad_set(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv)
983{
984 t_biquadctl *c = x->x_ctl;
985 c->c_x1 = atom_getfloatarg(0, argc, argv);
986 c->c_x2 = atom_getfloatarg(1, argc, argv);
987}
988
989static void sigbiquad_dsp(t_sigbiquad *x, t_signal **sp)
990{
991 dsp_add(sigbiquad_perform, 4,
992 sp[0]->s_vec, sp[1]->s_vec,
993 x->x_ctl, sp[0]->s_n);
994
995}
996
997void sigbiquad_setup(void)
998{
999 sigbiquad_class = class_new(gensym("biquad~"), (t_newmethod)sigbiquad_new,
1000 0, sizeof(t_sigbiquad), 0, A_GIMME, 0);
1001 CLASS_MAINSIGNALIN(sigbiquad_class, t_sigbiquad, x_f);
1002 class_addmethod(sigbiquad_class, (t_method)sigbiquad_dsp, gensym("dsp"), 0);
1003 class_addlist(sigbiquad_class, sigbiquad_list);
1004 class_addmethod(sigbiquad_class, (t_method)sigbiquad_set, gensym("set"),
1005 A_GIMME, 0);
1006 class_addmethod(sigbiquad_class, (t_method)sigbiquad_set, gensym("clear"),
1007 A_GIMME, 0);
1008}
1009
1010/* ---------------- samphold~ - sample and hold ----------------- */
1011
1012typedef struct sigsamphold
1013{
1014 t_object x_obj;
1015 float x_f;
1016 float x_lastin;
1017 float x_lastout;
1018} t_sigsamphold;
1019
1020t_class *sigsamphold_class;
1021
1022static void *sigsamphold_new(void)
1023{
1024 t_sigsamphold *x = (t_sigsamphold *)pd_new(sigsamphold_class);
1025 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
1026 outlet_new(&x->x_obj, gensym("signal"));
1027 x->x_lastin = 0;
1028 x->x_lastout = 0;
1029 x->x_f = 0;
1030 return (x);
1031}
1032
1033static t_int *sigsamphold_perform(t_int *w)
1034{
1035 float *in1 = (float *)(w[1]);
1036 float *in2 = (float *)(w[2]);
1037 float *out = (float *)(w[3]);
1038 t_sigsamphold *x = (t_sigsamphold *)(w[4]);
1039 int n = (t_int)(w[5]);
1040 int i;
1041 float lastin = x->x_lastin;
1042 float lastout = x->x_lastout;
1043 for (i = 0; i < n; i++, *in1++)
1044 {
1045 float next = *in2++;
1046 if (next < lastin) lastout = *in1;
1047 *out++ = lastout;
1048 lastin = next;
1049 }
1050 x->x_lastin = lastin;
1051 x->x_lastout = lastout;
1052 return (w+6);
1053}
1054
1055static void sigsamphold_dsp(t_sigsamphold *x, t_signal **sp)
1056{
1057 dsp_add(sigsamphold_perform, 5,
1058 sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec,
1059 x, sp[0]->s_n);
1060}
1061
1062static void sigsamphold_reset(t_sigsamphold *x)
1063{
1064 x->x_lastin = 1e20;
1065}
1066
1067static void sigsamphold_set(t_sigsamphold *x, t_float f)
1068{
1069 x->x_lastout = f;
1070}
1071
1072void sigsamphold_setup(void)
1073{
1074 sigsamphold_class = class_new(gensym("samphold~"),
1075 (t_newmethod)sigsamphold_new, 0, sizeof(t_sigsamphold), 0, 0);
1076 CLASS_MAINSIGNALIN(sigsamphold_class, t_sigsamphold, x_f);
1077 class_addmethod(sigsamphold_class, (t_method)sigsamphold_set,
1078 gensym("set"), A_FLOAT, 0);
1079 class_addmethod(sigsamphold_class, (t_method)sigsamphold_reset,
1080 gensym("reset"), 0);
1081 class_addmethod(sigsamphold_class, (t_method)sigsamphold_dsp,
1082 gensym("dsp"), 0);
1083}
1084
1085/* ------------------------ setup routine ------------------------- */
1086
1087void d_filter_setup(void)
1088{
1089 sighip_setup();
1090 siglop_setup();
1091 sigbp_setup();
1092 sigbiquad_setup();
1093 sigsamphold_setup();
1094}