diff options
Diffstat (limited to 'apps/plugins/pdbox/PDa/extra/lowpass.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/extra/lowpass.c | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/extra/lowpass.c b/apps/plugins/pdbox/PDa/extra/lowpass.c new file mode 100644 index 0000000000..c242aff0a8 --- /dev/null +++ b/apps/plugins/pdbox/PDa/extra/lowpass.c | |||
@@ -0,0 +1,178 @@ | |||
1 | /* (C) Guenter Geiger <geiger@epy.co.at> */ | ||
2 | |||
3 | |||
4 | /* | ||
5 | |||
6 | These filter coefficients computations are taken from | ||
7 | http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt | ||
8 | |||
9 | written by Robert Bristow-Johnson | ||
10 | |||
11 | */ | ||
12 | |||
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" | ||
20 | |||
21 | |||
22 | |||
23 | /* ------------------- lowpass ----------------------------*/ | ||
24 | |||
25 | static t_class *lowpass_class; | ||
26 | |||
27 | void lowpass_bang(t_rbjfilter *x) | ||
28 | { | ||
29 | t_atom at[5]; | ||
30 | t_float omega = e_omega(x->x_freq,x->x_rate); | ||
31 | t_float alpha = e_alpha(x->x_bw*0.01,omega); | ||
32 | t_float b1 = 1 - cos(omega); | ||
33 | t_float b0 = b1/2.; | ||
34 | t_float b2 = b0; | ||
35 | t_float a0 = 1 + alpha; | ||
36 | t_float a1 = -2.*cos(omega); | ||
37 | t_float a2 = 1 - alpha; | ||
38 | |||
39 | /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw); */ | ||
40 | |||
41 | if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) { | ||
42 | post("lowpass: filter unstable -> resetting"); | ||
43 | a0=1.;a1=0.;a2=0.; | ||
44 | b0=1.;b1=0.;b2=0.; | ||
45 | } | ||
46 | |||
47 | SETFLOAT(at,-a1/a0); | ||
48 | SETFLOAT(at+1,-a2/a0); | ||
49 | SETFLOAT(at+2,b0/a0); | ||
50 | SETFLOAT(at+3,b1/a0); | ||
51 | SETFLOAT(at+4,b2/a0); | ||
52 | |||
53 | outlet_list(x->x_obj.ob_outlet,&s_list,5,at); | ||
54 | } | ||
55 | |||
56 | |||
57 | void lowpass_float(t_rbjfilter *x,t_floatarg f) | ||
58 | { | ||
59 | x->x_freq = f; | ||
60 | lowpass_bang(x); | ||
61 | } | ||
62 | |||
63 | |||
64 | static void *lowpass_new(t_floatarg f,t_floatarg bw) | ||
65 | { | ||
66 | t_rbjfilter *x = (t_rbjfilter *)pd_new(lowpass_class); | ||
67 | |||
68 | x->x_rate = 44100.0; | ||
69 | outlet_new(&x->x_obj,&s_float); | ||
70 | /* floatinlet_new(&x->x_obj, &x->x_gain); */ | ||
71 | floatinlet_new(&x->x_obj, &x->x_bw); | ||
72 | |||
73 | if (f > 0.) x->x_freq = f; | ||
74 | if (bw > 0.) x->x_bw = bw; | ||
75 | return (x); | ||
76 | } | ||
77 | |||
78 | |||
79 | void lowpass_setup(void) | ||
80 | { | ||
81 | lowpass_class = class_new(gensym("lowpass"), (t_newmethod)lowpass_new, 0, | ||
82 | sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,0); | ||
83 | class_addbang(lowpass_class,lowpass_bang); | ||
84 | class_addfloat(lowpass_class,lowpass_float); | ||
85 | } | ||
86 | |||
87 | |||
88 | |||
89 | |||
90 | /* (C) Guenter Geiger <geiger@epy.co.at> */ | ||
91 | |||
92 | |||
93 | /* | ||
94 | |||
95 | These filter coefficients computations are taken from | ||
96 | http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt | ||
97 | |||
98 | written by Robert Bristow-Johnson | ||
99 | |||
100 | */ | ||
101 | |||
102 | #include "m_pd.h" | ||
103 | #ifdef NT | ||
104 | #pragma warning( disable : 4244 ) | ||
105 | #pragma warning( disable : 4305 ) | ||
106 | #endif | ||
107 | #include <math.h> | ||
108 | #include "filters.h" | ||
109 | |||
110 | |||
111 | |||
112 | /* ------------------- lowpass ----------------------------*/ | ||
113 | |||
114 | static t_class *lowpass_class; | ||
115 | |||
116 | void lowpass_bang(t_rbjfilter *x) | ||
117 | { | ||
118 | t_atom at[5]; | ||
119 | t_float omega = e_omega(x->x_freq,x->x_rate); | ||
120 | t_float alpha = e_alpha(x->x_bw*0.01,omega); | ||
121 | t_float b1 = 1 - cos(omega); | ||
122 | t_float b0 = b1/2.; | ||
123 | t_float b2 = b0; | ||
124 | t_float a0 = 1 + alpha; | ||
125 | t_float a1 = -2.*cos(omega); | ||
126 | t_float a2 = 1 - alpha; | ||
127 | |||
128 | /* post("bang %f %f %f",x->x_freq, x->x_gain, x->x_bw); */ | ||
129 | |||
130 | if (!check_stability(-a1/a0,-a2/a0,b0/a0,b1/a0,b2/a0)) { | ||
131 | post("lowpass: filter unstable -> resetting"); | ||
132 | a0=1.;a1=0.;a2=0.; | ||
133 | b0=1.;b1=0.;b2=0.; | ||
134 | } | ||
135 | |||
136 | SETFLOAT(at,-a1/a0); | ||
137 | SETFLOAT(at+1,-a2/a0); | ||
138 | SETFLOAT(at+2,b0/a0); | ||
139 | SETFLOAT(at+3,b1/a0); | ||
140 | SETFLOAT(at+4,b2/a0); | ||
141 | |||
142 | outlet_list(x->x_obj.ob_outlet,&s_list,5,at); | ||
143 | } | ||
144 | |||
145 | |||
146 | void lowpass_float(t_rbjfilter *x,t_floatarg f) | ||
147 | { | ||
148 | x->x_freq = f; | ||
149 | lowpass_bang(x); | ||
150 | } | ||
151 | |||
152 | |||
153 | static void *lowpass_new(t_floatarg f,t_floatarg bw) | ||
154 | { | ||
155 | t_rbjfilter *x = (t_rbjfilter *)pd_new(lowpass_class); | ||
156 | |||
157 | x->x_rate = 44100.0; | ||
158 | outlet_new(&x->x_obj,&s_float); | ||
159 | /* floatinlet_new(&x->x_obj, &x->x_gain); */ | ||
160 | floatinlet_new(&x->x_obj, &x->x_bw); | ||
161 | |||
162 | if (f > 0.) x->x_freq = f; | ||
163 | if (bw > 0.) x->x_bw = bw; | ||
164 | return (x); | ||
165 | } | ||
166 | |||
167 | |||
168 | void lowpass_setup(void) | ||
169 | { | ||
170 | lowpass_class = class_new(gensym("lowpass"), (t_newmethod)lowpass_new, 0, | ||
171 | sizeof(t_rbjfilter), 0,A_DEFFLOAT,A_DEFFLOAT,0); | ||
172 | class_addbang(lowpass_class,lowpass_bang); | ||
173 | class_addfloat(lowpass_class,lowpass_float); | ||
174 | } | ||
175 | |||
176 | |||
177 | |||
178 | |||