1 | #include "aubio-types.h" |
---|
2 | |
---|
3 | typedef struct |
---|
4 | { |
---|
5 | PyObject_HEAD |
---|
6 | aubio_filter_t * o; |
---|
7 | uint_t order; |
---|
8 | fvec_t vec; |
---|
9 | PyObject *out; |
---|
10 | fvec_t c_out; |
---|
11 | } Py_filter; |
---|
12 | |
---|
13 | static char Py_filter_doc[] = "" |
---|
14 | "digital_filter(order=7)\n" |
---|
15 | "\n" |
---|
16 | "Create a digital filter.\n" |
---|
17 | ""; |
---|
18 | |
---|
19 | static char Py_filter_set_c_weighting_doc[] = "" |
---|
20 | "set_c_weighting(samplerate)\n" |
---|
21 | "\n" |
---|
22 | "Set filter coefficients to C-weighting.\n" |
---|
23 | "\n" |
---|
24 | "`samplerate` should be one of 8000, 11025, 16000, 22050, 24000, 32000,\n" |
---|
25 | "44100, 48000, 88200, 96000, or 192000. `order` of the filter should be 5.\n" |
---|
26 | "\n" |
---|
27 | "Parameters\n" |
---|
28 | "----------\n" |
---|
29 | "samplerate : int\n" |
---|
30 | " Sampling-rate of the input signal, in Hz.\n" |
---|
31 | ""; |
---|
32 | |
---|
33 | static char Py_filter_set_a_weighting_doc[] = "" |
---|
34 | "set_a_weighting(samplerate)\n" |
---|
35 | "\n" |
---|
36 | "Set filter coefficients to A-weighting.\n" |
---|
37 | "\n" |
---|
38 | "`samplerate` should be one of 8000, 11025, 16000, 22050, 24000, 32000,\n" |
---|
39 | "44100, 48000, 88200, 96000, or 192000. `order` of the filter should be 7.\n" |
---|
40 | "\n" |
---|
41 | "Parameters\n" |
---|
42 | "----------\n" |
---|
43 | "samplerate : int\n" |
---|
44 | " Sampling-rate of the input signal.\n" |
---|
45 | ""; |
---|
46 | |
---|
47 | static char Py_filter_set_biquad_doc[] = "" |
---|
48 | "set_biquad(b0, b1, b2, a1, a2)\n" |
---|
49 | "\n" |
---|
50 | "Set biquad coefficients. `order` of the filter should be 3.\n" |
---|
51 | "\n" |
---|
52 | "Parameters\n" |
---|
53 | "----------\n" |
---|
54 | "b0 : float\n" |
---|
55 | " Forward filter coefficient.\n" |
---|
56 | "b1 : float\n" |
---|
57 | " Forward filter coefficient.\n" |
---|
58 | "b2 : float\n" |
---|
59 | " Forward filter coefficient.\n" |
---|
60 | "a1 : float\n" |
---|
61 | " Feedback filter coefficient.\n" |
---|
62 | "a2 : float\n" |
---|
63 | " Feedback filter coefficient.\n" |
---|
64 | ""; |
---|
65 | |
---|
66 | static PyObject * |
---|
67 | Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
68 | { |
---|
69 | int order= 0; |
---|
70 | Py_filter *self; |
---|
71 | static char *kwlist[] = { "order", NULL }; |
---|
72 | |
---|
73 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
74 | &order)) { |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | self = (Py_filter *) type->tp_alloc (type, 0); |
---|
79 | |
---|
80 | if (self == NULL) { |
---|
81 | return NULL; |
---|
82 | } |
---|
83 | |
---|
84 | self->order = 7; |
---|
85 | |
---|
86 | if (order > 0) { |
---|
87 | self->order = order; |
---|
88 | } else if (order < 0) { |
---|
89 | PyErr_SetString (PyExc_ValueError, |
---|
90 | "can not use negative order"); |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | return (PyObject *) self; |
---|
95 | } |
---|
96 | |
---|
97 | static int |
---|
98 | Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) |
---|
99 | { |
---|
100 | self->o = new_aubio_filter (self->order); |
---|
101 | if (self->o == NULL) { |
---|
102 | return -1; |
---|
103 | } |
---|
104 | self->out = NULL; |
---|
105 | return 0; |
---|
106 | } |
---|
107 | |
---|
108 | static void |
---|
109 | Py_filter_del (Py_filter * self) |
---|
110 | { |
---|
111 | Py_XDECREF(self->out); |
---|
112 | del_aubio_filter (self->o); |
---|
113 | Py_TYPE(self)->tp_free ((PyObject *) self); |
---|
114 | } |
---|
115 | |
---|
116 | static PyObject * |
---|
117 | Py_filter_do(Py_filter * self, PyObject * args) |
---|
118 | { |
---|
119 | PyObject *input; |
---|
120 | |
---|
121 | if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) { |
---|
122 | return NULL; |
---|
123 | } |
---|
124 | |
---|
125 | if (input == NULL) { |
---|
126 | return NULL; |
---|
127 | } |
---|
128 | |
---|
129 | if (!PyAubio_ArrayToCFvec(input, &(self->vec))) { |
---|
130 | return NULL; |
---|
131 | } |
---|
132 | |
---|
133 | // initialize output now |
---|
134 | if (self->out == NULL) { |
---|
135 | self->out = new_py_fvec(self->vec.length); |
---|
136 | } |
---|
137 | |
---|
138 | Py_INCREF(self->out); |
---|
139 | if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out)) ) { |
---|
140 | return NULL; |
---|
141 | } |
---|
142 | // compute the function |
---|
143 | aubio_filter_do_outplace (self->o, &(self->vec), &(self->c_out)); |
---|
144 | return self->out; |
---|
145 | } |
---|
146 | |
---|
147 | static PyObject * |
---|
148 | Py_filter_set_c_weighting (Py_filter * self, PyObject *args) |
---|
149 | { |
---|
150 | uint_t err = 0; |
---|
151 | uint_t samplerate; |
---|
152 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
153 | return NULL; |
---|
154 | } |
---|
155 | |
---|
156 | err = aubio_filter_set_c_weighting (self->o, samplerate); |
---|
157 | if (err > 0) { |
---|
158 | PyErr_SetString (PyExc_ValueError, |
---|
159 | "error when setting filter to C-weighting"); |
---|
160 | return NULL; |
---|
161 | } |
---|
162 | Py_RETURN_NONE; |
---|
163 | } |
---|
164 | |
---|
165 | static PyObject * |
---|
166 | Py_filter_set_a_weighting (Py_filter * self, PyObject *args) |
---|
167 | { |
---|
168 | uint_t err = 0; |
---|
169 | uint_t samplerate; |
---|
170 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
171 | return NULL; |
---|
172 | } |
---|
173 | |
---|
174 | err = aubio_filter_set_a_weighting (self->o, samplerate); |
---|
175 | if (err > 0) { |
---|
176 | PyErr_SetString (PyExc_ValueError, |
---|
177 | "error when setting filter to A-weighting"); |
---|
178 | return NULL; |
---|
179 | } |
---|
180 | Py_RETURN_NONE; |
---|
181 | } |
---|
182 | |
---|
183 | static PyObject * |
---|
184 | Py_filter_set_biquad(Py_filter * self, PyObject *args) |
---|
185 | { |
---|
186 | uint_t err = 0; |
---|
187 | lsmp_t b0, b1, b2, a1, a2; |
---|
188 | if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) { |
---|
189 | return NULL; |
---|
190 | } |
---|
191 | |
---|
192 | err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2); |
---|
193 | if (err > 0) { |
---|
194 | PyErr_SetString (PyExc_ValueError, |
---|
195 | "error when setting filter with biquad coefficients"); |
---|
196 | return NULL; |
---|
197 | } |
---|
198 | Py_RETURN_NONE; |
---|
199 | } |
---|
200 | |
---|
201 | static PyMemberDef Py_filter_members[] = { |
---|
202 | // TODO remove READONLY flag and define getter/setter |
---|
203 | {"order", T_INT, offsetof (Py_filter, order), READONLY, |
---|
204 | "order of the filter"}, |
---|
205 | {NULL} /* Sentinel */ |
---|
206 | }; |
---|
207 | |
---|
208 | static PyMethodDef Py_filter_methods[] = { |
---|
209 | {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS, |
---|
210 | Py_filter_set_c_weighting_doc}, |
---|
211 | {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS, |
---|
212 | Py_filter_set_a_weighting_doc}, |
---|
213 | {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS, |
---|
214 | Py_filter_set_biquad_doc}, |
---|
215 | {NULL} |
---|
216 | }; |
---|
217 | |
---|
218 | PyTypeObject Py_filterType = { |
---|
219 | PyVarObject_HEAD_INIT(NULL, 0) |
---|
220 | "aubio.digital_filter", /* tp_name */ |
---|
221 | sizeof (Py_filter), /* tp_basicsize */ |
---|
222 | 0, /* tp_itemsize */ |
---|
223 | (destructor) Py_filter_del, /* tp_dealloc */ |
---|
224 | 0, /* tp_print */ |
---|
225 | 0, /* tp_getattr */ |
---|
226 | 0, /* tp_setattr */ |
---|
227 | 0, /* tp_compare */ |
---|
228 | 0, //(reprfunc) Py_filter_repr, /* tp_repr */ |
---|
229 | 0, /* tp_as_number */ |
---|
230 | 0, /* tp_as_sequence */ |
---|
231 | 0, /* tp_as_mapping */ |
---|
232 | 0, /* tp_hash */ |
---|
233 | (ternaryfunc)Py_filter_do, /* tp_call */ |
---|
234 | 0, /* tp_str */ |
---|
235 | 0, /* tp_getattro */ |
---|
236 | 0, /* tp_setattro */ |
---|
237 | 0, /* tp_as_buffer */ |
---|
238 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
239 | Py_filter_doc, /* tp_doc */ |
---|
240 | 0, /* tp_traverse */ |
---|
241 | 0, /* tp_clear */ |
---|
242 | 0, /* tp_richcompare */ |
---|
243 | 0, /* tp_weaklistoffset */ |
---|
244 | 0, /* tp_iter */ |
---|
245 | 0, /* tp_iternext */ |
---|
246 | Py_filter_methods, /* tp_methods */ |
---|
247 | Py_filter_members, /* tp_members */ |
---|
248 | 0, /* tp_getset */ |
---|
249 | 0, /* tp_base */ |
---|
250 | 0, /* tp_dict */ |
---|
251 | 0, /* tp_descr_get */ |
---|
252 | 0, /* tp_descr_set */ |
---|
253 | 0, /* tp_dictoffset */ |
---|
254 | (initproc) Py_filter_init, /* tp_init */ |
---|
255 | 0, /* tp_alloc */ |
---|
256 | Py_filter_new, /* tp_new */ |
---|
257 | 0, |
---|
258 | 0, |
---|
259 | 0, |
---|
260 | 0, |
---|
261 | 0, |
---|
262 | 0, |
---|
263 | 0, |
---|
264 | 0, |
---|
265 | 0, |
---|
266 | }; |
---|