[py] add minimal docstring to fft
[aubio.git] / python / ext / py-fft.c
index 19d7b49..a08af4e 100644 (file)
@@ -1,10 +1,38 @@
-#include "aubiowraphell.h"
-
-static char Py_fft_doc[] = "fft object";
-
-AUBIO_DECLARE(fft, uint_t win_s)
+#include "aubio-types.h"
+
+static char Py_fft_doc[] = ""
+"fft(size=1024)\n"
+"\n"
+"Compute Fast Fourier Transorms.\n"
+"\n"
+"Parameters\n"
+"----------\n"
+"size : int\n"
+"    size of the FFT to compute\n"
+"\n"
+"Example\n"
+"-------\n"
+">>> x = aubio.fvec(512)\n"
+">>> f = aubio.fft(512)\n"
+">>> c = f(x); c\n"
+"aubio cvec of 257 elements\n"
+">>> x2 = f.rdo(c); x2.shape\n"
+"(512,)\n"
+"";
+
+typedef struct
+{
+  PyObject_HEAD
+  aubio_fft_t * o;
+  uint_t win_s;
+  // do / rdo input vectors
+  fvec_t vecin;
+  cvec_t cvecin;
+  // do / rdo output results
+  PyObject *doout;
+  PyObject *rdoout;
+} Py_fft;
 
-//AUBIO_NEW(fft)
 static PyObject *
 Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
 {
@@ -25,10 +53,6 @@ Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
 
   self->win_s = Py_default_vector_length;
 
-  if (self == NULL) {
-    return NULL;
-  }
-
   if (win_s > 0) {
     self->win_s = win_s;
   } else if (win_s < 0) {
@@ -40,62 +64,97 @@ Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
   return (PyObject *) self;
 }
 
+static int
+Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
+{
+  self->o = new_aubio_fft (self->win_s);
+  if (self->o == NULL) {
+    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
+    // AUBIO_ERR when failing
+    return -1;
+  }
 
-AUBIO_INIT(fft, self->win_s)
+  self->doout = new_py_cvec(self->win_s);
+  self->rdoout = new_py_fvec(self->win_s);
 
-AUBIO_DEL(fft)
+  return 0;
+}
 
-static PyObject * 
-Py_fft_do(PyObject * self, PyObject * args)
+static void
+Py_fft_del (Py_fft *self, PyObject *unused)
+{
+  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 *
+Py_fft_do(Py_fft * self, PyObject * args)
 {
   PyObject *input;
-  fvec_t *vec;
-  cvec_t *output;
+  cvec_t c_out;
 
   if (!PyArg_ParseTuple (args, "O", &input)) {
     return NULL;
   }
 
-  vec = PyAubio_ArrayToCFvec (input);
-
-  if (vec == NULL) {
+  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
     return NULL;
   }
 
-  output = new_cvec(((Py_fft *) self)->win_s);
+  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;
+  }
 
+  Py_INCREF(self->doout);
+  if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) {
+    return NULL;
+  }
   // compute the function
-  aubio_fft_do (((Py_fft *)self)->o, vec, output);
-  return (PyObject *)PyAubio_CCvecToPyCvec(output);
+  aubio_fft_do (self->o, &(self->vecin), &c_out);
+  return self->doout;
 }
 
-AUBIO_MEMBERS_START(fft) 
+static PyMemberDef Py_fft_members[] = {
   {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
     "size of the window"},
-AUBIO_MEMBERS_STOP(fft)
+  {NULL}
+};
 
-static PyObject * 
+static PyObject *
 Py_fft_rdo(Py_fft * self, PyObject * args)
 {
   PyObject *input;
-  cvec_t *vec;
-  fvec_t *output;
+  fvec_t out;
 
   if (!PyArg_ParseTuple (args, "O", &input)) {
     return NULL;
   }
 
-  vec = PyAubio_ArrayToCCvec (input);
-
-  if (vec == NULL) {
+  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
     return NULL;
   }
 
-  output = new_fvec(self->win_s);
+  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;
+  }
 
+  Py_INCREF(self->rdoout);
+  if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
+    return NULL;
+  }
   // compute the function
-  aubio_fft_rdo (((Py_fft *)self)->o, vec, output);
-  return (PyObject *)PyAubio_CFvecToArray(output);
+  aubio_fft_rdo (self->o, &(self->cvecin), &out);
+  return self->rdoout;
 }
 
 static PyMethodDef Py_fft_methods[] = {
@@ -104,4 +163,52 @@ static PyMethodDef Py_fft_methods[] = {
   {NULL}
 };
 
-AUBIO_TYPEOBJECT(fft, "aubio.fft")
+PyTypeObject Py_fftType = {
+  PyVarObject_HEAD_INIT (NULL, 0)
+  "aubio.fft",
+  sizeof (Py_fft),
+  0,
+  (destructor) Py_fft_del,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  (ternaryfunc)Py_fft_do,
+  0,
+  0,
+  0,
+  0,
+  Py_TPFLAGS_DEFAULT,
+  Py_fft_doc,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  Py_fft_methods,
+  Py_fft_members,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  (initproc) Py_fft_init,
+  0,
+  Py_fft_new,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+};