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