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->channels = vec->channels; |
---|
71 | output->length = self->n_filters; |
---|
72 | output->o = new_fvec(self->n_filters, vec->channels); |
---|
73 | |
---|
74 | // compute the function |
---|
75 | aubio_filterbank_do (self->o, vec->o, output->o); |
---|
76 | return (PyObject *)PyAubio_FvecToArray(output); |
---|
77 | } |
---|
78 | |
---|
79 | AUBIO_MEMBERS_START(filterbank) |
---|
80 | {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY, |
---|
81 | "size of the window"}, |
---|
82 | {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY, |
---|
83 | "number of filters"}, |
---|
84 | AUBIO_MEMBERS_STOP(filterbank) |
---|
85 | |
---|
86 | static PyObject * |
---|
87 | Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) |
---|
88 | { |
---|
89 | uint_t err = 0; |
---|
90 | |
---|
91 | PyObject *input; |
---|
92 | uint_t samplerate; |
---|
93 | Py_fvec *freqs; |
---|
94 | if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) { |
---|
95 | return NULL; |
---|
96 | } |
---|
97 | |
---|
98 | if (input == NULL) { |
---|
99 | return NULL; |
---|
100 | } |
---|
101 | |
---|
102 | freqs = PyAubio_ArrayToFvec (input); |
---|
103 | |
---|
104 | if (freqs == NULL) { |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | |
---|
108 | err = aubio_filterbank_set_triangle_bands (self->o, |
---|
109 | freqs->o, samplerate); |
---|
110 | if (err > 0) { |
---|
111 | PyErr_SetString (PyExc_ValueError, |
---|
112 | "error when setting filter to A-weighting"); |
---|
113 | return NULL; |
---|
114 | } |
---|
115 | return Py_None; |
---|
116 | } |
---|
117 | |
---|
118 | static PyObject * |
---|
119 | Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) |
---|
120 | { |
---|
121 | uint_t err = 0; |
---|
122 | |
---|
123 | uint_t samplerate; |
---|
124 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
125 | return NULL; |
---|
126 | } |
---|
127 | |
---|
128 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); |
---|
129 | if (err > 0) { |
---|
130 | PyErr_SetString (PyExc_ValueError, |
---|
131 | "error when setting filter to A-weighting"); |
---|
132 | return NULL; |
---|
133 | } |
---|
134 | return Py_None; |
---|
135 | } |
---|
136 | |
---|
137 | static PyObject * |
---|
138 | Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) |
---|
139 | { |
---|
140 | Py_fvec *output = (Py_fvec *) PyObject_New (Py_fvec, &Py_fvecType); |
---|
141 | output->channels = self->n_filters; |
---|
142 | output->length = self->win_s / 2 + 1; |
---|
143 | output->o = aubio_filterbank_get_coeffs (self->o); |
---|
144 | return (PyObject *)PyAubio_FvecToArray(output); |
---|
145 | } |
---|
146 | |
---|
147 | static PyMethodDef Py_filterbank_methods[] = { |
---|
148 | {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, |
---|
149 | METH_VARARGS, "set coefficients of filterbanks"}, |
---|
150 | {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, |
---|
151 | METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, |
---|
152 | {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, |
---|
153 | METH_NOARGS, "get coefficients of filterbank"}, |
---|
154 | {NULL} |
---|
155 | }; |
---|
156 | |
---|
157 | AUBIO_TYPEOBJECT(filterbank, "aubio.filterbank") |
---|