From: Paul Brossier Date: Mon, 4 Mar 2013 19:46:56 +0000 (-0500) Subject: python/: move source files to ext/ X-Git-Tag: 0.4.0-beta1~259 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=164980f6849b73ca4c354c9b6329bd9e881ed2bd;p=aubio.git python/: move source files to ext/ --- diff --git a/python/aubio-types.h b/python/aubio-types.h deleted file mode 100644 index b2747a60..00000000 --- a/python/aubio-types.h +++ /dev/null @@ -1,63 +0,0 @@ -#include "Python.h" -#include - -//#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION - -// define numpy unique symbols for aubio -#define PY_ARRAY_UNIQUE_SYMBOL PYAUBIO_ARRAY_API -#define PY_UFUNC_UNIQUE_SYMBOL PYAUBIO_UFUNC_API - -// only import array and ufunc from main module -#ifndef PY_AUBIO_MODULE_MAIN -#define NO_IMPORT_ARRAY -#define NO_IMPORT_UFUNC -#endif - -// import aubio -#include -#include -#include - -#define AUBIO_UNSTABLE 1 -#include - -#define Py_default_vector_length 1024 - -#define Py_aubio_default_samplerate 44100 - -#if HAVE_AUBIO_DOUBLE -#error "Ouch! Python interface for aubio has not been much tested yet." -#define AUBIO_NPY_SMPL NPY_DOUBLE -#else -#define AUBIO_NPY_SMPL NPY_FLOAT -#endif - -// special python type for cvec -typedef struct -{ - PyObject_HEAD - cvec_t * o; - uint_t length; - uint_t channels; -} Py_cvec; -extern PyTypeObject Py_cvecType; - -// defined in aubio-proxy.c -extern PyObject *PyAubio_CFvecToArray (fvec_t * self); -extern fvec_t *PyAubio_ArrayToCFvec (PyObject * self); - -extern Py_cvec *PyAubio_CCvecToPyCvec (cvec_t * self); -extern cvec_t *PyAubio_ArrayToCCvec (PyObject *input); - -extern PyObject *PyAubio_CFmatToArray (fmat_t * self); -extern fmat_t *PyAubio_ArrayToCFmat (PyObject *input); - -// hand written wrappers -extern PyTypeObject Py_filterType; - -extern PyTypeObject Py_filterbankType; - -extern PyTypeObject Py_fftType; - -extern PyTypeObject Py_pvocType; - diff --git a/python/aubiomodule.c b/python/aubiomodule.c deleted file mode 100644 index 293758ab..00000000 --- a/python/aubiomodule.c +++ /dev/null @@ -1,283 +0,0 @@ -#define PY_AUBIO_MODULE_MAIN -#include "aubio-types.h" -#include "generated/aubio-generated.h" - -static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; - -static PyObject * -Py_alpha_norm (PyObject * self, PyObject * args) -{ - PyObject *input; - fvec_t *vec; - smpl_t alpha; - PyObject *result; - - if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { - return NULL; - } - - if (input == NULL) { - return NULL; - } - - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { - return NULL; - } - - // compute the function - result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); - if (result == NULL) { - return NULL; - } - - return result; -} - -static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]"; - -static PyObject * -Py_unwrap2pi (PyObject * self, PyObject * args) -{ - smpl_t input; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|f", &input)) { - return NULL; - } - - output = aubio_unwrap2pi (input); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_bintomidi_doc[] = "convert bin to midi"; - -static PyObject * -Py_bintomidi (PyObject * self, PyObject * args) -{ - smpl_t input, samplerate, fftsize; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { - return NULL; - } - - output = aubio_bintomidi (input, samplerate, fftsize); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_miditobin_doc[] = "convert midi to bin"; - -static PyObject * -Py_miditobin (PyObject * self, PyObject * args) -{ - smpl_t input, samplerate, fftsize; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { - return NULL; - } - - output = aubio_miditobin (input, samplerate, fftsize); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_bintofreq_doc[] = "convert bin to freq"; - -static PyObject * -Py_bintofreq (PyObject * self, PyObject * args) -{ - smpl_t input, samplerate, fftsize; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { - return NULL; - } - - output = aubio_bintofreq (input, samplerate, fftsize); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_freqtobin_doc[] = "convert freq to bin"; - -static PyObject * -Py_freqtobin (PyObject * self, PyObject * args) -{ - smpl_t input, samplerate, fftsize; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { - return NULL; - } - - output = aubio_freqtobin (input, samplerate, fftsize); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_freqtomidi_doc[] = "convert freq to midi"; - -static PyObject * -Py_freqtomidi (PyObject * self, PyObject * args) -{ - smpl_t input; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|f", &input)) { - return NULL; - } - - output = aubio_freqtomidi (input); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_miditofreq_doc[] = "convert midi to freq"; - -static PyObject * -Py_miditofreq (PyObject * self, PyObject * args) -{ - smpl_t input; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|f", &input)) { - return NULL; - } - - output = aubio_miditofreq (input); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; - -static PyObject * -Py_zero_crossing_rate (PyObject * self, PyObject * args) -{ - PyObject *input; - fvec_t *vec; - PyObject *result; - - if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { - return NULL; - } - - if (input == NULL) { - return NULL; - } - - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { - return NULL; - } - - // compute the function - result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); - if (result == NULL) { - return NULL; - } - - return result; -} - -static char Py_min_removal_doc[] = "compute zero crossing rate"; - -static PyObject * -Py_min_removal(PyObject * self, PyObject * args) -{ - PyObject *input; - fvec_t *vec; - - if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { - return NULL; - } - - if (input == NULL) { - return NULL; - } - - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { - return NULL; - } - - // compute the function - fvec_min_removal (vec); - - // since this function does not return, we could return None - //return Py_None; - // however it is convenient to return the modified vector - return (PyObject *) PyAubio_CFvecToArray(vec); - // or even without converting it back to an array - //Py_INCREF(vec); - //return (PyObject *)vec; -} - -static PyMethodDef aubio_methods[] = { - {"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc}, - {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, - {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, - {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, - {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, - {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc}, - {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc}, - {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, - {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, - {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, - {NULL, NULL} /* Sentinel */ -}; - -static char aubio_module_doc[] = "Python module for the aubio library"; - -PyMODINIT_FUNC -init_aubio (void) -{ - PyObject *m; - int err; - - // fvec is defined in __init__.py - if ( (PyType_Ready (&Py_cvecType) < 0) - || (PyType_Ready (&Py_filterType) < 0) - || (PyType_Ready (&Py_filterbankType) < 0) - || (PyType_Ready (&Py_fftType) < 0) - || (PyType_Ready (&Py_pvocType) < 0) - // generated objects - || (generated_types_ready() < 0 ) - ) { - return; - } - - m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); - - if (m == NULL) { - return; - } - - err = _import_array (); - - if (err != 0) { - fprintf (stderr, - "Unable to import Numpy C API from aubio module (error %d)\n", err); - } - - Py_INCREF (&Py_cvecType); - PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); - Py_INCREF (&Py_filterType); - PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); - Py_INCREF (&Py_filterbankType); - PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); - Py_INCREF (&Py_fftType); - PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); - Py_INCREF (&Py_pvocType); - PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); - - // add generated objects - add_generated_objects(m); -} diff --git a/python/aubioproxy.c b/python/aubioproxy.c deleted file mode 100644 index 9b7f9179..00000000 --- a/python/aubioproxy.c +++ /dev/null @@ -1,153 +0,0 @@ -#include "aubio-types.h" - -fvec_t * -PyAubio_ArrayToCFvec (PyObject *input) { - PyObject *array; - fvec_t *vec; - if (input == NULL) { - PyErr_SetString (PyExc_ValueError, "input array is not a python object"); - goto fail; - } - // parsing input object into a Py_fvec - if (PyArray_Check(input)) { - - // we got an array, convert it to an fvec - if (PyArray_NDIM ((PyArrayObject *)input) == 0) { - PyErr_SetString (PyExc_ValueError, "input array is a scalar"); - goto fail; - } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) { - PyErr_SetString (PyExc_ValueError, - "input array has more than one dimensions"); - goto fail; - } - - if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { - PyErr_SetString (PyExc_ValueError, "input array should be float"); - goto fail; - } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - goto fail; - } else { - // input data type is float32, nothing else to do - array = input; - } - - // vec = new_fvec (vec->length); - // no need to really allocate fvec, just its struct member - vec = (fvec_t *)malloc(sizeof(fvec_t)); - vec->length = PyArray_SIZE ((PyArrayObject *)array); - vec->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)array, 0); - - } else if (PyObject_TypeCheck (input, &PyList_Type)) { - PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); - return NULL; - } else { - PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input"); - return NULL; - } - - return vec; - -fail: - return NULL; -} - -PyObject * -PyAubio_CFvecToArray (fvec_t * self) -{ - npy_intp dims[] = { self->length, 1 }; - return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data); -} - -Py_cvec * -PyAubio_CCvecToPyCvec (cvec_t * input) { - Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); - vec->length = input->length; - vec->o = input; - Py_INCREF(vec); - return vec; -} - -cvec_t * -PyAubio_ArrayToCCvec (PyObject *input) { - if (PyObject_TypeCheck (input, &Py_cvecType)) { - return ((Py_cvec*)input)->o; - } else { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - return NULL; - } -} - -PyObject * -PyAubio_CFmatToArray (fmat_t * input) -{ - PyObject *array = NULL; - uint_t i; - npy_intp dims[] = { input->length, 1 }; - PyObject *concat = PyList_New (0), *tmp = NULL; - for (i = 0; i < input->height; i++) { - tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->data[i]); - PyList_Append (concat, tmp); - Py_DECREF (tmp); - } - array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2); - Py_DECREF (concat); - return array; -} - -fmat_t * -PyAubio_ArrayToCFmat (PyObject *input) { - PyObject *array; - fmat_t *mat; - uint_t i; - if (input == NULL) { - PyErr_SetString (PyExc_ValueError, "input array is not a python object"); - goto fail; - } - // parsing input object into a Py_fvec - if (PyArray_Check(input)) { - - // we got an array, convert it to an fvec - if (PyArray_NDIM ((PyArrayObject *)input) == 0) { - PyErr_SetString (PyExc_ValueError, "input array is a scalar"); - goto fail; - } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { - PyErr_SetString (PyExc_ValueError, - "input array has more than two dimensions"); - goto fail; - } - - if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { - PyErr_SetString (PyExc_ValueError, "input array should be float"); - goto fail; - } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - goto fail; - } else { - // input data type is float32, nothing else to do - array = input; - } - - // no need to really allocate fvec, just its struct member - mat = (fmat_t *)malloc(sizeof(fmat_t)); - mat->length = PyArray_DIM ((PyArrayObject *)array, 1); - mat->height = PyArray_DIM ((PyArrayObject *)array, 0); - mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height); - for (i=0; i< mat->height; i++) { - mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i); - } - - } else if (PyObject_TypeCheck (input, &PyList_Type)) { - PyErr_SetString (PyExc_ValueError, "can not convert list to fmat"); - return NULL; - } else { - PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input"); - return NULL; - } - - return mat; - -fail: - return NULL; -} - diff --git a/python/aubiowraphell.h b/python/aubiowraphell.h deleted file mode 100644 index d60cc30a..00000000 --- a/python/aubiowraphell.h +++ /dev/null @@ -1,90 +0,0 @@ -#include "aubio-types.h" - -#define AUBIO_DECLARE(NAME, PARAMS...) \ -typedef struct { \ - PyObject_HEAD \ - aubio_ ## NAME ## _t * o; \ - PARAMS; \ -} Py_## NAME; - -#define AUBIO_INIT(NAME, PARAMS... ) \ -static int \ -Py_ ## NAME ## _init (Py_ ## NAME * self, PyObject * args, PyObject * kwds) \ -{ \ - self->o = new_aubio_## NAME ( PARAMS ); \ - if (self->o == NULL) { \ - PyErr_SetString (PyExc_StandardError, "error creating object"); \ - return -1; \ - } \ -\ - return 0; \ -} - -#define AUBIO_DEL(NAME) \ -static void \ -Py_ ## NAME ## _del ( Py_ ## NAME * self) \ -{ \ - del_aubio_ ## NAME (self->o); \ - self->ob_type->tp_free ((PyObject *) self); \ -} - -#define AUBIO_MEMBERS_START(NAME) \ -static PyMemberDef Py_ ## NAME ## _members[] = { - -#define AUBIO_MEMBERS_STOP(NAME) \ - {NULL} \ -}; - -#define AUBIO_METHODS(NAME) \ -static PyMethodDef Py_ ## NAME ## _methods[] = { \ - {NULL} \ -}; - - -#define AUBIO_TYPEOBJECT(NAME, PYNAME) \ -PyTypeObject Py_ ## NAME ## Type = { \ - PyObject_HEAD_INIT (NULL) \ - 0, \ - PYNAME, \ - sizeof (Py_ ## NAME), \ - 0, \ - (destructor) Py_ ## NAME ## _del, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - (ternaryfunc)Py_ ## NAME ## _do, \ - 0, \ - 0, \ - 0, \ - 0, \ - Py_TPFLAGS_DEFAULT, \ - Py_ ## NAME ## _doc, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - Py_ ## NAME ## _methods, \ - Py_ ## NAME ## _members, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - (initproc) Py_ ## NAME ## _init, \ - 0, \ - Py_ ## NAME ## _new, \ -}; - -// some more helpers -#define AUBIO_NEW_VEC(name, type, lengthval) \ - name = (type *) PyObject_New (type, & type ## Type); \ - name->length = lengthval; diff --git a/python/ext/aubio-types.h b/python/ext/aubio-types.h new file mode 100644 index 00000000..b2747a60 --- /dev/null +++ b/python/ext/aubio-types.h @@ -0,0 +1,63 @@ +#include "Python.h" +#include + +//#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION + +// define numpy unique symbols for aubio +#define PY_ARRAY_UNIQUE_SYMBOL PYAUBIO_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL PYAUBIO_UFUNC_API + +// only import array and ufunc from main module +#ifndef PY_AUBIO_MODULE_MAIN +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#endif + +// import aubio +#include +#include +#include + +#define AUBIO_UNSTABLE 1 +#include + +#define Py_default_vector_length 1024 + +#define Py_aubio_default_samplerate 44100 + +#if HAVE_AUBIO_DOUBLE +#error "Ouch! Python interface for aubio has not been much tested yet." +#define AUBIO_NPY_SMPL NPY_DOUBLE +#else +#define AUBIO_NPY_SMPL NPY_FLOAT +#endif + +// special python type for cvec +typedef struct +{ + PyObject_HEAD + cvec_t * o; + uint_t length; + uint_t channels; +} Py_cvec; +extern PyTypeObject Py_cvecType; + +// defined in aubio-proxy.c +extern PyObject *PyAubio_CFvecToArray (fvec_t * self); +extern fvec_t *PyAubio_ArrayToCFvec (PyObject * self); + +extern Py_cvec *PyAubio_CCvecToPyCvec (cvec_t * self); +extern cvec_t *PyAubio_ArrayToCCvec (PyObject *input); + +extern PyObject *PyAubio_CFmatToArray (fmat_t * self); +extern fmat_t *PyAubio_ArrayToCFmat (PyObject *input); + +// hand written wrappers +extern PyTypeObject Py_filterType; + +extern PyTypeObject Py_filterbankType; + +extern PyTypeObject Py_fftType; + +extern PyTypeObject Py_pvocType; + diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c new file mode 100644 index 00000000..293758ab --- /dev/null +++ b/python/ext/aubiomodule.c @@ -0,0 +1,283 @@ +#define PY_AUBIO_MODULE_MAIN +#include "aubio-types.h" +#include "generated/aubio-generated.h" + +static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; + +static PyObject * +Py_alpha_norm (PyObject * self, PyObject * args) +{ + PyObject *input; + fvec_t *vec; + smpl_t alpha; + PyObject *result; + + if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { + return NULL; + } + + if (input == NULL) { + return NULL; + } + + vec = PyAubio_ArrayToCFvec (input); + + if (vec == NULL) { + return NULL; + } + + // compute the function + result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); + if (result == NULL) { + return NULL; + } + + return result; +} + +static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]"; + +static PyObject * +Py_unwrap2pi (PyObject * self, PyObject * args) +{ + smpl_t input; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|f", &input)) { + return NULL; + } + + output = aubio_unwrap2pi (input); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_bintomidi_doc[] = "convert bin to midi"; + +static PyObject * +Py_bintomidi (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_bintomidi (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_miditobin_doc[] = "convert midi to bin"; + +static PyObject * +Py_miditobin (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_miditobin (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_bintofreq_doc[] = "convert bin to freq"; + +static PyObject * +Py_bintofreq (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_bintofreq (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_freqtobin_doc[] = "convert freq to bin"; + +static PyObject * +Py_freqtobin (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_freqtobin (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_freqtomidi_doc[] = "convert freq to midi"; + +static PyObject * +Py_freqtomidi (PyObject * self, PyObject * args) +{ + smpl_t input; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|f", &input)) { + return NULL; + } + + output = aubio_freqtomidi (input); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_miditofreq_doc[] = "convert midi to freq"; + +static PyObject * +Py_miditofreq (PyObject * self, PyObject * args) +{ + smpl_t input; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|f", &input)) { + return NULL; + } + + output = aubio_miditofreq (input); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; + +static PyObject * +Py_zero_crossing_rate (PyObject * self, PyObject * args) +{ + PyObject *input; + fvec_t *vec; + PyObject *result; + + if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { + return NULL; + } + + if (input == NULL) { + return NULL; + } + + vec = PyAubio_ArrayToCFvec (input); + + if (vec == NULL) { + return NULL; + } + + // compute the function + result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); + if (result == NULL) { + return NULL; + } + + return result; +} + +static char Py_min_removal_doc[] = "compute zero crossing rate"; + +static PyObject * +Py_min_removal(PyObject * self, PyObject * args) +{ + PyObject *input; + fvec_t *vec; + + if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { + return NULL; + } + + if (input == NULL) { + return NULL; + } + + vec = PyAubio_ArrayToCFvec (input); + + if (vec == NULL) { + return NULL; + } + + // compute the function + fvec_min_removal (vec); + + // since this function does not return, we could return None + //return Py_None; + // however it is convenient to return the modified vector + return (PyObject *) PyAubio_CFvecToArray(vec); + // or even without converting it back to an array + //Py_INCREF(vec); + //return (PyObject *)vec; +} + +static PyMethodDef aubio_methods[] = { + {"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc}, + {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, + {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, + {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, + {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, + {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc}, + {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc}, + {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, + {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, + {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, + {NULL, NULL} /* Sentinel */ +}; + +static char aubio_module_doc[] = "Python module for the aubio library"; + +PyMODINIT_FUNC +init_aubio (void) +{ + PyObject *m; + int err; + + // fvec is defined in __init__.py + if ( (PyType_Ready (&Py_cvecType) < 0) + || (PyType_Ready (&Py_filterType) < 0) + || (PyType_Ready (&Py_filterbankType) < 0) + || (PyType_Ready (&Py_fftType) < 0) + || (PyType_Ready (&Py_pvocType) < 0) + // generated objects + || (generated_types_ready() < 0 ) + ) { + return; + } + + m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); + + if (m == NULL) { + return; + } + + err = _import_array (); + + if (err != 0) { + fprintf (stderr, + "Unable to import Numpy C API from aubio module (error %d)\n", err); + } + + Py_INCREF (&Py_cvecType); + PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); + Py_INCREF (&Py_filterType); + PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); + Py_INCREF (&Py_filterbankType); + PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); + Py_INCREF (&Py_fftType); + PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); + Py_INCREF (&Py_pvocType); + PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); + + // add generated objects + add_generated_objects(m); +} diff --git a/python/ext/aubioproxy.c b/python/ext/aubioproxy.c new file mode 100644 index 00000000..9b7f9179 --- /dev/null +++ b/python/ext/aubioproxy.c @@ -0,0 +1,153 @@ +#include "aubio-types.h" + +fvec_t * +PyAubio_ArrayToCFvec (PyObject *input) { + PyObject *array; + fvec_t *vec; + if (input == NULL) { + PyErr_SetString (PyExc_ValueError, "input array is not a python object"); + goto fail; + } + // parsing input object into a Py_fvec + if (PyArray_Check(input)) { + + // we got an array, convert it to an fvec + if (PyArray_NDIM ((PyArrayObject *)input) == 0) { + PyErr_SetString (PyExc_ValueError, "input array is a scalar"); + goto fail; + } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) { + PyErr_SetString (PyExc_ValueError, + "input array has more than one dimensions"); + goto fail; + } + + if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { + PyErr_SetString (PyExc_ValueError, "input array should be float"); + goto fail; + } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { + PyErr_SetString (PyExc_ValueError, "input array should be float32"); + goto fail; + } else { + // input data type is float32, nothing else to do + array = input; + } + + // vec = new_fvec (vec->length); + // no need to really allocate fvec, just its struct member + vec = (fvec_t *)malloc(sizeof(fvec_t)); + vec->length = PyArray_SIZE ((PyArrayObject *)array); + vec->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)array, 0); + + } else if (PyObject_TypeCheck (input, &PyList_Type)) { + PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); + return NULL; + } else { + PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input"); + return NULL; + } + + return vec; + +fail: + return NULL; +} + +PyObject * +PyAubio_CFvecToArray (fvec_t * self) +{ + npy_intp dims[] = { self->length, 1 }; + return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data); +} + +Py_cvec * +PyAubio_CCvecToPyCvec (cvec_t * input) { + Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); + vec->length = input->length; + vec->o = input; + Py_INCREF(vec); + return vec; +} + +cvec_t * +PyAubio_ArrayToCCvec (PyObject *input) { + if (PyObject_TypeCheck (input, &Py_cvecType)) { + return ((Py_cvec*)input)->o; + } else { + PyErr_SetString (PyExc_ValueError, "input array should be float32"); + return NULL; + } +} + +PyObject * +PyAubio_CFmatToArray (fmat_t * input) +{ + PyObject *array = NULL; + uint_t i; + npy_intp dims[] = { input->length, 1 }; + PyObject *concat = PyList_New (0), *tmp = NULL; + for (i = 0; i < input->height; i++) { + tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->data[i]); + PyList_Append (concat, tmp); + Py_DECREF (tmp); + } + array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2); + Py_DECREF (concat); + return array; +} + +fmat_t * +PyAubio_ArrayToCFmat (PyObject *input) { + PyObject *array; + fmat_t *mat; + uint_t i; + if (input == NULL) { + PyErr_SetString (PyExc_ValueError, "input array is not a python object"); + goto fail; + } + // parsing input object into a Py_fvec + if (PyArray_Check(input)) { + + // we got an array, convert it to an fvec + if (PyArray_NDIM ((PyArrayObject *)input) == 0) { + PyErr_SetString (PyExc_ValueError, "input array is a scalar"); + goto fail; + } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { + PyErr_SetString (PyExc_ValueError, + "input array has more than two dimensions"); + goto fail; + } + + if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { + PyErr_SetString (PyExc_ValueError, "input array should be float"); + goto fail; + } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { + PyErr_SetString (PyExc_ValueError, "input array should be float32"); + goto fail; + } else { + // input data type is float32, nothing else to do + array = input; + } + + // no need to really allocate fvec, just its struct member + mat = (fmat_t *)malloc(sizeof(fmat_t)); + mat->length = PyArray_DIM ((PyArrayObject *)array, 1); + mat->height = PyArray_DIM ((PyArrayObject *)array, 0); + mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height); + for (i=0; i< mat->height; i++) { + mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i); + } + + } else if (PyObject_TypeCheck (input, &PyList_Type)) { + PyErr_SetString (PyExc_ValueError, "can not convert list to fmat"); + return NULL; + } else { + PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input"); + return NULL; + } + + return mat; + +fail: + return NULL; +} + diff --git a/python/ext/aubiowraphell.h b/python/ext/aubiowraphell.h new file mode 100644 index 00000000..d60cc30a --- /dev/null +++ b/python/ext/aubiowraphell.h @@ -0,0 +1,90 @@ +#include "aubio-types.h" + +#define AUBIO_DECLARE(NAME, PARAMS...) \ +typedef struct { \ + PyObject_HEAD \ + aubio_ ## NAME ## _t * o; \ + PARAMS; \ +} Py_## NAME; + +#define AUBIO_INIT(NAME, PARAMS... ) \ +static int \ +Py_ ## NAME ## _init (Py_ ## NAME * self, PyObject * args, PyObject * kwds) \ +{ \ + self->o = new_aubio_## NAME ( PARAMS ); \ + if (self->o == NULL) { \ + PyErr_SetString (PyExc_StandardError, "error creating object"); \ + return -1; \ + } \ +\ + return 0; \ +} + +#define AUBIO_DEL(NAME) \ +static void \ +Py_ ## NAME ## _del ( Py_ ## NAME * self) \ +{ \ + del_aubio_ ## NAME (self->o); \ + self->ob_type->tp_free ((PyObject *) self); \ +} + +#define AUBIO_MEMBERS_START(NAME) \ +static PyMemberDef Py_ ## NAME ## _members[] = { + +#define AUBIO_MEMBERS_STOP(NAME) \ + {NULL} \ +}; + +#define AUBIO_METHODS(NAME) \ +static PyMethodDef Py_ ## NAME ## _methods[] = { \ + {NULL} \ +}; + + +#define AUBIO_TYPEOBJECT(NAME, PYNAME) \ +PyTypeObject Py_ ## NAME ## Type = { \ + PyObject_HEAD_INIT (NULL) \ + 0, \ + PYNAME, \ + sizeof (Py_ ## NAME), \ + 0, \ + (destructor) Py_ ## NAME ## _del, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + (ternaryfunc)Py_ ## NAME ## _do, \ + 0, \ + 0, \ + 0, \ + 0, \ + Py_TPFLAGS_DEFAULT, \ + Py_ ## NAME ## _doc, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + Py_ ## NAME ## _methods, \ + Py_ ## NAME ## _members, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + (initproc) Py_ ## NAME ## _init, \ + 0, \ + Py_ ## NAME ## _new, \ +}; + +// some more helpers +#define AUBIO_NEW_VEC(name, type, lengthval) \ + name = (type *) PyObject_New (type, & type ## Type); \ + name->length = lengthval; diff --git a/python/ext/py-cvec.c b/python/ext/py-cvec.c new file mode 100644 index 00000000..940508fb --- /dev/null +++ b/python/ext/py-cvec.c @@ -0,0 +1,302 @@ +#include "aubio-types.h" + +/* cvec type definition + +class cvec(): + def __init__(self, length = 1024): + self.length = length + self.norm = array(length) + self.phas = array(length) + +*/ + +static char Py_cvec_doc[] = "cvec object"; + +static PyObject * +Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) +{ + int length= 0; + Py_cvec *self; + static char *kwlist[] = { "length", NULL }; + + if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, + &length)) { + return NULL; + } + + + self = (Py_cvec *) type->tp_alloc (type, 0); + + self->length = Py_default_vector_length / 2 + 1; + + if (self == NULL) { + return NULL; + } + + if (length > 0) { + self->length = length / 2 + 1; + } else if (length < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative number of elements"); + return NULL; + } + + return (PyObject *) self; +} + +static int +Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) +{ + self->o = new_cvec ((self->length - 1) * 2); + if (self->o == NULL) { + return -1; + } + + return 0; +} + +static void +Py_cvec_del (Py_cvec * self) +{ + del_cvec (self->o); + self->ob_type->tp_free ((PyObject *) self); +} + +static PyObject * +Py_cvec_repr (Py_cvec * self, PyObject * unused) +{ + PyObject *format = NULL; + PyObject *args = NULL; + PyObject *result = NULL; + + format = PyString_FromString ("aubio cvec of %d elements"); + if (format == NULL) { + goto fail; + } + + args = Py_BuildValue ("I", self->length); + if (args == NULL) { + goto fail; + } + cvec_print ( self->o ); + + result = PyString_Format (format, args); + +fail: + Py_XDECREF (format); + Py_XDECREF (args); + + return result; +} + +PyObject * +PyAubio_CvecNormToArray (Py_cvec * self) +{ + npy_intp dims[] = { self->o->length, 1 }; + return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm); +} + + +PyObject * +PyAubio_CvecPhasToArray (Py_cvec * self) +{ + npy_intp dims[] = { self->o->length, 1 }; + return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas); +} + +PyObject * +PyAubio_ArrayToCvecPhas (PyObject * self) +{ + return NULL; +} + +PyObject * +Py_cvec_get_norm (Py_cvec * self, void *closure) +{ + return PyAubio_CvecNormToArray(self); +} + +PyObject * +Py_cvec_get_phas (Py_cvec * self, void *closure) +{ + return PyAubio_CvecPhasToArray(self); +} + +static int +Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) +{ + PyArrayObject * array; + if (input == NULL) { + PyErr_SetString (PyExc_ValueError, "input array is not a python object"); + goto fail; + } + if (PyArray_Check(input)) { + + // we got an array, convert it to a cvec.norm + if (PyArray_NDIM ((PyArrayObject *)input) == 0) { + PyErr_SetString (PyExc_ValueError, "input array is a scalar"); + goto fail; + } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { + PyErr_SetString (PyExc_ValueError, + "input array has more than two dimensions"); + goto fail; + } + + if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { + PyErr_SetString (PyExc_ValueError, "input array should be float"); + goto fail; + } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { + PyErr_SetString (PyExc_ValueError, "input array should be float32"); + goto fail; + } + array = (PyArrayObject *)input; + + // check input array dimensions + if (PyArray_NDIM (array) != 1) { + PyErr_Format (PyExc_ValueError, + "input array has %d dimensions, not 1", + PyArray_NDIM (array)); + goto fail; + } else { + if (vec->o->length != PyArray_SIZE (array)) { + PyErr_Format (PyExc_ValueError, + "input array has length %d, but cvec has length %d", + (int)PyArray_SIZE (array), vec->o->length); + goto fail; + } + } + + vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); + + } else { + PyErr_SetString (PyExc_ValueError, "can only accept array as input"); + return 1; + } + + Py_INCREF(array); + return 0; + +fail: + return 1; +} + +static int +Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) +{ + PyArrayObject * array; + if (input == NULL) { + PyErr_SetString (PyExc_ValueError, "input array is not a python object"); + goto fail; + } + if (PyArray_Check(input)) { + + // we got an array, convert it to a cvec.phas + if (PyArray_NDIM ((PyArrayObject *)input) == 0) { + PyErr_SetString (PyExc_ValueError, "input array is a scalar"); + goto fail; + } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { + PyErr_SetString (PyExc_ValueError, + "input array has more than two dimensions"); + goto fail; + } + + if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { + PyErr_SetString (PyExc_ValueError, "input array should be float"); + goto fail; + } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { + PyErr_SetString (PyExc_ValueError, "input array should be float32"); + goto fail; + } + array = (PyArrayObject *)input; + + // check input array dimensions + if (PyArray_NDIM (array) != 1) { + PyErr_Format (PyExc_ValueError, + "input array has %d dimensions, not 1", + PyArray_NDIM (array)); + goto fail; + } else { + if (vec->o->length != PyArray_SIZE (array)) { + PyErr_Format (PyExc_ValueError, + "input array has length %d, but cvec has length %d", + (int)PyArray_SIZE (array), vec->o->length); + goto fail; + } + } + + vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); + + } else { + PyErr_SetString (PyExc_ValueError, "can only accept array as input"); + return 1; + } + + Py_INCREF(array); + return 0; + +fail: + return 1; +} + +static PyMemberDef Py_cvec_members[] = { + // TODO remove READONLY flag and define getter/setter + {"length", T_INT, offsetof (Py_cvec, length), READONLY, + "length attribute"}, + {NULL} /* Sentinel */ +}; + +static PyMethodDef Py_cvec_methods[] = { + {NULL} +}; + +static PyGetSetDef Py_cvec_getseters[] = { + {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, + "Numpy vector of shape (length,) containing the magnitude", + NULL}, + {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, + "Numpy vector of shape (length,) containing the phase", + NULL}, + {NULL} /* sentinel */ +}; + +PyTypeObject Py_cvecType = { + PyObject_HEAD_INIT (NULL) + 0, /* ob_size */ + "aubio.cvec", /* tp_name */ + sizeof (Py_cvec), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor) Py_cvec_del, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + (reprfunc) Py_cvec_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + Py_cvec_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Py_cvec_methods, /* tp_methods */ + Py_cvec_members, /* tp_members */ + Py_cvec_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc) Py_cvec_init, /* tp_init */ + 0, /* tp_alloc */ + Py_cvec_new, /* tp_new */ +}; diff --git a/python/ext/py-fft.c b/python/ext/py-fft.c new file mode 100644 index 00000000..19d7b49b --- /dev/null +++ b/python/ext/py-fft.c @@ -0,0 +1,107 @@ +#include "aubiowraphell.h" + +static char Py_fft_doc[] = "fft object"; + +AUBIO_DECLARE(fft, uint_t win_s) + +//AUBIO_NEW(fft) +static PyObject * +Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds) +{ + int win_s = 0; + Py_fft *self; + static char *kwlist[] = { "win_s", NULL }; + + if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, + &win_s)) { + return NULL; + } + + self = (Py_fft *) type->tp_alloc (type, 0); + + if (self == NULL) { + return NULL; + } + + self->win_s = Py_default_vector_length; + + if (self == NULL) { + return NULL; + } + + if (win_s > 0) { + self->win_s = win_s; + } else if (win_s < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative window size"); + return NULL; + } + + return (PyObject *) self; +} + + +AUBIO_INIT(fft, self->win_s) + +AUBIO_DEL(fft) + +static PyObject * +Py_fft_do(PyObject * self, PyObject * args) +{ + PyObject *input; + fvec_t *vec; + cvec_t *output; + + if (!PyArg_ParseTuple (args, "O", &input)) { + return NULL; + } + + vec = PyAubio_ArrayToCFvec (input); + + if (vec == NULL) { + return NULL; + } + + output = new_cvec(((Py_fft *) self)->win_s); + + // compute the function + aubio_fft_do (((Py_fft *)self)->o, vec, output); + return (PyObject *)PyAubio_CCvecToPyCvec(output); +} + +AUBIO_MEMBERS_START(fft) + {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY, + "size of the window"}, +AUBIO_MEMBERS_STOP(fft) + +static PyObject * +Py_fft_rdo(Py_fft * self, PyObject * args) +{ + PyObject *input; + cvec_t *vec; + fvec_t *output; + + if (!PyArg_ParseTuple (args, "O", &input)) { + return NULL; + } + + vec = PyAubio_ArrayToCCvec (input); + + if (vec == NULL) { + return NULL; + } + + output = new_fvec(self->win_s); + + // compute the function + aubio_fft_rdo (((Py_fft *)self)->o, vec, output); + return (PyObject *)PyAubio_CFvecToArray(output); +} + +static PyMethodDef Py_fft_methods[] = { + {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS, + "synthesis of spectral grain"}, + {NULL} +}; + +AUBIO_TYPEOBJECT(fft, "aubio.fft") diff --git a/python/ext/py-filter.c b/python/ext/py-filter.c new file mode 100644 index 00000000..5b9fee64 --- /dev/null +++ b/python/ext/py-filter.c @@ -0,0 +1,198 @@ +#include "aubio-types.h" + +typedef struct +{ + PyObject_HEAD + aubio_filter_t * o; + uint_t order; +} Py_filter; + +static char Py_filter_doc[] = "filter object"; + +static PyObject * +Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds) +{ + int order= 0; + Py_filter *self; + static char *kwlist[] = { "order", NULL }; + + if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, + &order)) { + return NULL; + } + + self = (Py_filter *) type->tp_alloc (type, 0); + + if (self == NULL) { + return NULL; + } + + self->order = 7; + + if (order > 0) { + self->order = order; + } else if (order < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative order"); + return NULL; + } + + return (PyObject *) self; +} + +static int +Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) +{ + self->o = new_aubio_filter (self->order); + if (self->o == NULL) { + return -1; + } + + return 0; +} + +static void +Py_filter_del (Py_filter * self) +{ + del_aubio_filter (self->o); + self->ob_type->tp_free ((PyObject *) self); +} + +static PyObject * +Py_filter_do(Py_filter * self, PyObject * args) +{ + PyObject *input; + fvec_t *vec; + + if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) { + return NULL; + } + + if (input == NULL) { + return NULL; + } + + vec = PyAubio_ArrayToCFvec (input); + + if (vec == NULL) { + return NULL; + } + + // compute the function + fvec_t * out = new_fvec(vec->length); + aubio_filter_do_outplace (self->o, vec, out); + return PyAubio_CFvecToArray(out); +} + +static PyObject * +Py_filter_set_c_weighting (Py_filter * self, PyObject *args) +{ + uint_t err = 0; + uint_t samplerate; + if (!PyArg_ParseTuple (args, "I", &samplerate)) { + return NULL; + } + + err = aubio_filter_set_c_weighting (self->o, samplerate); + if (err > 0) { + PyErr_SetString (PyExc_ValueError, + "error when setting filter to C-weighting"); + return NULL; + } + return Py_None; +} + +static PyObject * +Py_filter_set_a_weighting (Py_filter * self, PyObject *args) +{ + uint_t err = 0; + uint_t samplerate; + if (!PyArg_ParseTuple (args, "I", &samplerate)) { + return NULL; + } + + err = aubio_filter_set_a_weighting (self->o, samplerate); + if (err > 0) { + PyErr_SetString (PyExc_ValueError, + "error when setting filter to A-weighting"); + return NULL; + } + return Py_None; +} + +static PyObject * +Py_filter_set_biquad(Py_filter * self, PyObject *args) +{ + uint_t err = 0; + lsmp_t b0, b1, b2, a1, a2; + if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) { + return NULL; + } + + err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2); + if (err > 0) { + PyErr_SetString (PyExc_ValueError, + "error when setting filter with biquad coefficients"); + return NULL; + } + return Py_None; +} + +static PyMemberDef Py_filter_members[] = { + // TODO remove READONLY flag and define getter/setter + {"order", T_INT, offsetof (Py_filter, order), READONLY, + "order of the filter"}, + {NULL} /* Sentinel */ +}; + +static PyMethodDef Py_filter_methods[] = { + {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS, + "set filter coefficients to C-weighting"}, + {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS, + "set filter coefficients to A-weighting"}, + {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS, + "set b0, b1, b2, a1, a2 biquad coefficients"}, + {NULL} +}; + +PyTypeObject Py_filterType = { + PyObject_HEAD_INIT (NULL) + 0, /* ob_size */ + "aubio.digital_filter", /* tp_name */ + sizeof (Py_filter), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor) Py_filter_del, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, //(reprfunc) Py_filter_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + (ternaryfunc)Py_filter_do, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + Py_filter_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Py_filter_methods, /* tp_methods */ + Py_filter_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc) Py_filter_init, /* tp_init */ + 0, /* tp_alloc */ + Py_filter_new, /* tp_new */ +}; diff --git a/python/ext/py-filterbank.c b/python/ext/py-filterbank.c new file mode 100644 index 00000000..cc0da481 --- /dev/null +++ b/python/ext/py-filterbank.c @@ -0,0 +1,183 @@ +#include "aubiowraphell.h" + +static char Py_filterbank_doc[] = "filterbank object"; + +AUBIO_DECLARE(filterbank, uint_t n_filters; uint_t win_s) + +//AUBIO_NEW(filterbank) +static PyObject * +Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds) +{ + int win_s = 0, n_filters = 0; + Py_filterbank *self; + static char *kwlist[] = { "n_filters", "win_s", NULL }; + + if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, + &n_filters, &win_s)) { + return NULL; + } + + self = (Py_filterbank *) type->tp_alloc (type, 0); + + if (self == NULL) { + return NULL; + } + + self->win_s = Py_default_vector_length; + if (win_s > 0) { + self->win_s = win_s; + } else if (win_s < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative window size"); + return NULL; + } + + self->n_filters = 40; + if (n_filters > 0) { + self->n_filters = n_filters; + } else if (n_filters < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative number of filters"); + return NULL; + } + + return (PyObject *) self; +} + + +AUBIO_INIT(filterbank, self->n_filters, self->win_s) + +AUBIO_DEL(filterbank) + +static PyObject * +Py_filterbank_do(Py_filterbank * self, PyObject * args) +{ + PyObject *input; + cvec_t *vec; + fvec_t *out; + + if (!PyArg_ParseTuple (args, "O", &input)) { + return NULL; + } + + vec = PyAubio_ArrayToCCvec (input); + + if (vec == NULL) { + return NULL; + } + + out = new_fvec (self->n_filters); + + // compute the function + aubio_filterbank_do (self->o, vec, out); + return (PyObject *)PyAubio_CFvecToArray(out); +} + +AUBIO_MEMBERS_START(filterbank) + {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY, + "size of the window"}, + {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY, + "number of filters"}, +AUBIO_MEMBERS_STOP(filterbank) + +static PyObject * +Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) +{ + uint_t err = 0; + + PyObject *input; + uint_t samplerate; + fvec_t *freqs; + if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { + return NULL; + } + + if (input == NULL) { + return NULL; + } + + freqs = PyAubio_ArrayToCFvec (input); + + if (freqs == NULL) { + return NULL; + } + + err = aubio_filterbank_set_triangle_bands (self->o, + freqs, samplerate); + if (err > 0) { + PyErr_SetString (PyExc_ValueError, + "error when setting filter to A-weighting"); + return NULL; + } + return Py_None; +} + +static PyObject * +Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) +{ + uint_t err = 0; + + uint_t samplerate; + if (!PyArg_ParseTuple (args, "I", &samplerate)) { + return NULL; + } + + err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); + if (err > 0) { + PyErr_SetString (PyExc_ValueError, + "error when setting filter to A-weighting"); + return NULL; + } + return Py_None; +} + +static PyObject * +Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args) +{ + uint_t err = 0; + + PyObject *input; + fmat_t *coeffs; + + if (!PyArg_ParseTuple (args, "O", &input)) { + return NULL; + } + + coeffs = PyAubio_ArrayToCFmat (input); + + if (coeffs == NULL) { + PyErr_SetString (PyExc_ValueError, + "unable to parse input array"); + return NULL; + } + + err = aubio_filterbank_set_coeffs (self->o, coeffs); + + if (err > 0) { + PyErr_SetString (PyExc_ValueError, + "error when setting filter coefficients"); + return NULL; + } + return Py_None; +} + +static PyObject * +Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) +{ + return (PyObject *)PyAubio_CFmatToArray( + aubio_filterbank_get_coeffs (self->o) ); +} + +static PyMethodDef Py_filterbank_methods[] = { + {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, + METH_VARARGS, "set coefficients of filterbanks"}, + {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, + METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, + {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, + METH_NOARGS, "get coefficients of filterbank"}, + {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs, + METH_VARARGS, "set coefficients of filterbank"}, + {NULL} +}; + +AUBIO_TYPEOBJECT(filterbank, "aubio.filterbank") diff --git a/python/ext/py-phasevoc.c b/python/ext/py-phasevoc.c new file mode 100644 index 00000000..6767e921 --- /dev/null +++ b/python/ext/py-phasevoc.c @@ -0,0 +1,118 @@ +#include "aubiowraphell.h" + +static char Py_pvoc_doc[] = "pvoc object"; + +AUBIO_DECLARE(pvoc, uint_t win_s; uint_t hop_s) + +//AUBIO_NEW(pvoc) +static PyObject * +Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds) +{ + int win_s = 0, hop_s = 0; + Py_pvoc *self; + static char *kwlist[] = { "win_s", "hop_s", NULL }; + + if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, + &win_s, &hop_s)) { + return NULL; + } + + self = (Py_pvoc *) type->tp_alloc (type, 0); + + if (self == NULL) { + return NULL; + } + + self->win_s = Py_default_vector_length; + self->hop_s = Py_default_vector_length/2; + + if (self == NULL) { + return NULL; + } + + if (win_s > 0) { + self->win_s = win_s; + } else if (win_s < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative window size"); + return NULL; + } + + if (hop_s > 0) { + self->hop_s = hop_s; + } else if (hop_s < 0) { + PyErr_SetString (PyExc_ValueError, + "can not use negative hop size"); + return NULL; + } + + return (PyObject *) self; +} + + +AUBIO_INIT(pvoc, self->win_s, self->hop_s) + +AUBIO_DEL(pvoc) + +static PyObject * +Py_pvoc_do(Py_pvoc * self, PyObject * args) +{ + PyObject *input; + fvec_t *vec; + cvec_t *output; + + if (!PyArg_ParseTuple (args, "O", &input)) { + return NULL; + } + + vec = PyAubio_ArrayToCFvec (input); + + if (vec == NULL) { + return NULL; + } + + output = new_cvec(self->win_s); + + // compute the function + aubio_pvoc_do (self->o, vec, output); + return (PyObject *)PyAubio_CCvecToPyCvec(output); +} + +AUBIO_MEMBERS_START(pvoc) + {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY, + "size of the window"}, + {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY, + "size of the hop"}, +AUBIO_MEMBERS_STOP(pvoc) + +static PyObject * +Py_pvoc_rdo(Py_pvoc * self, PyObject * args) +{ + PyObject *input; + cvec_t *vec; + fvec_t *output; + + if (!PyArg_ParseTuple (args, "O", &input)) { + return NULL; + } + + vec = PyAubio_ArrayToCCvec (input); + + if (vec == NULL) { + return NULL; + } + + output = new_fvec(self->hop_s); + + // compute the function + aubio_pvoc_rdo (self->o, vec, output); + return (PyObject *)PyAubio_CFvecToArray(output); +} + +static PyMethodDef Py_pvoc_methods[] = { + {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS, + "synthesis of spectral grain"}, + {NULL} +}; + +AUBIO_TYPEOBJECT(pvoc, "aubio.pvoc") diff --git a/python/gen_pyobject.py b/python/gen_pyobject.py index 20f4534a..14920fbc 100644 --- a/python/gen_pyobject.py +++ b/python/gen_pyobject.py @@ -172,7 +172,7 @@ def gen_new_init(newfunc, name): // WARNING: this file is generated, DO NOT EDIT // WARNING: if you haven't read the first line yet, please do so -#include "aubiowraphell.h" +#include "ext/aubiowraphell.h" typedef struct { diff --git a/python/py-cvec.c b/python/py-cvec.c deleted file mode 100644 index 940508fb..00000000 --- a/python/py-cvec.c +++ /dev/null @@ -1,302 +0,0 @@ -#include "aubio-types.h" - -/* cvec type definition - -class cvec(): - def __init__(self, length = 1024): - self.length = length - self.norm = array(length) - self.phas = array(length) - -*/ - -static char Py_cvec_doc[] = "cvec object"; - -static PyObject * -Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) -{ - int length= 0; - Py_cvec *self; - static char *kwlist[] = { "length", NULL }; - - if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, - &length)) { - return NULL; - } - - - self = (Py_cvec *) type->tp_alloc (type, 0); - - self->length = Py_default_vector_length / 2 + 1; - - if (self == NULL) { - return NULL; - } - - if (length > 0) { - self->length = length / 2 + 1; - } else if (length < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative number of elements"); - return NULL; - } - - return (PyObject *) self; -} - -static int -Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) -{ - self->o = new_cvec ((self->length - 1) * 2); - if (self->o == NULL) { - return -1; - } - - return 0; -} - -static void -Py_cvec_del (Py_cvec * self) -{ - del_cvec (self->o); - self->ob_type->tp_free ((PyObject *) self); -} - -static PyObject * -Py_cvec_repr (Py_cvec * self, PyObject * unused) -{ - PyObject *format = NULL; - PyObject *args = NULL; - PyObject *result = NULL; - - format = PyString_FromString ("aubio cvec of %d elements"); - if (format == NULL) { - goto fail; - } - - args = Py_BuildValue ("I", self->length); - if (args == NULL) { - goto fail; - } - cvec_print ( self->o ); - - result = PyString_Format (format, args); - -fail: - Py_XDECREF (format); - Py_XDECREF (args); - - return result; -} - -PyObject * -PyAubio_CvecNormToArray (Py_cvec * self) -{ - npy_intp dims[] = { self->o->length, 1 }; - return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm); -} - - -PyObject * -PyAubio_CvecPhasToArray (Py_cvec * self) -{ - npy_intp dims[] = { self->o->length, 1 }; - return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas); -} - -PyObject * -PyAubio_ArrayToCvecPhas (PyObject * self) -{ - return NULL; -} - -PyObject * -Py_cvec_get_norm (Py_cvec * self, void *closure) -{ - return PyAubio_CvecNormToArray(self); -} - -PyObject * -Py_cvec_get_phas (Py_cvec * self, void *closure) -{ - return PyAubio_CvecPhasToArray(self); -} - -static int -Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) -{ - PyArrayObject * array; - if (input == NULL) { - PyErr_SetString (PyExc_ValueError, "input array is not a python object"); - goto fail; - } - if (PyArray_Check(input)) { - - // we got an array, convert it to a cvec.norm - if (PyArray_NDIM ((PyArrayObject *)input) == 0) { - PyErr_SetString (PyExc_ValueError, "input array is a scalar"); - goto fail; - } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { - PyErr_SetString (PyExc_ValueError, - "input array has more than two dimensions"); - goto fail; - } - - if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { - PyErr_SetString (PyExc_ValueError, "input array should be float"); - goto fail; - } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - goto fail; - } - array = (PyArrayObject *)input; - - // check input array dimensions - if (PyArray_NDIM (array) != 1) { - PyErr_Format (PyExc_ValueError, - "input array has %d dimensions, not 1", - PyArray_NDIM (array)); - goto fail; - } else { - if (vec->o->length != PyArray_SIZE (array)) { - PyErr_Format (PyExc_ValueError, - "input array has length %d, but cvec has length %d", - (int)PyArray_SIZE (array), vec->o->length); - goto fail; - } - } - - vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); - - } else { - PyErr_SetString (PyExc_ValueError, "can only accept array as input"); - return 1; - } - - Py_INCREF(array); - return 0; - -fail: - return 1; -} - -static int -Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) -{ - PyArrayObject * array; - if (input == NULL) { - PyErr_SetString (PyExc_ValueError, "input array is not a python object"); - goto fail; - } - if (PyArray_Check(input)) { - - // we got an array, convert it to a cvec.phas - if (PyArray_NDIM ((PyArrayObject *)input) == 0) { - PyErr_SetString (PyExc_ValueError, "input array is a scalar"); - goto fail; - } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { - PyErr_SetString (PyExc_ValueError, - "input array has more than two dimensions"); - goto fail; - } - - if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { - PyErr_SetString (PyExc_ValueError, "input array should be float"); - goto fail; - } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - goto fail; - } - array = (PyArrayObject *)input; - - // check input array dimensions - if (PyArray_NDIM (array) != 1) { - PyErr_Format (PyExc_ValueError, - "input array has %d dimensions, not 1", - PyArray_NDIM (array)); - goto fail; - } else { - if (vec->o->length != PyArray_SIZE (array)) { - PyErr_Format (PyExc_ValueError, - "input array has length %d, but cvec has length %d", - (int)PyArray_SIZE (array), vec->o->length); - goto fail; - } - } - - vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); - - } else { - PyErr_SetString (PyExc_ValueError, "can only accept array as input"); - return 1; - } - - Py_INCREF(array); - return 0; - -fail: - return 1; -} - -static PyMemberDef Py_cvec_members[] = { - // TODO remove READONLY flag and define getter/setter - {"length", T_INT, offsetof (Py_cvec, length), READONLY, - "length attribute"}, - {NULL} /* Sentinel */ -}; - -static PyMethodDef Py_cvec_methods[] = { - {NULL} -}; - -static PyGetSetDef Py_cvec_getseters[] = { - {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, - "Numpy vector of shape (length,) containing the magnitude", - NULL}, - {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, - "Numpy vector of shape (length,) containing the phase", - NULL}, - {NULL} /* sentinel */ -}; - -PyTypeObject Py_cvecType = { - PyObject_HEAD_INIT (NULL) - 0, /* ob_size */ - "aubio.cvec", /* tp_name */ - sizeof (Py_cvec), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor) Py_cvec_del, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - (reprfunc) Py_cvec_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - Py_cvec_doc, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - Py_cvec_methods, /* tp_methods */ - Py_cvec_members, /* tp_members */ - Py_cvec_getseters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc) Py_cvec_init, /* tp_init */ - 0, /* tp_alloc */ - Py_cvec_new, /* tp_new */ -}; diff --git a/python/py-fft.c b/python/py-fft.c deleted file mode 100644 index 19d7b49b..00000000 --- a/python/py-fft.c +++ /dev/null @@ -1,107 +0,0 @@ -#include "aubiowraphell.h" - -static char Py_fft_doc[] = "fft object"; - -AUBIO_DECLARE(fft, uint_t win_s) - -//AUBIO_NEW(fft) -static PyObject * -Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds) -{ - int win_s = 0; - Py_fft *self; - static char *kwlist[] = { "win_s", NULL }; - - if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, - &win_s)) { - return NULL; - } - - self = (Py_fft *) type->tp_alloc (type, 0); - - if (self == NULL) { - return NULL; - } - - self->win_s = Py_default_vector_length; - - if (self == NULL) { - return NULL; - } - - if (win_s > 0) { - self->win_s = win_s; - } else if (win_s < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative window size"); - return NULL; - } - - return (PyObject *) self; -} - - -AUBIO_INIT(fft, self->win_s) - -AUBIO_DEL(fft) - -static PyObject * -Py_fft_do(PyObject * self, PyObject * args) -{ - PyObject *input; - fvec_t *vec; - cvec_t *output; - - if (!PyArg_ParseTuple (args, "O", &input)) { - return NULL; - } - - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { - return NULL; - } - - output = new_cvec(((Py_fft *) self)->win_s); - - // compute the function - aubio_fft_do (((Py_fft *)self)->o, vec, output); - return (PyObject *)PyAubio_CCvecToPyCvec(output); -} - -AUBIO_MEMBERS_START(fft) - {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY, - "size of the window"}, -AUBIO_MEMBERS_STOP(fft) - -static PyObject * -Py_fft_rdo(Py_fft * self, PyObject * args) -{ - PyObject *input; - cvec_t *vec; - fvec_t *output; - - if (!PyArg_ParseTuple (args, "O", &input)) { - return NULL; - } - - vec = PyAubio_ArrayToCCvec (input); - - if (vec == NULL) { - return NULL; - } - - output = new_fvec(self->win_s); - - // compute the function - aubio_fft_rdo (((Py_fft *)self)->o, vec, output); - return (PyObject *)PyAubio_CFvecToArray(output); -} - -static PyMethodDef Py_fft_methods[] = { - {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS, - "synthesis of spectral grain"}, - {NULL} -}; - -AUBIO_TYPEOBJECT(fft, "aubio.fft") diff --git a/python/py-filter.c b/python/py-filter.c deleted file mode 100644 index 5b9fee64..00000000 --- a/python/py-filter.c +++ /dev/null @@ -1,198 +0,0 @@ -#include "aubio-types.h" - -typedef struct -{ - PyObject_HEAD - aubio_filter_t * o; - uint_t order; -} Py_filter; - -static char Py_filter_doc[] = "filter object"; - -static PyObject * -Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds) -{ - int order= 0; - Py_filter *self; - static char *kwlist[] = { "order", NULL }; - - if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, - &order)) { - return NULL; - } - - self = (Py_filter *) type->tp_alloc (type, 0); - - if (self == NULL) { - return NULL; - } - - self->order = 7; - - if (order > 0) { - self->order = order; - } else if (order < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative order"); - return NULL; - } - - return (PyObject *) self; -} - -static int -Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) -{ - self->o = new_aubio_filter (self->order); - if (self->o == NULL) { - return -1; - } - - return 0; -} - -static void -Py_filter_del (Py_filter * self) -{ - del_aubio_filter (self->o); - self->ob_type->tp_free ((PyObject *) self); -} - -static PyObject * -Py_filter_do(Py_filter * self, PyObject * args) -{ - PyObject *input; - fvec_t *vec; - - if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) { - return NULL; - } - - if (input == NULL) { - return NULL; - } - - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { - return NULL; - } - - // compute the function - fvec_t * out = new_fvec(vec->length); - aubio_filter_do_outplace (self->o, vec, out); - return PyAubio_CFvecToArray(out); -} - -static PyObject * -Py_filter_set_c_weighting (Py_filter * self, PyObject *args) -{ - uint_t err = 0; - uint_t samplerate; - if (!PyArg_ParseTuple (args, "I", &samplerate)) { - return NULL; - } - - err = aubio_filter_set_c_weighting (self->o, samplerate); - if (err > 0) { - PyErr_SetString (PyExc_ValueError, - "error when setting filter to C-weighting"); - return NULL; - } - return Py_None; -} - -static PyObject * -Py_filter_set_a_weighting (Py_filter * self, PyObject *args) -{ - uint_t err = 0; - uint_t samplerate; - if (!PyArg_ParseTuple (args, "I", &samplerate)) { - return NULL; - } - - err = aubio_filter_set_a_weighting (self->o, samplerate); - if (err > 0) { - PyErr_SetString (PyExc_ValueError, - "error when setting filter to A-weighting"); - return NULL; - } - return Py_None; -} - -static PyObject * -Py_filter_set_biquad(Py_filter * self, PyObject *args) -{ - uint_t err = 0; - lsmp_t b0, b1, b2, a1, a2; - if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) { - return NULL; - } - - err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2); - if (err > 0) { - PyErr_SetString (PyExc_ValueError, - "error when setting filter with biquad coefficients"); - return NULL; - } - return Py_None; -} - -static PyMemberDef Py_filter_members[] = { - // TODO remove READONLY flag and define getter/setter - {"order", T_INT, offsetof (Py_filter, order), READONLY, - "order of the filter"}, - {NULL} /* Sentinel */ -}; - -static PyMethodDef Py_filter_methods[] = { - {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS, - "set filter coefficients to C-weighting"}, - {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS, - "set filter coefficients to A-weighting"}, - {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS, - "set b0, b1, b2, a1, a2 biquad coefficients"}, - {NULL} -}; - -PyTypeObject Py_filterType = { - PyObject_HEAD_INIT (NULL) - 0, /* ob_size */ - "aubio.digital_filter", /* tp_name */ - sizeof (Py_filter), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor) Py_filter_del, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - 0, //(reprfunc) Py_filter_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - (ternaryfunc)Py_filter_do, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - Py_filter_doc, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - Py_filter_methods, /* tp_methods */ - Py_filter_members, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc) Py_filter_init, /* tp_init */ - 0, /* tp_alloc */ - Py_filter_new, /* tp_new */ -}; diff --git a/python/py-filterbank.c b/python/py-filterbank.c deleted file mode 100644 index cc0da481..00000000 --- a/python/py-filterbank.c +++ /dev/null @@ -1,183 +0,0 @@ -#include "aubiowraphell.h" - -static char Py_filterbank_doc[] = "filterbank object"; - -AUBIO_DECLARE(filterbank, uint_t n_filters; uint_t win_s) - -//AUBIO_NEW(filterbank) -static PyObject * -Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds) -{ - int win_s = 0, n_filters = 0; - Py_filterbank *self; - static char *kwlist[] = { "n_filters", "win_s", NULL }; - - if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, - &n_filters, &win_s)) { - return NULL; - } - - self = (Py_filterbank *) type->tp_alloc (type, 0); - - if (self == NULL) { - return NULL; - } - - self->win_s = Py_default_vector_length; - if (win_s > 0) { - self->win_s = win_s; - } else if (win_s < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative window size"); - return NULL; - } - - self->n_filters = 40; - if (n_filters > 0) { - self->n_filters = n_filters; - } else if (n_filters < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative number of filters"); - return NULL; - } - - return (PyObject *) self; -} - - -AUBIO_INIT(filterbank, self->n_filters, self->win_s) - -AUBIO_DEL(filterbank) - -static PyObject * -Py_filterbank_do(Py_filterbank * self, PyObject * args) -{ - PyObject *input; - cvec_t *vec; - fvec_t *out; - - if (!PyArg_ParseTuple (args, "O", &input)) { - return NULL; - } - - vec = PyAubio_ArrayToCCvec (input); - - if (vec == NULL) { - return NULL; - } - - out = new_fvec (self->n_filters); - - // compute the function - aubio_filterbank_do (self->o, vec, out); - return (PyObject *)PyAubio_CFvecToArray(out); -} - -AUBIO_MEMBERS_START(filterbank) - {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY, - "size of the window"}, - {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY, - "number of filters"}, -AUBIO_MEMBERS_STOP(filterbank) - -static PyObject * -Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) -{ - uint_t err = 0; - - PyObject *input; - uint_t samplerate; - fvec_t *freqs; - if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { - return NULL; - } - - if (input == NULL) { - return NULL; - } - - freqs = PyAubio_ArrayToCFvec (input); - - if (freqs == NULL) { - return NULL; - } - - err = aubio_filterbank_set_triangle_bands (self->o, - freqs, samplerate); - if (err > 0) { - PyErr_SetString (PyExc_ValueError, - "error when setting filter to A-weighting"); - return NULL; - } - return Py_None; -} - -static PyObject * -Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) -{ - uint_t err = 0; - - uint_t samplerate; - if (!PyArg_ParseTuple (args, "I", &samplerate)) { - return NULL; - } - - err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); - if (err > 0) { - PyErr_SetString (PyExc_ValueError, - "error when setting filter to A-weighting"); - return NULL; - } - return Py_None; -} - -static PyObject * -Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args) -{ - uint_t err = 0; - - PyObject *input; - fmat_t *coeffs; - - if (!PyArg_ParseTuple (args, "O", &input)) { - return NULL; - } - - coeffs = PyAubio_ArrayToCFmat (input); - - if (coeffs == NULL) { - PyErr_SetString (PyExc_ValueError, - "unable to parse input array"); - return NULL; - } - - err = aubio_filterbank_set_coeffs (self->o, coeffs); - - if (err > 0) { - PyErr_SetString (PyExc_ValueError, - "error when setting filter coefficients"); - return NULL; - } - return Py_None; -} - -static PyObject * -Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) -{ - return (PyObject *)PyAubio_CFmatToArray( - aubio_filterbank_get_coeffs (self->o) ); -} - -static PyMethodDef Py_filterbank_methods[] = { - {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, - METH_VARARGS, "set coefficients of filterbanks"}, - {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, - METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, - {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, - METH_NOARGS, "get coefficients of filterbank"}, - {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs, - METH_VARARGS, "set coefficients of filterbank"}, - {NULL} -}; - -AUBIO_TYPEOBJECT(filterbank, "aubio.filterbank") diff --git a/python/py-phasevoc.c b/python/py-phasevoc.c deleted file mode 100644 index 6767e921..00000000 --- a/python/py-phasevoc.c +++ /dev/null @@ -1,118 +0,0 @@ -#include "aubiowraphell.h" - -static char Py_pvoc_doc[] = "pvoc object"; - -AUBIO_DECLARE(pvoc, uint_t win_s; uint_t hop_s) - -//AUBIO_NEW(pvoc) -static PyObject * -Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds) -{ - int win_s = 0, hop_s = 0; - Py_pvoc *self; - static char *kwlist[] = { "win_s", "hop_s", NULL }; - - if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, - &win_s, &hop_s)) { - return NULL; - } - - self = (Py_pvoc *) type->tp_alloc (type, 0); - - if (self == NULL) { - return NULL; - } - - self->win_s = Py_default_vector_length; - self->hop_s = Py_default_vector_length/2; - - if (self == NULL) { - return NULL; - } - - if (win_s > 0) { - self->win_s = win_s; - } else if (win_s < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative window size"); - return NULL; - } - - if (hop_s > 0) { - self->hop_s = hop_s; - } else if (hop_s < 0) { - PyErr_SetString (PyExc_ValueError, - "can not use negative hop size"); - return NULL; - } - - return (PyObject *) self; -} - - -AUBIO_INIT(pvoc, self->win_s, self->hop_s) - -AUBIO_DEL(pvoc) - -static PyObject * -Py_pvoc_do(Py_pvoc * self, PyObject * args) -{ - PyObject *input; - fvec_t *vec; - cvec_t *output; - - if (!PyArg_ParseTuple (args, "O", &input)) { - return NULL; - } - - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { - return NULL; - } - - output = new_cvec(self->win_s); - - // compute the function - aubio_pvoc_do (self->o, vec, output); - return (PyObject *)PyAubio_CCvecToPyCvec(output); -} - -AUBIO_MEMBERS_START(pvoc) - {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY, - "size of the window"}, - {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY, - "size of the hop"}, -AUBIO_MEMBERS_STOP(pvoc) - -static PyObject * -Py_pvoc_rdo(Py_pvoc * self, PyObject * args) -{ - PyObject *input; - cvec_t *vec; - fvec_t *output; - - if (!PyArg_ParseTuple (args, "O", &input)) { - return NULL; - } - - vec = PyAubio_ArrayToCCvec (input); - - if (vec == NULL) { - return NULL; - } - - output = new_fvec(self->hop_s); - - // compute the function - aubio_pvoc_rdo (self->o, vec, output); - return (PyObject *)PyAubio_CFvecToArray(output); -} - -static PyMethodDef Py_pvoc_methods[] = { - {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS, - "synthesis of spectral grain"}, - {NULL} -}; - -AUBIO_TYPEOBJECT(pvoc, "aubio.pvoc") diff --git a/python/setup.py b/python/setup.py index 55a6f882..10415465 100755 --- a/python/setup.py +++ b/python/setup.py @@ -17,16 +17,17 @@ include_dirs = ['../build/src', '../src', '.' ] library_dirs = filter (lambda x: os.path.isdir(x), library_dirs) include_dirs = filter (lambda x: os.path.isdir(x), include_dirs) -aubio_extension = Extension("aubio._aubio", - ["aubiomodule.c", - "aubioproxy.c", - "py-cvec.c", +aubio_extension = Extension("aubio._aubio", [ + "ext/aubiomodule.c", + "ext/aubioproxy.c", + "ext/ufuncs.c", + "ext/py-cvec.c", # example without macro - "py-filter.c", + "ext/py-filter.c", # macroised - "py-filterbank.c", - "py-fft.c", - "py-phasevoc.c", + "ext/py-filterbank.c", + "ext/py-fft.c", + "ext/py-phasevoc.c", # generated files ] + generate_object_files(), include_dirs = include_dirs + [ numpy.get_include() ],