Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / x_acoustics.c
blob8fc04f77d11f5edc0675aee315489e9c2bf03e6a
1 /* Copyright (c) 1997-1999 Miller Puckette.
2 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
3 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
5 /* utility functions for signals
6 */
8 #include "m_pd.h"
9 #include <math.h>
10 #define LOGTEN 2.302585092994
12 float mtof(float f)
14 if (f <= -1500) return(0);
15 else if (f > 1499) return(mtof(1499));
16 else return (8.17579891564 * exp(.0577622650 * f));
19 float ftom(float f)
21 return (f > 0 ? 17.3123405046 * log(.12231220585 * f) : -1500);
24 float powtodb(float f)
26 if (f <= 0) return (0);
27 else
29 float val = 100 + 10./LOGTEN * log(f);
30 return (val < 0 ? 0 : val);
34 float rmstodb(float f)
36 if (f <= 0) return (0);
37 else
39 float val = 100 + 20./LOGTEN * log(f);
40 return (val < 0 ? 0 : val);
44 float dbtopow(float f)
46 if (f <= 0)
47 return(0);
48 else
50 if (f > 870)
51 f = 870;
52 return (exp((LOGTEN * 0.1) * (f-100.)));
56 float dbtorms(float f)
58 if (f <= 0)
59 return(0);
60 else
62 if (f > 485)
63 f = 485;
65 return (exp((LOGTEN * 0.05) * (f-100.)));
68 /* ------------- corresponding objects ----------------------- */
70 static t_class *mtof_class;
72 static void *mtof_new(void)
74 t_object *x = (t_object *)pd_new(mtof_class);
75 outlet_new(x, &s_float);
76 return (x);
79 static void mtof_float(t_object *x, t_float f)
81 outlet_float(x->ob_outlet, mtof(f));
85 static t_class *ftom_class;
87 static void *ftom_new(void)
89 t_object *x = (t_object *)pd_new(ftom_class);
90 outlet_new(x, &s_float);
91 return (x);
94 static void ftom_float(t_object *x, t_float f)
96 outlet_float(x->ob_outlet, ftom(f));
100 static t_class *rmstodb_class;
102 static void *rmstodb_new(void)
104 t_object *x = (t_object *)pd_new(rmstodb_class);
105 outlet_new(x, &s_float);
106 return (x);
109 static void rmstodb_float(t_object *x, t_float f)
111 outlet_float(x->ob_outlet, rmstodb(f));
115 static t_class *powtodb_class;
117 static void *powtodb_new(void)
119 t_object *x = (t_object *)pd_new(powtodb_class);
120 outlet_new(x, &s_float);
121 return (x);
124 static void powtodb_float(t_object *x, t_float f)
126 outlet_float(x->ob_outlet, powtodb(f));
130 static t_class *dbtopow_class;
132 static void *dbtopow_new(void)
134 t_object *x = (t_object *)pd_new(dbtopow_class);
135 outlet_new(x, &s_float);
136 return (x);
139 static void dbtopow_float(t_object *x, t_float f)
141 outlet_float(x->ob_outlet, dbtopow(f));
145 static t_class *dbtorms_class;
147 static void *dbtorms_new(void)
149 t_object *x = (t_object *)pd_new(dbtorms_class);
150 outlet_new(x, &s_float);
151 return (x);
154 static void dbtorms_float(t_object *x, t_float f)
156 outlet_float(x->ob_outlet, dbtorms(f));
160 void x_acoustics_setup(void)
162 t_symbol *s = gensym("acoustics.pd");
163 mtof_class = class_new(gensym("mtof"), mtof_new, 0,
164 sizeof(t_object), 0, 0);
165 class_addfloat(mtof_class, (t_method)mtof_float);
166 class_sethelpsymbol(mtof_class, s);
168 ftom_class = class_new(gensym("ftom"), ftom_new, 0,
169 sizeof(t_object), 0, 0);
170 class_addfloat(ftom_class, (t_method)ftom_float);
171 class_sethelpsymbol(ftom_class, s);
173 powtodb_class = class_new(gensym("powtodb"), powtodb_new, 0,
174 sizeof(t_object), 0, 0);
175 class_addfloat(powtodb_class, (t_method)powtodb_float);
176 class_sethelpsymbol(powtodb_class, s);
178 rmstodb_class = class_new(gensym("rmstodb"), rmstodb_new, 0,
179 sizeof(t_object), 0, 0);
180 class_addfloat(rmstodb_class, (t_method)rmstodb_float);
181 class_sethelpsymbol(rmstodb_class, s);
183 dbtopow_class = class_new(gensym("dbtopow"), dbtopow_new, 0,
184 sizeof(t_object), 0, 0);
185 class_addfloat(dbtopow_class, (t_method)dbtopow_float);
186 class_sethelpsymbol(dbtopow_class, s);
188 dbtorms_class = class_new(gensym("dbtorms"), dbtorms_new, 0,
189 sizeof(t_object), 0, 0);
190 class_addfloat(dbtorms_class, (t_method)dbtorms_float);
191 class_sethelpsymbol(dbtorms_class, s);
194 /* Copyright (c) 1997-1999 Miller Puckette.
195 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
196 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
198 /* utility functions for signals
201 #include "m_pd.h"
202 #include <math.h>
203 #define LOGTEN 2.302585092994
205 float mtof(float f)
207 if (f <= -1500) return(0);
208 else if (f > 1499) return(mtof(1499));
209 else return (8.17579891564 * exp(.0577622650 * f));
212 float ftom(float f)
214 return (f > 0 ? 17.3123405046 * log(.12231220585 * f) : -1500);
217 float powtodb(float f)
219 if (f <= 0) return (0);
220 else
222 float val = 100 + 10./LOGTEN * log(f);
223 return (val < 0 ? 0 : val);
227 float rmstodb(float f)
229 if (f <= 0) return (0);
230 else
232 float val = 100 + 20./LOGTEN * log(f);
233 return (val < 0 ? 0 : val);
237 float dbtopow(float f)
239 if (f <= 0)
240 return(0);
241 else
243 if (f > 870)
244 f = 870;
245 return (exp((LOGTEN * 0.1) * (f-100.)));
249 float dbtorms(float f)
251 if (f <= 0)
252 return(0);
253 else
255 if (f > 485)
256 f = 485;
258 return (exp((LOGTEN * 0.05) * (f-100.)));
261 /* ------------- corresponding objects ----------------------- */
263 static t_class *mtof_class;
265 static void *mtof_new(void)
267 t_object *x = (t_object *)pd_new(mtof_class);
268 outlet_new(x, &s_float);
269 return (x);
272 static void mtof_float(t_object *x, t_float f)
274 outlet_float(x->ob_outlet, mtof(f));
278 static t_class *ftom_class;
280 static void *ftom_new(void)
282 t_object *x = (t_object *)pd_new(ftom_class);
283 outlet_new(x, &s_float);
284 return (x);
287 static void ftom_float(t_object *x, t_float f)
289 outlet_float(x->ob_outlet, ftom(f));
293 static t_class *rmstodb_class;
295 static void *rmstodb_new(void)
297 t_object *x = (t_object *)pd_new(rmstodb_class);
298 outlet_new(x, &s_float);
299 return (x);
302 static void rmstodb_float(t_object *x, t_float f)
304 outlet_float(x->ob_outlet, rmstodb(f));
308 static t_class *powtodb_class;
310 static void *powtodb_new(void)
312 t_object *x = (t_object *)pd_new(powtodb_class);
313 outlet_new(x, &s_float);
314 return (x);
317 static void powtodb_float(t_object *x, t_float f)
319 outlet_float(x->ob_outlet, powtodb(f));
323 static t_class *dbtopow_class;
325 static void *dbtopow_new(void)
327 t_object *x = (t_object *)pd_new(dbtopow_class);
328 outlet_new(x, &s_float);
329 return (x);
332 static void dbtopow_float(t_object *x, t_float f)
334 outlet_float(x->ob_outlet, dbtopow(f));
338 static t_class *dbtorms_class;
340 static void *dbtorms_new(void)
342 t_object *x = (t_object *)pd_new(dbtorms_class);
343 outlet_new(x, &s_float);
344 return (x);
347 static void dbtorms_float(t_object *x, t_float f)
349 outlet_float(x->ob_outlet, dbtorms(f));
353 void x_acoustics_setup(void)
355 t_symbol *s = gensym("acoustics.pd");
356 mtof_class = class_new(gensym("mtof"), mtof_new, 0,
357 sizeof(t_object), 0, 0);
358 class_addfloat(mtof_class, (t_method)mtof_float);
359 class_sethelpsymbol(mtof_class, s);
361 ftom_class = class_new(gensym("ftom"), ftom_new, 0,
362 sizeof(t_object), 0, 0);
363 class_addfloat(ftom_class, (t_method)ftom_float);
364 class_sethelpsymbol(ftom_class, s);
366 powtodb_class = class_new(gensym("powtodb"), powtodb_new, 0,
367 sizeof(t_object), 0, 0);
368 class_addfloat(powtodb_class, (t_method)powtodb_float);
369 class_sethelpsymbol(powtodb_class, s);
371 rmstodb_class = class_new(gensym("rmstodb"), rmstodb_new, 0,
372 sizeof(t_object), 0, 0);
373 class_addfloat(rmstodb_class, (t_method)rmstodb_float);
374 class_sethelpsymbol(rmstodb_class, s);
376 dbtopow_class = class_new(gensym("dbtopow"), dbtopow_new, 0,
377 sizeof(t_object), 0, 0);
378 class_addfloat(dbtopow_class, (t_method)dbtopow_float);
379 class_sethelpsymbol(dbtopow_class, s);
381 dbtorms_class = class_new(gensym("dbtorms"), dbtorms_new, 0,
382 sizeof(t_object), 0, 0);
383 class_addfloat(dbtorms_class, (t_method)dbtorms_float);
384 class_sethelpsymbol(dbtorms_class, s);