map01bf@620: // pyadbmodule.c map01bf@620: // map01bf@620: // the internal portion of the wrapper for audio map01bf@620: // see pyadb.py for the public classes map01bf@620: // map01bf@620: // Created by Benjamin Fields on 2009-09-04. map01bf@620: // Copyright (c) 2009 Goldsmith University of London. All rights reserved. map01bf@620: // map01bf@620: #include map01bf@620: #include "Python.h" map01bf@620: #include "audioDB_API.h" map01bf@620: #include "numpy/arrayobject.h" map01bf@620: map01bf@620: static void _pyadb_close(void *ptr); map01bf@620: map01bf@620: map01bf@620: /* create a new database */ map01bf@620: /* returns a struct or NULL on failure */ map01bf@620: /* api call: */ map01bf@620: /* adb_ptr audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim);*/ map01bf@620: PyObject * _pyadb_create(PyObject *self, PyObject *args) map01bf@620: { map01bf@620: unsigned datasize, ntracks, datadim; map01bf@620: const char *path; map01bf@620: int ok; map01bf@620: adb_ptr new_database; map01bf@620: ok = PyArg_ParseTuple(args, "sIII", &path, &datasize, &ntracks, &datadim); map01bf@620: if (!ok) return 0; map01bf@620: new_database = audiodb_create(path, datasize, ntracks, datadim); map01bf@620: if (!new_database) return 0; map01bf@620: map01bf@620: return PyCObject_FromVoidPtr( new_database, _pyadb_close); map01bf@620: } map01bf@620: map01bf@620: /* open an existing database */ map01bf@620: /* returns a struct or NULL on failure */ map01bf@620: /* flags expects fcntl flags concerning the opening mode */ map01bf@620: /* api call: */ map01bf@620: // adb_ptr audiodb_open(const char *path, int flags); map01bf@620: PyObject * _pyadb_open(PyObject *self, PyObject *args) map01bf@620: { map01bf@620: const char *path; map01bf@620: char mode; map01bf@620: int ok;//in python layer need to translate boolean flags to byte mask map01bf@620: adb_ptr fresh_database; map01bf@620: ok = PyArg_ParseTuple(args, "sc", &path, &mode); map01bf@620: if (!ok) return 0; map01bf@620: if (mode == 'r'){ map01bf@620: fresh_database = audiodb_open(path, O_RDONLY); map01bf@620: }else if (mode == 'w'){ map01bf@620: fresh_database = audiodb_open(path, O_RDWR); map01bf@620: }else{ map01bf@620: PyErr_SetString(PyExc_ValueError, map01bf@620: "mode must be either \'r\' or \'w\'. It appears to be something else."); map01bf@620: return 0; map01bf@620: } map01bf@620: if (!fresh_database) return 0; map01bf@620: map01bf@620: return PyCObject_FromVoidPtr( fresh_database, _pyadb_close); map01bf@620: } map01bf@620: map01bf@620: /* database status */ map01bf@620: /* api call: */ map01bf@620: // int audiodb_status(adb_ptr mydb, adb_status_ptr status); map01bf@620: PyObject * _pyadb_status(PyObject *self, PyObject *args) map01bf@620: { map01bf@620: adb_ptr check_db; map01bf@620: adb_status_ptr status; map01bf@620: int flags, ok; map01bf@620: PyObject * incoming = 0; map01bf@620: status = (adb_status_ptr)malloc(sizeof(struct adbstatus)); map01bf@620: map01bf@620: ok = PyArg_ParseTuple(args, "O", &incoming); map01bf@620: if (!ok) return 0; map01bf@620: check_db = (adb_ptr)PyCObject_AsVoidPtr(incoming); map01bf@620: map01bf@620: map01bf@620: flags = audiodb_status(check_db, status); map01bf@620: return Py_BuildValue("IIIIILL", status->numFiles, map01bf@620: status->dim, map01bf@620: status->dudCount, map01bf@620: status->nullCount, map01bf@620: status->flags, map01bf@620: status->length, map01bf@620: status->data_region_size); map01bf@620: map01bf@620: } map01bf@620: map01bf@620: /*engage l2norm in the referenced db*/ map01bf@620: /*api call:*/ map01bf@620: //int audiodb_l2norm(adb_ptr mydb); map01bf@620: PyObject * _pyadb_l2norm(PyObject *self, PyObject *args) map01bf@620: { map01bf@620: adb_ptr current_db; map01bf@620: int ok; map01bf@620: PyObject * incoming = 0; map01bf@620: map01bf@620: ok = PyArg_ParseTuple(args, "O", &incoming); map01bf@620: if (!ok) return 0; map01bf@620: current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming); map01bf@620: map01bf@620: ok = audiodb_l2norm(current_db); map01bf@620: return Py_BuildValue("i", ok); map01bf@620: map01bf@620: } map01bf@620: map01bf@620: /*engage power thresholding in the referenced db*/ map01bf@620: /*api call:*/ map01bf@620: // int audiodb_power(adb_ptr mydb); map01bf@620: PyObject * _pyadb_power(PyObject *self, PyObject *args) map01bf@620: { map01bf@620: adb_ptr current_db; map01bf@620: int ok; map01bf@620: PyObject * incoming = 0; map01bf@620: map01bf@620: ok = PyArg_ParseTuple(args, "O", &incoming); map01bf@620: if (!ok) return 0; map01bf@620: current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming); map01bf@620: map01bf@620: ok = audiodb_power(current_db); map01bf@620: return Py_BuildValue("i", ok); map01bf@620: map01bf@620: } map01bf@620: map01bf@620: /* close a database */ map01bf@620: /* api call: */ map01bf@620: // void audiodb_close(adb_ptr db); map01bf@620: static void _pyadb_close(void *ptr) map01bf@620: { map01bf@620: adb_ptr stale_database; map01bf@620: stale_database = (adb_ptr)ptr; map01bf@620: map01bf@620: audiodb_close(stale_database); map01bf@620: } map01bf@620: map01bf@620: static PyMethodDef _pyadbMethods[] = map01bf@620: { map01bf@620: { "_pyadb_create", _pyadb_create, METH_VARARGS, map01bf@620: "_pyadb_create(string path, unsigned datasize, unsigned ntracks, unsigned datadim)->adb object"}, map01bf@620: { "_pyadb_open", _pyadb_open, METH_VARARGS, map01bf@620: "_pyadb_open(string path, [\'r\'|\'w\'])->adb object\nNote that specifing \'w\' opens the file in read and write mode. \ map01bf@620: There is currently no way to open in write only."}, map01bf@620: { "_pyadb_status", _pyadb_status, METH_VARARGS, map01bf@620: "_status(adb_ptr)->(numFiles, dims, dudCount, nullCount, flags, length, data_region_size)"}, map01bf@620: { "_pyadb_l2norm", _pyadb_l2norm, METH_VARARGS, map01bf@620: "_pyadb_l2norm(adb_ptr)->int return code (0 for sucess)"}, map01bf@620: { "_pyadb_power", _pyadb_power, METH_VARARGS, map01bf@620: "_pyadb_power(adb_ptr)->int return code (0 for sucess)"}, map01bf@620: {NULL,NULL, 0, NULL} map01bf@620: }; map01bf@620: map01bf@620: void init_pyadb() map01bf@620: { map01bf@620: Py_InitModule3("_pyadb", _pyadbMethods, "internal c bindings for audioDB. Use pyadb for pythonic access to adb (when it exists)."); map01bf@620: // import_array(); map01bf@620: return; map01bf@620: } map01bf@620: map01bf@620: