1 #include "aubio-types.h"
3 static char Py_filterbank_doc[] = "filterbank object";
8 aubio_filterbank_t * o;
18 Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
20 int win_s = 0, n_filters = 0;
22 static char *kwlist[] = { "n_filters", "win_s", NULL };
24 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
25 &n_filters, &win_s)) {
29 self = (Py_filterbank *) type->tp_alloc (type, 0);
35 self->win_s = Py_default_vector_length;
38 } else if (win_s < 0) {
39 PyErr_SetString (PyExc_ValueError,
40 "can not use negative window size");
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");
53 return (PyObject *) self;
57 Py_filterbank_init (Py_filterbank * self, PyObject * args, PyObject * kwds)
59 self->o = new_aubio_filterbank (self->n_filters, self->win_s);
60 if (self->o == NULL) {
62 sprintf(errstr, "error creating filterbank with n_filters=%d, win_s=%d",
63 self->n_filters, self->win_s);
64 PyErr_SetString (PyExc_RuntimeError, errstr);
67 self->out = new_fvec(self->n_filters);
73 Py_filterbank_del (Py_filterbank *self, PyObject *unused)
75 del_aubio_filterbank(self->o);
77 free(self->coeffs.data);
78 Py_TYPE(self)->tp_free((PyObject *) self);
82 Py_filterbank_do(Py_filterbank * self, PyObject * args)
86 if (!PyArg_ParseTuple (args, "O", &input)) {
90 if (!PyAubio_PyCvecToCCvec(input, &(self->vec) )) {
94 // compute the function
95 aubio_filterbank_do (self->o, &(self->vec), self->out);
96 return (PyObject *)PyAubio_CFvecToArray(self->out);
99 static PyMemberDef Py_filterbank_members[] = {
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"},
104 {NULL} /* sentinel */
108 Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args)
114 if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) {
122 if (!PyAubio_ArrayToCFvec(input, &(self->freqs) )) {
126 err = aubio_filterbank_set_triangle_bands (self->o,
127 &(self->freqs), samplerate);
129 PyErr_SetString (PyExc_ValueError,
130 "error when setting filter to A-weighting");
137 Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args)
142 if (!PyArg_ParseTuple (args, "I", &samplerate)) {
146 err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate);
148 PyErr_SetString (PyExc_ValueError,
149 "error when setting filter to A-weighting");
156 Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args)
161 if (!PyArg_ParseTuple (args, "O", &input)) {
165 if (!PyAubio_ArrayToCFmat(input, &(self->coeffs))) {
169 err = aubio_filterbank_set_coeffs (self->o, &(self->coeffs));
172 PyErr_SetString (PyExc_ValueError,
173 "error when setting filter coefficients");
180 Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused)
182 return (PyObject *)PyAubio_CFmatToArray(
183 aubio_filterbank_get_coeffs (self->o) );
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"},
193 {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs,
194 METH_VARARGS, "set coefficients of filterbank"},
198 PyTypeObject Py_filterbankType = {
199 PyVarObject_HEAD_INIT (NULL, 0)
201 sizeof (Py_filterbank),
203 (destructor) Py_filterbank_del,
213 (ternaryfunc)Py_filterbank_do,
226 Py_filterbank_methods,
227 Py_filterbank_members,
234 (initproc) Py_filterbank_init,