Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / highshelf.c
blob0060d896c2ed49b91cd7a45a8b8effe93fd3f52f
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"
22 /* ------------------- highshelf ----------------------------*/
24 static t_class *highshelf_class;
26 void highshelf_bang(t_rbjfilter *x)
28 t_atom at[5];
29 t_float omega = e_omega(x->x_freq,x->x_rate);
30 t_float A = e_A(x->x_gain);
31 t_float cs = cos(omega);
32 t_float sn = sin(omega);
33 t_float beta = e_beta(A,x->x_bw* 0.01);
35 t_float b0 = A*((A+1) + (A-1)*cs + beta*sn);
36 t_float b1 =-2.*A*((A-1) + (A+1)*cs);
37 t_float b2 = A*((A+1) + (A-1)*cs - beta*sn);
38 t_float a0 = ((A+1) - (A-1)*cs + beta*sn);
39 t_float a1 = 2.*((A-1) - (A+1)*cs);
40 t_float a2 = ((A+1) - (A-1)*cs - beta*sn);
42 /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw);*/
44 if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) {
45 post("highshelf: filter unstable -> resetting");
46 a0=1.;a1=0.;a2=0.;
47 b0=1.;b1=0.;b2=0.;
50 SETFLOAT(at,-a1/a0);
51 SETFLOAT(at+1,-a2/a0);
52 SETFLOAT(at+2,b0/a0);
53 SETFLOAT(at+3,b1/a0);
54 SETFLOAT(at+4,b2/a0);
56 outlet_list(x->x_obj.ob_outlet,&s_list,5,at);
60 void highshelf_float(t_rbjfilter *x,t_floatarg f)
62 x->x_freq = f;
63 highshelf_bang(x);
67 static void *highshelf_new(t_floatarg f,t_floatarg g,t_floatarg bw)
69 t_rbjfilter *x = (t_rbjfilter *)pd_new(highshelf_class);
71 x->x_rate = 44100.0;
72 outlet_new(&x->x_obj,&s_float);
73 floatinlet_new(&x->x_obj, &x->x_gain);
74 floatinlet_new(&x->x_obj, &x->x_bw);
75 if (f > 0.) x->x_freq = f;
76 if (bw > 0.) x->x_bw = bw;
77 if (g != 0.) x->x_gain = g;
78 return (x);
82 void highshelf_setup(void)
84 highshelf_class = class_new(gensym("highshelf"), (t_newmethod)highshelf_new, 0,
85 sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
86 class_addbang(highshelf_class,highshelf_bang);
87 class_addfloat(highshelf_class,highshelf_float);
91 /* (C) Guenter Geiger <geiger@epy.co.at> */
96 These filter coefficients computations are taken from
97 http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt
99 written by Robert Bristow-Johnson
103 #include "m_pd.h"
104 #ifdef NT
105 #pragma warning( disable : 4244 )
106 #pragma warning( disable : 4305 )
107 #endif
108 #include <math.h>
109 #include "filters.h"
112 /* ------------------- highshelf ----------------------------*/
114 static t_class *highshelf_class;
116 void highshelf_bang(t_rbjfilter *x)
118 t_atom at[5];
119 t_float omega = e_omega(x->x_freq,x->x_rate);
120 t_float A = e_A(x->x_gain);
121 t_float cs = cos(omega);
122 t_float sn = sin(omega);
123 t_float beta = e_beta(A,x->x_bw* 0.01);
125 t_float b0 = A*((A+1) + (A-1)*cs + beta*sn);
126 t_float b1 =-2.*A*((A-1) + (A+1)*cs);
127 t_float b2 = A*((A+1) + (A-1)*cs - beta*sn);
128 t_float a0 = ((A+1) - (A-1)*cs + beta*sn);
129 t_float a1 = 2.*((A-1) - (A+1)*cs);
130 t_float a2 = ((A+1) - (A-1)*cs - beta*sn);
132 /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw);*/
134 if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) {
135 post("highshelf: filter unstable -> resetting");
136 a0=1.;a1=0.;a2=0.;
137 b0=1.;b1=0.;b2=0.;
140 SETFLOAT(at,-a1/a0);
141 SETFLOAT(at+1,-a2/a0);
142 SETFLOAT(at+2,b0/a0);
143 SETFLOAT(at+3,b1/a0);
144 SETFLOAT(at+4,b2/a0);
146 outlet_list(x->x_obj.ob_outlet,&s_list,5,at);
150 void highshelf_float(t_rbjfilter *x,t_floatarg f)
152 x->x_freq = f;
153 highshelf_bang(x);
157 static void *highshelf_new(t_floatarg f,t_floatarg g,t_floatarg bw)
159 t_rbjfilter *x = (t_rbjfilter *)pd_new(highshelf_class);
161 x->x_rate = 44100.0;
162 outlet_new(&x->x_obj,&s_float);
163 floatinlet_new(&x->x_obj, &x->x_gain);
164 floatinlet_new(&x->x_obj, &x->x_bw);
165 if (f > 0.) x->x_freq = f;
166 if (bw > 0.) x->x_bw = bw;
167 if (g != 0.) x->x_gain = g;
168 return (x);
172 void highshelf_setup(void)
174 highshelf_class = class_new(gensym("highshelf"), (t_newmethod)highshelf_new, 0,
175 sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
176 class_addbang(highshelf_class,highshelf_bang);
177 class_addfloat(highshelf_class,highshelf_float);