shithub: aubio

Download patch

ref: ede5d38940603e2f61e8fecefe18ccafa86889c5
parent: ee092a8841988350d41095b42a01c4d9f5424563
author: Paul Brossier <piem@piem.org>
date: Fri Apr 29 17:19:28 EDT 2016

python/ext/aubio-types.h: add new_py_ functions to create PyObjects instead of fvec_t, apply to py-fft.c

--- 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);
--- 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 };
--- a/python/ext/py-cvec.c
+++ b/python/ext/py-cvec.c
@@ -21,7 +21,18 @@
 
 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) {
       PyErr_SetString (PyExc_ValueError, "PyAubio_CCvecToPyCvec got a null cvec!");
@@ -39,14 +50,6 @@
 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,8 +94,9 @@
 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;
 }
 
@@ -99,8 +103,8 @@
 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 @@
 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 @@
 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);
 }
--- a/python/ext/py-fft.c
+++ b/python/ext/py-fft.c
@@ -11,8 +11,8 @@
   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 @@
     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 @@
 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 @@
     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 @@
     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[] = {