Add the identifying header
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / bandpass.c
blob09bbbacbbaff78fe866ddbf6aca4332fb67f3588
2 /* (C) Guenter Geiger <geiger@epy.co.at> */
5 /*
7 These filter coefficients computations are taken from
8 http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt
10 written by Robert Bristow-Johnson
14 #ifdef ROCKBOX
15 #include "plugin.h"
16 #include "../../pdbox.h"
17 #include "../src/m_pd.h"
18 #include "math.h"
19 #include "filters.h"
20 #else /* ROCKBOX */
21 #include "../src/m_pd.h"
22 #ifdef NT
23 #pragma warning( disable : 4244 )
24 #pragma warning( disable : 4305 )
25 #endif
26 #include <math.h>
27 #include "filters.h"
28 #endif /* ROCKBOX */
30 /* ------------------- bandpass ----------------------------*/
32 static t_class *bandpass_class;
34 void bandpass_bang(t_rbjfilter *x)
36 t_atom at[5];
37 t_float omega = e_omega(x->x_freq,x->x_rate);
38 t_float alpha = e_alpha(x->x_bw* 0.01,omega);
39 t_float b1 = 0.;
40 t_float b0 = alpha;
41 t_float b2 = -alpha;
42 t_float a0 = 1 + alpha;
43 t_float a1 = -2.*cos(omega);
44 t_float a2 = 1 - alpha;
46 /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw); */
48 if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) {
49 post("bandpass: filter unstable -> resetting");
50 a0=1.;a1=0.;a2=0.;
51 b0=1.;b1=0.;b2=0.;
54 SETFLOAT(at,-a1/a0);
55 SETFLOAT(at+1,-a2/a0);
56 SETFLOAT(at+2,b0/a0);
57 SETFLOAT(at+3,b1/a0);
58 SETFLOAT(at+4,b2/a0);
60 outlet_list(x->x_obj.ob_outlet,&s_list,5,at);
64 void bandpass_float(t_rbjfilter *x,t_floatarg f)
66 x->x_freq = f;
67 bandpass_bang(x);
71 static void *bandpass_new(t_floatarg f,t_floatarg bw)
73 t_rbjfilter *x = (t_rbjfilter *)pd_new(bandpass_class);
75 x->x_rate = 44100.0;
76 outlet_new(&x->x_obj,&s_float);
77 /* floatinlet_new(&x->x_obj, &x->x_gain); */
78 floatinlet_new(&x->x_obj, &x->x_bw);
79 if (f > 0.) x->x_freq = f;
80 if (bw > 0.) x->x_bw = bw;
81 return (x);
85 void bandpass_setup(void)
87 bandpass_class = class_new(gensym("bandpass"), (t_newmethod)bandpass_new, 0,
88 sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,0);
89 class_addbang(bandpass_class,bandpass_bang);
90 class_addfloat(bandpass_class,bandpass_float);