Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / lowshelf.c
blob52c30d839dd06de5e9df6cae8de039ea057fa450
1 /* (C) Guenter Geiger <geiger@epy.co.at> */
4 /*
6 These filter coefficients computations are taken from
7 http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt
9 written by Robert Bristow-Johnson
13 #include "m_pd.h"
14 #ifdef NT
15 #pragma warning( disable : 4244 )
16 #pragma warning( disable : 4305 )
17 #endif
18 #include <math.h>
19 #include "filters.h"
23 /* ------------------- lowshelf ----------------------------*/
25 static t_class *lowshelf_class;
27 void lowshelf_bang(t_rbjfilter *x)
29 t_atom at[5];
30 t_float omega = e_omega(x->x_freq,x->x_rate);
31 t_float A = e_A(x->x_gain);
32 t_float cs = cos(omega);
33 t_float sn = sin(omega);
34 t_float beta = e_beta(A,x->x_bw*0.01);
36 t_float b0 = A*((A+1) - (A-1)*cs + beta*sn);
37 t_float b1 = 2.*A*((A-1) - (A+1)*cs);
38 t_float b2 = A*((A+1) - (A-1)*cs - beta*sn);
39 t_float a0 = ((A+1) + (A-1)*cs + beta*sn);
40 t_float a1 = -2.*((A-1) + (A+1)*cs);
41 t_float a2 = ((A+1) + (A-1)*cs - beta*sn);
43 /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw); */
45 if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) {
46 post("lowshelf: filter unstable -> resetting");
47 a0=1.;a1=0.;a2=0.;
48 b0=1.;b1=0.;b2=0.;
51 SETFLOAT(at,-a1/a0);
52 SETFLOAT(at+1,-a2/a0);
53 SETFLOAT(at+2,b0/a0);
54 SETFLOAT(at+3,b1/a0);
55 SETFLOAT(at+4,b2/a0);
57 outlet_list(x->x_obj.ob_outlet,&s_list,5,at);
61 void lowshelf_float(t_rbjfilter *x,t_floatarg f)
63 x->x_freq = f;
64 lowshelf_bang(x);
68 static void *lowshelf_new(t_floatarg f,t_floatarg g,t_floatarg bw)
70 t_rbjfilter *x = (t_rbjfilter *)pd_new(lowshelf_class);
72 x->x_rate = 44100.0;
73 outlet_new(&x->x_obj,&s_float);
74 floatinlet_new(&x->x_obj, &x->x_gain);
75 floatinlet_new(&x->x_obj, &x->x_bw);
76 if (f > 0.) x->x_freq = f;
77 if (bw > 0.) x->x_bw = bw;
78 if (g != 0.) x->x_gain = g;
79 return (x);
83 void lowshelf_setup(void)
85 lowshelf_class = class_new(gensym("lowshelf"), (t_newmethod)lowshelf_new, 0,
86 sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
87 class_addbang(lowshelf_class,lowshelf_bang);
88 class_addfloat(lowshelf_class,lowshelf_float);
92 /* (C) Guenter Geiger <geiger@epy.co.at> */
97 These filter coefficients computations are taken from
98 http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt
100 written by Robert Bristow-Johnson
104 #include "m_pd.h"
105 #ifdef NT
106 #pragma warning( disable : 4244 )
107 #pragma warning( disable : 4305 )
108 #endif
109 #include <math.h>
110 #include "filters.h"
114 /* ------------------- lowshelf ----------------------------*/
116 static t_class *lowshelf_class;
118 void lowshelf_bang(t_rbjfilter *x)
120 t_atom at[5];
121 t_float omega = e_omega(x->x_freq,x->x_rate);
122 t_float A = e_A(x->x_gain);
123 t_float cs = cos(omega);
124 t_float sn = sin(omega);
125 t_float beta = e_beta(A,x->x_bw*0.01);
127 t_float b0 = A*((A+1) - (A-1)*cs + beta*sn);
128 t_float b1 = 2.*A*((A-1) - (A+1)*cs);
129 t_float b2 = A*((A+1) - (A-1)*cs - beta*sn);
130 t_float a0 = ((A+1) + (A-1)*cs + beta*sn);
131 t_float a1 = -2.*((A-1) + (A+1)*cs);
132 t_float a2 = ((A+1) + (A-1)*cs - beta*sn);
134 /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw); */
136 if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) {
137 post("lowshelf: filter unstable -> resetting");
138 a0=1.;a1=0.;a2=0.;
139 b0=1.;b1=0.;b2=0.;
142 SETFLOAT(at,-a1/a0);
143 SETFLOAT(at+1,-a2/a0);
144 SETFLOAT(at+2,b0/a0);
145 SETFLOAT(at+3,b1/a0);
146 SETFLOAT(at+4,b2/a0);
148 outlet_list(x->x_obj.ob_outlet,&s_list,5,at);
152 void lowshelf_float(t_rbjfilter *x,t_floatarg f)
154 x->x_freq = f;
155 lowshelf_bang(x);
159 static void *lowshelf_new(t_floatarg f,t_floatarg g,t_floatarg bw)
161 t_rbjfilter *x = (t_rbjfilter *)pd_new(lowshelf_class);
163 x->x_rate = 44100.0;
164 outlet_new(&x->x_obj,&s_float);
165 floatinlet_new(&x->x_obj, &x->x_gain);
166 floatinlet_new(&x->x_obj, &x->x_bw);
167 if (f > 0.) x->x_freq = f;
168 if (bw > 0.) x->x_bw = bw;
169 if (g != 0.) x->x_gain = g;
170 return (x);
174 void lowshelf_setup(void)
176 lowshelf_class = class_new(gensym("lowshelf"), (t_newmethod)lowshelf_new, 0,
177 sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
178 class_addbang(lowshelf_class,lowshelf_bang);
179 class_addfloat(lowshelf_class,lowshelf_float);