From: Paul Brossier Date: Thu, 21 Apr 2016 19:31:10 +0000 (+0200) Subject: ext/: use new proxy functions X-Git-Tag: 0.4.4~300^2~284 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=b5bef11ddd1694ea7702e2d182769ca4605c91c0;p=aubio.git ext/: use new proxy functions --- diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index a744899c..0d004902 100644 --- a/python/ext/aubiomodule.c +++ b/python/ext/aubiomodule.c @@ -95,14 +95,15 @@ Py_alpha_norm (PyObject * self, PyObject * args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } // compute the function result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); + free(vec); if (result == NULL) { return NULL; } @@ -185,14 +186,15 @@ Py_zero_crossing_rate (PyObject * self, PyObject * args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } // compute the function result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); + free(vec); if (result == NULL) { return NULL; } @@ -214,9 +216,9 @@ Py_min_removal(PyObject * self, PyObject * args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } diff --git a/python/ext/py-fft.c b/python/ext/py-fft.c index 4e010d86..ae0c87ba 100644 --- a/python/ext/py-fft.c +++ b/python/ext/py-fft.c @@ -7,7 +7,10 @@ typedef struct PyObject_HEAD aubio_fft_t * o; uint_t win_s; + fvec_t *vecin; cvec_t *out; + Py_cvec *py_out; + cvec_t *cvecin; fvec_t *rout; } Py_fft; @@ -52,7 +55,13 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds) PyErr_SetString (PyExc_Exception, errstr); return -1; } + + self->cvecin = (cvec_t *)malloc(sizeof(cvec_t)); + self->vecin = (fvec_t *)malloc(sizeof(fvec_t)); + self->out = new_cvec(self->win_s); + self->py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); + Py_XINCREF(self->py_out); self->rout = new_fvec(self->win_s); return 0; @@ -61,31 +70,38 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds) static void Py_fft_del (Py_fft *self, PyObject *unused) { + Py_XDECREF((PyObject*)(self->py_out)); del_aubio_fft(self->o); del_cvec(self->out); del_fvec(self->rout); + free(self->cvecin); + free(self->vecin); Py_TYPE(self)->tp_free((PyObject *) self); } -static PyObject * +static PyObject * Py_fft_do(Py_fft * self, PyObject * args) { PyObject *input; - fvec_t *vec; if (!PyArg_ParseTuple (args, "O", &input)) { return NULL; } - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { + if (!PyAubio_ArrayToCFvec(input, self->vecin)) { return NULL; } // compute the function - aubio_fft_do (((Py_fft *)self)->o, vec, self->out); - return (PyObject *)PyAubio_CCvecToPyCvec(self->out); + aubio_fft_do (((Py_fft *)self)->o, self->vecin, self->out); +#if 0 + Py_cvec * py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); + PyObject* output = PyAubio_CCvecToPyCvec(self->out, py_out); + return output; +#else + // convert cvec to py_cvec, incrementing refcount to keep a copy + return PyAubio_CCvecToPyCvec(self->out, self->py_out); +#endif } static PyMemberDef Py_fft_members[] = { @@ -94,25 +110,22 @@ static PyMemberDef Py_fft_members[] = { {NULL} }; -static PyObject * +static PyObject * Py_fft_rdo(Py_fft * self, PyObject * args) { PyObject *input; - cvec_t *vec; if (!PyArg_ParseTuple (args, "O", &input)) { return NULL; } - vec = PyAubio_ArrayToCCvec (input); - - if (vec == NULL) { + if (!PyAubio_ArrayToCCvec (input, self->cvecin) ) { return NULL; } // compute the function - aubio_fft_rdo (((Py_fft *)self)->o, vec, self->rout); - return (PyObject *)PyAubio_CFvecToArray(self->rout); + aubio_fft_rdo (self->o, self->cvecin, self->rout); + return PyAubio_CFvecToArray(self->rout); } static PyMethodDef Py_fft_methods[] = { diff --git a/python/ext/py-filter.c b/python/ext/py-filter.c index 732b3669..fdf0fb05 100644 --- a/python/ext/py-filter.c +++ b/python/ext/py-filter.c @@ -5,6 +5,7 @@ typedef struct PyObject_HEAD aubio_filter_t * o; uint_t order; + fvec_t *vec; fvec_t *out; } Py_filter; @@ -49,6 +50,7 @@ Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) return -1; } self->out = new_fvec(Py_default_vector_length); + self->vec = (fvec_t *)malloc(sizeof(fvec_t)); return 0; } @@ -57,6 +59,7 @@ Py_filter_del (Py_filter * self) { del_fvec(self->out); del_aubio_filter (self->o); + free(self->vec); Py_TYPE(self)->tp_free ((PyObject *) self); } @@ -64,7 +67,6 @@ 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; @@ -74,19 +76,17 @@ Py_filter_do(Py_filter * self, PyObject * args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { + if (!PyAubio_ArrayToCFvec(input, self->vec)) { return NULL; } // reallocate the output if needed - if (vec->length != self->out->length) { + if (self->vec->length != self->out->length) { del_fvec(self->out); - self->out = new_fvec(vec->length); + self->out = new_fvec(self->vec->length); } // compute the function - aubio_filter_do_outplace (self->o, vec, self->out); + aubio_filter_do_outplace (self->o, self->vec, self->out); return PyAubio_CFvecToArray(self->out); } diff --git a/python/ext/py-filterbank.c b/python/ext/py-filterbank.c index fd3af569..612a34f9 100644 --- a/python/ext/py-filterbank.c +++ b/python/ext/py-filterbank.c @@ -8,7 +8,10 @@ typedef struct aubio_filterbank_t * o; uint_t n_filters; uint_t win_s; + cvec_t *vec; fvec_t *out; + fvec_t *freqs; + fmat_t *coeffs; } Py_filterbank; static PyObject * @@ -63,6 +66,14 @@ Py_filterbank_init (Py_filterbank * self, PyObject * args, PyObject * kwds) } self->out = new_fvec(self->n_filters); + self->vec = (cvec_t *)malloc(sizeof(cvec_t)); + + self->freqs = (fvec_t *)malloc(sizeof(fvec_t)); + + self->coeffs = (fmat_t *)malloc(sizeof(fmat_t)); + self->coeffs->data = (smpl_t **)malloc(sizeof(smpl_t*) * self->n_filters); + self->coeffs->height = self->n_filters; + return 0; } @@ -71,6 +82,10 @@ Py_filterbank_del (Py_filterbank *self, PyObject *unused) { del_aubio_filterbank(self->o); del_fvec(self->out); + free(self->vec); + free(self->freqs); + free(self->coeffs->data); + free(self->coeffs); Py_TYPE(self)->tp_free((PyObject *) self); } @@ -78,20 +93,17 @@ static PyObject * Py_filterbank_do(Py_filterbank * self, PyObject * args) { PyObject *input; - cvec_t *vec; if (!PyArg_ParseTuple (args, "O", &input)) { return NULL; } - vec = PyAubio_ArrayToCCvec (input); - - if (vec == NULL) { + if (!PyAubio_ArrayToCCvec(input, self->vec)) { return NULL; } // compute the function - aubio_filterbank_do (self->o, vec, self->out); + aubio_filterbank_do (self->o, self->vec, self->out); return (PyObject *)PyAubio_CFvecToArray(self->out); } @@ -110,7 +122,6 @@ Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) PyObject *input; uint_t samplerate; - fvec_t *freqs; if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { return NULL; } @@ -119,14 +130,12 @@ Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) return NULL; } - freqs = PyAubio_ArrayToCFvec (input); - - if (freqs == NULL) { + if (!PyAubio_ArrayToCFvec(input, self->freqs)) { return NULL; } err = aubio_filterbank_set_triangle_bands (self->o, - freqs, samplerate); + self->freqs, samplerate); if (err > 0) { PyErr_SetString (PyExc_ValueError, "error when setting filter to A-weighting"); @@ -160,21 +169,15 @@ 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"); + if (!PyAubio_ArrayToCFmat(input, self->coeffs)) { return NULL; } - err = aubio_filterbank_set_coeffs (self->o, coeffs); + err = aubio_filterbank_set_coeffs (self->o, self->coeffs); if (err > 0) { PyErr_SetString (PyExc_ValueError, diff --git a/python/ext/py-musicutils.c b/python/ext/py-musicutils.c index d1e3d017..d5a05242 100644 --- a/python/ext/py-musicutils.c +++ b/python/ext/py-musicutils.c @@ -37,12 +37,14 @@ Py_aubio_level_lin(PyObject *self, PyObject *args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } level_lin = Py_BuildValue("f", aubio_level_lin(vec)); + free(vec); if (level_lin == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing level_lin"); return NULL; @@ -67,12 +69,14 @@ Py_aubio_db_spl(PyObject *self, PyObject *args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } db_spl = Py_BuildValue("f", aubio_db_spl(vec)); + free(vec); if (db_spl == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing db_spl"); return NULL; @@ -98,12 +102,14 @@ Py_aubio_silence_detection(PyObject *self, PyObject *args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } silence_detection = Py_BuildValue("I", aubio_silence_detection(vec, threshold)); + free(vec); if (silence_detection == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing silence_detection"); return NULL; @@ -129,12 +135,14 @@ Py_aubio_level_detection(PyObject *self, PyObject *args) return NULL; } - vec = PyAubio_ArrayToCFvec (input); - if (vec == NULL) { + vec = (fvec_t *)malloc(sizeof(fvec_t)); + if (!PyAubio_ArrayToCFvec(input, vec)) { + free(vec); return NULL; } level_detection = Py_BuildValue("f", aubio_level_detection(vec, threshold)); + free(vec); if (level_detection == NULL) { PyErr_SetString (PyExc_ValueError, "failed computing level_detection"); return NULL; diff --git a/python/ext/py-phasevoc.c b/python/ext/py-phasevoc.c index a9bf7ca8..e8c96f94 100644 --- a/python/ext/py-phasevoc.c +++ b/python/ext/py-phasevoc.c @@ -8,7 +8,10 @@ typedef struct aubio_pvoc_t * o; uint_t win_s; uint_t hop_s; + fvec_t *vecin; cvec_t *output; + Py_cvec *py_out; + cvec_t *cvecin; fvec_t *routput; } Py_pvoc; @@ -68,7 +71,11 @@ Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds) return -1; } + self->cvecin = (cvec_t *)malloc(sizeof(cvec_t)); + self->vecin = (fvec_t *)malloc(sizeof(fvec_t)); + self->output = new_cvec(self->win_s); + self->py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); self->routput = new_fvec(self->hop_s); return 0; @@ -81,29 +88,35 @@ Py_pvoc_del (Py_pvoc *self, PyObject *unused) del_aubio_pvoc(self->o); del_cvec(self->output); del_fvec(self->routput); + free(self->cvecin); + free(self->vecin); Py_TYPE(self)->tp_free((PyObject *) self); } -static PyObject * +static PyObject * Py_pvoc_do(Py_pvoc * self, PyObject * args) { PyObject *input; - fvec_t *vec; if (!PyArg_ParseTuple (args, "O", &input)) { return NULL; } - vec = PyAubio_ArrayToCFvec (input); - - if (vec == NULL) { + if (!PyAubio_ArrayToCFvec (input, self->vecin)) { return NULL; } // compute the function - aubio_pvoc_do (self->o, vec, self->output); - return (PyObject *)PyAubio_CCvecToPyCvec(self->output); + aubio_pvoc_do (self->o, self->vecin, self->output); +#if 0 + Py_cvec * py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); + PyObject* output = PyAubio_CCvecToPyCvec(self->output, py_out); + return output; +#else + // convert cvec to py_cvec, incrementing refcount to keep a copy + return PyAubio_CCvecToPyCvec(self->output, self->py_out); +#endif } static PyMemberDef Py_pvoc_members[] = { @@ -118,20 +131,17 @@ static PyObject * Py_pvoc_rdo(Py_pvoc * self, PyObject * args) { PyObject *input; - cvec_t *vec; if (!PyArg_ParseTuple (args, "O", &input)) { return NULL; } - vec = PyAubio_ArrayToCCvec (input); - - if (vec == NULL) { + if (!PyAubio_ArrayToCCvec (input, self->cvecin)) { return NULL; } // compute the function - aubio_pvoc_rdo (self->o, vec, self->routput); - return (PyObject *)PyAubio_CFvecToArray(self->routput); + aubio_pvoc_rdo (self->o, self->cvecin, self->routput); + return PyAubio_CFvecToArray(self->routput); } static PyMethodDef Py_pvoc_methods[] = { diff --git a/python/ext/py-sink.c b/python/ext/py-sink.c index 829e79c8..7e8cce1e 100644 --- a/python/ext/py-sink.c +++ b/python/ext/py-sink.c @@ -7,6 +7,8 @@ typedef struct char_t* uri; uint_t samplerate; uint_t channels; + fvec_t *write_data; + fmat_t *mwrite_data; } Py_sink; static char Py_sink_doc[] = "" @@ -121,6 +123,10 @@ Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds) self->samplerate = aubio_sink_get_samplerate ( self->o ); self->channels = aubio_sink_get_channels ( self->o ); + self->write_data = (fvec_t *)malloc(sizeof(fvec_t)); + self->mwrite_data = (fmat_t *)malloc(sizeof(fmat_t)); + self->mwrite_data->height = self->channels; + self->mwrite_data->data = (smpl_t **)malloc(sizeof(smpl_t*) * self->channels); return 0; } @@ -128,6 +134,9 @@ static void Py_sink_del (Py_sink *self, PyObject *unused) { del_aubio_sink(self->o); + free(self->write_data); + free(self->mwrite_data->data); + free(self->mwrite_data); Py_TYPE(self)->tp_free((PyObject *) self); } @@ -139,7 +148,6 @@ Py_sink_do(Py_sink * self, PyObject * args) PyObject * write_data_obj; /* input vectors prototypes */ - fvec_t* write_data; uint_t write; @@ -147,20 +155,14 @@ Py_sink_do(Py_sink * self, PyObject * args) return NULL; } - /* input vectors parsing */ - write_data = PyAubio_ArrayToCFvec (write_data_obj); - - if (write_data == NULL) { + if (!PyAubio_ArrayToCFvec(write_data_obj, self->write_data)) { return NULL; } - - - /* compute _do function */ - aubio_sink_do (self->o, write_data, write); + aubio_sink_do (self->o, self->write_data, write); Py_RETURN_NONE; } @@ -173,7 +175,6 @@ Py_sink_do_multi(Py_sink * self, PyObject * args) PyObject * write_data_obj; /* input vectors prototypes */ - fmat_t * write_data; uint_t write; @@ -183,18 +184,12 @@ Py_sink_do_multi(Py_sink * self, PyObject * args) /* input vectors parsing */ - write_data = PyAubio_ArrayToCFmat (write_data_obj); - - if (write_data == NULL) { + if (!PyAubio_ArrayToCFmat(write_data_obj, self->mwrite_data)) { return NULL; } - - - - /* compute _do function */ - aubio_sink_do_multi (self->o, write_data, write); + aubio_sink_do_multi (self->o, self->mwrite_data, write); Py_RETURN_NONE; }