[5652a8c] | 1 | #include "aubio-types.h" |
---|
[615ac7d] | 2 | |
---|
| 3 | static char Py_filterbank_doc[] = "filterbank object"; |
---|
| 4 | |
---|
[59cb451] | 5 | typedef struct |
---|
| 6 | { |
---|
| 7 | PyObject_HEAD |
---|
| 8 | aubio_filterbank_t * o; |
---|
| 9 | uint_t n_filters; |
---|
| 10 | uint_t win_s; |
---|
[569b363] | 11 | cvec_t vec; |
---|
| 12 | fvec_t freqs; |
---|
| 13 | fmat_t coeffs; |
---|
[59cb451] | 14 | fvec_t *out; |
---|
| 15 | } Py_filterbank; |
---|
[615ac7d] | 16 | |
---|
| 17 | static PyObject * |
---|
| 18 | Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 19 | { |
---|
| 20 | int win_s = 0, n_filters = 0; |
---|
| 21 | Py_filterbank *self; |
---|
| 22 | static char *kwlist[] = { "n_filters", "win_s", NULL }; |
---|
| 23 | |
---|
| 24 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, |
---|
| 25 | &n_filters, &win_s)) { |
---|
| 26 | return NULL; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | self = (Py_filterbank *) type->tp_alloc (type, 0); |
---|
| 30 | |
---|
| 31 | if (self == NULL) { |
---|
| 32 | return NULL; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | self->win_s = Py_default_vector_length; |
---|
| 36 | if (win_s > 0) { |
---|
| 37 | self->win_s = win_s; |
---|
| 38 | } else if (win_s < 0) { |
---|
| 39 | PyErr_SetString (PyExc_ValueError, |
---|
| 40 | "can not use negative window size"); |
---|
| 41 | return NULL; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | self->n_filters = 40; |
---|
| 45 | if (n_filters > 0) { |
---|
| 46 | self->n_filters = n_filters; |
---|
| 47 | } else if (n_filters < 0) { |
---|
| 48 | PyErr_SetString (PyExc_ValueError, |
---|
| 49 | "can not use negative number of filters"); |
---|
| 50 | return NULL; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | return (PyObject *) self; |
---|
| 54 | } |
---|
| 55 | |
---|
[59cb451] | 56 | static int |
---|
| 57 | Py_filterbank_init (Py_filterbank * self, PyObject * args, PyObject * kwds) |
---|
| 58 | { |
---|
| 59 | self->o = new_aubio_filterbank (self->n_filters, self->win_s); |
---|
| 60 | if (self->o == NULL) { |
---|
| 61 | char_t errstr[30]; |
---|
| 62 | sprintf(errstr, "error creating filterbank with n_filters=%d, win_s=%d", |
---|
| 63 | self->n_filters, self->win_s); |
---|
[5c1200a] | 64 | PyErr_SetString (PyExc_RuntimeError, errstr); |
---|
[59cb451] | 65 | return -1; |
---|
| 66 | } |
---|
| 67 | self->out = new_fvec(self->n_filters); |
---|
[615ac7d] | 68 | |
---|
[59cb451] | 69 | return 0; |
---|
| 70 | } |
---|
[615ac7d] | 71 | |
---|
[59cb451] | 72 | static void |
---|
| 73 | Py_filterbank_del (Py_filterbank *self, PyObject *unused) |
---|
| 74 | { |
---|
| 75 | del_aubio_filterbank(self->o); |
---|
| 76 | del_fvec(self->out); |
---|
[569b363] | 77 | free(self->coeffs.data); |
---|
[5c1200a] | 78 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[59cb451] | 79 | } |
---|
[615ac7d] | 80 | |
---|
[f6cbefe] | 81 | static PyObject * |
---|
[615ac7d] | 82 | Py_filterbank_do(Py_filterbank * self, PyObject * args) |
---|
| 83 | { |
---|
| 84 | PyObject *input; |
---|
| 85 | |
---|
| 86 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 87 | return NULL; |
---|
| 88 | } |
---|
| 89 | |
---|
[569b363] | 90 | if (!PyAubio_ArrayToCCvec(input, &(self->vec) )) { |
---|
[615ac7d] | 91 | return NULL; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // compute the function |
---|
[569b363] | 95 | aubio_filterbank_do (self->o, &(self->vec), self->out); |
---|
[59cb451] | 96 | return (PyObject *)PyAubio_CFvecToArray(self->out); |
---|
[615ac7d] | 97 | } |
---|
| 98 | |
---|
[5652a8c] | 99 | static PyMemberDef Py_filterbank_members[] = { |
---|
[615ac7d] | 100 | {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY, |
---|
| 101 | "size of the window"}, |
---|
| 102 | {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY, |
---|
| 103 | "number of filters"}, |
---|
[5652a8c] | 104 | {NULL} /* sentinel */ |
---|
| 105 | }; |
---|
[615ac7d] | 106 | |
---|
[f6cbefe] | 107 | static PyObject * |
---|
[615ac7d] | 108 | Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) |
---|
| 109 | { |
---|
| 110 | uint_t err = 0; |
---|
| 111 | |
---|
| 112 | PyObject *input; |
---|
| 113 | uint_t samplerate; |
---|
| 114 | if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { |
---|
| 115 | return NULL; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if (input == NULL) { |
---|
| 119 | return NULL; |
---|
| 120 | } |
---|
| 121 | |
---|
[569b363] | 122 | if (!PyAubio_ArrayToCFvec(input, &(self->freqs) )) { |
---|
[615ac7d] | 123 | return NULL; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | err = aubio_filterbank_set_triangle_bands (self->o, |
---|
[569b363] | 127 | &(self->freqs), samplerate); |
---|
[615ac7d] | 128 | if (err > 0) { |
---|
| 129 | PyErr_SetString (PyExc_ValueError, |
---|
| 130 | "error when setting filter to A-weighting"); |
---|
| 131 | return NULL; |
---|
| 132 | } |
---|
[9e6695d] | 133 | Py_RETURN_NONE; |
---|
[615ac7d] | 134 | } |
---|
| 135 | |
---|
[f6cbefe] | 136 | static PyObject * |
---|
[615ac7d] | 137 | Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) |
---|
| 138 | { |
---|
| 139 | uint_t err = 0; |
---|
| 140 | |
---|
| 141 | uint_t samplerate; |
---|
| 142 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
| 143 | return NULL; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); |
---|
| 147 | if (err > 0) { |
---|
| 148 | PyErr_SetString (PyExc_ValueError, |
---|
| 149 | "error when setting filter to A-weighting"); |
---|
| 150 | return NULL; |
---|
| 151 | } |
---|
[9e6695d] | 152 | Py_RETURN_NONE; |
---|
[615ac7d] | 153 | } |
---|
| 154 | |
---|
[f6cbefe] | 155 | static PyObject * |
---|
| 156 | Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args) |
---|
| 157 | { |
---|
| 158 | uint_t err = 0; |
---|
| 159 | |
---|
| 160 | PyObject *input; |
---|
| 161 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | |
---|
[569b363] | 165 | if (!PyAubio_ArrayToCFmat(input, &(self->coeffs))) { |
---|
[f6cbefe] | 166 | return NULL; |
---|
| 167 | } |
---|
| 168 | |
---|
[569b363] | 169 | err = aubio_filterbank_set_coeffs (self->o, &(self->coeffs)); |
---|
[f6cbefe] | 170 | |
---|
| 171 | if (err > 0) { |
---|
| 172 | PyErr_SetString (PyExc_ValueError, |
---|
| 173 | "error when setting filter coefficients"); |
---|
| 174 | return NULL; |
---|
| 175 | } |
---|
[9e6695d] | 176 | Py_RETURN_NONE; |
---|
[f6cbefe] | 177 | } |
---|
| 178 | |
---|
| 179 | static PyObject * |
---|
[615ac7d] | 180 | Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) |
---|
| 181 | { |
---|
[363ce7a] | 182 | return (PyObject *)PyAubio_CFmatToArray( |
---|
| 183 | aubio_filterbank_get_coeffs (self->o) ); |
---|
[615ac7d] | 184 | } |
---|
| 185 | |
---|
| 186 | static PyMethodDef Py_filterbank_methods[] = { |
---|
| 187 | {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, |
---|
| 188 | METH_VARARGS, "set coefficients of filterbanks"}, |
---|
| 189 | {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, |
---|
| 190 | METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, |
---|
| 191 | {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, |
---|
| 192 | METH_NOARGS, "get coefficients of filterbank"}, |
---|
[f6cbefe] | 193 | {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs, |
---|
| 194 | METH_VARARGS, "set coefficients of filterbank"}, |
---|
[615ac7d] | 195 | {NULL} |
---|
| 196 | }; |
---|
| 197 | |
---|
[5652a8c] | 198 | PyTypeObject Py_filterbankType = { |
---|
| 199 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 200 | "aubio.filterbank", |
---|
| 201 | sizeof (Py_filterbank), |
---|
| 202 | 0, |
---|
| 203 | (destructor) Py_filterbank_del, |
---|
| 204 | 0, |
---|
| 205 | 0, |
---|
| 206 | 0, |
---|
| 207 | 0, |
---|
| 208 | 0, |
---|
| 209 | 0, |
---|
| 210 | 0, |
---|
| 211 | 0, |
---|
| 212 | 0, |
---|
| 213 | (ternaryfunc)Py_filterbank_do, |
---|
| 214 | 0, |
---|
| 215 | 0, |
---|
| 216 | 0, |
---|
| 217 | 0, |
---|
| 218 | Py_TPFLAGS_DEFAULT, |
---|
| 219 | Py_filterbank_doc, |
---|
| 220 | 0, |
---|
| 221 | 0, |
---|
| 222 | 0, |
---|
| 223 | 0, |
---|
| 224 | 0, |
---|
| 225 | 0, |
---|
| 226 | Py_filterbank_methods, |
---|
| 227 | Py_filterbank_members, |
---|
| 228 | 0, |
---|
| 229 | 0, |
---|
| 230 | 0, |
---|
| 231 | 0, |
---|
| 232 | 0, |
---|
| 233 | 0, |
---|
| 234 | (initproc) Py_filterbank_init, |
---|
| 235 | 0, |
---|
| 236 | Py_filterbank_new, |
---|
| 237 | }; |
---|