shithub: aubio

Download patch

ref: b5bef11ddd1694ea7702e2d182769ca4605c91c0
parent: bfe8256155757161c8d96033b2c000685a63e001
author: Paul Brossier <piem@piem.org>
date: Thu Apr 21 17:31:10 EDT 2016

ext/: use new proxy functions

--- a/python/ext/aubiomodule.c
+++ b/python/ext/aubiomodule.c
@@ -95,14 +95,15 @@
     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 @@
     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 @@
     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;
   }
 
--- a/python/ext/py-fft.c
+++ b/python/ext/py-fft.c
@@ -7,7 +7,10 @@
   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 @@
     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 @@
 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 @@
   {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[] = {
--- a/python/ext/py-filter.c
+++ b/python/ext/py-filter.c
@@ -5,6 +5,7 @@
   PyObject_HEAD
   aubio_filter_t * o;
   uint_t order;
+  fvec_t *vec;
   fvec_t *out;
 } Py_filter;
 
@@ -49,6 +50,7 @@
     return -1;
   }
   self->out = new_fvec(Py_default_vector_length);
+  self->vec = (fvec_t *)malloc(sizeof(fvec_t));
   return 0;
 }
 
@@ -57,6 +59,7 @@
 {
   del_fvec(self->out);
   del_aubio_filter (self->o);
+  free(self->vec);
   Py_TYPE(self)->tp_free ((PyObject *) self);
 }
 
@@ -64,7 +67,6 @@
 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 @@
     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);
 }
 
--- a/python/ext/py-filterbank.c
+++ b/python/ext/py-filterbank.c
@@ -8,7 +8,10 @@
   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 @@
   }
   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 @@
 {
   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 @@
 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 @@
 
   PyObject *input;
   uint_t samplerate;
-  fvec_t *freqs;
   if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) {
     return NULL;
   }
@@ -119,14 +130,12 @@
     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 @@
   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,
--- a/python/ext/py-musicutils.c
+++ b/python/ext/py-musicutils.c
@@ -37,12 +37,14 @@
     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 @@
     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 @@
     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 @@
     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;
--- a/python/ext/py-phasevoc.c
+++ b/python/ext/py-phasevoc.c
@@ -8,7 +8,10 @@
   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 @@
     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 @@
   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 @@
 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[] = {
--- a/python/ext/py-sink.c
+++ b/python/ext/py-sink.c
@@ -7,6 +7,8 @@
   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 @@
   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 @@
 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 @@
   PyObject * write_data_obj;
 
   /* input vectors prototypes */
-  fvec_t* write_data;
   uint_t write;
 
 
@@ -147,20 +155,14 @@
     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 @@
   PyObject * write_data_obj;
 
   /* input vectors prototypes */
-  fmat_t * write_data;
   uint_t write;
 
 
@@ -183,18 +184,12 @@
 
 
   /* 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;
 }