view l2norm.cpp @ 622:695651b8c1a3

added a query hook. Should compile a run, but I haven't exhaustively tested the various input parameters yet. As such, there may well be some ways to get to the api calls that bring the module down. Let me know if you find any. The actual query call is a bit of a mess, but will be more intuitive from the native python layer (to be written)... so the python bindings now have a complete path: >>import _pyadb >>aDB = _pyadb._pyadb_create("test.adb", 0,0,0) >>_pyadb._pyadb_status(aDB) >>_pyadb._pyadb_insertFromFile(aDB, "someFeats.mfcc12") ...(add some more data) >>result = _pyadb._pyadb_queryFromKey(aDB, "a Key in aDB", [options]) and then result has a nice dict of your results.
author map01bf
date Mon, 21 Sep 2009 17:42:52 +0000
parents 70acfcb5010a
children
line wrap: on
line source
extern "C" {
#include "audioDB_API.h"
}
#include "audioDB-internals.h"

static int audiodb_l2norm_existing(adb_t *adb) {
  double *data_buffer = 0, *l2norm_buffer = 0;
  size_t data_buffer_size = align_page_up(adb->header->length);
  size_t nvectors = adb->header->length / (sizeof(double) * adb->header->dim);
  /* FIXME: this allocation of the vector data buffer will lose if we
   * ever turn the l2norm flag on when we have already inserted a
   * large number of vectors, as the malloc() will fail.  "Don't do
   * that, then" is one possible answer. */
  data_buffer = (double *) malloc(data_buffer_size);
  if (!data_buffer) {
    goto error;
  }
  lseek_set_or_goto_error(adb->fd, adb->header->dataOffset);
  read_or_goto_error(adb->fd, data_buffer, data_buffer_size);

  l2norm_buffer = (double *) malloc(nvectors * sizeof(double));
  if(!l2norm_buffer) {
    goto error;
  }
  audiodb_l2norm_buffer(data_buffer, adb->header->dim, nvectors, l2norm_buffer);
  lseek_set_or_goto_error(adb->fd, adb->header->l2normTableOffset);
  write_or_goto_error(adb->fd, l2norm_buffer, nvectors * sizeof(double));

  free(l2norm_buffer);
  free(data_buffer);

  return 0;

 error:
  if(data_buffer) {
    free(data_buffer);
  }
  if(l2norm_buffer) {
    free(l2norm_buffer);
  }
  return 1;
}

int audiodb_l2norm(adb_t *adb) {
  if(!(adb->flags & O_RDWR)) {
    return 1;
  }
  if(adb->header->flags & ADB_HEADER_FLAG_L2NORM) {
    /* non-error code for forthcoming backwards-compatibility
     * reasons */
    return 0;
  }
  if((!(adb->header->flags & ADB_HEADER_FLAG_REFERENCES)) && 
     (adb->header->length > 0)) {
    if(audiodb_l2norm_existing(adb)) {
      goto error;
    }
  }
  adb->header->flags |= ADB_HEADER_FLAG_L2NORM;
  return audiodb_sync_header(adb);

 error:
  return 1;
}