Changeset 615ac7d for interfaces/python
- Timestamp:
- Oct 19, 2009, 3:08:43 PM (15 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- ec1ce52
- Parents:
- 3f99693
- Location:
- interfaces/python
- Files:
-
- 3 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
interfaces/python/aubio-types.h
r3f99693 r615ac7d 10 10 #define Py_aubio_default_samplerate 44100 11 11 12 #ifdef HAVE_AUBIO_DOUBLE 13 #define AUBIO_FLOAT NPY_FLOAT 12 #if HAVE_AUBIO_DOUBLE 13 #error "Ouch! Python interface for aubio has not been much tested yet." 14 #define AUBIO_NPY_SMPL NPY_DOUBLE 14 15 #else 15 #define AUBIO_ FLOAT NPY_LONG16 #define AUBIO_NPY_SMPL NPY_FLOAT 16 17 #endif 17 18 … … 33 34 } Py_fvec; 34 35 extern PyTypeObject Py_fvecType; 36 extern PyObject *PyAubio_FvecToArray (Py_fvec * self); 37 extern Py_fvec *PyAubio_ArrayToFvec (PyObject * self); 35 38 39 typedef struct 40 { 41 PyObject_HEAD 42 cvec_t * o; 43 uint_t length; 44 uint_t channels; 45 } Py_cvec; 36 46 extern PyTypeObject Py_cvecType; 47 extern PyObject *PyAubio_CvecToArray (Py_cvec * self); 48 extern Py_cvec *PyAubio_ArrayToCvec (PyObject * self); 37 49 38 50 extern PyTypeObject Py_filterType; 39 51 40 extern Py Object *PyAubio_FvecToArray (Py_fvec * self);52 extern PyTypeObject Py_filterbankType; 41 53 42 extern Py_fvec *PyAubio_ArrayToFvec (PyObject * self); 54 extern PyTypeObject Py_fftType; 55 56 extern PyTypeObject Py_pvocType; -
interfaces/python/aubiomodule.c
r3f99693 r615ac7d 94 94 // compute the function 95 95 fvec_min_removal (vec->o); 96 96 97 // since this function does not return, we could return None 97 98 //return Py_None; … … 119 120 int err; 120 121 121 if ((PyType_Ready (&Py_fvecType) < 0) || 122 (PyType_Ready (&Py_cvecType) < 0) || 123 (PyType_Ready (&Py_filterType) < 0)) { 122 if ((PyType_Ready (&Py_fvecType) < 0) 123 || (PyType_Ready (&Py_cvecType) < 0) 124 || (PyType_Ready (&Py_filterType) < 0) 125 || (PyType_Ready (&Py_filterbankType) < 0) 126 || (PyType_Ready (&Py_fftType) < 0) 127 || (PyType_Ready (&Py_pvocType) < 0) 128 ) { 124 129 return; 125 130 } … … 144 149 Py_INCREF (&Py_filterType); 145 150 PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); 151 Py_INCREF (&Py_filterbankType); 152 PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); 153 Py_INCREF (&Py_fftType); 154 PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); 155 Py_INCREF (&Py_pvocType); 156 PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); 146 157 } -
interfaces/python/py-cvec.c
r3f99693 r615ac7d 9 9 self.norm = array(length, channels) 10 10 self.phas = array(length, channels) 11 12 11 */ 13 12 14 typedef struct15 {16 PyObject_HEAD17 cvec_t * o;18 uint_t length;19 uint_t channels;20 } Py_cvec;21 13 22 14 static char Py_cvec_doc[] = "cvec object"; … … 97 89 goto fail; 98 90 } 99 cvec_print ( self->o );91 //cvec_print ( self->o ); 100 92 101 93 result = PyString_Format (format, args); … … 133 125 goto fail; 134 126 #if AUBIO_DO_CASTING 135 } else if (PyArray_TYPE (input) != AUBIO_ FLOAT) {127 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 136 128 // input data type is not float32, casting 137 array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_ FLOAT);129 array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL); 138 130 if (array == NULL) { 139 131 PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT"); … … 141 133 } 142 134 #else 143 } else if (PyArray_TYPE (input) != AUBIO_ FLOAT) {135 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 144 136 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 145 137 goto fail; … … 153 145 vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); 154 146 if (PyArray_NDIM (array) == 1) { 147 PyErr_SetString (PyExc_ValueError, 148 "input array should be have at least two rows for norm and phas"); 149 goto fail; 150 } else if (PyArray_NDIM (array) == 2) { 155 151 vec->channels = 1; 156 152 vec->length = PyArray_SIZE (array); 157 153 } else { 158 vec->channels = PyArray_DIM (array, 0) ;154 vec->channels = PyArray_DIM (array, 0) / 2; 159 155 vec->length = PyArray_DIM (array, 1); 160 156 } … … 167 163 vec->o->phas = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*)); 168 164 // hat data[i] point to array line 169 for (i = 0; i < vec->channels; i+ +) {165 for (i = 0; i < vec->channels; i+=2) { 170 166 vec->o->norm[i] = (smpl_t *) PyArray_GETPTR1 (array, i); 167 vec->o->phas[i] = (smpl_t *) PyArray_GETPTR1 (array, i+1); 171 168 } 172 169 … … 186 183 { 187 184 PyObject *array = NULL; 188 if (self->channels == 1) { 189 npy_intp dims[] = { self->length, 1 }; 190 array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[0]); 191 } else { 192 uint_t i; 193 npy_intp dims[] = { self->length, 1 }; 194 PyObject *concat = PyList_New (0), *tmp = NULL; 195 for (i = 0; i < self->channels; i++) { 196 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]); 197 PyList_Append (concat, tmp); 198 Py_DECREF (tmp); 199 } 200 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2); 201 Py_DECREF (concat); 202 } 185 uint_t i; 186 npy_intp dims[] = { self->o->length, 1 }; 187 PyObject *concat = PyList_New (0), *tmp = NULL; 188 for (i = 0; i < self->channels; i++) { 189 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]); 190 PyList_Append (concat, tmp); 191 Py_DECREF (tmp); 192 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]); 193 PyList_Append (concat, tmp); 194 Py_DECREF (tmp); 195 } 196 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2); 197 Py_DECREF (concat); 203 198 return array; 199 } 200 201 PyObject * 202 PyAubio_CvecNormToArray (Py_cvec * self) 203 { 204 PyObject *array = NULL; 205 uint_t i; 206 npy_intp dims[] = { self->o->length, 1 }; 207 PyObject *concat = PyList_New (0), *tmp = NULL; 208 for (i = 0; i < self->channels; i++) { 209 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]); 210 PyList_Append (concat, tmp); 211 Py_DECREF (tmp); 212 } 213 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2); 214 Py_DECREF (concat); 215 return array; 216 } 217 218 PyObject * 219 PyAubio_ArrayToCvecNorm (PyObject * self) 220 { 221 return NULL; 222 } 223 224 PyObject * 225 PyAubio_CvecPhasToArray (Py_cvec * self) 226 { 227 PyObject *array = NULL; 228 uint_t i; 229 npy_intp dims[] = { self->o->length, 1 }; 230 PyObject *concat = PyList_New (0), *tmp = NULL; 231 for (i = 0; i < self->channels; i++) { 232 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]); 233 PyList_Append (concat, tmp); 234 Py_DECREF (tmp); 235 } 236 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2); 237 Py_DECREF (concat); 238 return array; 239 } 240 241 PyObject * 242 PyAubio_ArrayToCvecPhas (PyObject * self) 243 { 244 return NULL; 245 } 246 247 PyObject * 248 Py_cvec_get_norm (Py_cvec * self, void *closure) 249 { 250 return PyAubio_CvecNormToArray(self); 251 } 252 253 PyObject * 254 Py_cvec_get_phas (Py_cvec * self, void *closure) 255 { 256 return PyAubio_CvecPhasToArray(self); 257 } 258 259 static int 260 Py_cvec_set_norm (Py_cvec * self, PyObject *value, void * closure) 261 { 262 return 0; 263 } 264 265 static int 266 Py_cvec_set_phas (Py_cvec * self, PyObject *value, void * closure) 267 { 268 return 0; 204 269 } 205 270 … … 270 335 271 336 static PyMethodDef Py_cvec_methods[] = { 272 {"__array__", (PyCFunction) PyAubio_FvecToArray, METH_NOARGS, 273 "Returns the first channel as a numpy array."}, 337 {"__array__", (PyCFunction) PyAubio_CvecToArray, METH_NOARGS, 338 "Returns the content of this cvec as a numpy array"}, 339 /* 340 {"norm", (PyCFunction) PyAubio_CvecNormToArray, METH_NOARGS, 341 "Returns the content of the magnitude of this cvec as a numpy array."}, 342 {"phas", (PyCFunction) PyAubio_CvecPhasToArray, METH_NOARGS, 343 "Returns the content of the phase of this cvec as a numpy array."}, 344 */ 274 345 {NULL} 346 }; 347 348 static PyGetSetDef Py_cvec_getseters[] = { 349 {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, 350 "Content of the magnitude of this cvec", 351 NULL}, 352 {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, 353 "Content of the magnitude of this cvec", 354 NULL}, 355 {NULL} /* sentinel */ 275 356 }; 276 357 … … 320 401 Py_cvec_methods, /* tp_methods */ 321 402 Py_cvec_members, /* tp_members */ 322 0,/* tp_getset */403 Py_cvec_getseters, /* tp_getset */ 323 404 0, /* tp_base */ 324 405 0, /* tp_dict */ -
interfaces/python/py-filter.c
r3f99693 r615ac7d 5 5 PyObject_HEAD 6 6 aubio_filter_t * o; 7 uint_t samplerate;8 7 uint_t order; 9 8 uint_t channels; … … 15 14 Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds) 16 15 { 17 int samplerate= 0,order= 0, channels = 0;16 int order= 0, channels = 0; 18 17 Py_filter *self; 19 static char *kwlist[] = { " samplerate", "order", "channels", NULL };18 static char *kwlist[] = { "order", "channels", NULL }; 20 19 21 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II I", kwlist,22 & samplerate, &order, &channels)) {20 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, 21 &order, &channels)) { 23 22 return NULL; 24 23 } … … 26 25 self = (Py_filter *) type->tp_alloc (type, 0); 27 26 28 self->samplerate = Py_aubio_default_samplerate;29 self->order = 7;30 self->channels = Py_default_vector_channels;31 32 27 if (self == NULL) { 33 28 return NULL; 34 29 } 35 30 36 if (samplerate > 0) { 37 self->samplerate = samplerate; 38 } else if (samplerate < 0) { 39 PyErr_SetString (PyExc_ValueError, 40 "can not use negative samplerate"); 41 return NULL; 42 } 31 self->order = 7; 32 self->channels = Py_default_vector_channels; 43 33 44 34 if (order > 0) { … … 64 54 Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) 65 55 { 66 self->o = new_aubio_filter (self-> samplerate, self->order, self->channels);56 self->o = new_aubio_filter (self->order, self->channels); 67 57 if (self->o == NULL) { 68 58 return -1; … … 113 103 114 104 static PyObject * 115 Py_filter_set_c_weighting (Py_filter * self, PyObject * unused)105 Py_filter_set_c_weighting (Py_filter * self, PyObject *args) 116 106 { 117 uint_t err = aubio_filter_set_c_weighting (((Py_filter *)self)->o); 107 uint_t err = 0; 108 uint_t samplerate; 109 if (!PyArg_ParseTuple (args, "I", &samplerate)) { 110 return NULL; 111 } 112 113 err = aubio_filter_set_c_weighting (self->o, samplerate); 118 114 if (err > 0) { 119 115 PyErr_SetString (PyExc_ValueError, 120 "error when setting filter to C-weighting");116 "error when setting filter to A-weighting"); 121 117 return NULL; 122 118 } … … 125 121 126 122 static PyObject * 127 Py_filter_set_a_weighting (Py_filter * self, PyObject * unused)123 Py_filter_set_a_weighting (Py_filter * self, PyObject *args) 128 124 { 129 uint_t err = aubio_filter_set_a_weighting (((Py_filter *)self)->o); 125 uint_t err = 0; 126 uint_t samplerate; 127 if (!PyArg_ParseTuple (args, "I", &samplerate)) { 128 return NULL; 129 } 130 131 err = aubio_filter_set_a_weighting (self->o, samplerate); 130 132 if (err > 0) { 131 133 PyErr_SetString (PyExc_ValueError, … … 138 140 static PyMemberDef Py_filter_members[] = { 139 141 // TODO remove READONLY flag and define getter/setter 140 {"samplerate", T_INT, offsetof (Py_filter, samplerate), READONLY,141 "sampling rate"},142 142 {"order", T_INT, offsetof (Py_filter, order), READONLY, 143 143 "order of the filter"}, … … 148 148 149 149 static PyMethodDef Py_filter_methods[] = { 150 {"do", (PyCFunction) Py_filter_do, METH_VARARGS,151 "filter input vector"},152 150 {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_NOARGS, 153 151 "set filter coefficients to C-weighting"}, -
interfaces/python/py-fvec.c
r3f99693 r615ac7d 128 128 goto fail; 129 129 #if AUBIO_DO_CASTING 130 } else if (PyArray_TYPE (input) != AUBIO_ FLOAT) {130 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 131 131 // input data type is not float32, casting 132 array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_ FLOAT);132 array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL); 133 133 if (array == NULL) { 134 PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");134 PyErr_SetString (PyExc_IndexError, "failed converting to AUBIO_NPY_SMPL"); 135 135 goto fail; 136 136 } 137 137 #else 138 } else if (PyArray_TYPE (input) != AUBIO_ FLOAT) {138 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 139 139 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 140 140 goto fail; … … 182 182 if (self->channels == 1) { 183 183 npy_intp dims[] = { self->length, 1 }; 184 array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[0]);184 array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[0]); 185 185 } else { 186 186 uint_t i; … … 188 188 PyObject *concat = PyList_New (0), *tmp = NULL; 189 189 for (i = 0; i < self->channels; i++) { 190 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[i]);190 tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[i]); 191 191 PyList_Append (concat, tmp); 192 192 Py_DECREF (tmp); 193 193 } 194 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);194 array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2); 195 195 Py_DECREF (concat); 196 196 } … … 215 215 216 216 npy_intp dims[] = { self->length, 1 }; 217 array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[index]);217 array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[index]); 218 218 return array; 219 219 } … … 229 229 } 230 230 231 array = PyArray_FROM_OT (o, NPY_FLOAT);231 array = PyArray_FROM_OT (o, AUBIO_NPY_SMPL); 232 232 if (array == NULL) { 233 233 PyErr_SetString (PyExc_ValueError, "should be an array of float"); -
interfaces/python/setup.py
r3f99693 r615ac7d 1 1 from distutils.core import setup, Extension 2 3 from os import listdir 4 generated_files = listdir('generated') 5 generated_files = ['generated/'+f for f in generated_files] 2 6 3 7 setup(name="_aubio", version="1.0", 4 8 ext_modules = [ 5 9 Extension("_aubio", 6 ["aubiomodule.c", "py-fvec.c", "py-cvec.c", "py-filter.c"], 7 include_dirs=['../../build/default/src', '../../src' ], 10 ["aubiomodule.c", 11 "py-fvec.c", 12 "py-cvec.c", 13 "py-filter.c", 14 # macroised 15 "py-filterbank.c", 16 "py-fft.c", 17 "py-phasevoc.c", 18 # generated files 19 ] + generated_files, 20 include_dirs=['../../build/default/src', '../../src', '.' ], 8 21 library_dirs=['../../build/default/src', '../../src/.libs' ], 9 22 libraries=['aubio'])]) -
interfaces/python/test_filter.py
r3f99693 r615ac7d 12 12 def test_members(self): 13 13 f = digital_filter() 14 assert_equal ([f.channels, f.order , f.samplerate], [1, 7, 44100])15 f = digital_filter( 48000,5, 2)16 assert_equal ([f.channels, f.order , f.samplerate], [2, 5, 48000])14 assert_equal ([f.channels, f.order], [1, 7]) 15 f = digital_filter(5, 2) 16 assert_equal ([f.channels, f.order], [2, 5]) 17 17 f(fvec()) 18 18 … … 33 33 def test_c_weighting(self): 34 34 expected = array_from_text_file('c_weighting_test_simple.expected') 35 f = digital_filter( 44100,5, 1)36 f.set_c_weighting( )35 f = digital_filter(5, 1) 36 f.set_c_weighting(44100) 37 37 v = fvec(32) 38 38 v[0][12] = .5 … … 42 42 def test_a_weighting(self): 43 43 expected = array_from_text_file('a_weighting_test_simple.expected') 44 f = digital_filter( 44100,7, 1)45 f.set_a_weighting( )44 f = digital_filter(7, 1) 45 f.set_a_weighting(44100) 46 46 v = fvec(32) 47 47 v[0][12] = .5 … … 51 51 def test_a_weighting_parted(self): 52 52 expected = array_from_text_file('a_weighting_test_simple.expected') 53 f = digital_filter( 44100,7, 1)54 f.set_a_weighting( )53 f = digital_filter(7, 1) 54 f.set_a_weighting(44100) 55 55 v = fvec(16) 56 56 v[0][12] = .5
Note: See TracChangeset
for help on using the changeset viewer.