1 #include "aubio-types.h"
13 static char Py_filter_doc[] = ""
14 "digital_filter(order=7)\n"
16 "Create a digital filter.\n"
19 static char Py_filter_set_c_weighting_doc[] = ""
20 "set_c_weighting(samplerate)\n"
22 "Set filter coefficients to C-weighting.\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"
30 " Sampling-rate of the input signal, in Hz.\n"
33 static char Py_filter_set_a_weighting_doc[] = ""
34 "set_a_weighting(samplerate)\n"
36 "Set filter coefficients to A-weighting.\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"
44 " Sampling-rate of the input signal.\n"
47 static char Py_filter_set_biquad_doc[] = ""
48 "set_biquad(b0, b1, b2, a1, a2)\n"
50 "Set biquad coefficients. `order` of the filter should be 3.\n"
55 " Forward filter coefficient.\n"
57 " Forward filter coefficient.\n"
59 " Forward filter coefficient.\n"
61 " Feedback filter coefficient.\n"
63 " Feedback filter coefficient.\n"
67 Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
71 static char *kwlist[] = { "order", NULL };
73 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
78 self = (Py_filter *) type->tp_alloc (type, 0);
88 } else if (order < 0) {
89 PyErr_SetString (PyExc_ValueError,
90 "can not use negative order");
94 return (PyObject *) self;
98 Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds)
100 self->o = new_aubio_filter (self->order);
101 if (self->o == NULL) {
109 Py_filter_del (Py_filter * self)
111 Py_XDECREF(self->out);
113 del_aubio_filter (self->o);
114 Py_TYPE(self)->tp_free ((PyObject *) self);
118 Py_filter_do(Py_filter * self, PyObject * args)
122 if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) {
130 if (!PyAubio_ArrayToCFvec(input, &(self->vec))) {
134 // initialize output now
135 if (self->out == NULL) {
136 self->out = new_py_fvec(self->vec.length);
139 Py_INCREF(self->out);
140 if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out)) ) {
143 // compute the function
144 aubio_filter_do_outplace (self->o, &(self->vec), &(self->c_out));
149 Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
153 if (!PyArg_ParseTuple (args, "I", &samplerate)) {
157 err = aubio_filter_set_c_weighting (self->o, samplerate);
159 if (PyErr_Occurred() == NULL) {
160 PyErr_SetString (PyExc_ValueError,
161 "error when setting filter to C-weighting");
163 // change the RuntimeError into ValueError
164 PyObject *type, *value, *traceback;
165 PyErr_Fetch(&type, &value, &traceback);
167 type = PyExc_ValueError;
169 PyErr_Restore(type, value, traceback);
177 Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
181 if (!PyArg_ParseTuple (args, "I", &samplerate)) {
185 err = aubio_filter_set_a_weighting (self->o, samplerate);
187 if (PyErr_Occurred() == NULL) {
188 PyErr_SetString (PyExc_ValueError,
189 "error when setting filter to A-weighting");
191 // change the RuntimeError into ValueError
192 PyObject *type, *value, *traceback;
193 PyErr_Fetch(&type, &value, &traceback);
195 type = PyExc_ValueError;
197 PyErr_Restore(type, value, traceback);
205 Py_filter_set_biquad(Py_filter * self, PyObject *args)
208 lsmp_t b0, b1, b2, a1, a2;
209 if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) {
213 err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2);
215 if (PyErr_Occurred() == NULL) {
216 PyErr_SetString (PyExc_ValueError,
217 "error when setting filter with biquad coefficients");
219 // change the RuntimeError into ValueError
220 PyObject *type, *value, *traceback;
221 PyErr_Fetch(&type, &value, &traceback);
223 type = PyExc_ValueError;
225 PyErr_Restore(type, value, traceback);
232 static PyMemberDef Py_filter_members[] = {
233 // TODO remove READONLY flag and define getter/setter
234 {"order", T_INT, offsetof (Py_filter, order), READONLY,
235 "order of the filter"},
236 {NULL} /* Sentinel */
239 static PyMethodDef Py_filter_methods[] = {
240 {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS,
241 Py_filter_set_c_weighting_doc},
242 {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS,
243 Py_filter_set_a_weighting_doc},
244 {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS,
245 Py_filter_set_biquad_doc},
249 PyTypeObject Py_filterType = {
250 PyVarObject_HEAD_INIT(NULL, 0)
251 "aubio.digital_filter", /* tp_name */
252 sizeof (Py_filter), /* tp_basicsize */
254 (destructor) Py_filter_del, /* tp_dealloc */
259 0, //(reprfunc) Py_filter_repr, /* tp_repr */
260 0, /* tp_as_number */
261 0, /* tp_as_sequence */
262 0, /* tp_as_mapping */
264 (ternaryfunc)Py_filter_do, /* tp_call */
268 0, /* tp_as_buffer */
269 Py_TPFLAGS_DEFAULT, /* tp_flags */
270 Py_filter_doc, /* tp_doc */
273 0, /* tp_richcompare */
274 0, /* tp_weaklistoffset */
277 Py_filter_methods, /* tp_methods */
278 Py_filter_members, /* tp_members */
282 0, /* tp_descr_get */
283 0, /* tp_descr_set */
284 0, /* tp_dictoffset */
285 (initproc) Py_filter_init, /* tp_init */
287 Py_filter_new, /* tp_new */