Max 5 API Reference
00001 /* 00002 minimetro 00003 Copyright 2004 - Cycling '74 00004 jeremy bernstein - jeremy@bootsquad.com 00005 */ 00006 00007 #include "ext.h" 00008 #include "ext_common.h" 00009 #include "ext_obex.h" 00010 00011 typedef struct _minimetro 00012 { 00013 t_object ob; 00014 void *obex; 00015 double speed; 00016 void *clock; 00017 long state; 00018 long id; 00019 } t_minimetro; 00020 00021 void minimetro_init(void); 00022 void *minimetro_new(long id, long speed); 00023 void minimetro_free(t_minimetro *x); 00024 00025 void minimetro_clock(t_minimetro *x); 00026 void minimetro_onoff(t_minimetro *x, long onoff); 00027 void minimetro_speed(t_minimetro *x, void *attr, long ac, t_atom *av); 00028 00029 void *minimetro_class; 00030 00031 void minimetro_init(void) 00032 { 00033 t_class *c; 00034 void *attr; 00035 long attrflags = 0; 00036 00037 c = class_new("minimetro", (method)minimetro_new, (method)minimetro_free, 00038 (short)sizeof(t_minimetro), 0L, A_CANT, 0); // A_CANT so that we can have arbitrary args 00039 00040 // initialize the common symbols, since we want to use them 00041 common_symbols_init(); 00042 00043 // register the byte offset of obex with the class 00044 class_obexoffset_set(c, calcoffset(t_minimetro, obex)); 00045 00046 // add some methods 00047 class_addmethod(c, (method)minimetro_onoff, "onoff", A_LONG, 0); 00048 // add some attributes 00049 attr = attr_offset_new("speed", _sym_float64, attrflags, 00050 (method)0L, (method)minimetro_speed, calcoffset(t_minimetro, speed)); 00051 class_addattr(c, attr); 00052 00053 // we do not need this class to instantiate inside of the Max UI; ergo CLASS_NOBOX 00054 class_register(CLASS_NOBOX, c); 00055 minimetro_class = c; 00056 } 00057 00058 void minimetro_onoff(t_minimetro *x, long onoff) 00059 { 00060 if (onoff && !x->state) { 00061 clock_fdelay(x->clock, x->speed); 00062 x->state = 1; 00063 } 00064 else if (!onoff && x->state) { 00065 clock_unset(x->clock); 00066 x->state = 0; 00067 } 00068 } 00069 00070 void minimetro_clock(t_minimetro *x) 00071 { 00072 // send a notification. in our case, this causes the parent object to bang 00073 object_notify(x, _sym_bang, (void *)x->id); 00074 clock_fdelay(x->clock, x->speed); 00075 } 00076 00077 void minimetro_speed(t_minimetro *x, void *attr, long ac, t_atom *av) 00078 { 00079 if (ac && av) { // we wait until the next tick to change the tempo 00080 x->speed = atom_getfloat(av); 00081 } 00082 } 00083 00084 00085 void minimetro_free(t_minimetro *x) 00086 { 00087 clock_unset(x->clock); // just in case 00088 freeobject((t_object *)x->clock); 00089 object_obex_free(x); 00090 } 00091 00092 void *minimetro_new(long id, long speed) 00093 { 00094 t_minimetro *x; 00095 t_symbol *name; 00096 00097 // we use object_alloc here, rather than newobject 00098 if (x = (t_minimetro *)object_alloc(minimetro_class)) { 00099 // get the outlet id sent in 00100 x->id = id; 00101 x->speed = (speed < 0.) ? 0. : speed; 00102 x->clock = clock_new(x, (method)minimetro_clock); 00103 x->state = 0; 00104 } 00105 return (x); 00106 }
Copyright © 2008, Cycling '74