mas01cr@554: #include mas01cr@553: #include mas01cr@554: #include mas01cr@553: #include mas01cr@553: #include mas01cr@553: #include mas01cr@553: #include mas01cr@553: mas01cr@553: #include mas01cr@553: mas01cr@553: const char * qstring = mas01cr@553: " PREFIX af: " mas01cr@553: " PREFIX dc: " mas01cr@553: " PREFIX mo: " mas01cr@553: " PREFIX tl: " mas01cr@553: mas01cr@553: " SELECT ?key ?value ?sample_rate ?window_length ?hop_size" mas01cr@553: " FROM " mas01cr@553: mas01cr@553: " WHERE { " mas01cr@553: mas01cr@553: " ?signal mo:available_as ?key ." mas01cr@553: mas01cr@553: " ?signal mo:time [ tl:onTimeLine ?signal_timeline ] . " mas01cr@553: mas01cr@553: " ?timeline_map a tl:UniformSamplingWindowingMap ; " mas01cr@553: " tl:rangeTimeLine ?feature_timeline ; " mas01cr@553: " tl:domainTimeLine ?signal_timeline ; " mas01cr@553: " tl:sampleRate ?sample_rate ; " mas01cr@553: " tl:windowLength ?window_length ; " mas01cr@553: " tl:hopSize ?hop_size . " mas01cr@553: mas01cr@553: " ?signal af:signal_feature ?feature . " mas01cr@553: mas01cr@553: " ?feature a ?feature_signal_type ; " mas01cr@553: " mo:time [ tl:onTimeLine ?feature_timeline ] ; " mas01cr@553: " af:value ?value . " mas01cr@553: mas01cr@553: " ?feature_signal_type dc:title \"Key Strength Plot\"" mas01cr@553: mas01cr@553: " } " mas01cr@553: ; mas01cr@553: mas01cr@553: double *parse_value_string(const char *value_string, size_t *nelements) { mas01cr@553: /* What error checking? */ mas01cr@553: mas01cr@553: *nelements = 0; mas01cr@553: mas01cr@553: const char *current = value_string; mas01cr@553: char *next = 0; mas01cr@553: mas01cr@553: size_t size = 1; mas01cr@553: double *buf = (double *) malloc(size * sizeof(double)); mas01cr@553: double value = strtod(current, &next); mas01cr@553: while(next != current) { mas01cr@553: buf[(*nelements)++] = value; mas01cr@553: if((*nelements) == size) { mas01cr@553: size *= 2; mas01cr@553: buf = (double *) realloc(buf, 2 * size * sizeof(double)); mas01cr@553: } mas01cr@553: current = next; mas01cr@553: value = strtod(current, &next); mas01cr@553: } mas01cr@571: return buf; mas01cr@553: } mas01cr@553: mas01cr@553: int main() { mas01cr@554: librdf_world *world; mas01cr@554: if (!(world = librdf_new_world())) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: librdf_storage *storage; mas01cr@554: if (!(storage = librdf_new_storage(world, "memory", NULL, NULL))) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: librdf_model *model; mas01cr@554: if (!(model = librdf_new_model(world, storage, NULL))) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: librdf_uri *uri; mas01cr@554: if (!(uri = librdf_new_uri(world, "file:data/test.n3"))) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: librdf_parser *parser; mas01cr@554: if (!(parser = librdf_new_parser(world, "guess", NULL, NULL))) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: if(librdf_parser_parse_into_model(parser, uri, NULL, model)) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: librdf_query *query; mas01cr@554: if (!(query = librdf_new_query(world, "sparql", NULL, qstring, NULL))) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: librdf_query_results *results; mas01cr@554: if (!(results = librdf_query_execute(query, model))) mas01cr@554: goto librdf_error; mas01cr@553: mas01cr@554: if(!librdf_query_results_is_bindings(results)) mas01cr@554: goto librdf_error; mas01cr@554: mas01cr@554: adb_t *adb; mas01cr@554: if(!(adb = audiodb_open("keyplot.adb", O_RDWR))) { mas01cr@554: struct stat st; mas01cr@554: if(!(stat("keyplot.adb", &st))) { mas01cr@554: fprintf(stderr, "keyplot.adb not opened.\n"); mas01cr@554: return 1; mas01cr@554: } else { mas01cr@554: /* FIXME: if we are doing a proper SPARQL query over a mas01cr@554: * potentially unbounded number of results, we could use mas01cr@554: * librdf_query_results_get_count() to estimate how much space mas01cr@554: * our database will need. mas01cr@554: mas01cr@554: * If we're doing a SPARQL query over a number of RDF files, mas01cr@554: * with an expected number of datasets per file of 1 (as might mas01cr@554: * be produced by runner(?)) then obviously we can use that as mas01cr@554: * an estimate of number of tracks instead. mas01cr@554: mas01cr@554: * Specifying the data dimensionality should be easy from the mas01cr@554: * semantics of the feature being inserted; it's not immediately mas01cr@554: * obvious to me how to estimate the data size required, unless mas01cr@554: * we maybe do a preliminary query to find out the total time mas01cr@554: * (or similar) of all the tracks we're about to insert. mas01cr@554: mas01cr@554: * (also NOTE: this audiodb_create() interface is scheduled for mas01cr@554: * being made less inelegant.) */ mas01cr@554: if(!(adb = audiodb_create("keyplot.adb", 0, 0, 0))) { mas01cr@554: fprintf(stderr, "failed to create keyplot.adb.\n"); mas01cr@554: return 1; mas01cr@554: } mas01cr@554: } mas01cr@553: } mas01cr@554: mas01cr@553: while(!librdf_query_results_finished(results)) { mas01cr@553: int count = librdf_query_results_get_bindings_count(results); mas01cr@553: adb_datum_t datum = {0}; mas01cr@553: datum.dim = 25; mas01cr@553: for (int i = 0; i < count; i++) { mas01cr@553: const char *name = librdf_query_results_get_binding_name(results, i); mas01cr@553: librdf_node *node = librdf_query_results_get_binding_value(results, i); mas01cr@553: if(!node) return 2; mas01cr@553: mas01cr@553: if(!strcmp(name, "key")) { mas01cr@553: datum.key = librdf_uri_as_string(librdf_node_get_uri(node)); mas01cr@553: } else if(!strcmp(name, "value")) { mas01cr@553: size_t nelements = 0; mas01cr@553: datum.data = parse_value_string(librdf_node_get_literal_value(node), &nelements); mas01cr@553: if(nelements % 25) return 4; mas01cr@553: datum.nvectors = nelements / 25; mas01cr@553: } else { mas01cr@554: /* do something with the timeline (and other) information */ mas01cr@553: printf("%s: %s\n", name, librdf_node_get_literal_value(node)); mas01cr@553: } mas01cr@553: librdf_free_node(node); mas01cr@553: } mas01cr@553: if(audiodb_insert_datum(adb, &datum)) { mas01cr@554: fprintf(stderr, "failed to insert datum with key %s.\n", datum.key); mas01cr@554: return 1; mas01cr@553: } mas01cr@571: free(datum.data); mas01cr@553: librdf_query_results_next(results); mas01cr@553: } mas01cr@553: mas01cr@553: audiodb_close(adb); mas01cr@553: librdf_free_query_results(results); mas01cr@553: librdf_free_query(query); mas01cr@571: librdf_free_parser(parser); mas01cr@553: librdf_free_uri(uri); mas01cr@553: librdf_free_model(model); mas01cr@553: librdf_free_storage(storage); mas01cr@553: librdf_free_world(world); mas01cr@554: mas01cr@554: return 0; mas01cr@554: mas01cr@554: librdf_error: mas01cr@554: fprintf(stderr, "Something went wrong in librdf.\n"); mas01cr@554: return 1; mas01cr@553: }