mas01mj@567: #include "m_pd.h" mas01mj@567: #include mas01mj@567: #include mas01mj@567: mas01mj@567: #include mas01mj@567: #include mas01mj@567: #include mas01mj@567: #include mas01cr@576: #include mas01mj@567: mas01mj@567: #include mas01mj@567: mas01mj@567: #define ADB_MAXSTR (512U) mas01mj@567: #define MAXSTR ADB_MAXSTR mas01mj@567: mas01mj@567: // Query types mas01mj@567: #define O2_POINT_QUERY (0x4U) mas01mj@567: #define O2_SEQUENCE_QUERY (0x8U) mas01mj@567: #define O2_TRACK_QUERY (0x10U) mas01mj@567: #define O2_N_SEQUENCE_QUERY (0x20U) mas01mj@567: #define O2_ONE_TO_ONE_N_SEQUENCE_QUERY (0x40U) mas01mj@567: mas01mj@567: static t_class *adbpd_class; /* so this bit is different */ mas01mj@567: mas01mj@567: typedef struct _adbpd { mas01cr@573: t_object x_obj; mas01cr@573: t_outlet *x_key; mas01cr@573: t_outlet *x_dist; mas01cr@573: t_outlet *x_qpos; mas01cr@573: t_outlet *x_spos; mas01mj@569: t_symbol *x_dbname; //database name mas01cr@573: mas01mj@569: t_int x_dbquerytype; //sequence, track, etc. mas01mj@569: t_symbol *x_dbfeature; mas01mj@567: // t_symbol *x_dbpower; mas01mj@569: t_symbol *x_dbkey; mas01mj@569: t_float x_dbqpoint; mas01mj@569: t_float x_dbnumpoints; mas01mj@569: t_float x_dbradius; mas01mj@569: t_float x_dbresultlength; mas01mj@569: t_float x_dbsequencelength; mas01cr@671: adb_t *db; mas01mj@567: } t_adbpd; mas01mj@567: mas01mj@567: mas01cr@578: static void adbpd_create(t_adbpd *, t_floatarg, t_floatarg, t_floatarg); mas01mj@567: static void adbpd_open(t_adbpd *x); mas01cr@579: static void adbpd_close(t_adbpd *x); mas01mj@567: static void adbpd_setname(t_adbpd *x, t_symbol *s, int argc, t_atom *argv); mas01mj@567: static void adbpd_status(t_adbpd *x); mas01mj@567: static void adbpd_l2norm(t_adbpd *x); mas01mj@567: static void adbpd_power(t_adbpd *x); mas01mj@567: static void adbpd_setfeatures(t_adbpd *x,t_symbol *s,int argc, t_atom *argv); mas01mj@567: static void adbpd_setquerytype(t_adbpd *x,t_symbol *s,int argc, t_atom *argv); mas01mj@567: static void adbpd_setqpoint(t_adbpd *x,t_floatarg f); mas01mj@567: static void adbpd_setresultlength(t_adbpd *x,t_floatarg f); mas01mj@567: static void adbpd_setradius(t_adbpd *x,t_floatarg f); mas01mj@567: static void adbpd_setnumpoints(t_adbpd *x,t_floatarg f); mas01mj@567: static void adbpd_setsequencelength(t_adbpd *x,t_floatarg f); mas01mj@567: static void adbpd_doquery(t_adbpd *x); mas01mj@567: static void adbpd_parameters(t_adbpd *x); mas01mj@567: static void adbpd_getname(t_adbpd *x); mas01mj@567: mas01mj@567: mas01mj@567: mas01mj@567: /* input arguments are only used for startup vals */ mas01mj@567: static void *adbpd_new(t_symbol *s) { mas01mj@567: mas01cr@573: /* some sort of standard declaration line */ mas01cr@578: t_adbpd *x = (t_adbpd *) pd_new(adbpd_class); mas01cr@578: x->x_dbname = s; mas01cr@573: mas01cr@573: /* setup some inlets */ mas01cr@573: floatinlet_new(&x->x_obj,&x->x_dbqpoint); /* second inlet */ mas01cr@573: floatinlet_new(&x->x_obj,&x->x_dbnumpoints); /* third inlet */ mas01cr@573: floatinlet_new(&x->x_obj,&x->x_dbradius); /* fourth inlet */ mas01cr@573: floatinlet_new(&x->x_obj,&x->x_dbresultlength); /* fifth inlet */ mas01cr@573: floatinlet_new(&x->x_obj,&x->x_dbsequencelength); /* sixth inlet */ mas01cr@573: mas01cr@573: /* outlets */ mas01cr@573: outlet_new(&x->x_obj, &s_float); mas01cr@573: mas01cr@573: x->x_key = outlet_new(&x->x_obj, &s_symbol); mas01cr@573: x->x_dist = outlet_new(&x->x_obj, &s_float); mas01cr@573: x->x_qpos = outlet_new(&x->x_obj, &s_float); mas01cr@573: x->x_spos = outlet_new(&x->x_obj, &s_float); mas01mj@567: mas01mj@569: /* adb defaults. These are the same but we need to init them */ mas01mj@569: x->x_dbquerytype=O2_POINT_QUERY; mas01mj@569: x->x_dbfeature=gensym("No feature file yet specified"); mas01mj@569: x->x_dbqpoint=1; mas01mj@569: x->x_dbnumpoints=10; mas01mj@569: x->x_dbradius=1; mas01mj@569: x->x_dbresultlength=10; mas01mj@569: x->x_dbsequencelength=12; mas01cr@573: return(x); mas01mj@567: } mas01mj@567: mas01cr@578: static void adbpd_free(t_adbpd *x) { mas01cr@579: adbpd_close(x); mas01cr@578: } mas01cr@578: mas01cr@574: /* bang executes the defined query */ mas01mj@567: static void adbpd_bang(t_adbpd *x) { mas01mj@569: adbpd_doquery(x); mas01mj@567: } mas01mj@567: mas01mj@567: /* create a database */ mas01cr@578: static void adbpd_create(t_adbpd *x, t_floatarg datasize, t_floatarg ntracks, t_floatarg dim) { mas01cr@579: adbpd_close(x); mas01cr@579: mas01cr@579: post("Creating db '%s'", x->x_dbname->s_name); mas01cr@578: x->db = audiodb_create(x->x_dbname->s_name, datasize, ntracks, dim); mas01mj@569: mas01cr@574: if (x->db) { mas01cr@578: post("Created and opened"); mas01mj@569: } else { mas01cr@579: error("Could not create db '%s'.", x->x_dbname->s_name); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01cr@579: /* open a database, closing an existing one if necessary. */ mas01cr@574: static void adbpd_open(t_adbpd *x) { mas01cr@579: adbpd_close(x); mas01cr@579: mas01cr@574: post("Opening db '%s'", x->x_dbname->s_name); mas01cr@579: if((x->db = audiodb_open(x->x_dbname->s_name, O_RDWR))) { mas01cr@574: post("Opened"); mas01mj@569: } else { mas01cr@574: error("Could not open db '%s'.", x->x_dbname->s_name); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01cr@579: static void adbpd_close(t_adbpd *x) { mas01cr@579: if(x->db) { mas01cr@579: post("Closing db"); mas01cr@579: audiodb_close(x->db); mas01cr@579: x->db = NULL; mas01cr@579: } mas01cr@579: } mas01cr@579: mas01cr@573: /* This is accessed via the 'set' message. It sets the name and opens mas01cr@573: the database. */ mas01cr@574: static void adbpd_setname(t_adbpd *x, t_symbol *s, int argc, t_atom *argv) { mas01cr@573: /* if we have a properly formed instruction */ mas01cr@573: if (argc == 1 && argv->a_type == A_SYMBOL) { mas01cr@573: /* make the internal database reference name the same as the name mas01cr@573: of the database we want. This is stupid. There must be a mas01cr@573: better way of doing this. */ mas01cr@574: x->x_dbname = gensym(argv->a_w.w_symbol->s_name); mas01cr@574: post("Name set to '%s'.", x->x_dbname->s_name); mas01cr@573: } mas01cr@574: /* now we can open the database as we did with the audiodb_open call mas01cr@574: above */ mas01cr@574: /* FIXME: well, we _can_, but why is that a good idea? */ mas01cr@574: adbpd_open(x); mas01cr@573: } mas01cr@573: mas01mj@567: mas01mj@567: /* this is a status call to the audioDB API */ mas01mj@567: static void adbpd_status(t_adbpd *x){ mas01mj@567: mas01mj@569: adb_status_t mystatus; mas01mj@569: mas01mj@569: post("Getting Status"); mas01cr@573: mas01mj@569: if (x->db && !(audiodb_status(x->db,&mystatus))){ mas01mj@569: post("numFiles %d",mystatus.numFiles); mas01mj@569: post("dim %d",mystatus.dim); mas01mj@569: post("length %d",mystatus.length); mas01mj@569: post("dudCount %d",mystatus.dudCount); mas01mj@569: post("numCount %d",mystatus.nullCount); mas01mj@569: post("flags %d",mystatus.flags); mas01mj@569: post("Bytes Available %d",mystatus.data_region_size); mas01mj@569: } else { mas01mj@569: error("Can't get Status. Have you selected a db?"); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01mj@567: mas01mj@567: static void adbpd_doquery(t_adbpd *x){ mas01mj@567: mas01mj@569: adb_datum_t datum = {0}; mas01mj@569: adb_query_id_t qid = {0}; mas01mj@569: adb_query_parameters_t params = {0}; mas01mj@569: adb_query_refine_t refine = {0}; mas01cr@576: adb_query_spec_t spec; mas01mj@569: mas01mj@569: // Configure the start+length of the query, and zero mas01mj@569: // the flags initially. mas01mj@569: qid.datum = &datum; mas01mj@569: qid.sequence_length = x->x_dbsequencelength; mas01mj@569: qid.sequence_start = x->x_dbqpoint; mas01mj@569: qid.flags = 0; mas01mj@567: mas01mj@569: refine.flags |= ADB_REFINE_RADIUS; mas01mj@569: refine.radius = x->x_dbradius; mas01mj@567: mas01mj@569: spec.qid = qid; mas01mj@569: spec.params = params; mas01mj@569: spec.refine = refine; mas01mj@569: mas01mj@569: int fd; mas01mj@569: struct stat st; mas01mj@569: mas01mj@569: // Read in the feature file - note that this code may contain leaks mas01mj@569: // (the same chunk also exists in audioDB.cpp). mas01mj@569: fd = open(x->x_dbfeature->s_name, O_RDONLY); mas01mj@569: if(fd < 0) { mas01mj@569: error("failed to open feature file", x->x_dbfeature->s_name); mas01mj@569: return; mas01mj@569: } mas01mj@569: fstat(fd, &st); mas01mj@569: read(fd, &datum.dim, sizeof(uint32_t)); mas01mj@569: datum.nvectors = (st.st_size - sizeof(uint32_t)) / (datum.dim * sizeof(double)); mas01mj@569: datum.data = (double *) malloc(st.st_size - sizeof(uint32_t)); mas01mj@569: read(fd, datum.data, st.st_size - sizeof(uint32_t)); mas01mj@569: close(fd); mas01mj@569: mas01mj@569: // Set up query spec params depending on the query type. mas01mj@569: switch(x->x_dbquerytype) mas01cr@573: { mas01mj@569: case O2_POINT_QUERY: mas01mj@569: spec.qid.sequence_length = 1; mas01mj@569: spec.params.accumulation = ADB_ACCUMULATION_DB; mas01mj@569: spec.params.distance = ADB_DISTANCE_DOT_PRODUCT; mas01mj@569: spec.params.npoints = x->x_dbnumpoints; mas01mj@569: spec.params.ntracks = x->x_dbresultlength; mas01mj@569: break; mas01mj@569: case O2_TRACK_QUERY: mas01mj@569: spec.qid.sequence_length = 1; mas01mj@569: spec.params.accumulation = ADB_ACCUMULATION_PER_TRACK; mas01mj@569: spec.params.distance = ADB_DISTANCE_DOT_PRODUCT; mas01mj@569: spec.params.npoints = x->x_dbnumpoints; mas01mj@569: spec.params.ntracks = x->x_dbresultlength; mas01mj@569: case O2_SEQUENCE_QUERY: mas01mj@569: case O2_N_SEQUENCE_QUERY: mas01mj@569: spec.params.accumulation = ADB_ACCUMULATION_PER_TRACK; mas01mj@569: // TODO : Add unit norming param. Defaults to false. mas01cr@573: // mas01cr@573: // spec.params.distance = no_unit_norming ? mas01cr@573: // ADB_DISTANCE_EUCLIDEAN : ADB_DISTANCE_EUCLIDEAN_NORMED; mas01mj@569: spec.params.distance = ADB_DISTANCE_EUCLIDEAN_NORMED; mas01mj@569: spec.params.npoints = x->x_dbnumpoints; mas01mj@569: spec.params.ntracks = x->x_dbresultlength; mas01mj@569: break; mas01mj@569: case O2_ONE_TO_ONE_N_SEQUENCE_QUERY: mas01mj@569: spec.params.accumulation = ADB_ACCUMULATION_ONE_TO_ONE; mas01mj@569: // TODO : Add unit norming param. Defaults to false. mas01cr@573: // mas01cr@573: // spec.params.distance = no_unit_norming ? mas01cr@573: // ADB_DISTANCE_EUCLIDEAN : ADB_DISTANCE_EUCLIDEAN_NORMED; mas01mj@569: spec.params.distance =ADB_DISTANCE_EUCLIDEAN_NORMED; mas01mj@569: spec.params.npoints = 0; mas01mj@569: spec.params.ntracks = 0; mas01mj@569: break; mas01mj@569: default: mas01mj@569: post("Unsupported query type"); mas01mj@569: } mas01mj@569: mas01mj@569: adb_query_results_t *rs = audiodb_query_spec(x->db, &spec); mas01mj@569: mas01mj@569: if(datum.data) { mas01cr@573: free(datum.data); mas01cr@573: datum.data = NULL; mas01mj@569: } mas01mj@569: if(datum.power) { mas01cr@577: free(datum.power); mas01cr@577: datum.power = NULL; mas01mj@569: } mas01mj@569: if(datum.times) { mas01cr@577: free(datum.times); mas01cr@577: datum.times = NULL; mas01mj@569: } mas01mj@569: mas01mj@569: if(rs == NULL) mas01mj@569: { mas01mj@569: error("Query failed"); mas01mj@569: return; mas01mj@569: } mas01mj@569: mas01mj@569: int size = rs->nresults; mas01mj@569: post("result size:[%d]",(int)size); mas01mj@569: int i = 0; mas01mj@569: for(i=0; iresults[i]; mas01mj@567: mas01mj@569: outlet_float(x->x_dist,r.dist); mas01mj@569: outlet_float(x->x_qpos,r.qpos); mas01mj@569: outlet_float(x->x_spos,r.ipos); mas01mj@567: mas01cr@672: post("in obj ikey:%s",r.ikey); mas01mj@569: post("in obj Dist:%f", r.dist); mas01mj@569: post("in obj qpos:%d", r.qpos); mas01mj@569: post("in obj ipos:%d", r.ipos); mas01mj@569: } mas01cr@577: audiodb_query_free_results(x->db, &spec, rs); mas01mj@567: } mas01mj@567: mas01cr@573: /* Do I need to set a the power file for this flag to work ? mas01cr@573: Hmmm. Dunno. Also, should be on/off*/ mas01mj@567: static void adbpd_power(t_adbpd *x){ mas01mj@569: post("power"); mas01mj@569: mas01mj@569: if (x->db && !(audiodb_power(x->db))){ mas01mj@569: post("power successfully set on db"); mas01mj@569: } else { mas01mj@569: error("power flag not working"); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01cr@573: /* works fine but would be better if it took an argument to switch it mas01cr@573: on and off */ mas01mj@567: static void adbpd_l2norm(t_adbpd *x){ mas01mj@569: post("l2norm"); mas01mj@569: mas01mj@569: if (x->db && !(audiodb_l2norm(x->db))){ mas01mj@569: post("l2norm successfully set on db"); mas01mj@569: } else { mas01mj@569: error("l2norm flag not working"); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01mj@567: /* reports the name of the current db */ mas01mj@567: static void adbpd_getname(t_adbpd *x){ mas01mj@569: post("db name is '%s'", x->x_dbname->s_name); mas01mj@567: } mas01mj@567: mas01mj@567: /* this sets the qpoint for the current query*/ mas01mj@567: static void adbpd_setqpoint(t_adbpd *x,t_floatarg f){ mas01cr@573: post("qpoint %.3f",f); mas01mj@569: x->x_dbqpoint=f; mas01mj@567: } mas01mj@567: mas01mj@567: /* This sets the number of points for current query */ mas01mj@567: static void adbpd_setnumpoints(t_adbpd *x,t_floatarg f){ mas01cr@573: post("numpoints %.3f",f); mas01cr@573: x->x_dbnumpoints=(int)f; mas01mj@567: } mas01mj@567: mas01mj@567: /* This sets the radius */ mas01mj@567: static void adbpd_setradius(t_adbpd *x,t_floatarg f){ mas01cr@573: post("radius %.3f",f); mas01cr@573: x->x_dbradius=f; mas01mj@567: } mas01mj@567: mas01mj@567: /* This sets the result length */ mas01mj@567: static void adbpd_setresultlength(t_adbpd *x,t_floatarg f){ mas01cr@573: post("resultlength %.3f",f); mas01cr@573: x->x_dbresultlength=(int)f; mas01mj@567: } mas01mj@567: mas01mj@567: /* This sets the sequence length */ mas01mj@567: static void adbpd_setsequencelength(t_adbpd *x,t_floatarg f){ mas01cr@573: post("sequencelength %.3f",f); mas01cr@573: x->x_dbsequencelength=(int)f; mas01mj@567: } mas01mj@567: mas01mj@567: /* This sets the feature file to use for the query */ mas01mj@567: static void adbpd_setfeatures(t_adbpd *x,t_symbol *s,int argc, t_atom *argv){ mas01mj@569: mas01mj@569: if (argc == 1 && argv->a_type == A_SYMBOL){ mas01mj@569: x->x_dbfeature=gensym(argv->a_w.w_symbol->s_name); mas01mj@569: post("Features file has been set to '%s'",x->x_dbfeature->s_name); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01mj@567: /* This sets the query type */ mas01mj@567: static void adbpd_setquerytype(t_adbpd *x,t_symbol *s,int argc, t_atom *argv){ mas01cr@573: if (argc == 1 && argv->a_type == A_SYMBOL) { mas01cr@575: t_symbol *type = atom_getsymbol(argv); mas01cr@575: if(type == gensym("track")) { mas01mj@569: x->x_dbquerytype=O2_TRACK_QUERY; mas01cr@575: } else if(type == gensym("point")) { mas01mj@569: x->x_dbquerytype=O2_POINT_QUERY; mas01cr@575: } else if(type == gensym("sequence")) { mas01mj@569: x->x_dbquerytype=O2_SEQUENCE_QUERY; mas01cr@575: } else if(type == gensym("nsequence")) { mas01mj@569: x->x_dbquerytype=O2_N_SEQUENCE_QUERY; mas01cr@575: } else if(type == gensym("onetoonensequence")) { mas01mj@569: x->x_dbquerytype=O2_ONE_TO_ONE_N_SEQUENCE_QUERY; mas01cr@575: } else { mas01cr@575: error("unsupported query type: '%s'", type->s_name); mas01cr@575: return; mas01cr@575: } mas01cr@575: post("Query type has been set to '%s'", type->s_name); mas01mj@569: } mas01mj@567: } mas01mj@567: mas01mj@567: /* This lists all the parameters */ mas01mj@567: mas01mj@567: static void adbpd_parameters(t_adbpd *x){ mas01mj@567: mas01mj@569: post("dbname %s",x->x_dbname->s_name); mas01mj@569: post("querytype %d",x->x_dbquerytype); mas01mj@569: post("features %s",x->x_dbfeature->s_name); mas01mj@569: post("qpoint %.3f",x->x_dbqpoint); mas01mj@569: post("numpoints %.3f",x->x_dbnumpoints); mas01mj@569: post("radius %.3f",x->x_dbradius); mas01mj@569: post("resultlength %.3f",x->x_dbresultlength); mas01mj@569: post("sequencelength %.3f",x->x_dbsequencelength); mas01mj@567: } mas01mj@567: mas01cr@573: /* THAR SHE BLOWS. This sets up the object, takes the messages with mas01cr@573: args and maps them to methods. */ mas01mj@567: void adbpd_setup(void) { mas01cr@573: /* the arguments in this line refer to INPUT ARGUMENTS and not mas01cr@573: OUTPUTS */ mas01cr@578: adbpd_class = mas01cr@578: class_new(gensym("adbpd"), (t_newmethod) adbpd_new, (t_method) adbpd_free, mas01cr@578: sizeof(t_adbpd), CLASS_DEFAULT, A_DEFSYMBOL, 0); mas01cr@575: mas01cr@573: /* all methods that respond to input must be defined here */ mas01cr@573: class_addbang(adbpd_class, adbpd_bang); mas01cr@573: class_addfloat(adbpd_class, adbpd_setqpoint); mas01cr@573: class_addfloat(adbpd_class, adbpd_setnumpoints); mas01cr@573: class_addfloat(adbpd_class, adbpd_setradius); mas01cr@573: class_addfloat(adbpd_class, adbpd_setresultlength); mas01cr@573: class_addfloat(adbpd_class, adbpd_setsequencelength); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_setname, gensym("set"), A_GIMME, 0); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_getname, gensym("get"), A_NULL); mas01cr@578: class_addmethod(adbpd_class, (t_method)adbpd_create, gensym("create"), A_FLOAT, A_FLOAT, A_FLOAT, 0); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_open, gensym("open"), A_NULL); mas01cr@579: class_addmethod(adbpd_class, (t_method)adbpd_close, gensym("close"), A_NULL); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_status, gensym("status"), A_NULL); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_l2norm, gensym("l2norm"), A_NULL); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_power, gensym("power"), A_NULL); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_doquery, gensym("doquery"), A_NULL); mas01mj@569: class_addmethod(adbpd_class, (t_method)adbpd_setquerytype, gensym("querytype"), A_GIMME, 0); mas01mj@569: class_addmethod(adbpd_class, (t_method)adbpd_setfeatures, gensym("features"), A_GIMME, 0); mas01cr@573: class_addmethod(adbpd_class, (t_method)adbpd_parameters, gensym("parameters"), A_NULL); mas01mj@567: } mas01mj@567: