python/ext/py-fft.c: improve error message, dont delete if not created
[aubio.git] / python / ext / py-fft.c
index 4e010d8..c55cc53 100644 (file)
@@ -7,8 +7,12 @@ typedef struct
   PyObject_HEAD
   aubio_fft_t * o;
   uint_t win_s;
-  cvec_t *out;
-  fvec_t *rout;
+  // do / rdo input vectors
+  fvec_t vecin;
+  cvec_t cvecin;
+  // do / rdo output results
+  PyObject *doout;
+  PyObject *rdoout;
 } Py_fft;
 
 static PyObject *
@@ -47,13 +51,16 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
 {
   self->o = new_aubio_fft (self->win_s);
   if (self->o == NULL) {
-    char_t errstr[30];
-    sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
-    PyErr_SetString (PyExc_Exception, errstr);
+    PyErr_Format(PyExc_RuntimeError,
+        "error creating fft with win_s=%d "
+        "(should be a power of 2 greater than 1; "
+        "try recompiling aubio with --enable-fftw3)",
+        self->win_s);
     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;
 }
@@ -61,31 +68,42 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
 static void
 Py_fft_del (Py_fft *self, PyObject *unused)
 {
-  del_aubio_fft(self->o);
-  del_cvec(self->out);
-  del_fvec(self->rout);
+  Py_XDECREF(self->doout);
+  Py_XDECREF(self->rdoout);
+  if (self->o) {
+    del_aubio_fft(self->o);
+  }
   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 (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
+    return NULL;
+  }
 
-  if (vec == NULL) {
+  if (self->vecin.length != self->win_s) {
+    PyErr_Format(PyExc_ValueError,
+                 "input array has length %d, but fft expects length %d",
+                 self->vecin.length, self->win_s);
     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, vec, self->out);
-  return (PyObject *)PyAubio_CCvecToPyCvec(self->out);
+  aubio_fft_do (self->o, &(self->vecin), &c_out);
+  return self->doout;
 }
 
 static PyMemberDef Py_fft_members[] = {
@@ -94,25 +112,34 @@ 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 (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
+    return NULL;
+  }
 
-  if (vec == NULL) {
+  if (self->cvecin.length != self->win_s / 2 + 1) {
+    PyErr_Format(PyExc_ValueError,
+                 "input cvec has length %d, but fft expects length %d",
+                 self->cvecin.length, self->win_s / 2 + 1);
     return NULL;
   }
 
+  fvec_t out;
+  Py_INCREF(self->rdoout);
+  if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
+    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), &out);
+  return self->rdoout;
 }
 
 static PyMethodDef Py_fft_methods[] = {