[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; |
---|
| 10 | cvec_t *out; |
---|
| 11 | fvec_t *rout; |
---|
| 12 | } Py_fft; |
---|
[615ac7d] | 13 | |
---|
| 14 | static PyObject * |
---|
| 15 | Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 16 | { |
---|
[96fe713] | 17 | int win_s = 0; |
---|
[615ac7d] | 18 | Py_fft *self; |
---|
[96fe713] | 19 | static char *kwlist[] = { "win_s", NULL }; |
---|
[615ac7d] | 20 | |
---|
[96fe713] | 21 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
| 22 | &win_s)) { |
---|
[615ac7d] | 23 | return NULL; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | self = (Py_fft *) type->tp_alloc (type, 0); |
---|
| 27 | |
---|
| 28 | if (self == NULL) { |
---|
| 29 | return NULL; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | self->win_s = Py_default_vector_length; |
---|
| 33 | |
---|
| 34 | if (win_s > 0) { |
---|
| 35 | self->win_s = win_s; |
---|
| 36 | } else if (win_s < 0) { |
---|
| 37 | PyErr_SetString (PyExc_ValueError, |
---|
| 38 | "can not use negative window size"); |
---|
| 39 | return NULL; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | return (PyObject *) self; |
---|
| 43 | } |
---|
| 44 | |
---|
[de81d2b] | 45 | static int |
---|
| 46 | Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds) |
---|
| 47 | { |
---|
| 48 | self->o = new_aubio_fft (self->win_s); |
---|
| 49 | if (self->o == NULL) { |
---|
| 50 | char_t errstr[30]; |
---|
| 51 | sprintf(errstr, "error creating fft with win_s=%d", self->win_s); |
---|
[5c1200a] | 52 | PyErr_SetString (PyExc_Exception, errstr); |
---|
[de81d2b] | 53 | return -1; |
---|
| 54 | } |
---|
| 55 | self->out = new_cvec(self->win_s); |
---|
| 56 | self->rout = new_fvec(self->win_s); |
---|
[615ac7d] | 57 | |
---|
[de81d2b] | 58 | return 0; |
---|
| 59 | } |
---|
[615ac7d] | 60 | |
---|
[de81d2b] | 61 | static void |
---|
| 62 | Py_fft_del (Py_fft *self, PyObject *unused) |
---|
| 63 | { |
---|
| 64 | del_aubio_fft(self->o); |
---|
| 65 | del_cvec(self->out); |
---|
| 66 | del_fvec(self->rout); |
---|
[5c1200a] | 67 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[de81d2b] | 68 | } |
---|
[615ac7d] | 69 | |
---|
| 70 | static PyObject * |
---|
[de81d2b] | 71 | Py_fft_do(Py_fft * self, PyObject * args) |
---|
[615ac7d] | 72 | { |
---|
| 73 | PyObject *input; |
---|
[965b302] | 74 | fvec_t *vec; |
---|
[615ac7d] | 75 | |
---|
| 76 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 77 | return NULL; |
---|
| 78 | } |
---|
| 79 | |
---|
[965b302] | 80 | vec = PyAubio_ArrayToCFvec (input); |
---|
[615ac7d] | 81 | |
---|
| 82 | if (vec == NULL) { |
---|
| 83 | return NULL; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // compute the function |
---|
[de81d2b] | 87 | aubio_fft_do (((Py_fft *)self)->o, vec, self->out); |
---|
| 88 | return (PyObject *)PyAubio_CCvecToPyCvec(self->out); |
---|
[615ac7d] | 89 | } |
---|
| 90 | |
---|
[5652a8c] | 91 | static PyMemberDef Py_fft_members[] = { |
---|
[615ac7d] | 92 | {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY, |
---|
| 93 | "size of the window"}, |
---|
[5652a8c] | 94 | {NULL} |
---|
| 95 | }; |
---|
[615ac7d] | 96 | |
---|
| 97 | static PyObject * |
---|
[965b302] | 98 | Py_fft_rdo(Py_fft * self, PyObject * args) |
---|
[615ac7d] | 99 | { |
---|
| 100 | PyObject *input; |
---|
[965b302] | 101 | cvec_t *vec; |
---|
[615ac7d] | 102 | |
---|
| 103 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 104 | return NULL; |
---|
| 105 | } |
---|
| 106 | |
---|
[965b302] | 107 | vec = PyAubio_ArrayToCCvec (input); |
---|
[615ac7d] | 108 | |
---|
| 109 | if (vec == NULL) { |
---|
| 110 | return NULL; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // compute the function |
---|
[de81d2b] | 114 | aubio_fft_rdo (((Py_fft *)self)->o, vec, self->rout); |
---|
| 115 | return (PyObject *)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 | }; |
---|