ext/py-fft.c: continue fixing memory leak (#49)
[aubio.git] / python / ext / py-fft.c
1 #include "aubiowraphell.h"
2
3 static char Py_fft_doc[] = "fft object";
4
5 typedef struct
6 {
7   PyObject_HEAD
8   aubio_fft_t * o;
9   uint_t win_s;
10   cvec_t *out;
11   fvec_t *rout;
12 } Py_fft;
13
14 //AUBIO_NEW(fft)
15 static PyObject *
16 Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
17 {
18   int win_s = 0;
19   Py_fft *self;
20   static char *kwlist[] = { "win_s", NULL };
21
22   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
23           &win_s)) {
24     return NULL;
25   }
26
27   self = (Py_fft *) type->tp_alloc (type, 0);
28
29   if (self == NULL) {
30     return NULL;
31   }
32
33   self->win_s = Py_default_vector_length;
34
35   if (win_s > 0) {
36     self->win_s = win_s;
37   } else if (win_s < 0) {
38     PyErr_SetString (PyExc_ValueError,
39         "can not use negative window size");
40     return NULL;
41   }
42
43   return (PyObject *) self;
44 }
45
46 static int
47 Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
48 {
49   self->o = new_aubio_fft (self->win_s);
50   if (self->o == NULL) {
51     char_t errstr[30];
52     sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
53     PyErr_SetString (PyExc_StandardError, errstr);
54     return -1;
55   }
56   self->out = new_cvec(self->win_s);
57   self->rout = new_fvec(self->win_s);
58
59   return 0;
60 }
61
62 static void
63 Py_fft_del (Py_fft *self, PyObject *unused)
64 {
65   del_aubio_fft(self->o);
66   del_cvec(self->out);
67   del_fvec(self->rout);
68   self->ob_type->tp_free((PyObject *) self);
69 }
70
71 static PyObject * 
72 Py_fft_do(Py_fft * self, PyObject * args)
73 {
74   PyObject *input;
75   fvec_t *vec;
76
77   if (!PyArg_ParseTuple (args, "O", &input)) {
78     return NULL;
79   }
80
81   vec = PyAubio_ArrayToCFvec (input);
82
83   if (vec == NULL) {
84     return NULL;
85   }
86
87   // compute the function
88   aubio_fft_do (((Py_fft *)self)->o, vec, self->out);
89   return (PyObject *)PyAubio_CCvecToPyCvec(self->out);
90 }
91
92 AUBIO_MEMBERS_START(fft) 
93   {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
94     "size of the window"},
95 AUBIO_MEMBERS_STOP(fft)
96
97 static PyObject * 
98 Py_fft_rdo(Py_fft * self, PyObject * args)
99 {
100   PyObject *input;
101   cvec_t *vec;
102
103   if (!PyArg_ParseTuple (args, "O", &input)) {
104     return NULL;
105   }
106
107   vec = PyAubio_ArrayToCCvec (input);
108
109   if (vec == NULL) {
110     return NULL;
111   }
112
113   // compute the function
114   aubio_fft_rdo (((Py_fft *)self)->o, vec, self->rout);
115   return (PyObject *)PyAubio_CFvecToArray(self->rout);
116 }
117
118 static PyMethodDef Py_fft_methods[] = {
119   {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
120     "synthesis of spectral grain"},
121   {NULL}
122 };
123
124 AUBIO_TYPEOBJECT(fft, "aubio.fft")