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