[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; |
---|
| 16 | } Py_fft; |
---|
[615ac7d] | 17 | |
---|
| 18 | static PyObject * |
---|
| 19 | Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 20 | { |
---|
[96fe713] | 21 | int win_s = 0; |
---|
[615ac7d] | 22 | Py_fft *self; |
---|
[96fe713] | 23 | static char *kwlist[] = { "win_s", NULL }; |
---|
[615ac7d] | 24 | |
---|
[96fe713] | 25 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
| 26 | &win_s)) { |
---|
[615ac7d] | 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 | |
---|
[de81d2b] | 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); |
---|
[5c1200a] | 56 | PyErr_SetString (PyExc_Exception, errstr); |
---|
[de81d2b] | 57 | return -1; |
---|
| 58 | } |
---|
[b5bef11] | 59 | |
---|
[de81d2b] | 60 | self->out = new_cvec(self->win_s); |
---|
| 61 | self->rout = new_fvec(self->win_s); |
---|
[615ac7d] | 62 | |
---|
[de81d2b] | 63 | return 0; |
---|
| 64 | } |
---|
[615ac7d] | 65 | |
---|
[de81d2b] | 66 | static void |
---|
| 67 | Py_fft_del (Py_fft *self, PyObject *unused) |
---|
| 68 | { |
---|
| 69 | del_aubio_fft(self->o); |
---|
| 70 | del_cvec(self->out); |
---|
| 71 | del_fvec(self->rout); |
---|
[5c1200a] | 72 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[de81d2b] | 73 | } |
---|
[615ac7d] | 74 | |
---|
[b5bef11] | 75 | static PyObject * |
---|
[de81d2b] | 76 | Py_fft_do(Py_fft * self, PyObject * args) |
---|
[615ac7d] | 77 | { |
---|
| 78 | PyObject *input; |
---|
| 79 | |
---|
| 80 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | |
---|
[569b363] | 84 | if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) { |
---|
[615ac7d] | 85 | return NULL; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // compute the function |
---|
[569b363] | 89 | aubio_fft_do (((Py_fft *)self)->o, &(self->vecin), self->out); |
---|
[92a8800] | 90 | // convert cvec to py_cvec |
---|
| 91 | return PyAubio_CCvecToPyCvec(self->out); |
---|
[615ac7d] | 92 | } |
---|
| 93 | |
---|
[5652a8c] | 94 | static PyMemberDef Py_fft_members[] = { |
---|
[615ac7d] | 95 | {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY, |
---|
| 96 | "size of the window"}, |
---|
[5652a8c] | 97 | {NULL} |
---|
| 98 | }; |
---|
[615ac7d] | 99 | |
---|
[b5bef11] | 100 | static PyObject * |
---|
[965b302] | 101 | Py_fft_rdo(Py_fft * self, PyObject * args) |
---|
[615ac7d] | 102 | { |
---|
| 103 | PyObject *input; |
---|
| 104 | |
---|
| 105 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 106 | return NULL; |
---|
| 107 | } |
---|
| 108 | |
---|
[92a8800] | 109 | if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) { |
---|
[615ac7d] | 110 | return NULL; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // compute the function |
---|
[569b363] | 114 | aubio_fft_rdo (self->o, &(self->cvecin), self->rout); |
---|
[b5bef11] | 115 | return PyAubio_CFvecToArray(self->rout); |
---|
[615ac7d] | 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 | |
---|
[5652a8c] | 124 | PyTypeObject Py_fftType = { |
---|
| 125 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 126 | "aubio.fft", |
---|
| 127 | sizeof (Py_fft), |
---|
| 128 | 0, |
---|
| 129 | (destructor) Py_fft_del, |
---|
| 130 | 0, |
---|
| 131 | 0, |
---|
| 132 | 0, |
---|
| 133 | 0, |
---|
| 134 | 0, |
---|
| 135 | 0, |
---|
| 136 | 0, |
---|
| 137 | 0, |
---|
| 138 | 0, |
---|
| 139 | (ternaryfunc)Py_fft_do, |
---|
| 140 | 0, |
---|
| 141 | 0, |
---|
| 142 | 0, |
---|
| 143 | 0, |
---|
| 144 | Py_TPFLAGS_DEFAULT, |
---|
| 145 | Py_fft_doc, |
---|
| 146 | 0, |
---|
| 147 | 0, |
---|
| 148 | 0, |
---|
| 149 | 0, |
---|
| 150 | 0, |
---|
| 151 | 0, |
---|
| 152 | Py_fft_methods, |
---|
| 153 | Py_fft_members, |
---|
| 154 | 0, |
---|
| 155 | 0, |
---|
| 156 | 0, |
---|
| 157 | 0, |
---|
| 158 | 0, |
---|
| 159 | 0, |
---|
| 160 | (initproc) Py_fft_init, |
---|
| 161 | 0, |
---|
| 162 | Py_fft_new, |
---|
| 163 | }; |
---|