WoMax Documentation OMax Logo

OMax.build.c

Go to the documentation of this file.
00001 /*--------------------------------------
00002  * OMax.build - OMax.build.c
00003  * Created on 18/04/09 by BenCello
00004  *--------------------------------------*/
00005 
00007 
00009 
00010 
00011 #ifndef __OMAX_LEARN_MAX_API_
00012 #define __OMAX_LEARN_MAX_API_
00013 
00014 #include "Oracle_data.hpp"
00015 
00016 extern "C"
00017 {
00018         
00019 #include "ext.h"                                // standard Max include, always required
00020 #include "ext_obex.h"                   // required for new style Max object
00021 #include "jpatcher_api.h"               // required for the color
00022 #include "jgraphics.h"                  // required for the color
00023         
00024 #include "OMax.data.h"
00025         
00030         typedef struct _OMax_build 
00031         {
00032                 t_object        ob;                     
00033                 
00034                 t_symbol*       oname;          
00035                 t_symbol*       odataname;      
00036                 O_data*         odata;          
00037                 bool            odatabound;     
00038                 t_symbol*       idataname;      
00039                 O_data*         idata;          
00040                 bool            idatabound;     
00041                 O_DataType      datatype;       
00042                 
00043                 long            date;           
00044                 t_atom*         addout;         
00045                 
00046                 void*           out_state;      
00047                 void*           out_added;      
00048                 void*           out_date;       
00049         } t_OMax_build;
00050         
00052         
00054         // Prototypes //
00056         
00057         // Standard Max5 methodes
00058         void *OMax_build_new(t_symbol *s, long argc, t_atom *argv);
00059         void OMax_build_free(t_OMax_build *x);
00060         void OMax_build_assist(t_OMax_build *x, void *b, long io, long index, char *s);
00061         
00062         // Input/ouput routines
00063         void OMax_build_bang(t_OMax_build *x);
00064         void OMax_build_add(t_OMax_build *x, long ostatenb);
00065         
00066         // Internal routines
00067         t_symbol* OMax_build_dataname(t_symbol* name);
00068         bool OMax_build_bind(t_OMax_build *x);
00069         
00070         // Global class pointer variable
00071         t_class *OMax_build_class;
00072         
00074         // Functions //
00076         
00077         int main(void)
00078         {       
00079                 t_class *c;
00080                 
00081                 c = class_new("OMax.build", (method)OMax_build_new, (method)OMax_build_free, (long)sizeof(t_OMax_build), 
00082                                           0L /* leave NULL!! */, A_GIMME, 0);
00083                 
00084                 // assistance
00085                 class_addmethod(c, (method)OMax_build_assist,"assist",A_CANT, 0); 
00086                 
00087                 // input methods
00088                 class_addmethod(c, (method)OMax_build_bang, "bang", 0);
00089                 class_addmethod(c, (method)OMax_build_add, "int", A_LONG, 0);
00090                 
00091                 class_register(CLASS_BOX, c); /* CLASS_NOBOX */
00092                 OMax_build_class = c;
00093                 
00094                 return 0;
00095         }
00096         
00098 
00099         
00102         void *OMax_build_new(t_symbol *s, long argc, t_atom *argv)
00103         {
00104                 t_OMax_build *x = NULL;
00105                 
00106                 if (x = (t_OMax_build *)object_alloc(OMax_build_class))
00107                 {
00108                         // allocation
00109                         char err;
00110                         long i;
00111                         atom_alloc_array(2,&i,&(x->addout),&err);
00112                         if (!err && i!=2)
00113                                 object_error((t_object*)x, "Allocation error");
00114                         
00115                         // oulets
00116                         x->out_date = intout(x);
00117                         x->out_state = intout(x);
00118                         x->out_added = listout(x);
00119                         
00121                         x->odatabound = FALSE;
00122                         x->idatabound = FALSE;
00123                         if (argc < 2)
00124                                 object_error((t_object *)x,"Missing arguments (oracle's data & new sequence's data)");
00125                         else
00126                         {
00127                                 if (argv->a_type!=A_SYM && (argv+1)->a_type!=A_SYM)
00128                                         object_error((t_object *)x,"Both arguments must be symbols (oracle's data & new sequence's data)");
00129                                 else
00130                                 {
00131                                         x->oname = atom_getsym(argv);
00132                                         x->odataname = OMax_build_dataname(x->oname);
00133                                         x->idataname = OMax_build_dataname(atom_getsym(argv+1));
00134                                 }
00135                                 
00136                         }
00137                         
00138                         // color
00139                         t_object *box;
00140                         t_jrgba colorvals;
00141                         jrgba_set(&colorvals, 0.30, 1.0, 0.15, 1.0);
00142                         object_obex_lookup((t_object *)x, gensym("#B"), &box);
00143                         jbox_set_color(box, &colorvals);
00144                 }
00145                 return (x);
00146         }
00147         
00150         void OMax_build_free(t_OMax_build *x)
00151         {
00152                 ;
00153         }
00154         
00157         void OMax_build_assist(t_OMax_build *x, void *b, long io, long index, char *s)
00158         {
00159                 switch (io)
00160                 {
00161             case 1: // inlets
00162                                 switch (index)
00163                         {
00164                                 case 0: // leftmost
00165                                         sprintf(s, "int state to add");
00166                                         break;
00167                         }
00168                 break;
00169                         case 2: // outlets
00170                                 switch (index)
00171                         {
00172                                 case 0: // leftmost
00173                                         sprintf(s, "added");
00174                                         break;
00175                                 case 1:
00176                                         sprintf(s, "size");
00177                                         break;
00178                                 case 2:
00179                                         sprintf(s, "ending date");
00180                                         break;
00181                         }
00182                 }
00183         }
00184         
00186         
00188 
00189         
00192         t_symbol * OMax_build_dataname(t_symbol * name)
00193         {
00194                 char dataname[128];
00195                 strcpy(dataname,name->s_name);
00197                 strcat(dataname,"_data");
00198                 return gensym(dataname);
00199         }
00200         
00203         bool OMax_build_bind(t_OMax_build *x)
00204         {
00206                 if ((x->odatabound==FALSE) || (x->idatabound==FALSE))
00207                 {
00209                         if ((x->odataname->s_thing) && (ob_sym(x->odataname->s_thing) == gensym("OMax.data")))
00210                         {
00211                                 x->odata = &(((t_OMax_data*)(x->odataname->s_thing))->data);
00212                                 x->odatabound = TRUE;
00213                                 object_post((t_object *)x,"Builder bound to Data of Oracle %s", x->oname->s_name);
00214                         }
00215                         else
00216                         {
00217                                 object_error((t_object *)x,"No data for Oracle %s declared", x->oname->s_name);
00218                         }
00220                         if ((x->idataname->s_thing) && (ob_sym(x->idataname->s_thing) == gensym("OMax.data")))
00221                         {
00222                                 if (((t_OMax_data*)(x->idataname->s_thing))->datatype == ((t_OMax_data*)(x->odataname->s_thing))->datatype 
00223                                         && ((t_OMax_data*)(x->idataname->s_thing))->noDelete == TRUE)
00224                                 {
00225                                         x->idata = &(((t_OMax_data*)(x->idataname->s_thing))->data);
00226                                         x->idatabound = TRUE;
00227                                         x->datatype = ((t_OMax_data*)(x->odataname->s_thing))->datatype;
00228                                         object_post((t_object *)x,"Builder writing Sequence %s", x->idataname->s_name);
00229                                 }
00230                                 else
00231                                         object_error((t_object *)x,"Data type mismatch");
00232                         }
00233                         else
00234                         {
00235                                 object_error((t_object *)x,"No new data declared to write");
00236                         }
00237                 }
00239                 return (x->odatabound && x->idatabound);
00240         }
00241         
00243         
00245 
00246         
00250         void OMax_build_bang(t_OMax_build *x)
00251         {
00252                 if (OMax_build_bind(x))
00253                 {
00254                         ATOMIC_INCREMENT(&(((t_OMax_data *)(x->idataname->s_thing))->readcount));
00255                         if(!(((t_OMax_data *)(x->idataname->s_thing))->wflag))
00256                         {
00257                                 if(x->idata->get_size()<=1)
00258                                         x->date = 0;
00259                                 outlet_int(x->out_date, x->date);
00260                                 outlet_int(x->out_state, (long)x->idata->get_size());
00261                         }
00262                         else
00263                                 object_error((t_object*)x, "Data busy");
00264                         ATOMIC_DECREMENT(&(((t_OMax_data *)(x->idataname->s_thing))->readcount));
00265                 }
00266         }
00267         
00271         void OMax_build_add(t_OMax_build *x, long statein)
00272         {
00273                 if (OMax_build_bind(x))
00274                 {
00275                         ATOMIC_INCREMENT(&(((t_OMax_data *)(x->odataname->s_thing))->readcount));
00276                         if(!(((t_OMax_data *)(x->odataname->s_thing))->wflag)
00277                            && !(((t_OMax_data *)(x->idataname->s_thing))->wflag)
00278                            && !(((t_OMax_data *)(x->idataname->s_thing))->readcount))
00279                         {
00280                                 
00281                                 if((*x->odata)[statein]!=NULL)
00282                                 {
00283                                         int statnb;
00284                                         if(x->idata->get_size()<=1)
00285                                                 x->date = 0;
00287                                         switch (x->datatype)
00288                                         {
00289                                                 case LETTERS:
00290                                                 {
00291                                                         ATOMIC_INCREMENT(&((t_OMax_data *)(x->idataname->s_thing))->wflag);
00292                                                         statnb = x->idata->add<O_char>(x->date,(*x->odata)[statein]);
00293                                                         ATOMIC_DECREMENT(&((t_OMax_data *)(x->idataname->s_thing))->wflag);
00294                                                         break;
00295                                                 }
00296                                                 case MIDI_MONO:
00297                                                 {
00298                                                         ATOMIC_INCREMENT(&((t_OMax_data *)(x->idataname->s_thing))->wflag);
00299                                                         statnb = x->idata->add<O_MIDI_mono>(x->date,(*x->odata)[statein]);
00300                                                         ATOMIC_DECREMENT(&((t_OMax_data *)(x->idataname->s_thing))->wflag);
00301                                                         break;
00302                                                 }
00303                                                 case SPECTRAL:
00304                                                 {
00305                                                         ATOMIC_INCREMENT(&((t_OMax_data *)(x->idataname->s_thing))->wflag);
00306                                                         statnb = x->idata->add<O_spectral>(x->date,(*x->odata)[statein]);
00307                                                         ATOMIC_DECREMENT(&((t_OMax_data *)(x->idataname->s_thing))->wflag);
00308                                                         break;
00309                                                 }
00310                                         }
00311                                         
00313                                         x->date = (x->date + (*x->odata)[statein]->get_duration());
00314                                         outlet_int(x->out_date, x->date);       
00315                                         outlet_int(x->out_state, (long)x->idata->get_size()); 
00316                                         
00317                                         long added[2];
00318                                         added[0] = statnb;
00319                                         added[1] = statein;
00320                                         atom_setlong_array(2,x->addout,2,added);
00321                                         outlet_list(x->out_added, NULL, 2, x->addout); 
00322                                 }
00323                         }
00324                         else
00325                                 object_error((t_object*)x, "Data of Oracle %s being read (%d)",x->idataname->s_name,((t_OMax_data *)(x->idataname->s_thing))->readcount);
00326                         ATOMIC_DECREMENT(&(((t_OMax_data *)(x->odataname->s_thing))->readcount));
00327                 }
00328         }
00329         
00331 }
00332 
00333 #endif

Generated on 16 Mar 2010 for Benjamin Lévy by  doxygen 1.6.1