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