aboutsummaryrefslogtreecommitdiff
path: root/op~
diff options
context:
space:
mode:
authorMartin Peach <mrpeach@users.sourceforge.net>2010-03-23 21:11:28 +0000
committerMartin Peach <mrpeach@users.sourceforge.net>2010-03-23 21:11:28 +0000
commitaac2fa3f2d131ebca1378d93445bd1090b16c84c (patch)
treedf172d03effa3dccf7bb03f7af46f1fdb2ae6b13 /op~
parentf6971e380f4a69324d25eab22e08a6d9f0dd727e (diff)
signal/float operators like [>=~] from zexy but operator can be reconfigured on the fly.
svn path=/trunk/externals/mrpeach/; revision=13248
Diffstat (limited to 'op~')
-rw-r--r--op~/op~-help.pd82
-rw-r--r--op~/op~.c238
2 files changed, 320 insertions, 0 deletions
diff --git a/op~/op~-help.pd b/op~/op~-help.pd
new file mode 100644
index 0000000..7a36dc3
--- /dev/null
+++ b/op~/op~-help.pd
@@ -0,0 +1,82 @@
+#N canvas 317 25 826 629 10;
+#X obj 152 267 dac~;
+#X obj 168 205 line~;
+#X obj 171 162 hsl 128 15 0 1 0 0 empty empty level -2 -8 0 10 -4034
+-13381 -1 0 1;
+#X msg 168 182 \$1 100;
+#X obj 153 230 *~;
+#X obj 154 99 op~ <;
+#X obj 180 -25 osc~ 120;
+#X obj 153 -46 osc~ 0.01;
+#X obj 154 125 lop~ 10000;
+#X obj -197 216 env~;
+#X floatatom -197 238 5 0 0 0 - - -;
+#X floatatom -196 148 5 0 0 0 - - -;
+#X floatatom -163 170 5 0 0 0 - - -;
+#X obj -196 194 op~ !=;
+#X text -197 94 op~ compares two inlet signals or floats according
+to the operator specified in the creation argument.;
+#X text -186 330 Author: Martin Peach \, 2010/03/23. Based on zexy
+[<~] etc.;
+#X msg 113 41 help;
+#X obj 361 133 op~ >=;
+#X obj 360 68 noise~;
+#X floatatom 394 102 5 0 0 0 - - -;
+#X obj 360 305 dac~;
+#X obj 376 243 line~;
+#X obj 379 200 hsl 128 15 0 1 0 0 empty empty level -2 -8 0 10 -4034
+-13381 -1 0 1;
+#X msg 376 220 \$1 100;
+#X obj 361 268 *~;
+#X obj 362 164 lop~ 100;
+#X msg 22 -50 op <;
+#X msg 42 -30 op >;
+#X msg 62 -10 op ==;
+#X msg 82 10 op !=;
+#X msg 2 -70 op <=;
+#X msg -20 -92 op >=;
+#X msg -105 -177 op ge;
+#X msg -125 -197 op le;
+#X msg -145 -217 op lt;
+#X msg -82 -154 op gt;
+#X msg -61 -133 op eq;
+#X msg -41 -113 op ne;
+#X msg -166 -238 op nop;
+#X text -98 40 Print a list of allowed operators:;
+#X text -148 -92 set operator to >=:;
+#X connect 1 0 4 1;
+#X connect 2 0 3 0;
+#X connect 3 0 1 0;
+#X connect 4 0 0 0;
+#X connect 4 0 0 1;
+#X connect 5 0 8 0;
+#X connect 6 0 5 1;
+#X connect 7 0 5 0;
+#X connect 8 0 4 0;
+#X connect 9 0 10 0;
+#X connect 11 0 13 0;
+#X connect 12 0 13 1;
+#X connect 13 0 9 0;
+#X connect 16 0 5 0;
+#X connect 17 0 25 0;
+#X connect 18 0 17 0;
+#X connect 19 0 17 1;
+#X connect 21 0 24 1;
+#X connect 22 0 23 0;
+#X connect 23 0 21 0;
+#X connect 24 0 20 0;
+#X connect 24 0 20 1;
+#X connect 25 0 24 0;
+#X connect 26 0 5 0;
+#X connect 27 0 5 0;
+#X connect 28 0 5 0;
+#X connect 29 0 5 0;
+#X connect 30 0 5 0;
+#X connect 31 0 5 0;
+#X connect 32 0 5 0;
+#X connect 33 0 5 0;
+#X connect 34 0 5 0;
+#X connect 35 0 5 0;
+#X connect 36 0 5 0;
+#X connect 37 0 5 0;
+#X connect 38 0 5 0;
diff --git a/op~/op~.c b/op~/op~.c
new file mode 100644
index 0000000..8a15bc3
--- /dev/null
+++ b/op~/op~.c
@@ -0,0 +1,238 @@
+/* op~ by Martin Peach 201003123, based on the zexy binop externals: */
+/******************************************************
+ *
+ * zexy - implementation file
+ *
+ * copyleft (c) IOhannes m zmölnig
+ *
+ * 1999:forum::für::umläute:2004
+ *
+ * institute of electronic music and acoustics (iem)
+ *
+ ******************************************************
+ *
+ * license: GNU General Public License v.2
+ *
+ ******************************************************/
+
+/* ----------------------------- op_tilde ----------------------------- */
+#include "m_pd.h"
+
+static t_class *op_tilde_class;
+
+static char *operator_names[] = {"<=", ">=", "!=", "==", "<", ">", "le", "ge", "ne", "eq", "lt", "gt", "nop"};
+static int n_opnames = 13;
+
+typedef enum
+{
+ le = 0,
+ ge,
+ ne,
+ eq,
+ lt,
+ gt,
+ nop,
+ none
+} op_op;
+
+typedef struct _op_tilde
+{
+ t_object x_obj;
+ t_float x_f;
+ t_float x_g; /* inlet value */
+ op_op x_operator;
+} t_op_tilde;
+
+
+static void *op_tilde_new(t_symbol *s, int argc, t_atom *argv);
+static t_int *op_tilde_perform(t_int *w);
+static void op_tilde_help(t_op_tilde *x);
+static void op_tilde_op(t_op_tilde *x, t_symbol *op);
+static void op_tilde_dsp(t_op_tilde *x, t_signal **sp);
+void op_tilde_setup(void);
+
+static void *op_tilde_new(t_symbol *s, int argc, t_atom *argv)
+{
+ t_symbol *argsym;
+
+ if (argc > 1) post("op~: extra arguments ignored");
+ if (argc == 0) post("op~: need to specify operator");
+ argsym = atom_getsymbolarg(0, argc, argv);
+// post ("op~: %s", argsym->s_name);
+
+ t_op_tilde *x = (t_op_tilde *)pd_new(op_tilde_class);
+
+ x->x_operator = none;
+
+ switch (argsym->s_name[0])
+ {
+ case '<':
+ if (argsym->s_name[1] == 0) x->x_operator = lt;
+ else if (argsym->s_name[1] == '=') x->x_operator = le;
+ break;
+ case '>':
+ if (argsym->s_name[1] == 0) x->x_operator = gt;
+ else if (argsym->s_name[1] == '=') x->x_operator = ge;
+ break;
+ case '=':
+ if (argsym->s_name[1] == '=') x->x_operator = eq;
+ break;
+ case '!':
+ if (argsym->s_name[1] == '=') x->x_operator = ne;
+ break;
+ case 'n':
+ if
+ (
+ (argsym->s_name[1] == 'o')
+ && (argsym->s_name[2] == 'p')
+ && (argsym->s_name[3] == '\0')
+ )
+ x->x_operator = nop;
+ break;
+ }
+ if (x->x_operator == none)
+ {
+ post("op~: unknown operator");
+ x->x_operator = nop;
+ }
+ inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
+ outlet_new(&x->x_obj, &s_signal);
+ x->x_f = 0;
+ x->x_g = 0;
+ return (x);
+}
+
+static t_int *op_tilde_perform(t_int *w)
+{
+ t_op_tilde *x = (t_op_tilde *)(w[1]);
+ t_sample *in1 = (t_sample *)(w[2]);
+ t_sample *in2 = (t_sample *)(w[3]);
+ t_sample *out = (t_sample *)(w[4]);
+ int n = (int)(w[5]);
+
+ switch (x->x_operator)
+ {
+ case le:
+ while (n--) *out++ = *in1++ <= *in2++;
+ break;
+ case ge:
+ while (n--) *out++ = *in1++ >= *in2++;
+ break;
+ case ne:
+ while (n--) *out++ = *in1++ != *in2++;
+ break;
+ case eq:
+ while (n--) *out++ = *in1++ == *in2++;
+ break;
+ case lt:
+ while (n--) *out++ = *in1++ < *in2++;
+ break;
+ case gt:
+ while (n--) *out++ = *in1++ > *in2++;
+ break;
+ case nop:
+ default:
+ while (n--) *out++ = 0;
+ break;
+ }
+ return (w+6);
+}
+
+static void op_tilde_help(t_op_tilde *x)
+{
+ int i;
+
+ post("op~: available operators:");
+ for (i = 0; i < n_opnames; ++i)
+ {
+ post ("%s", operator_names[i]);
+ }
+}
+
+static void op_tilde_op(t_op_tilde *x, t_symbol *op)
+{
+ int i, j = 0;
+
+ while ((op->s_name[j] != '\0') && (j < 3)) ++j;
+ if ((j < 1) || (j > 3))
+ {
+ post("op~: %s is not a valid operator (length %d)", op->s_name, j);
+ return;
+ }
+ for (i = 0; i < n_opnames; ++i)
+ {
+ if (operator_names[i][0] != op->s_name[0]) continue;
+ if ((j == 2) && (operator_names[i][1] != op->s_name[1])) continue;
+ if ((j == 3) && (operator_names[i][2] != op->s_name[2])) continue;
+ if ((j == 1) && (operator_names[i][1] != '\0')) continue;
+ break;
+ }
+ if (i == n_opnames)
+ {
+ post("op~: %s is not a valid operator (match)", op->s_name);
+ return;
+ }
+switch(i)
+ {
+ /*"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"*/
+ /*"<=", ">=", "!=", "==", "<", ">", "le", "ge", "ne", "eq", "lt", "gt", "nop"*/
+ case 0:
+ case 6:
+ /* "<=" "le" */
+ x->x_operator = 0;
+ break;
+ case 1:
+ case 7:
+ /* ">=" "ge" */
+ x->x_operator = 1;
+ break;
+ case 2:
+ case 8:
+ /* "!=" "ne" */
+ x->x_operator = 2;
+ break;
+ case 3:
+ case 9:
+ /* "==" "eq" */
+ x->x_operator = 3;
+ break;
+ case 4:
+ case 10:
+ /* "<" "lt" */
+ x->x_operator = 4;
+ break;
+ case 5:
+ case 11:
+ /* ">" "gt" */
+ x->x_operator = 5;
+ break;
+ default:
+ /* "nop" */
+ x->x_operator = 6;
+ break;
+ }
+// post("op~: operator %s", operator_names[i]);
+}
+
+static void op_tilde_dsp(t_op_tilde *x, t_signal **sp)
+{
+ t_sample *in1 = sp[0]->s_vec;
+ t_sample *in2 = sp[1]->s_vec;
+ t_sample *out = sp[2]->s_vec;
+ int n = sp[0]->s_n;
+
+ dsp_add(op_tilde_perform, 5, x, in1, in2, out, n);
+}
+
+void op_tilde_setup(void)
+{
+ op_tilde_class = class_new(gensym("op~"), (t_newmethod)op_tilde_new, 0,
+ sizeof(t_op_tilde), 0, A_GIMME, 0);
+ class_addmethod(op_tilde_class, (t_method)op_tilde_dsp, gensym("dsp"), 0);
+ class_addmethod(op_tilde_class, (t_method)op_tilde_help, gensym("help"), 0);
+ class_addmethod(op_tilde_class, (t_method)op_tilde_op, gensym("op"), A_DEFSYMBOL, 0);
+ CLASS_MAINSIGNALIN(op_tilde_class, t_op_tilde, x_f);
+}
+
+/* fin op~.c */
+