From c6388f4affa69eb201fa5de663dd8d27fef12b74 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 24 Apr 2016 23:45:45 +0200 Subject: [PATCH] python/{ext,lib}: prepare for double precision --- python/ext/aubio-types.h | 6 +++++- python/ext/aubiomodule.c | 16 +++++++++------- python/ext/aubioproxy.c | 6 +++--- python/ext/py-cvec.c | 4 ++-- python/ext/py-musicutils.c | 10 +++++----- python/lib/gen_code.py | 2 +- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/python/ext/aubio-types.h b/python/ext/aubio-types.h index b77abc6a..b856ba4f 100644 --- a/python/ext/aubio-types.h +++ b/python/ext/aubio-types.h @@ -33,10 +33,14 @@ #define Py_aubio_default_samplerate 44100 #if HAVE_AUBIO_DOUBLE -#error "Ouch! Python interface for aubio has not been much tested yet." +#warning "double mode needs love" #define AUBIO_NPY_SMPL NPY_DOUBLE +#define AUBIO_NPY_SMPL_STR "float64" +#define AUBIO_NPY_SMPL_CHR "d" #else #define AUBIO_NPY_SMPL NPY_FLOAT +#define AUBIO_NPY_SMPL_STR "float32" +#define AUBIO_NPY_SMPL_CHR "f" #endif // compat with Python < 2.6 diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index 4c724976..2ab15fa6 100644 --- a/python/ext/aubiomodule.c +++ b/python/ext/aubiomodule.c @@ -87,7 +87,7 @@ Py_alpha_norm (PyObject * self, PyObject * args) smpl_t alpha; PyObject *result; - if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { + if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) { return NULL; } @@ -100,7 +100,7 @@ Py_alpha_norm (PyObject * self, PyObject * args) } // compute the function - result = Py_BuildValue ("f", fvec_alpha_norm (&vec, alpha)); + result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha)); if (result == NULL) { return NULL; } @@ -114,7 +114,7 @@ Py_bintomidi (PyObject * self, PyObject * args) smpl_t input, samplerate, fftsize; smpl_t output; - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { return NULL; } @@ -129,7 +129,7 @@ Py_miditobin (PyObject * self, PyObject * args) smpl_t input, samplerate, fftsize; smpl_t output; - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { return NULL; } @@ -144,7 +144,7 @@ Py_bintofreq (PyObject * self, PyObject * args) smpl_t input, samplerate, fftsize; smpl_t output; - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { return NULL; } @@ -159,7 +159,7 @@ Py_freqtobin (PyObject * self, PyObject * args) smpl_t input, samplerate, fftsize; smpl_t output; - if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { return NULL; } @@ -188,7 +188,7 @@ Py_zero_crossing_rate (PyObject * self, PyObject * args) } // compute the function - result = Py_BuildValue ("f", aubio_zero_crossing_rate (&vec)); + result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec)); if (result == NULL) { return NULL; } @@ -308,6 +308,8 @@ initaubio (void) Py_INCREF (&Py_sinkType); PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType); + PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR); + // add generated objects add_generated_objects(m); diff --git a/python/ext/aubioproxy.c b/python/ext/aubioproxy.c index c6755b50..6ee61e9a 100644 --- a/python/ext/aubioproxy.c +++ b/python/ext/aubioproxy.c @@ -30,7 +30,7 @@ PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { PyErr_SetString (PyExc_ValueError, "input array should be float"); return 0; } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); + PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR); return 0; } @@ -73,7 +73,7 @@ PyAubio_ArrayToCCvec (PyObject *input, cvec_t *i) { i->length = ((Py_cvec*)input)->o->length; return 1; } else { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); + PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec"); return 0; } } @@ -119,7 +119,7 @@ PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) { PyErr_SetString (PyExc_ValueError, "input array should be float"); return 0; } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); + PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR); return 0; } diff --git a/python/ext/py-cvec.c b/python/ext/py-cvec.c index 6c70f05b..dc7e71c5 100644 --- a/python/ext/py-cvec.c +++ b/python/ext/py-cvec.c @@ -93,7 +93,7 @@ PyObject * PyAubio_CvecNormToArray (Py_cvec * self) { npy_intp dims[] = { self->o->length, 1 }; - return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm); + return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->norm); } @@ -101,7 +101,7 @@ PyObject * PyAubio_CvecPhasToArray (Py_cvec * self) { npy_intp dims[] = { self->o->length, 1 }; - return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas); + return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->phas); } PyObject * diff --git a/python/ext/py-musicutils.c b/python/ext/py-musicutils.c index ec266760..0f29697b 100644 --- a/python/ext/py-musicutils.c +++ b/python/ext/py-musicutils.c @@ -41,7 +41,7 @@ Py_aubio_level_lin(PyObject *self, PyObject *args) return NULL; } - level_lin = Py_BuildValue("f", aubio_level_lin(&vec)); + level_lin = Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_level_lin(&vec)); if (level_lin == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing level_lin"); return NULL; @@ -70,7 +70,7 @@ Py_aubio_db_spl(PyObject *self, PyObject *args) return NULL; } - db_spl = Py_BuildValue("f", aubio_db_spl(&vec)); + db_spl = Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_db_spl(&vec)); if (db_spl == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing db_spl"); return NULL; @@ -87,7 +87,7 @@ Py_aubio_silence_detection(PyObject *self, PyObject *args) PyObject *silence_detection; smpl_t threshold; - if (!PyArg_ParseTuple (args, "Of:silence_detection", &input, &threshold)) { + if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":silence_detection", &input, &threshold)) { PyErr_SetString (PyExc_ValueError, "failed parsing arguments"); return NULL; } @@ -117,7 +117,7 @@ Py_aubio_level_detection(PyObject *self, PyObject *args) PyObject *level_detection; smpl_t threshold; - if (!PyArg_ParseTuple (args, "Of:level_detection", &input, &threshold)) { + if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":level_detection", &input, &threshold)) { PyErr_SetString (PyExc_ValueError, "failed parsing arguments"); return NULL; } @@ -130,7 +130,7 @@ Py_aubio_level_detection(PyObject *self, PyObject *args) return NULL; } - level_detection = Py_BuildValue("f", aubio_level_detection(&vec, threshold)); + level_detection = Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_level_detection(&vec, threshold)); if (level_detection == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing level_detection"); return NULL; diff --git a/python/lib/gen_code.py b/python/lib/gen_code.py index d2f54596..7c8bc062 100644 --- a/python/lib/gen_code.py +++ b/python/lib/gen_code.py @@ -25,7 +25,7 @@ member_types = { 'name': 'type', 'char_t*': 'T_STRING', 'uint_t': 'T_INT', - 'smpl_t': 'T_FLOAT', + 'smpl_t': 'AUBIO_NPY_SMPL', } pyfromtype_fn = { -- 2.11.0