From: Paul Brossier Date: Fri, 29 Apr 2016 19:19:28 +0000 (+0200) Subject: python/ext/aubio-types.h: add new_py_ functions to create PyObjects instead of fvec_t... X-Git-Tag: 0.4.4~300^2~193 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=ede5d38940603e2f61e8fecefe18ccafa86889c5;p=aubio.git python/ext/aubio-types.h: add new_py_ functions to create PyObjects instead of fvec_t, apply to py-fft.c --- diff --git a/python/ext/aubio-types.h b/python/ext/aubio-types.h index 9bf92d3b..250d7093 100644 --- a/python/ext/aubio-types.h +++ b/python/ext/aubio-types.h @@ -52,6 +52,10 @@ extern PyTypeObject Py_cvecType; +PyObject * new_py_fvec(uint_t length); +PyObject * new_py_cvec(uint_t length); +PyObject * new_py_fmat(uint_t height, uint_t length); + // defined in aubio-proxy.c extern PyObject *PyAubio_CFvecToArray (fvec_t * self); extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out); diff --git a/python/ext/aubioproxy.c b/python/ext/aubioproxy.c index 7abd24ec..f3fd8727 100644 --- a/python/ext/aubioproxy.c +++ b/python/ext/aubioproxy.c @@ -1,6 +1,18 @@ #include "aubio-types.h" PyObject * +new_py_fvec(uint_t length) { + npy_intp dims[] = { length, 1 }; + return PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); +} + +PyObject * +new_py_fmat(uint_t height, uint_t length) { + npy_intp dims[] = { height, length, 1 }; + return PyArray_ZEROS(2, dims, AUBIO_NPY_SMPL, 0); +} + +PyObject * PyAubio_CFvecToArray (fvec_t * self) { npy_intp dims[] = { self->length, 1 }; diff --git a/python/ext/py-cvec.c b/python/ext/py-cvec.c index f81e1c8a..2c433d0c 100644 --- a/python/ext/py-cvec.c +++ b/python/ext/py-cvec.c @@ -21,6 +21,17 @@ typedef struct static char Py_cvec_doc[] = "cvec object"; + +PyObject * +new_py_cvec(uint_t length) { + Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); + npy_intp dims[] = { length / 2 + 1, 1 }; + vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); + vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); + vec->length = length / 2 + 1; + return (PyObject*)vec; +} + PyObject * PyAubio_CCvecToPyCvec (cvec_t * input) { if (input == NULL) { @@ -39,14 +50,6 @@ int PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) { if (PyObject_TypeCheck (input, &Py_cvecType)) { Py_cvec * in = (Py_cvec *)input; - if (in->norm == NULL) { - npy_intp dims[] = { in->length, 1 }; - in->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); - } - if (in->phas == NULL) { - npy_intp dims[] = { in->length, 1 }; - in->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); - } i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0); i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0); i->length = ((Py_cvec*)input)->length; @@ -91,16 +94,17 @@ Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) static int Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) { - self->norm = NULL; - self->phas = NULL; + npy_intp dims[] = { self->length, 1 }; + self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); + self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); return 0; } static void Py_cvec_del (Py_cvec * self) { - Py_XDECREF(self->norm); - Py_XDECREF(self->phas); + Py_DECREF(self->norm); + Py_DECREF(self->phas); Py_TYPE(self)->tp_free ((PyObject *) self); } @@ -134,11 +138,7 @@ fail: PyObject * Py_cvec_get_norm (Py_cvec * self, void *closure) { - // if it norm hasn't been created, create it now - if (self->norm == NULL) { - npy_intp dims[] = { self->length, 1 }; - self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); - } + // we want self->norm to still exist after our caller return it Py_INCREF(self->norm); return (PyObject*)(self->norm); } @@ -146,11 +146,7 @@ Py_cvec_get_norm (Py_cvec * self, void *closure) PyObject * Py_cvec_get_phas (Py_cvec * self, void *closure) { - // if it phas hasn't been created, create it now - if (self->phas == NULL) { - npy_intp dims[] = { self->length, 1 }; - self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); - } + // we want self->phas to still exist after our caller return it Py_INCREF(self->phas); return (PyObject *)(self->phas); } diff --git a/python/ext/py-fft.c b/python/ext/py-fft.c index 61595b95..93094a4b 100644 --- a/python/ext/py-fft.c +++ b/python/ext/py-fft.c @@ -11,8 +11,8 @@ typedef struct fvec_t vecin; cvec_t cvecin; // do / rdo output results - cvec_t *out; - fvec_t *rout; + PyObject *doout; + PyObject *rdoout; } Py_fft; static PyObject * @@ -57,8 +57,8 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds) return -1; } - self->out = new_cvec(self->win_s); - self->rout = new_fvec(self->win_s); + self->doout = new_py_cvec(self->win_s); + self->rdoout = new_py_fvec(self->win_s); return 0; } @@ -66,9 +66,9 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds) static void Py_fft_del (Py_fft *self, PyObject *unused) { + Py_XDECREF(self->doout); + Py_XDECREF(self->rdoout); del_aubio_fft(self->o); - del_cvec(self->out); - del_fvec(self->rout); Py_TYPE(self)->tp_free((PyObject *) self); } @@ -85,10 +85,14 @@ Py_fft_do(Py_fft * self, PyObject * args) return NULL; } + cvec_t c_out; + Py_INCREF(self->doout); + if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) { + return NULL; + } // compute the function - aubio_fft_do (((Py_fft *)self)->o, &(self->vecin), self->out); - // convert cvec to py_cvec - return PyAubio_CCvecToPyCvec(self->out); + aubio_fft_do (self->o, &(self->vecin), &c_out); + return self->doout; } static PyMemberDef Py_fft_members[] = { @@ -110,9 +114,14 @@ Py_fft_rdo(Py_fft * self, PyObject * args) return NULL; } + fvec_t out; + Py_INCREF(self->rdoout); + if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) { + return NULL; + } // compute the function - aubio_fft_rdo (self->o, &(self->cvecin), self->rout); - return PyAubio_CFvecToArray(self->rout); + aubio_fft_rdo (self->o, &(self->cvecin), &out); + return self->rdoout; } static PyMethodDef Py_fft_methods[] = {