1 #include "aubio-types.h"
3 static char Py_filterbank_doc[] = "filterbank object";
8 aubio_filterbank_t * o;
19 Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
21 int win_s = 0, n_filters = 0;
23 static char *kwlist[] = { "n_filters", "win_s", NULL };
25 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
26 &n_filters, &win_s)) {
30 self = (Py_filterbank *) type->tp_alloc (type, 0);
36 self->win_s = Py_default_vector_length;
39 } else if (win_s < 0) {
40 PyErr_SetString (PyExc_ValueError,
41 "can not use negative window size");
47 self->n_filters = n_filters;
48 } else if (n_filters < 0) {
49 PyErr_SetString (PyExc_ValueError,
50 "can not use negative number of filters");
54 return (PyObject *) self;
58 Py_filterbank_init (Py_filterbank * self, PyObject * args, PyObject * kwds)
60 self->o = new_aubio_filterbank (self->n_filters, self->win_s);
61 if (self->o == NULL) {
63 sprintf(errstr, "error creating filterbank with n_filters=%d, win_s=%d",
64 self->n_filters, self->win_s);
65 PyErr_SetString (PyExc_RuntimeError, errstr);
68 self->out = new_py_fvec(self->n_filters);
74 Py_filterbank_del (Py_filterbank *self, PyObject *unused)
76 del_aubio_filterbank(self->o);
78 free(self->coeffs.data);
79 Py_TYPE(self)->tp_free((PyObject *) self);
83 Py_filterbank_do(Py_filterbank * self, PyObject * args)
87 if (!PyArg_ParseTuple (args, "O", &input)) {
91 if (!PyAubio_PyCvecToCCvec(input, &(self->vec) )) {
95 if (self->vec.length != self->win_s / 2 + 1) {
96 PyErr_Format(PyExc_ValueError,
97 "input cvec has length %d, but fft expects length %d",
98 self->vec.length, self->win_s / 2 + 1);
102 Py_INCREF(self->out);
103 if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out))) {
106 // compute the function
107 aubio_filterbank_do (self->o, &(self->vec), &(self->c_out));
111 static PyMemberDef Py_filterbank_members[] = {
112 {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY,
113 "size of the window"},
114 {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY,
115 "number of filters"},
116 {NULL} /* sentinel */
120 Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args)
126 if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) {
134 if (!PyAubio_ArrayToCFvec(input, &(self->freqs) )) {
138 err = aubio_filterbank_set_triangle_bands (self->o,
139 &(self->freqs), samplerate);
141 PyErr_SetString (PyExc_ValueError,
142 "error when setting filter to A-weighting");
149 Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args)
154 if (!PyArg_ParseTuple (args, "I", &samplerate)) {
158 err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate);
160 PyErr_SetString (PyExc_ValueError,
161 "error when setting filter to A-weighting");
168 Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args)
173 if (!PyArg_ParseTuple (args, "O", &input)) {
177 if (!PyAubio_ArrayToCFmat(input, &(self->coeffs))) {
181 err = aubio_filterbank_set_coeffs (self->o, &(self->coeffs));
184 PyErr_SetString (PyExc_ValueError,
185 "error when setting filter coefficients");
192 Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused)
194 return (PyObject *)PyAubio_CFmatToArray(
195 aubio_filterbank_get_coeffs (self->o) );
198 static PyMethodDef Py_filterbank_methods[] = {
199 {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands,
200 METH_VARARGS, "set coefficients of filterbanks"},
201 {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney,
202 METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"},
203 {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs,
204 METH_NOARGS, "get coefficients of filterbank"},
205 {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs,
206 METH_VARARGS, "set coefficients of filterbank"},
210 PyTypeObject Py_filterbankType = {
211 PyVarObject_HEAD_INIT (NULL, 0)
213 sizeof (Py_filterbank),
215 (destructor) Py_filterbank_del,
225 (ternaryfunc)Py_filterbank_do,
238 Py_filterbank_methods,
239 Py_filterbank_members,
246 (initproc) Py_filterbank_init,