annotate bindings/python/pyadbmodule.c @ 621:2e9a6e4500d3

added an insert from file function. error handling is a bit gross. altered the license reference in the header comments.
author map01bf
date Wed, 16 Sep 2009 16:12:33 +0000
parents 70fc1a504138
children 695651b8c1a3
rev   line source
map01bf@620 1 // pyadbmodule.c
map01bf@620 2 //
map01bf@620 3 // the internal portion of the wrapper for audio
map01bf@620 4 // see pyadb.py for the public classes
map01bf@620 5 //
map01bf@620 6 // Created by Benjamin Fields on 2009-09-04.
map01bf@621 7 // Copyright (c) 2009 Goldsmith University of London.
map01bf@621 8 // Distributed and licensed under GPL2. See ../../license.txt for details.
map01bf@620 9 //
map01bf@620 10 #include <fcntl.h>
map01bf@621 11 #include <string.h>
map01bf@620 12 #include "Python.h"
map01bf@620 13 #include "audioDB_API.h"
map01bf@620 14 #include "numpy/arrayobject.h"
map01bf@620 15
map01bf@620 16 static void _pyadb_close(void *ptr);
map01bf@620 17
map01bf@620 18
map01bf@620 19 /* create a new database */
map01bf@620 20 /* returns a struct or NULL on failure */
map01bf@620 21 /* api call: */
map01bf@620 22 /* adb_ptr audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim);*/
map01bf@620 23 PyObject * _pyadb_create(PyObject *self, PyObject *args)
map01bf@620 24 {
map01bf@620 25 unsigned datasize, ntracks, datadim;
map01bf@620 26 const char *path;
map01bf@620 27 int ok;
map01bf@620 28 adb_ptr new_database;
map01bf@620 29 ok = PyArg_ParseTuple(args, "sIII", &path, &datasize, &ntracks, &datadim);
map01bf@620 30 if (!ok) return 0;
map01bf@620 31 new_database = audiodb_create(path, datasize, ntracks, datadim);
map01bf@620 32 if (!new_database) return 0;
map01bf@620 33
map01bf@620 34 return PyCObject_FromVoidPtr( new_database, _pyadb_close);
map01bf@620 35 }
map01bf@620 36
map01bf@620 37 /* open an existing database */
map01bf@620 38 /* returns a struct or NULL on failure */
map01bf@620 39 /* flags expects fcntl flags concerning the opening mode */
map01bf@620 40 /* api call: */
map01bf@620 41 // adb_ptr audiodb_open(const char *path, int flags);
map01bf@620 42 PyObject * _pyadb_open(PyObject *self, PyObject *args)
map01bf@620 43 {
map01bf@620 44 const char *path;
map01bf@620 45 char mode;
map01bf@620 46 int ok;//in python layer need to translate boolean flags to byte mask
map01bf@620 47 adb_ptr fresh_database;
map01bf@620 48 ok = PyArg_ParseTuple(args, "sc", &path, &mode);
map01bf@620 49 if (!ok) return 0;
map01bf@620 50 if (mode == 'r'){
map01bf@620 51 fresh_database = audiodb_open(path, O_RDONLY);
map01bf@620 52 }else if (mode == 'w'){
map01bf@620 53 fresh_database = audiodb_open(path, O_RDWR);
map01bf@620 54 }else{
map01bf@620 55 PyErr_SetString(PyExc_ValueError,
map01bf@620 56 "mode must be either \'r\' or \'w\'. It appears to be something else.");
map01bf@620 57 return 0;
map01bf@620 58 }
map01bf@620 59 if (!fresh_database) return 0;
map01bf@620 60
map01bf@620 61 return PyCObject_FromVoidPtr( fresh_database, _pyadb_close);
map01bf@620 62 }
map01bf@620 63
map01bf@620 64 /* database status */
map01bf@620 65 /* api call: */
map01bf@620 66 // int audiodb_status(adb_ptr mydb, adb_status_ptr status);
map01bf@620 67 PyObject * _pyadb_status(PyObject *self, PyObject *args)
map01bf@620 68 {
map01bf@620 69 adb_ptr check_db;
map01bf@620 70 adb_status_ptr status;
map01bf@620 71 int flags, ok;
map01bf@620 72 PyObject * incoming = 0;
map01bf@620 73 status = (adb_status_ptr)malloc(sizeof(struct adbstatus));
map01bf@620 74
map01bf@620 75 ok = PyArg_ParseTuple(args, "O", &incoming);
map01bf@620 76 if (!ok) return 0;
map01bf@620 77 check_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@620 78
map01bf@620 79
map01bf@620 80 flags = audiodb_status(check_db, status);
map01bf@620 81 return Py_BuildValue("IIIIILL", status->numFiles,
map01bf@620 82 status->dim,
map01bf@620 83 status->dudCount,
map01bf@620 84 status->nullCount,
map01bf@620 85 status->flags,
map01bf@620 86 status->length,
map01bf@620 87 status->data_region_size);
map01bf@620 88
map01bf@620 89 }
map01bf@620 90
map01bf@620 91 /*engage l2norm in the referenced db*/
map01bf@620 92 /*api call:*/
map01bf@620 93 //int audiodb_l2norm(adb_ptr mydb);
map01bf@620 94 PyObject * _pyadb_l2norm(PyObject *self, PyObject *args)
map01bf@620 95 {
map01bf@620 96 adb_ptr current_db;
map01bf@620 97 int ok;
map01bf@620 98 PyObject * incoming = 0;
map01bf@620 99
map01bf@620 100 ok = PyArg_ParseTuple(args, "O", &incoming);
map01bf@620 101 if (!ok) return 0;
map01bf@620 102 current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@620 103
map01bf@620 104 ok = audiodb_l2norm(current_db);
map01bf@620 105 return Py_BuildValue("i", ok);
map01bf@620 106
map01bf@620 107 }
map01bf@620 108
map01bf@620 109 /*engage power thresholding in the referenced db*/
map01bf@620 110 /*api call:*/
map01bf@620 111 // int audiodb_power(adb_ptr mydb);
map01bf@620 112 PyObject * _pyadb_power(PyObject *self, PyObject *args)
map01bf@620 113 {
map01bf@620 114 adb_ptr current_db;
map01bf@620 115 int ok;
map01bf@620 116 PyObject * incoming = 0;
map01bf@620 117
map01bf@620 118 ok = PyArg_ParseTuple(args, "O", &incoming);
map01bf@620 119 if (!ok) return 0;
map01bf@620 120 current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@620 121
map01bf@620 122 ok = audiodb_power(current_db);
map01bf@620 123 return Py_BuildValue("i", ok);
map01bf@620 124
map01bf@620 125 }
map01bf@621 126 /* insert feature data stored in a file */
map01bf@621 127 /* this is a bit gross, */
map01bf@621 128 /* should be replaced eventually by a numpy based feature.*/
map01bf@621 129 /* api call: */
map01bf@621 130 // struct adbinsert {
map01bf@621 131 // const char *features;
map01bf@621 132 // const char *power;
map01bf@621 133 // const char *key;
map01bf@621 134 // const char *times;
map01bf@621 135 // };
map01bf@621 136 // int audiodb_insert(adb_ptr mydb, adb_insert_ptr ins);
map01bf@621 137 PyObject * _pyadb_insertFromFile(PyObject *self, PyObject *args, PyObject *keywds)
map01bf@621 138 {
map01bf@621 139 adb_ptr current_db;
map01bf@621 140 adb_insert_ptr ins;
map01bf@621 141 int ok;
map01bf@621 142 const char *features;
map01bf@621 143 const char *power = NULL;
map01bf@621 144 const char *key = NULL;
map01bf@621 145 const char *times = NULL;
map01bf@621 146 PyObject * incoming = 0;
map01bf@621 147 static char *kwlist[] = { "db", "features", "power", "key", "times" , NULL};
map01bf@621 148
map01bf@621 149 ok = PyArg_ParseTupleAndKeywords(args, keywds, "Os|sss", kwlist, &incoming, &features, &power, &key, &times);
map01bf@621 150 if (!ok){return NULL;}
map01bf@621 151
map01bf@621 152 current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@621 153 ins = (adb_insert_ptr)malloc(sizeof(adb_insert_t));
map01bf@621 154 ins->features = features;
map01bf@621 155 ins->power = power;
map01bf@621 156 ins->key = key;
map01bf@621 157 ins->times = times;
map01bf@621 158 printf("features::%s\npower::%s\nkey::%s\ntimes::%s\n", ins->features, ins->power, ins->key, ins->times);
map01bf@621 159 ok = audiodb_insert(current_db, ins);
map01bf@621 160 return Py_BuildValue("i", ok);
map01bf@621 161
map01bf@621 162 }
map01bf@621 163
map01bf@621 164
map01bf@621 165
map01bf@620 166
map01bf@620 167 /* close a database */
map01bf@620 168 /* api call: */
map01bf@620 169 // void audiodb_close(adb_ptr db);
map01bf@620 170 static void _pyadb_close(void *ptr)
map01bf@620 171 {
map01bf@620 172 adb_ptr stale_database;
map01bf@620 173 stale_database = (adb_ptr)ptr;
map01bf@620 174
map01bf@620 175 audiodb_close(stale_database);
map01bf@620 176 }
map01bf@620 177
map01bf@620 178 static PyMethodDef _pyadbMethods[] =
map01bf@620 179 {
map01bf@620 180 { "_pyadb_create", _pyadb_create, METH_VARARGS,
map01bf@620 181 "_pyadb_create(string path, unsigned datasize, unsigned ntracks, unsigned datadim)->adb object"},
map01bf@620 182 { "_pyadb_open", _pyadb_open, METH_VARARGS,
map01bf@620 183 "_pyadb_open(string path, [\'r\'|\'w\'])->adb object\nNote that specifing \'w\' opens the file in read and write mode. \
map01bf@620 184 There is currently no way to open in write only."},
map01bf@620 185 { "_pyadb_status", _pyadb_status, METH_VARARGS,
map01bf@620 186 "_status(adb_ptr)->(numFiles, dims, dudCount, nullCount, flags, length, data_region_size)"},
map01bf@620 187 { "_pyadb_l2norm", _pyadb_l2norm, METH_VARARGS,
map01bf@620 188 "_pyadb_l2norm(adb_ptr)->int return code (0 for sucess)"},
map01bf@620 189 { "_pyadb_power", _pyadb_power, METH_VARARGS,
map01bf@620 190 "_pyadb_power(adb_ptr)->int return code (0 for sucess)"},
map01bf@621 191 { "_pyadb_insertFromFile", _pyadb_insertFromFile, METH_VARARGS | METH_KEYWORDS,
map01bf@621 192 "_pyadb_insertFromFile(adb_ptr, features=featureFile, [power=powerfile | key=keystring | times=timingFile])->\
map01bf@621 193 int return code (0 for sucess)"},
map01bf@620 194 {NULL,NULL, 0, NULL}
map01bf@620 195 };
map01bf@620 196
map01bf@620 197 void init_pyadb()
map01bf@620 198 {
map01bf@620 199 Py_InitModule3("_pyadb", _pyadbMethods, "internal c bindings for audioDB. Use pyadb for pythonic access to adb (when it exists).");
map01bf@620 200 // import_array();
map01bf@620 201 return;
map01bf@620 202 }
map01bf@620 203
map01bf@620 204