Max 5 API Reference
00001 /** 00002 @file 00003 past - triggering after numbers get "past" a certain point 00004 00005 updated 3/22/09 ajm: new API 00006 00007 @ingroup examples 00008 */ 00009 00010 #include "ext.h" 00011 #include "ext_obex.h" 00012 00013 typedef struct past 00014 { 00015 t_object p_ob; 00016 t_atom p_vector[8]; 00017 short p_size; 00018 short p_set; 00019 void *p_out; 00020 } t_past; 00021 00022 void past_int(t_past *x, long n); 00023 void past_float(t_past *x, double f); 00024 void past_list(t_past *x, t_symbol *s, short ac, t_atom *av); 00025 void past_clear(t_past *x); 00026 void past_set(t_past *x, t_symbol *s, short ac, t_atom *av); 00027 void past_assist(t_past *x, void *b, long m, long a, char *s); 00028 void *past_new(t_symbol *s, short ac, t_atom *av); 00029 void past_assign(t_past *x, short ac, t_atom *av); 00030 short past_compare(t_past *x, t_atom *a, long index); 00031 00032 void *past_class; 00033 00034 int main() 00035 { 00036 t_class *c; 00037 00038 c = class_new("past", (method)past_new,0L, (short)sizeof(t_past), 0L, A_GIMME, 0); 00039 class_addmethod(c, (method)past_int, "int", A_LONG, 0); 00040 class_addmethod(c, (method)past_float, "float", A_FLOAT, 0); 00041 class_addmethod(c, (method)past_list, "list", A_GIMME,0); 00042 class_addmethod(c, (method)past_clear, "clear", 0); 00043 class_addmethod(c, (method)past_assist, "assist", A_CANT,0); 00044 class_addmethod(c, (method)past_set, "set", A_GIMME,0); 00045 class_register(CLASS_BOX, c); 00046 past_class = c; 00047 00048 return 0; 00049 } 00050 00051 void past_int(t_past *x, long n) 00052 { 00053 t_atom a; 00054 00055 atom_setlong(&a, n); 00056 if (x->p_size==1) { 00057 if (past_compare(x,&a,0) > 0) { 00058 if (!x->p_set) { 00059 x->p_set = 1; 00060 outlet_bang(x->p_out); 00061 } 00062 } else { 00063 x->p_set = 0; 00064 } 00065 } else { 00066 if (past_compare(x,&a,0) < 0) 00067 x->p_set = 0; 00068 } 00069 } 00070 00071 void past_float(t_past *x, double f) 00072 { 00073 t_atom a; 00074 00075 atom_setfloat(&a, f); 00076 if (x->p_size==1) { 00077 if (past_compare(x,&a,0) > 0) { 00078 if (!x->p_set) { 00079 x->p_set = 1; 00080 outlet_bang(x->p_out); 00081 } 00082 } else { 00083 x->p_set = 0; 00084 } 00085 } else { 00086 if (past_compare(x,&a,0) < 0) 00087 x->p_set = 0; 00088 } 00089 } 00090 00091 void past_list(t_past *x, t_symbol *s, short ac, t_atom *av) 00092 { 00093 short i; 00094 short canfail,result; 00095 00096 canfail = false; 00097 if (ac <= x->p_size) { 00098 result = past_compare(x,av,0); 00099 if (result >= 0) { 00100 if (result == 0) 00101 canfail = true; 00102 for (i=1; i < ac; i++) { 00103 result = past_compare(x,av+i,i); 00104 if (result < 0) { 00105 if (canfail) { 00106 x->p_set = 0; 00107 return; /* move this inside bracket 7/8/92 */ 00108 } 00109 } 00110 if (canfail && result == 0) 00111 canfail = 1; 00112 else 00113 canfail = 0; 00114 } 00115 if (!x->p_set) { 00116 x->p_set = 1; 00117 outlet_bang(x->p_out); 00118 } 00119 } else { 00120 x->p_set = 0; 00121 } 00122 } 00123 } 00124 00125 void past_clear(t_past *x) 00126 { 00127 x->p_set = 0; 00128 } 00129 00130 void past_set(t_past *x, t_symbol *s, short ac, t_atom *av) 00131 { 00132 if (ac) 00133 past_assign(x,ac,av); 00134 } 00135 00136 void past_assist(t_past *x, void *b, long m, long a, char *s) 00137 { 00138 if (m == ASSIST_INLET) 00139 sprintf(s,"Input to be Synchronized"); 00140 else 00141 sprintf(s,"bang When Numbers Rise Above Specified Value"); 00142 } 00143 00144 void *past_new(t_symbol *s, short ac, t_atom *av) 00145 { 00146 t_past *x; 00147 00148 x = object_alloc(past_class); 00149 x->p_out = bangout((t_object *)x); 00150 past_assign(x,ac,av); 00151 x->p_set = 0; 00152 return x; 00153 } 00154 00155 void past_assign(t_past *x, short ac, t_atom *av) 00156 { 00157 short i; 00158 00159 if (ac > 8) 00160 ac = 8; 00161 for (i = 0; i < ac; i++,av++) 00162 x->p_vector[i] = *av; 00163 x->p_size = ac; 00164 } 00165 00166 short past_compare(t_past *x, t_atom *a, long index) 00167 { 00168 t_atom *b; 00169 float temp1; 00170 00171 b = x->p_vector + index; 00172 if (atom_gettype(a) == A_LONG) { 00173 if (atom_gettype(b) == A_LONG) { 00174 if (atom_getlong(a) > atom_getlong(b)) 00175 return 1; 00176 else if (atom_getlong(a) == atom_getlong(b)) 00177 return 0; 00178 else 00179 return -1; 00180 } else if (atom_gettype(b) == A_FLOAT) { 00181 temp1 = (float)atom_getlong(a); 00182 if (temp1 > atom_getfloat(b)) 00183 return 1; 00184 else if (temp1 == atom_getfloat(b)) 00185 return 0; 00186 else 00187 return -1; 00188 } else { 00189 if (atom_getlong(a) > 0) 00190 return 1; 00191 else if (atom_getlong(a) == 0) 00192 return 0; 00193 else 00194 return -1; 00195 } 00196 } else if (atom_gettype(a) == A_FLOAT) { 00197 if (atom_gettype(b) == A_LONG) { 00198 temp1 = (float)atom_getlong(b); 00199 if (atom_getfloat(a) > temp1) 00200 return 1; 00201 else if (atom_getfloat(a) == temp1) 00202 return 0; 00203 else 00204 return -1; 00205 } else if (atom_gettype(b) == A_FLOAT) { 00206 if (atom_getfloat(a) > atom_getfloat(b)) 00207 return 1; 00208 else if (atom_getfloat(a) == atom_getfloat(b)) 00209 return 0; 00210 else 00211 return -1; 00212 } else { 00213 if (atom_getfloat(a) > 0.) 00214 return 1; 00215 else if (atom_getfloat(a) == 0.) 00216 return 0; 00217 else 00218 return -1; 00219 } 00220 } 00221 return -1; 00222 // uh, yeah, I guess. return -1 when in doubt. makes as much sense as anything else. 00223 // if someone's trying to compare a t_symbol you just say it's less than whatever you're comparing it with. 00224 // in theory this should never happen with the past object. 00225 }
Copyright © 2008, Cycling '74