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);
112 del_aubio_filter (self->o);
113 Py_TYPE(self)->tp_free ((PyObject *) self);
117 Py_filter_do(Py_filter * self, PyObject * args)
121 if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) {
129 if (!PyAubio_ArrayToCFvec(input, &(self->vec))) {
133 // initialize output now
134 if (self->out == NULL) {
135 self->out = new_py_fvec(self->vec.length);
138 Py_INCREF(self->out);
139 if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out)) ) {
142 // compute the function
143 aubio_filter_do_outplace (self->o, &(self->vec), &(self->c_out));
148 Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
152 if (!PyArg_ParseTuple (args, "I", &samplerate)) {
156 err = aubio_filter_set_c_weighting (self->o, samplerate);
158 PyErr_SetString (PyExc_ValueError,
159 "error when setting filter to C-weighting");
166 Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
170 if (!PyArg_ParseTuple (args, "I", &samplerate)) {
174 err = aubio_filter_set_a_weighting (self->o, samplerate);
176 PyErr_SetString (PyExc_ValueError,
177 "error when setting filter to A-weighting");
184 Py_filter_set_biquad(Py_filter * self, PyObject *args)
187 lsmp_t b0, b1, b2, a1, a2;
188 if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) {
192 err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2);
194 PyErr_SetString (PyExc_ValueError,
195 "error when setting filter with biquad coefficients");
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 */
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},
218 PyTypeObject Py_filterType = {
219 PyVarObject_HEAD_INIT(NULL, 0)
220 "aubio.digital_filter", /* tp_name */
221 sizeof (Py_filter), /* tp_basicsize */
223 (destructor) Py_filter_del, /* tp_dealloc */
228 0, //(reprfunc) Py_filter_repr, /* tp_repr */
229 0, /* tp_as_number */
230 0, /* tp_as_sequence */
231 0, /* tp_as_mapping */
233 (ternaryfunc)Py_filter_do, /* tp_call */
237 0, /* tp_as_buffer */
238 Py_TPFLAGS_DEFAULT, /* tp_flags */
239 Py_filter_doc, /* tp_doc */
242 0, /* tp_richcompare */
243 0, /* tp_weaklistoffset */
246 Py_filter_methods, /* tp_methods */
247 Py_filter_members, /* tp_members */
251 0, /* tp_descr_get */
252 0, /* tp_descr_set */
253 0, /* tp_dictoffset */
254 (initproc) Py_filter_init, /* tp_init */
256 Py_filter_new, /* tp_new */