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