Max 5 API Reference
00001 /** 00002 @file 00003 thresh - forming lists 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 #include "ext_common.h" 00013 00014 void *thresh_class; 00015 00016 #define MAXSIZE 4096 00017 00018 typedef struct thresh { 00019 t_object t_ob; 00020 t_atom *t_av; 00021 int t_ac; 00022 void *t_clock; 00023 long t_interval; 00024 long t_time; 00025 00026 void *t_out; 00027 } t_thresh; 00028 00029 t_symbol *ps_list; 00030 00031 void thresh_int(t_thresh *x, long n); 00032 void thresh_float(t_thresh *x, double f); 00033 void thresh_bang(t_thresh *x); 00034 void thresh_list(t_thresh *x, t_symbol *s, short ac, t_atom *av); 00035 void thresh_tick(t_thresh *x); 00036 void thresh_in1(t_thresh *x, long n); 00037 void thresh_free(t_thresh *x); 00038 void thresh_assist(t_thresh *x, void *b, long m, long a, char *s); 00039 void thresh_inletinfo(t_thresh *x, void *b, long a, char *t); 00040 void *thresh_new(long interval); 00041 00042 int main() 00043 { 00044 t_class *c; 00045 00046 c = class_new("thresh", (method)thresh_new, (method)thresh_free, (short)sizeof(t_thresh), 0L, A_DEFLONG, 0); 00047 class_addmethod(c, (method)thresh_int, "int", A_LONG, 0); 00048 class_addmethod(c, (method)thresh_list,"list", A_GIMME, 0); 00049 class_addmethod(c, (method)thresh_in1, "in1", A_LONG, 0); 00050 class_addmethod(c, (method)thresh_float, "float", A_FLOAT, 0); 00051 class_addmethod(c, (method)thresh_assist,"assist",A_CANT,0); 00052 class_addmethod(c, (method)thresh_inletinfo, "inletinfo", A_CANT, 0); 00053 class_register(CLASS_BOX, c); 00054 thresh_class = c; 00055 00056 ps_list = gensym("list"); 00057 00058 return 0; 00059 } 00060 00061 void thresh_int(t_thresh *x, long n) 00062 { 00063 if (x->t_ac < MAXSIZE - 1) { 00064 x->t_time = gettime(); 00065 atom_setlong(x->t_av+x->t_ac,n); 00066 x->t_ac++; 00067 clock_delay(x->t_clock,x->t_interval); 00068 } 00069 } 00070 00071 void thresh_float(t_thresh *x, double f) 00072 { 00073 if (x->t_ac < MAXSIZE - 1) { 00074 x->t_time = gettime(); 00075 atom_setfloat(x->t_av+x->t_ac,f); 00076 x->t_ac++; 00077 clock_delay(x->t_clock,x->t_interval); 00078 } 00079 } 00080 00081 void thresh_bang(t_thresh *x) 00082 { 00083 if (x->t_ac) 00084 outlet_list(x->t_out,ps_list,x->t_ac,x->t_av); 00085 x->t_ac = 0; 00086 } 00087 00088 void thresh_list(t_thresh *x, t_symbol *s, short ac, t_atom *av) 00089 { 00090 short i,upto; 00091 00092 x->t_time = gettime(); 00093 upto = MIN(ac+x->t_ac,MAXSIZE); 00094 for (i=x->t_ac; i < upto; i++,av++) { 00095 x->t_av[i] = *av; 00096 } 00097 x->t_ac = upto; 00098 clock_delay(x->t_clock,x->t_interval); 00099 } 00100 00101 void thresh_tick(t_thresh *x) 00102 { 00103 long tt; 00104 tt = gettime(); 00105 if (tt - x->t_time >= x->t_interval) 00106 thresh_bang(x); 00107 else 00108 clock_delay(x->t_clock,x->t_interval); 00109 } 00110 00111 void thresh_in1(t_thresh *x, long n) 00112 { 00113 x->t_interval = n; 00114 } 00115 00116 void thresh_free(t_thresh *x) 00117 { 00118 object_free(x->t_clock); 00119 if (x->t_av) 00120 sysmem_freeptr(x->t_av); 00121 } 00122 00123 void thresh_inletinfo(t_thresh *x, void *b, long a, char *t) 00124 { 00125 if (a) 00126 *t = 1; 00127 } 00128 00129 void thresh_assist(t_thresh *x, void *b, long m, long a, char *s) 00130 { 00131 if (m == ASSIST_OUTLET) 00132 sprintf(s,"Gathered list"); 00133 else { 00134 switch (a) { 00135 case 0: 00136 sprintf(s,"Numbers to be Gathered Into list"); 00137 break; 00138 case 1: 00139 sprintf(s,"Arrival Threshold"); 00140 break; 00141 } 00142 } 00143 } 00144 00145 00146 void *thresh_new(long interval) 00147 { 00148 t_thresh *x; 00149 short i; 00150 00151 x = object_alloc(thresh_class); 00152 intin(x,1); 00153 x->t_out = outlet_new((t_object *)x,0); // cause it sends out lists and ints 00154 x->t_clock = clock_new(x,(method)thresh_tick); 00155 x->t_time = gettime(); 00156 x->t_interval = interval < 5 ? 5 : interval; 00157 x->t_av = (t_atom *) sysmem_newptr(MAXSIZE*sizeof(t_atom)); 00158 x->t_ac = 0; 00159 for (i=0; i < MAXSIZE; i++) 00160 atom_setlong(x->t_av+i,0); 00161 00162 return (x); 00163 }
Copyright © 2008, Cycling '74