[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; |
---|
[b5bef11] | 11 | cvec_t *vec; |
---|
[59cb451] | 12 | fvec_t *out; |
---|
[b5bef11] | 13 | fvec_t *freqs; |
---|
| 14 | fmat_t *coeffs; |
---|
[59cb451] | 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 | |
---|
[b5bef11] | 69 | self->vec = (cvec_t *)malloc(sizeof(cvec_t)); |
---|
| 70 | |
---|
| 71 | self->freqs = (fvec_t *)malloc(sizeof(fvec_t)); |
---|
| 72 | |
---|
| 73 | self->coeffs = (fmat_t *)malloc(sizeof(fmat_t)); |
---|
| 74 | self->coeffs->data = (smpl_t **)malloc(sizeof(smpl_t*) * self->n_filters); |
---|
| 75 | self->coeffs->height = self->n_filters; |
---|
| 76 | |
---|
[59cb451] | 77 | return 0; |
---|
| 78 | } |
---|
[615ac7d] | 79 | |
---|
[59cb451] | 80 | static void |
---|
| 81 | Py_filterbank_del (Py_filterbank *self, PyObject *unused) |
---|
| 82 | { |
---|
| 83 | del_aubio_filterbank(self->o); |
---|
| 84 | del_fvec(self->out); |
---|
[b5bef11] | 85 | free(self->vec); |
---|
| 86 | free(self->freqs); |
---|
| 87 | free(self->coeffs->data); |
---|
| 88 | free(self->coeffs); |
---|
[5c1200a] | 89 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[59cb451] | 90 | } |
---|
[615ac7d] | 91 | |
---|
[f6cbefe] | 92 | static PyObject * |
---|
[615ac7d] | 93 | Py_filterbank_do(Py_filterbank * self, PyObject * args) |
---|
| 94 | { |
---|
| 95 | PyObject *input; |
---|
| 96 | |
---|
| 97 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 98 | return NULL; |
---|
| 99 | } |
---|
| 100 | |
---|
[b5bef11] | 101 | if (!PyAubio_ArrayToCCvec(input, self->vec)) { |
---|
[615ac7d] | 102 | return NULL; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // compute the function |
---|
[b5bef11] | 106 | aubio_filterbank_do (self->o, self->vec, self->out); |
---|
[59cb451] | 107 | return (PyObject *)PyAubio_CFvecToArray(self->out); |
---|
[615ac7d] | 108 | } |
---|
| 109 | |
---|
[5652a8c] | 110 | static PyMemberDef Py_filterbank_members[] = { |
---|
[615ac7d] | 111 | {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY, |
---|
| 112 | "size of the window"}, |
---|
| 113 | {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY, |
---|
| 114 | "number of filters"}, |
---|
[5652a8c] | 115 | {NULL} /* sentinel */ |
---|
| 116 | }; |
---|
[615ac7d] | 117 | |
---|
[f6cbefe] | 118 | static PyObject * |
---|
[615ac7d] | 119 | Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) |
---|
| 120 | { |
---|
| 121 | uint_t err = 0; |
---|
| 122 | |
---|
| 123 | PyObject *input; |
---|
| 124 | uint_t samplerate; |
---|
| 125 | if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { |
---|
| 126 | return NULL; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if (input == NULL) { |
---|
| 130 | return NULL; |
---|
| 131 | } |
---|
| 132 | |
---|
[b5bef11] | 133 | if (!PyAubio_ArrayToCFvec(input, self->freqs)) { |
---|
[615ac7d] | 134 | return NULL; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | err = aubio_filterbank_set_triangle_bands (self->o, |
---|
[b5bef11] | 138 | self->freqs, samplerate); |
---|
[615ac7d] | 139 | if (err > 0) { |
---|
| 140 | PyErr_SetString (PyExc_ValueError, |
---|
| 141 | "error when setting filter to A-weighting"); |
---|
| 142 | return NULL; |
---|
| 143 | } |
---|
[9e6695d] | 144 | Py_RETURN_NONE; |
---|
[615ac7d] | 145 | } |
---|
| 146 | |
---|
[f6cbefe] | 147 | static PyObject * |
---|
[615ac7d] | 148 | Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) |
---|
| 149 | { |
---|
| 150 | uint_t err = 0; |
---|
| 151 | |
---|
| 152 | uint_t samplerate; |
---|
| 153 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
| 154 | return NULL; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); |
---|
| 158 | if (err > 0) { |
---|
| 159 | PyErr_SetString (PyExc_ValueError, |
---|
| 160 | "error when setting filter to A-weighting"); |
---|
| 161 | return NULL; |
---|
| 162 | } |
---|
[9e6695d] | 163 | Py_RETURN_NONE; |
---|
[615ac7d] | 164 | } |
---|
| 165 | |
---|
[f6cbefe] | 166 | static PyObject * |
---|
| 167 | Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args) |
---|
| 168 | { |
---|
| 169 | uint_t err = 0; |
---|
| 170 | |
---|
| 171 | PyObject *input; |
---|
| 172 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
| 173 | return NULL; |
---|
| 174 | } |
---|
| 175 | |
---|
[b5bef11] | 176 | if (!PyAubio_ArrayToCFmat(input, self->coeffs)) { |
---|
[f6cbefe] | 177 | return NULL; |
---|
| 178 | } |
---|
| 179 | |
---|
[b5bef11] | 180 | err = aubio_filterbank_set_coeffs (self->o, self->coeffs); |
---|
[f6cbefe] | 181 | |
---|
| 182 | if (err > 0) { |
---|
| 183 | PyErr_SetString (PyExc_ValueError, |
---|
| 184 | "error when setting filter coefficients"); |
---|
| 185 | return NULL; |
---|
| 186 | } |
---|
[9e6695d] | 187 | Py_RETURN_NONE; |
---|
[f6cbefe] | 188 | } |
---|
| 189 | |
---|
| 190 | static PyObject * |
---|
[615ac7d] | 191 | Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) |
---|
| 192 | { |
---|
[363ce7a] | 193 | return (PyObject *)PyAubio_CFmatToArray( |
---|
| 194 | aubio_filterbank_get_coeffs (self->o) ); |
---|
[615ac7d] | 195 | } |
---|
| 196 | |
---|
| 197 | static PyMethodDef Py_filterbank_methods[] = { |
---|
| 198 | {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, |
---|
| 199 | METH_VARARGS, "set coefficients of filterbanks"}, |
---|
| 200 | {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, |
---|
| 201 | METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, |
---|
| 202 | {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, |
---|
| 203 | METH_NOARGS, "get coefficients of filterbank"}, |
---|
[f6cbefe] | 204 | {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs, |
---|
| 205 | METH_VARARGS, "set coefficients of filterbank"}, |
---|
[615ac7d] | 206 | {NULL} |
---|
| 207 | }; |
---|
| 208 | |
---|
[5652a8c] | 209 | PyTypeObject Py_filterbankType = { |
---|
| 210 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 211 | "aubio.filterbank", |
---|
| 212 | sizeof (Py_filterbank), |
---|
| 213 | 0, |
---|
| 214 | (destructor) Py_filterbank_del, |
---|
| 215 | 0, |
---|
| 216 | 0, |
---|
| 217 | 0, |
---|
| 218 | 0, |
---|
| 219 | 0, |
---|
| 220 | 0, |
---|
| 221 | 0, |
---|
| 222 | 0, |
---|
| 223 | 0, |
---|
| 224 | (ternaryfunc)Py_filterbank_do, |
---|
| 225 | 0, |
---|
| 226 | 0, |
---|
| 227 | 0, |
---|
| 228 | 0, |
---|
| 229 | Py_TPFLAGS_DEFAULT, |
---|
| 230 | Py_filterbank_doc, |
---|
| 231 | 0, |
---|
| 232 | 0, |
---|
| 233 | 0, |
---|
| 234 | 0, |
---|
| 235 | 0, |
---|
| 236 | 0, |
---|
| 237 | Py_filterbank_methods, |
---|
| 238 | Py_filterbank_members, |
---|
| 239 | 0, |
---|
| 240 | 0, |
---|
| 241 | 0, |
---|
| 242 | 0, |
---|
| 243 | 0, |
---|
| 244 | 0, |
---|
| 245 | (initproc) Py_filterbank_init, |
---|
| 246 | 0, |
---|
| 247 | Py_filterbank_new, |
---|
| 248 | }; |
---|