Mercurial > hg > audiodb
comparison query.cpp @ 433:681837f7c903 api-inversion
More on the query rationalization
Turn audioDB::delete_arrays and audioDB::read_data into ordinary
functions.
Also partially do audioDB::initialize_arrays, except for the bits that
we can't yet do because we haven't got a query datum/identifier going
through yet.
author | mas01cr |
---|---|
date | Wed, 24 Dec 2008 10:55:48 +0000 |
parents | 8632cd387e24 |
children | 7af140bf8a0a |
comparison
equal
deleted
inserted
replaced
432:62a0515f59be | 433:681837f7c903 |
---|---|
169 } | 169 } |
170 | 170 |
171 reporter->report(fileTable, adbQueryResponse); | 171 reporter->report(fileTable, adbQueryResponse); |
172 } | 172 } |
173 | 173 |
174 void audioDB::initialize_arrays(int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD) { | 174 void audioDB::initialize_arrays(adb_t *adb, int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD) { |
175 unsigned int j, k, l, w; | 175 unsigned int j, k, l, w; |
176 double *dp, *qp, *sp; | 176 double *dp, *qp, *sp; |
177 | 177 |
178 const unsigned HOP_SIZE = sequenceHop; | 178 const unsigned HOP_SIZE = sequenceHop; |
179 const unsigned wL = sequenceLength; | 179 const unsigned wL = sequenceLength; |
180 | 180 |
181 for(j = 0; j < numVectors; j++) { | 181 for(j = 0; j < numVectors; j++) { |
182 // Sum products matrix | 182 // Sum products matrix |
183 D[j] = new double[trackTable[track]]; | 183 D[j] = new double[(*adb->track_lengths)[track]]; |
184 assert(D[j]); | 184 assert(D[j]); |
185 // Matched filter matrix | 185 // Matched filter matrix |
186 DD[j]=new double[trackTable[track]]; | 186 DD[j]=new double[(*adb->track_lengths)[track]]; |
187 assert(DD[j]); | 187 assert(DD[j]); |
188 } | 188 } |
189 | 189 |
190 // Dot product | 190 // Dot product |
191 for(j = 0; j < numVectors; j++) | 191 for(j = 0; j < numVectors; j++) |
192 for(k = 0; k < trackTable[track]; k++){ | 192 for(k = 0; k < (*adb->track_lengths)[track]; k++){ |
193 qp = query + j * dbH->dim; | 193 qp = query + j * dbH->dim; |
194 sp = data_buffer + k * dbH->dim; | 194 sp = data_buffer + k * dbH->dim; |
195 DD[j][k] = 0.0; // Initialize matched filter array | 195 DD[j][k] = 0.0; // Initialize matched filter array |
196 dp = &D[j][k]; // point to correlation cell j,k | 196 dp = &D[j][k]; // point to correlation cell j,k |
197 *dp = 0.0; // initialize correlation cell | 197 *dp = 0.0; // initialize correlation cell |
206 if(HOP_SIZE == 1) { // HOP_SIZE = shingleHop | 206 if(HOP_SIZE == 1) { // HOP_SIZE = shingleHop |
207 for(w = 0; w < wL; w++) { | 207 for(w = 0; w < wL; w++) { |
208 for(j = 0; j < numVectors - w; j++) { | 208 for(j = 0; j < numVectors - w; j++) { |
209 sp = DD[j]; | 209 sp = DD[j]; |
210 spd = D[j+w] + w; | 210 spd = D[j+w] + w; |
211 k = trackTable[track] - w; | 211 k = (*adb->track_lengths)[track] - w; |
212 while(k--) | 212 while(k--) |
213 *sp++ += *spd++; | 213 *sp++ += *spd++; |
214 } | 214 } |
215 } | 215 } |
216 } else { // HOP_SIZE != 1 | 216 } else { // HOP_SIZE != 1 |
217 for(w = 0; w < wL; w++) { | 217 for(w = 0; w < wL; w++) { |
218 for(j = 0; j < numVectors - w; j += HOP_SIZE) { | 218 for(j = 0; j < numVectors - w; j += HOP_SIZE) { |
219 sp = DD[j]; | 219 sp = DD[j]; |
220 spd = D[j+w]+w; | 220 spd = D[j+w]+w; |
221 for(k = 0; k < trackTable[track] - w; k += HOP_SIZE) { | 221 for(k = 0; k < (*adb->track_lengths)[track] - w; k += HOP_SIZE) { |
222 *sp += *spd; | 222 *sp += *spd; |
223 sp += HOP_SIZE; | 223 sp += HOP_SIZE; |
224 spd += HOP_SIZE; | 224 spd += HOP_SIZE; |
225 } | 225 } |
226 } | 226 } |
227 } | 227 } |
228 } | 228 } |
229 } | 229 } |
230 | 230 |
231 void audioDB::delete_arrays(int track, unsigned int numVectors, double **D, double **DD) { | 231 static void audiodb_delete_arrays(int track, unsigned int numVectors, double **D, double **DD) { |
232 if(D != NULL) { | 232 if(D != NULL) { |
233 for(unsigned int j = 0; j < numVectors; j++) { | 233 for(unsigned int j = 0; j < numVectors; j++) { |
234 delete[] D[j]; | 234 delete[] D[j]; |
235 } | 235 } |
236 } | 236 } |
239 delete[] DD[j]; | 239 delete[] DD[j]; |
240 } | 240 } |
241 } | 241 } |
242 } | 242 } |
243 | 243 |
244 void audioDB::read_data(int trkfid, int track, double **data_buffer_p, size_t *data_buffer_size_p) { | 244 int audiodb_read_data(adb_t *adb, int trkfid, int track, double **data_buffer_p, size_t *data_buffer_size_p) { |
245 if (trackTable[track] * sizeof(double) * dbH->dim > *data_buffer_size_p) { | 245 uint32_t track_length = (*adb->track_lengths)[track]; |
246 size_t track_size = track_length * sizeof(double) * adb->header->dim; | |
247 if (track_size > *data_buffer_size_p) { | |
246 if(*data_buffer_p) { | 248 if(*data_buffer_p) { |
247 free(*data_buffer_p); | 249 free(*data_buffer_p); |
248 } | 250 } |
249 { | 251 { |
250 *data_buffer_size_p = trackTable[track] * sizeof(double) * dbH->dim; | 252 *data_buffer_size_p = track_size; |
251 void *tmp = malloc(*data_buffer_size_p); | 253 void *tmp = malloc(track_size); |
252 if (tmp == NULL) { | 254 if (tmp == NULL) { |
253 error("error allocating data buffer"); | 255 goto error; |
254 } | 256 } |
255 *data_buffer_p = (double *) tmp; | 257 *data_buffer_p = (double *) tmp; |
256 } | 258 } |
257 } | 259 } |
258 | 260 |
259 CHECKED_READ(trkfid, *data_buffer_p, trackTable[track] * sizeof(double) * dbH->dim); | 261 read_or_goto_error(trkfid, *data_buffer_p, track_size); |
262 return 0; | |
263 | |
264 error: | |
265 return 1; | |
260 } | 266 } |
261 | 267 |
262 void audioDB::insertTimeStamps(unsigned numVectors, std::ifstream *timesFile, double *timesdata) { | 268 void audioDB::insertTimeStamps(unsigned numVectors, std::ifstream *timesFile, double *timesdata) { |
263 assert(usingTimes); | 269 assert(usingTimes); |
264 | 270 |
408 prefix_name(&prefixedString, adb_feature_root); | 414 prefix_name(&prefixedString, adb_feature_root); |
409 if(tmpStr!=prefixedString) | 415 if(tmpStr!=prefixedString) |
410 delete[] tmpStr; | 416 delete[] tmpStr; |
411 initInputFile(prefixedString, false); // nommap, file pointer at correct position | 417 initInputFile(prefixedString, false); // nommap, file pointer at correct position |
412 size_t allocatedSize = 0; | 418 size_t allocatedSize = 0; |
413 read_data(infid, queryIndex, qp, &allocatedSize); // over-writes qp and allocatedSize | 419 if(audiodb_read_data(adb, infid, queryIndex, qp, &allocatedSize)) |
420 error("failed to read data"); // over-writes qp and allocatedSize | |
414 // Consistency check on allocated memory and query feature size | 421 // Consistency check on allocated memory and query feature size |
415 if(*nvp*sizeof(double)*dbH->dim != allocatedSize) | 422 if(*nvp*sizeof(double)*dbH->dim != allocatedSize) |
416 error("Query memory allocation failed consitency check","set_up_query_from_key"); | 423 error("Query memory allocation failed consitency check","set_up_query_from_key"); |
417 // Allocated and calculate auxillary sequences: l2norm and power | 424 // Allocated and calculate auxillary sequences: l2norm and power |
418 init_track_aux_data(queryIndex, *qp, qnp, vqnp, qpp, vqpp); | 425 init_track_aux_data(queryIndex, *qp, qnp, vqnp, qpp, vqpp); |
420 else{ // Load from self-contained ADB database | 427 else{ // Load from self-contained ADB database |
421 // Read query feature vectors from database | 428 // Read query feature vectors from database |
422 *qp = NULL; | 429 *qp = NULL; |
423 lseek(dbfid, dbH->dataOffset + trackOffsetTable[queryIndex] * sizeof(double), SEEK_SET); | 430 lseek(dbfid, dbH->dataOffset + trackOffsetTable[queryIndex] * sizeof(double), SEEK_SET); |
424 size_t allocatedSize = 0; | 431 size_t allocatedSize = 0; |
425 read_data(dbfid, queryIndex, qp, &allocatedSize); | 432 if(audiodb_read_data(adb, dbfid, queryIndex, qp, &allocatedSize)) |
433 error("failed to read data"); | |
426 // Consistency check on allocated memory and query feature size | 434 // Consistency check on allocated memory and query feature size |
427 if(*nvp*sizeof(double)*dbH->dim != allocatedSize) | 435 if(*nvp*sizeof(double)*dbH->dim != allocatedSize) |
428 error("Query memory allocation failed consitency check","set_up_query_from_key"); | 436 error("Query memory allocation failed consitency check","set_up_query_from_key"); |
429 | 437 |
430 Uns32T trackIndexOffset = trackOffsetTable[queryIndex]/dbH->dim; // Convert num data elements to num vectors | 438 Uns32T trackIndexOffset = trackOffsetTable[queryIndex]/dbH->dim; // Convert num data elements to num vectors |
604 prefix_name((char ** const) &prefixedString, adb_feature_root); | 612 prefix_name((char ** const) &prefixedString, adb_feature_root); |
605 if (prefixedString!=tmpStr) | 613 if (prefixedString!=tmpStr) |
606 delete[] tmpStr; | 614 delete[] tmpStr; |
607 initInputFile(prefixedString, false); // nommap, file pointer at correct position | 615 initInputFile(prefixedString, false); // nommap, file pointer at correct position |
608 // Load the feature vector data for current track into data_buffer | 616 // Load the feature vector data for current track into data_buffer |
609 read_data(infid, pp.trackID, &data_buffer, &data_buffer_size); | 617 if(audiodb_read_data(adb, infid, pp.trackID, &data_buffer, &data_buffer_size)) |
618 error("failed to read data"); | |
610 // Load power and calculate power and l2norm sequence sums | 619 // Load power and calculate power and l2norm sequence sums |
611 init_track_aux_data(pp.trackID, data_buffer, &sNorm, &snPtr, &sPower, &spPtr); | 620 init_track_aux_data(pp.trackID, data_buffer, &sNorm, &snPtr, &sPower, &spPtr); |
612 } | 621 } |
613 } | 622 } |
614 else{ | 623 else{ |
624 // Non-large ADB track data is loaded inside power test for efficiency | 633 // Non-large ADB track data is loaded inside power test for efficiency |
625 if( !(dbH->flags & O2_FLAG_LARGE_ADB) && (currentTrack!=pp.trackID) ){ | 634 if( !(dbH->flags & O2_FLAG_LARGE_ADB) && (currentTrack!=pp.trackID) ){ |
626 // On currentTrack change, allocate and load track data | 635 // On currentTrack change, allocate and load track data |
627 currentTrack=pp.trackID; | 636 currentTrack=pp.trackID; |
628 lseek(dbfid, dbH->dataOffset + trackOffset * sizeof(double), SEEK_SET); | 637 lseek(dbfid, dbH->dataOffset + trackOffset * sizeof(double), SEEK_SET); |
629 read_data(dbfid, currentTrack, &data_buffer, &data_buffer_size); | 638 if(audiodb_read_data(adb, dbfid, currentTrack, &data_buffer, &data_buffer_size)) |
639 error("failed to read data"); | |
630 } | 640 } |
631 // Compute distance | 641 // Compute distance |
632 dist = audiodb_dot_product(query+qPos*dbH->dim, data_buffer+pp.spos*dbH->dim, dbH->dim*sequenceLength); | 642 dist = audiodb_dot_product(query+qPos*dbH->dim, data_buffer+pp.spos*dbH->dim, dbH->dim*sequenceLength); |
633 double qn = qnPtr[qPos]; | 643 double qn = qnPtr[qPos]; |
634 double sn = sNorm[sPos]; | 644 double sn = sNorm[sPos]; |
728 } | 738 } |
729 } | 739 } |
730 | 740 |
731 trackIndexOffset=trackOffset/dbH->dim; // numVectors offset | 741 trackIndexOffset=trackOffset/dbH->dim; // numVectors offset |
732 | 742 |
733 read_data(dbfid, track, &data_buffer, &data_buffer_size); | 743 if(audiodb_read_data(adb, dbfid, track, &data_buffer, &data_buffer_size)) |
744 error("failed to read data"); | |
734 if(sequenceLength <= trackTable[track]) { // test for short sequences | 745 if(sequenceLength <= trackTable[track]) { // test for short sequences |
735 | 746 |
736 VERB_LOG(7,"%u.%jd.%u | ", track, (intmax_t) trackIndexOffset, trackTable[track]); | 747 VERB_LOG(7,"%u.%jd.%u | ", track, (intmax_t) trackIndexOffset, trackTable[track]); |
737 | 748 |
738 initialize_arrays(track, numVectors, query, data_buffer, D, DD); | 749 initialize_arrays(adb, track, numVectors, query, data_buffer, D, DD); |
739 | 750 |
740 if(refine->flags & ADB_REFINE_DURATION_RATIO) { | 751 if(refine->flags & ADB_REFINE_DURATION_RATIO) { |
741 VERB_LOG(3,"meanQdur=%f meanDBdur=%f\n", meanQdur, meanDBdur[track]); | 752 VERB_LOG(3,"meanQdur=%f meanDBdur=%f\n", meanQdur, meanDBdur[track]); |
742 } | 753 } |
743 | 754 |
775 } | 786 } |
776 } | 787 } |
777 } | 788 } |
778 } | 789 } |
779 } // Duration match | 790 } // Duration match |
780 delete_arrays(track, numVectors, D, DD); | 791 audiodb_delete_arrays(track, numVectors, D, DD); |
781 } | 792 } |
782 } | 793 } |
783 | 794 |
784 free(data_buffer); | 795 free(data_buffer); |
785 | 796 |