annotate bindings/python/pyadbmodule.c @ 620:70fc1a504138

Okay, I think my python bindings have reached the border of useful, so in the svn they go. The bindings currently allow for adb create and open, status and the setting of the l2norm and power flags. Note that these are the direct c bindings, when it's done there will be OO dressing on top for standard entry. Also, even though I don't use numpy yet, the include file is brought in, so the module won't build if you don't have numpy in an importable place.
author map01bf
date Tue, 15 Sep 2009 17:40:02 +0000
parents
children 2e9a6e4500d3
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@620 7 // Copyright (c) 2009 Goldsmith University of London. All rights reserved.
map01bf@620 8 //
map01bf@620 9 #include <fcntl.h>
map01bf@620 10 #include "Python.h"
map01bf@620 11 #include "audioDB_API.h"
map01bf@620 12 #include "numpy/arrayobject.h"
map01bf@620 13
map01bf@620 14 static void _pyadb_close(void *ptr);
map01bf@620 15
map01bf@620 16
map01bf@620 17 /* create a new database */
map01bf@620 18 /* returns a struct or NULL on failure */
map01bf@620 19 /* api call: */
map01bf@620 20 /* adb_ptr audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim);*/
map01bf@620 21 PyObject * _pyadb_create(PyObject *self, PyObject *args)
map01bf@620 22 {
map01bf@620 23 unsigned datasize, ntracks, datadim;
map01bf@620 24 const char *path;
map01bf@620 25 int ok;
map01bf@620 26 adb_ptr new_database;
map01bf@620 27 ok = PyArg_ParseTuple(args, "sIII", &path, &datasize, &ntracks, &datadim);
map01bf@620 28 if (!ok) return 0;
map01bf@620 29 new_database = audiodb_create(path, datasize, ntracks, datadim);
map01bf@620 30 if (!new_database) return 0;
map01bf@620 31
map01bf@620 32 return PyCObject_FromVoidPtr( new_database, _pyadb_close);
map01bf@620 33 }
map01bf@620 34
map01bf@620 35 /* open an existing database */
map01bf@620 36 /* returns a struct or NULL on failure */
map01bf@620 37 /* flags expects fcntl flags concerning the opening mode */
map01bf@620 38 /* api call: */
map01bf@620 39 // adb_ptr audiodb_open(const char *path, int flags);
map01bf@620 40 PyObject * _pyadb_open(PyObject *self, PyObject *args)
map01bf@620 41 {
map01bf@620 42 const char *path;
map01bf@620 43 char mode;
map01bf@620 44 int ok;//in python layer need to translate boolean flags to byte mask
map01bf@620 45 adb_ptr fresh_database;
map01bf@620 46 ok = PyArg_ParseTuple(args, "sc", &path, &mode);
map01bf@620 47 if (!ok) return 0;
map01bf@620 48 if (mode == 'r'){
map01bf@620 49 fresh_database = audiodb_open(path, O_RDONLY);
map01bf@620 50 }else if (mode == 'w'){
map01bf@620 51 fresh_database = audiodb_open(path, O_RDWR);
map01bf@620 52 }else{
map01bf@620 53 PyErr_SetString(PyExc_ValueError,
map01bf@620 54 "mode must be either \'r\' or \'w\'. It appears to be something else.");
map01bf@620 55 return 0;
map01bf@620 56 }
map01bf@620 57 if (!fresh_database) return 0;
map01bf@620 58
map01bf@620 59 return PyCObject_FromVoidPtr( fresh_database, _pyadb_close);
map01bf@620 60 }
map01bf@620 61
map01bf@620 62 /* database status */
map01bf@620 63 /* api call: */
map01bf@620 64 // int audiodb_status(adb_ptr mydb, adb_status_ptr status);
map01bf@620 65 PyObject * _pyadb_status(PyObject *self, PyObject *args)
map01bf@620 66 {
map01bf@620 67 adb_ptr check_db;
map01bf@620 68 adb_status_ptr status;
map01bf@620 69 int flags, ok;
map01bf@620 70 PyObject * incoming = 0;
map01bf@620 71 status = (adb_status_ptr)malloc(sizeof(struct adbstatus));
map01bf@620 72
map01bf@620 73 ok = PyArg_ParseTuple(args, "O", &incoming);
map01bf@620 74 if (!ok) return 0;
map01bf@620 75 check_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@620 76
map01bf@620 77
map01bf@620 78 flags = audiodb_status(check_db, status);
map01bf@620 79 return Py_BuildValue("IIIIILL", status->numFiles,
map01bf@620 80 status->dim,
map01bf@620 81 status->dudCount,
map01bf@620 82 status->nullCount,
map01bf@620 83 status->flags,
map01bf@620 84 status->length,
map01bf@620 85 status->data_region_size);
map01bf@620 86
map01bf@620 87 }
map01bf@620 88
map01bf@620 89 /*engage l2norm in the referenced db*/
map01bf@620 90 /*api call:*/
map01bf@620 91 //int audiodb_l2norm(adb_ptr mydb);
map01bf@620 92 PyObject * _pyadb_l2norm(PyObject *self, PyObject *args)
map01bf@620 93 {
map01bf@620 94 adb_ptr current_db;
map01bf@620 95 int ok;
map01bf@620 96 PyObject * incoming = 0;
map01bf@620 97
map01bf@620 98 ok = PyArg_ParseTuple(args, "O", &incoming);
map01bf@620 99 if (!ok) return 0;
map01bf@620 100 current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@620 101
map01bf@620 102 ok = audiodb_l2norm(current_db);
map01bf@620 103 return Py_BuildValue("i", ok);
map01bf@620 104
map01bf@620 105 }
map01bf@620 106
map01bf@620 107 /*engage power thresholding in the referenced db*/
map01bf@620 108 /*api call:*/
map01bf@620 109 // int audiodb_power(adb_ptr mydb);
map01bf@620 110 PyObject * _pyadb_power(PyObject *self, PyObject *args)
map01bf@620 111 {
map01bf@620 112 adb_ptr current_db;
map01bf@620 113 int ok;
map01bf@620 114 PyObject * incoming = 0;
map01bf@620 115
map01bf@620 116 ok = PyArg_ParseTuple(args, "O", &incoming);
map01bf@620 117 if (!ok) return 0;
map01bf@620 118 current_db = (adb_ptr)PyCObject_AsVoidPtr(incoming);
map01bf@620 119
map01bf@620 120 ok = audiodb_power(current_db);
map01bf@620 121 return Py_BuildValue("i", ok);
map01bf@620 122
map01bf@620 123 }
map01bf@620 124
map01bf@620 125 /* close a database */
map01bf@620 126 /* api call: */
map01bf@620 127 // void audiodb_close(adb_ptr db);
map01bf@620 128 static void _pyadb_close(void *ptr)
map01bf@620 129 {
map01bf@620 130 adb_ptr stale_database;
map01bf@620 131 stale_database = (adb_ptr)ptr;
map01bf@620 132
map01bf@620 133 audiodb_close(stale_database);
map01bf@620 134 }
map01bf@620 135
map01bf@620 136 static PyMethodDef _pyadbMethods[] =
map01bf@620 137 {
map01bf@620 138 { "_pyadb_create", _pyadb_create, METH_VARARGS,
map01bf@620 139 "_pyadb_create(string path, unsigned datasize, unsigned ntracks, unsigned datadim)->adb object"},
map01bf@620 140 { "_pyadb_open", _pyadb_open, METH_VARARGS,
map01bf@620 141 "_pyadb_open(string path, [\'r\'|\'w\'])->adb object\nNote that specifing \'w\' opens the file in read and write mode. \
map01bf@620 142 There is currently no way to open in write only."},
map01bf@620 143 { "_pyadb_status", _pyadb_status, METH_VARARGS,
map01bf@620 144 "_status(adb_ptr)->(numFiles, dims, dudCount, nullCount, flags, length, data_region_size)"},
map01bf@620 145 { "_pyadb_l2norm", _pyadb_l2norm, METH_VARARGS,
map01bf@620 146 "_pyadb_l2norm(adb_ptr)->int return code (0 for sucess)"},
map01bf@620 147 { "_pyadb_power", _pyadb_power, METH_VARARGS,
map01bf@620 148 "_pyadb_power(adb_ptr)->int return code (0 for sucess)"},
map01bf@620 149 {NULL,NULL, 0, NULL}
map01bf@620 150 };
map01bf@620 151
map01bf@620 152 void init_pyadb()
map01bf@620 153 {
map01bf@620 154 Py_InitModule3("_pyadb", _pyadbMethods, "internal c bindings for audioDB. Use pyadb for pythonic access to adb (when it exists).");
map01bf@620 155 // import_array();
map01bf@620 156 return;
map01bf@620 157 }
map01bf@620 158
map01bf@620 159