Add the identifying header
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / lowshelf.c
blob3722f8c0d4504aeed2c097e2e9512faa76190f02
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 #ifdef ROCKBOX
14 #include "plugin.h"
15 #include "../../pdbox.h"
16 #include "../src/m_pd.h"
17 #include "math.h"
18 #include "filters.h"
19 #else /* ROCKBOX */
20 #include "../src/m_pd.h"
21 #ifdef NT
22 #pragma warning( disable : 4244 )
23 #pragma warning( disable : 4305 )
24 #endif
25 #include <math.h>
26 #include "filters.h"
27 #endif /* ROCKBOX */
30 /* ------------------- lowshelf ----------------------------*/
32 static t_class *lowshelf_class;
34 void lowshelf_bang(t_rbjfilter *x)
36 t_atom at[5];
37 t_float omega = e_omega(x->x_freq,x->x_rate);
38 t_float A = e_A(x->x_gain);
39 t_float cs = cos(omega);
40 t_float sn = sin(omega);
41 t_float beta = e_beta(A,x->x_bw*0.01);
43 t_float b0 = A*((A+1) - (A-1)*cs + beta*sn);
44 t_float b1 = 2.*A*((A-1) - (A+1)*cs);
45 t_float b2 = A*((A+1) - (A-1)*cs - beta*sn);
46 t_float a0 = ((A+1) + (A-1)*cs + beta*sn);
47 t_float a1 = -2.*((A-1) + (A+1)*cs);
48 t_float a2 = ((A+1) + (A-1)*cs - beta*sn);
50 /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw); */
52 if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) {
53 post("lowshelf: filter unstable -> resetting");
54 a0=1.;a1=0.;a2=0.;
55 b0=1.;b1=0.;b2=0.;
58 SETFLOAT(at,-a1/a0);
59 SETFLOAT(at+1,-a2/a0);
60 SETFLOAT(at+2,b0/a0);
61 SETFLOAT(at+3,b1/a0);
62 SETFLOAT(at+4,b2/a0);
64 outlet_list(x->x_obj.ob_outlet,&s_list,5,at);
68 void lowshelf_float(t_rbjfilter *x,t_floatarg f)
70 x->x_freq = f;
71 lowshelf_bang(x);
75 static void *lowshelf_new(t_floatarg f,t_floatarg g,t_floatarg bw)
77 t_rbjfilter *x = (t_rbjfilter *)pd_new(lowshelf_class);
79 x->x_rate = 44100.0;
80 outlet_new(&x->x_obj,&s_float);
81 floatinlet_new(&x->x_obj, &x->x_gain);
82 floatinlet_new(&x->x_obj, &x->x_bw);
83 if (f > 0.) x->x_freq = f;
84 if (bw > 0.) x->x_bw = bw;
85 if (g != 0.) x->x_gain = g;
86 return (x);
90 void lowshelf_setup(void)
92 lowshelf_class = class_new(gensym("lowshelf"), (t_newmethod)lowshelf_new, 0,
93 sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
94 class_addbang(lowshelf_class,lowshelf_bang);
95 class_addfloat(lowshelf_class,lowshelf_float);