python/ext/aubio-types.h: add new_py_ functions to create PyObjects instead of fvec_t...
[aubio.git] / python / ext / py-fft.c
1 #include "aubio-types.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   // do / rdo input vectors
11   fvec_t vecin;
12   cvec_t cvecin;
13   // do / rdo output results
14   PyObject *doout;
15   PyObject *rdoout;
16 } Py_fft;
17
18 static PyObject *
19 Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
20 {
21   int win_s = 0;
22   Py_fft *self;
23   static char *kwlist[] = { "win_s", NULL };
24
25   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
26           &win_s)) {
27     return NULL;
28   }
29
30   self = (Py_fft *) type->tp_alloc (type, 0);
31
32   if (self == NULL) {
33     return NULL;
34   }
35
36   self->win_s = Py_default_vector_length;
37
38   if (win_s > 0) {
39     self->win_s = win_s;
40   } else if (win_s < 0) {
41     PyErr_SetString (PyExc_ValueError,
42         "can not use negative window size");
43     return NULL;
44   }
45
46   return (PyObject *) self;
47 }
48
49 static int
50 Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
51 {
52   self->o = new_aubio_fft (self->win_s);
53   if (self->o == NULL) {
54     char_t errstr[30];
55     sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
56     PyErr_SetString (PyExc_Exception, errstr);
57     return -1;
58   }
59
60   self->doout = new_py_cvec(self->win_s);
61   self->rdoout = new_py_fvec(self->win_s);
62
63   return 0;
64 }
65
66 static void
67 Py_fft_del (Py_fft *self, PyObject *unused)
68 {
69   Py_XDECREF(self->doout);
70   Py_XDECREF(self->rdoout);
71   del_aubio_fft(self->o);
72   Py_TYPE(self)->tp_free((PyObject *) self);
73 }
74
75 static PyObject *
76 Py_fft_do(Py_fft * self, PyObject * args)
77 {
78   PyObject *input;
79
80   if (!PyArg_ParseTuple (args, "O", &input)) {
81     return NULL;
82   }
83
84   if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
85     return NULL;
86   }
87
88   cvec_t c_out;
89   Py_INCREF(self->doout);
90   if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) {
91     return NULL;
92   }
93   // compute the function
94   aubio_fft_do (self->o, &(self->vecin), &c_out);
95   return self->doout;
96 }
97
98 static PyMemberDef Py_fft_members[] = {
99   {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
100     "size of the window"},
101   {NULL}
102 };
103
104 static PyObject *
105 Py_fft_rdo(Py_fft * self, PyObject * args)
106 {
107   PyObject *input;
108
109   if (!PyArg_ParseTuple (args, "O", &input)) {
110     return NULL;
111   }
112
113   if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
114     return NULL;
115   }
116
117   fvec_t out;
118   Py_INCREF(self->rdoout);
119   if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
120     return NULL;
121   }
122   // compute the function
123   aubio_fft_rdo (self->o, &(self->cvecin), &out);
124   return self->rdoout;
125 }
126
127 static PyMethodDef Py_fft_methods[] = {
128   {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
129     "synthesis of spectral grain"},
130   {NULL}
131 };
132
133 PyTypeObject Py_fftType = {
134   PyVarObject_HEAD_INIT (NULL, 0)
135   "aubio.fft",
136   sizeof (Py_fft),
137   0,
138   (destructor) Py_fft_del,
139   0,
140   0,
141   0,
142   0,
143   0,
144   0,
145   0,
146   0,
147   0,
148   (ternaryfunc)Py_fft_do,
149   0,
150   0,
151   0,
152   0,
153   Py_TPFLAGS_DEFAULT,
154   Py_fft_doc,
155   0,
156   0,
157   0,
158   0,
159   0,
160   0,
161   Py_fft_methods,
162   Py_fft_members,
163   0,
164   0,
165   0,
166   0,
167   0,
168   0,
169   (initproc) Py_fft_init,
170   0,
171   Py_fft_new,
172 };