aboutsummaryrefslogtreecommitdiff
path: root/average.c
diff options
context:
space:
mode:
authorHans-Christoph Steiner <eighthave@users.sourceforge.net>2005-12-08 05:24:58 +0000
committerHans-Christoph Steiner <eighthave@users.sourceforge.net>2005-12-08 05:24:58 +0000
commited5f9b5db036f8bf420b38d674c9b3b1822b3b7b (patch)
treeb287269dec9a86175f1059cfb562754855e7f6af /average.c
parent9b2014ed31750d9573d70d998cb3a3c3c4803b42 (diff)
converted things to fit in with the namespace
svn path=/trunk/externals/markex/; revision=4167
Diffstat (limited to 'average.c')
-rw-r--r--average.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/average.c b/average.c
new file mode 100644
index 0000000..39ac562
--- /dev/null
+++ b/average.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 1997-1999 Mark Danks.
+ * For information on usage and redistribution, and for a DISCLAIMER OF ALL
+ * WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
+ */
+
+#include "m_pd.h"
+
+/* -------------------------- alternate ------------------------------ */
+
+/* instance structure */
+static t_class *average_class;
+
+typedef struct _average
+{
+ t_object x_obj; /* obligatory object header */
+ int a_total; /* number of numbers to average */
+ int a_whichNum; /* which number are pointing at */
+ float a_numbers[100]; /* numbers to average, 100 maximum */
+ t_outlet *t_out1; /* the outlet */
+} t_average;
+
+void average_bang(t_average *x)
+{
+ float average = 0.0f;
+ int n;
+
+ for (n = 0; n < x->a_total; n++) average = average + x->a_numbers[n];
+ average = average / (float)x->a_total;
+
+ outlet_float(x->t_out1, average);
+}
+
+void average_float(t_average *x, t_floatarg n)
+{
+ if (x->a_whichNum >= x->a_total) x->a_whichNum = 0;
+ x->a_numbers[x->a_whichNum] = n;
+ x->a_whichNum++;
+ average_bang(x);
+}
+
+void average_total(t_average *x, t_floatarg n)
+{
+ x->a_total = (int)n;
+}
+
+void average_reset(t_average *x, t_floatarg newVal)
+{
+ int n;
+ for (n=0; n < 100; n ++) x->a_numbers[n] = newVal;
+}
+
+void average_clear(t_average *x)
+{
+ int n;
+ for ( n = 0; n < 100; n ++) x->a_numbers[n] = 0.0f;
+}
+
+void *average_new(t_floatarg f) /* init vals in struc */
+{
+ t_average *x = (t_average *)pd_new(average_class);
+ x->t_out1 = outlet_new(&x->x_obj, 0);
+ inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("fl1"));
+ average_clear(x);
+ if (f) x->a_total = (int)f;
+ else x->a_total = 10;
+ x->a_whichNum = 0;
+ return (x);
+}
+
+void average_setup(void)
+{
+ average_class = class_new(gensym("average"), (t_newmethod)average_new, 0,
+ sizeof(t_average), 0, A_DEFFLOAT, 0);
+ class_addbang(average_class, (t_method)average_bang);
+ class_addfloat(average_class, (t_method)average_float);
+ class_addmethod(average_class, (t_method)average_total, gensym("fl1"), A_FLOAT, 0);
+ class_addmethod(average_class, (t_method)average_clear, gensym("clear"), A_NULL);
+ class_addmethod(average_class, (t_method)average_reset, gensym("reset"), A_FLOAT, 0);
+
+ #if PD_MINOR_VERSION < 37
+
+#endif
+}
+