1 | #include "aubiowraphell.h" |
---|
2 | |
---|
3 | static char Py_filterbank_doc[] = "filterbank object"; |
---|
4 | |
---|
5 | AUBIO_DECLARE(filterbank, uint_t n_filters; uint_t win_s) |
---|
6 | |
---|
7 | //AUBIO_NEW(filterbank) |
---|
8 | static PyObject * |
---|
9 | Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
10 | { |
---|
11 | int win_s = 0, n_filters = 0; |
---|
12 | Py_filterbank *self; |
---|
13 | static char *kwlist[] = { "n_filters", "win_s", NULL }; |
---|
14 | |
---|
15 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, |
---|
16 | &n_filters, &win_s)) { |
---|
17 | return NULL; |
---|
18 | } |
---|
19 | |
---|
20 | self = (Py_filterbank *) type->tp_alloc (type, 0); |
---|
21 | |
---|
22 | if (self == NULL) { |
---|
23 | return NULL; |
---|
24 | } |
---|
25 | |
---|
26 | self->win_s = Py_default_vector_length; |
---|
27 | if (win_s > 0) { |
---|
28 | self->win_s = win_s; |
---|
29 | } else if (win_s < 0) { |
---|
30 | PyErr_SetString (PyExc_ValueError, |
---|
31 | "can not use negative window size"); |
---|
32 | return NULL; |
---|
33 | } |
---|
34 | |
---|
35 | self->n_filters = 40; |
---|
36 | if (n_filters > 0) { |
---|
37 | self->n_filters = n_filters; |
---|
38 | } else if (n_filters < 0) { |
---|
39 | PyErr_SetString (PyExc_ValueError, |
---|
40 | "can not use negative number of filters"); |
---|
41 | return NULL; |
---|
42 | } |
---|
43 | |
---|
44 | return (PyObject *) self; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | AUBIO_INIT(filterbank, self->n_filters, self->win_s) |
---|
49 | |
---|
50 | AUBIO_DEL(filterbank) |
---|
51 | |
---|
52 | static PyObject * |
---|
53 | Py_filterbank_do(Py_filterbank * self, PyObject * args) |
---|
54 | { |
---|
55 | PyObject *input; |
---|
56 | Py_cvec *vec; |
---|
57 | Py_fvec *output; |
---|
58 | |
---|
59 | if (!PyArg_ParseTuple (args, "O", &input)) { |
---|
60 | return NULL; |
---|
61 | } |
---|
62 | |
---|
63 | vec = PyAubio_ArrayToCvec (input); |
---|
64 | |
---|
65 | if (vec == NULL) { |
---|
66 | return NULL; |
---|
67 | } |
---|
68 | |
---|
69 | output = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType); |
---|
70 | output->length = self->n_filters; |
---|
71 | output->o = new_fvec(self->n_filters); |
---|
72 | |
---|
73 | // compute the function |
---|
74 | aubio_filterbank_do (self->o, vec->o, output->o); |
---|
75 | return (PyObject *)PyAubio_FvecToArray(output); |
---|
76 | } |
---|
77 | |
---|
78 | AUBIO_MEMBERS_START(filterbank) |
---|
79 | {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY, |
---|
80 | "size of the window"}, |
---|
81 | {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY, |
---|
82 | "number of filters"}, |
---|
83 | AUBIO_MEMBERS_STOP(filterbank) |
---|
84 | |
---|
85 | static PyObject * |
---|
86 | Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) |
---|
87 | { |
---|
88 | uint_t err = 0; |
---|
89 | |
---|
90 | PyObject *input; |
---|
91 | uint_t samplerate; |
---|
92 | Py_fvec *freqs; |
---|
93 | if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { |
---|
94 | return NULL; |
---|
95 | } |
---|
96 | |
---|
97 | if (input == NULL) { |
---|
98 | return NULL; |
---|
99 | } |
---|
100 | |
---|
101 | freqs = PyAubio_ArrayToFvec (input); |
---|
102 | |
---|
103 | if (freqs == NULL) { |
---|
104 | return NULL; |
---|
105 | } |
---|
106 | |
---|
107 | err = aubio_filterbank_set_triangle_bands (self->o, |
---|
108 | freqs->o, samplerate); |
---|
109 | if (err > 0) { |
---|
110 | PyErr_SetString (PyExc_ValueError, |
---|
111 | "error when setting filter to A-weighting"); |
---|
112 | return NULL; |
---|
113 | } |
---|
114 | return Py_None; |
---|
115 | } |
---|
116 | |
---|
117 | static PyObject * |
---|
118 | Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) |
---|
119 | { |
---|
120 | uint_t err = 0; |
---|
121 | |
---|
122 | uint_t samplerate; |
---|
123 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
124 | return NULL; |
---|
125 | } |
---|
126 | |
---|
127 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); |
---|
128 | if (err > 0) { |
---|
129 | PyErr_SetString (PyExc_ValueError, |
---|
130 | "error when setting filter to A-weighting"); |
---|
131 | return NULL; |
---|
132 | } |
---|
133 | return Py_None; |
---|
134 | } |
---|
135 | |
---|
136 | static PyObject * |
---|
137 | Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) |
---|
138 | { |
---|
139 | Py_fmat *output = (Py_fmat *) PyObject_New (Py_fmat, &Py_fvecType); |
---|
140 | output->o = aubio_filterbank_get_coeffs (self->o); |
---|
141 | return (PyObject *)PyAubio_FmatToArray(output); |
---|
142 | } |
---|
143 | |
---|
144 | static PyMethodDef Py_filterbank_methods[] = { |
---|
145 | {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, |
---|
146 | METH_VARARGS, "set coefficients of filterbanks"}, |
---|
147 | {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, |
---|
148 | METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, |
---|
149 | {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, |
---|
150 | METH_NOARGS, "get coefficients of filterbank"}, |
---|
151 | {NULL} |
---|
152 | }; |
---|
153 | |
---|
154 | AUBIO_TYPEOBJECT(filterbank, "aubio.filterbank") |
---|