diff options
author | Hans-Christoph Steiner <eighthave@users.sourceforge.net> | 2003-04-01 05:54:43 +0000 |
---|---|---|
committer | Hans-Christoph Steiner <eighthave@users.sourceforge.net> | 2003-04-01 05:54:43 +0000 |
commit | 5918e79f1e2930df61d8b9ffd5ad607156cae3e5 (patch) | |
tree | eba68c15f0beb9935144d38a0765d1e594fbec7e | |
parent | 3476906c5f949c393083a262060ac00d5e01054b (diff) |
renamed Olaf's maxlib/scale.c to range.c to get around name conflict with GEM
svn path=/trunk/externals/hcs/; revision=522
-rw-r--r-- | range.c | 128 | ||||
-rw-r--r-- | range.pd | 31 |
2 files changed, 159 insertions, 0 deletions
@@ -0,0 +1,128 @@ +/* ------------------------- range ------------------------------------------ */
+/* */
+/* Ranges input to lie within an output range. */
+/* Written by Olaf Matthes <olaf.matthes@gmx.de> */
+/* Get source at http://www.akustische-kunst.org/puredata/maxlib/ */
+/* */
+/* This program is free software; you can redistribute it and/or */
+/* modify it under the terms of the GNU General Public License */
+/* as published by the Free Software Foundation; either version 2 */
+/* of the License, or (at your option) any later version. */
+/* */
+/* This program is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
+/* GNU General Public License for more details. */
+/* */
+/* You should have received a copy of the GNU General Public License */
+/* along with this program; if not, write to the Free Software */
+/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+/* */
+/* Based on PureData by Miller Puckette and others. */
+/* */
+/* ---------------------------------------------------------------------------- */
+
+#include "m_pd.h"
+#include <stdio.h>
+#include <math.h>
+
+static char *version = "range v0.2, written by Olaf Matthes <olaf.matthes@gmx.de>";
+
+typedef struct range
+{
+ t_object x_ob;
+ t_float x_f; /* current input value */
+ t_float x_il; /* low border of input range */
+ t_float x_ih; /* high border of input range */
+ t_float x_ol; /* low border of output range */
+ t_float x_oh; /* high border of output range */
+ t_float x_logcoeff; /* log-coefficient */
+ t_outlet *x_outlet1; /* result */
+} t_range;
+
+static void range_float(t_range *x, t_floatarg f)
+{
+ t_float ir = x->x_ih - x->x_il;
+ t_float or = x->x_oh - x->x_ol;
+ double oq;
+ double result;
+ double k;
+ if(ir == 0)
+ {
+ post("range: input range must not be 0");
+ return;
+ }
+ /* we accept an output range of 0 in case someone really wants this */
+ if(!x->x_logcoeff) /* linear */
+ {
+ k = (or / ir);
+ result = ((f - x->x_il) * k) + x->x_ol;
+ }
+ else /* logarythmical range */
+ {
+ oq = x->x_oh / x->x_ol;
+ // k = (log((double)oq)/log(x->x_logcoeff))/((double)ir);
+ k = log((double)oq)/((double)ir);
+
+ if(x->x_ol)
+ {
+ // result = (double)x->x_ol*exp(k*(double)(f - x->x_il)*log(x->x_logcoeff));
+ result = (double)x->x_ol*exp(k*(double)(f - x->x_il));
+ }
+ else
+ {
+ /* in case the low output is 0 we have to cheat... */
+ /* okay, here's the chating: we calculate for a lower out limit
+ of 1 and remove this shift after the calculation */
+ result = ((double)(x->x_ol+1)*exp(k*(double)(f - x->x_il)))-1.0;
+ }
+ }
+
+ x->x_f = f; /* save current input value */
+
+ outlet_float(x->x_outlet1, result);
+}
+
+static void range_bang(t_range *x)
+{
+ range_float(x, x->x_f); /* recalculate result */
+}
+
+static t_class *range_class;
+
+static void *range_new(t_floatarg fil, t_floatarg fih, t_floatarg fol, t_floatarg foh, t_floatarg flc)
+{
+ t_range *x = (t_range *)pd_new(range_class);
+
+ floatinlet_new(&x->x_ob, &x->x_il);
+ floatinlet_new(&x->x_ob, &x->x_ih);
+ floatinlet_new(&x->x_ob, &x->x_ol);
+ floatinlet_new(&x->x_ob, &x->x_oh);
+ floatinlet_new(&x->x_ob, &x->x_logcoeff);
+
+ x->x_outlet1 = outlet_new(&x->x_ob, gensym("float"));
+
+ /* default values taken from Max's range */
+ x->x_il = fil;
+ x->x_ih = fih;
+ if(!x->x_ih)x->x_ih = 127.0;
+ x->x_ol = fol;
+ x->x_oh = foh;
+ if(!x->x_oh)x->x_oh = 1.0;
+ x->x_logcoeff = flc;
+ x->x_f = 0;
+
+ post(version);
+
+ return (void *)x;
+}
+
+void range_setup(void)
+{
+ range_class = class_new(gensym("range"), (t_newmethod)range_new,
+ 0, sizeof(t_range), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
+ class_addfloat(range_class, range_float);
+ class_addbang(range_class, range_bang);
+ class_sethelpsymbol(range_class, gensym("range.pd"));
+}
+
diff --git a/range.pd b/range.pd new file mode 100644 index 0000000..8a3b11c --- /dev/null +++ b/range.pd @@ -0,0 +1,31 @@ +#N canvas 381 126 552 355 12;
+#X floatatom 27 277 8 0 0;
+#X floatatom 27 73 5 0 0;
+#X text 213 48 written by <olaf.matthes@gmx.de>;
+#X text 37 306 creation:;
+#X text 141 11 range :: range input from a certain input range;
+#X text 212 29 to lie between output boundaries;
+#X floatatom 56 131 5 0 0;
+#X floatatom 85 152 5 0 0;
+#X floatatom 115 173 5 0 0;
+#X floatatom 144 194 5 0 0;
+#X text 84 71 input value;
+#X text 106 278 ranged output value;
+#X text 111 130 in low;
+#X text 137 151 in high;
+#X text 171 172 out low;
+#X text 200 194 out high;
+#X text 57 104 creation arguments can be changed dynamically:;
+#X text 53 323 range <in low> <in high> <out low> <out high> <log coeff>
+;
+#X obj 27 249 range 0 9 100 255 0;
+#X floatatom 174 220 5 0 0;
+#X text 227 219 log coefficient;
+#X text 265 237 0 = linear 1 = log;
+#X connect 1 0 18 0;
+#X connect 6 0 18 1;
+#X connect 7 0 18 2;
+#X connect 8 0 18 3;
+#X connect 9 0 18 4;
+#X connect 18 0 0 0;
+#X connect 19 0 18 5;
|