Max 5 API Reference
00001 #include "ext.h" 00002 #include "ext_obex.h" 00003 00004 /** 00005 @file 00006 buddy.c --- data flow management object 00007 00008 updated 3/15/09 ajm: new API 00009 updated 5/18/92 ddz: to use proxies, 00010 event num synchonization, and any 00011 number of inlets 00012 00013 @ingroup examples 00014 */ 00015 00016 typedef struct { 00017 void *m_proxy; 00018 void *m_out; 00019 short m_argc; 00020 t_atom m_argv[128]; 00021 short m_on; 00022 } t_member; 00023 00024 typedef struct { 00025 t_object b_ob; 00026 long b_num; 00027 t_member *b_mem; 00028 long b_id; 00029 } t_buddy; 00030 00031 void buddy_bang(t_buddy *x); 00032 void buddy_anything(t_buddy *x, t_symbol *s, short argc, t_atom *argv); 00033 void buddy_float(t_buddy *x,double f); 00034 void buddy_int(t_buddy *x,long n); 00035 void buddy_list(t_buddy *x, t_symbol *s, short argc, t_atom *argv); 00036 void buddy_atom(t_buddy *x, t_atom *a); 00037 short buddy_all(t_buddy *x); 00038 void buddy_off(t_buddy *x); 00039 void buddy_out(t_buddy *x); 00040 void outlet_member(void *out, short argc, t_atom *argv); 00041 void buddy_assist(t_buddy *x, void *b, long m, long a, char *s); 00042 void buddy_inletinfo(t_buddy *x, void *b, long a, char *t); 00043 void buddy_free(t_buddy *x); 00044 void *buddy_new(long num); 00045 void buddy_clear(t_buddy *x); 00046 00047 void *buddy_class; 00048 t_symbol *ps_list; 00049 00050 int main() 00051 { 00052 t_class *c; 00053 00054 c = class_new("buddy", (method)buddy_new, (method)buddy_free, sizeof(t_buddy), 0L, A_DEFLONG, 0); 00055 00056 class_addmethod(c, (method)buddy_bang, "bang", 0); 00057 class_addmethod(c, (method)buddy_int, "int", A_LONG, 0); 00058 class_addmethod(c, (method)buddy_float, "float", A_FLOAT, 0); 00059 class_addmethod(c, (method)buddy_list, "list", A_GIMME, 0); 00060 class_addmethod(c, (method)buddy_clear, "clear", 0); 00061 class_addmethod(c, (method)buddy_anything, "anything", A_GIMME, 0); 00062 class_addmethod(c, (method)buddy_assist, "assist", A_CANT, 0); 00063 class_addmethod(c, (method)buddy_inletinfo, "inletinfo",A_CANT, 0); 00064 00065 class_register(CLASS_BOX, c); 00066 buddy_class = c; 00067 00068 ps_list = gensym("list"); 00069 00070 return 0; 00071 } 00072 00073 void buddy_bang(t_buddy *x) 00074 { 00075 buddy_int(x,0L); 00076 } 00077 00078 void buddy_clear(t_buddy *x) 00079 { 00080 buddy_off(x); 00081 } 00082 00083 void buddy_anything(t_buddy *x, t_symbol *s, short argc, t_atom *argv) 00084 { 00085 t_member *m; 00086 long in = proxy_getinlet((t_object *)x); 00087 00088 m = x->b_mem + in; 00089 m->m_on = TRUE; 00090 m->m_argc = argc + 1; 00091 atom_setsym(&m->m_argv[0], s); 00092 if (argc > 127) 00093 argc = 127; 00094 sysmem_copyptr(argv,m->m_argv+1,argc * sizeof(t_atom)); 00095 if (buddy_all(x)) { 00096 buddy_off(x); 00097 buddy_out(x); 00098 } 00099 } 00100 00101 void buddy_list(t_buddy *x, t_symbol *s, short argc, t_atom *argv) 00102 { 00103 buddy_anything(x,ps_list,argc,argv); 00104 } 00105 00106 void buddy_float(t_buddy *x, double f) 00107 { 00108 t_atom a; 00109 atom_setfloat(&a, f); 00110 buddy_atom(x,&a); 00111 } 00112 00113 void buddy_int(t_buddy *x, long n) 00114 { 00115 t_atom a; 00116 atom_setlong(&a, n); 00117 buddy_atom(x,&a); 00118 } 00119 00120 void buddy_atom(t_buddy *x, t_atom *a) 00121 { 00122 t_member *m; 00123 long in = proxy_getinlet((t_object *)x); 00124 00125 m = x->b_mem + in; 00126 m->m_on = true; 00127 m->m_argc = 1; 00128 m->m_argv[0] = *a; 00129 if (buddy_all(x)) { 00130 buddy_off(x); 00131 buddy_out(x); 00132 } 00133 } 00134 00135 short buddy_all(t_buddy *x) 00136 { 00137 short i; 00138 t_member *m; 00139 00140 for (i=0,m = x->b_mem; i < x->b_num; i++,m++) 00141 if (!m->m_on) 00142 return false; 00143 return true; 00144 } 00145 00146 void buddy_off(t_buddy *x) 00147 { 00148 short i; 00149 t_member *m; 00150 00151 for (i=0,m = x->b_mem; i < x->b_num; i++,m++) 00152 m->m_on = 0; 00153 } 00154 00155 void buddy_out(t_buddy *x) 00156 { 00157 short i; 00158 t_member *m; 00159 00160 for (i=x->b_num-1,m = x->b_mem+i; i >= 0; i--,m--) 00161 outlet_member(m->m_out,m->m_argc,m->m_argv); 00162 } 00163 00164 void outlet_member(void *out, short argc, t_atom *argv) 00165 { 00166 if (argc == 1) { 00167 switch (atom_gettype(argv)) { 00168 case A_LONG: 00169 outlet_int(out,atom_getlong(argv)); 00170 break; 00171 case A_FLOAT: 00172 outlet_float(out,atom_getfloat(argv)); 00173 break; 00174 case A_SYM: 00175 outlet_anything(out,atom_getsym(argv),0,0L); 00176 break; 00177 } 00178 } else if (atom_getsym(argv) == ps_list) 00179 outlet_list(out,ps_list,argc-1,argv+1); 00180 else 00181 outlet_anything(out,atom_getsym(argv),argc-1,argv+1); 00182 } 00183 00184 void buddy_assist(t_buddy *x, void *b, long m, long a, char *s) 00185 { 00186 if (m == ASSIST_INLET) 00187 sprintf(s,"Input to be Synchronized"); 00188 else 00189 sprintf(s,"Synchronized Output of Inlet %ld", a+1); 00190 } 00191 00192 void buddy_inletinfo(t_buddy *x, void *b, long a, char *t) 00193 { 00194 /* 00195 * red if every input - 1 has been set and you're looking to the one which will trigger 00196 */ 00197 long i, count; 00198 t_member *m, *m2; 00199 00200 for (i = count = 0, m = x->b_mem; i < x->b_num; i++, m++) { 00201 if (i == a) 00202 m2 = m; 00203 if (m->m_on) 00204 count++; 00205 } 00206 00207 if (count >= (x->b_num - 1) && ! m2->m_on) 00208 *t = 0; 00209 else 00210 *t = 1; 00211 } 00212 00213 void buddy_free(t_buddy *x) 00214 { 00215 short i; 00216 00217 for (i=1; i < x->b_num; i++) 00218 object_free(x->b_mem[i].m_proxy); 00219 sysmem_freeptr(x->b_mem); 00220 } 00221 00222 void *buddy_new(long num) 00223 { 00224 t_buddy *x; 00225 short i; 00226 t_member *m; 00227 00228 x = (t_buddy *)object_alloc(buddy_class); 00229 if (num < 2) 00230 num = 2; 00231 x->b_num = num; 00232 x->b_id = 0; 00233 x->b_mem = (t_member *)sysmem_newptr((unsigned short)(num * sizeof(t_member))); 00234 for (i=num-1,m = x->b_mem + i; i >= 0; i--,m--) { 00235 if (i) 00236 m->m_proxy = proxy_new(x,(long)i,&x->b_id); 00237 m->m_out = outlet_new(x,0L); 00238 m->m_on = 0; 00239 m->m_argc = 1; 00240 atom_setlong(&m->m_argv[0], 0); 00241 } 00242 return x; 00243 } 00244
Copyright © 2008, Cycling '74