[f826349] | 1 | #include "aubio-types.h" |
---|
| 2 | |
---|
| 3 | typedef struct |
---|
| 4 | { |
---|
| 5 | PyObject_HEAD |
---|
| 6 | aubio_filter_t * o; |
---|
| 7 | uint_t order; |
---|
[569b363] | 8 | fvec_t vec; |
---|
[21e8408] | 9 | PyObject *out; |
---|
| 10 | fvec_t c_out; |
---|
[f826349] | 11 | } Py_filter; |
---|
| 12 | |
---|
[1030a7b] | 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 | ""; |
---|
[f826349] | 65 | |
---|
| 66 | static PyObject * |
---|
| 67 | Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 68 | { |
---|
[96fe713] | 69 | int order= 0; |
---|
[f826349] | 70 | Py_filter *self; |
---|
[96fe713] | 71 | static char *kwlist[] = { "order", NULL }; |
---|
[f826349] | 72 | |
---|
[a52d3ae] | 73 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
[96fe713] | 74 | &order)) { |
---|
[f826349] | 75 | return NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | self = (Py_filter *) type->tp_alloc (type, 0); |
---|
| 79 | |
---|
| 80 | if (self == NULL) { |
---|
| 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | |
---|
[615ac7d] | 84 | self->order = 7; |
---|
[f826349] | 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 | { |
---|
[96fe713] | 100 | self->o = new_aubio_filter (self->order); |
---|
[f826349] | 101 | if (self->o == NULL) { |
---|
| 102 | return -1; |
---|
| 103 | } |
---|
[21e8408] | 104 | self->out = NULL; |
---|
[f826349] | 105 | return 0; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | static void |
---|
| 109 | Py_filter_del (Py_filter * self) |
---|
| 110 | { |
---|
[21e8408] | 111 | Py_XDECREF(self->out); |
---|
[a9f463c] | 112 | if (self->o) |
---|
| 113 | del_aubio_filter (self->o); |
---|
[770b9e7] | 114 | Py_TYPE(self)->tp_free ((PyObject *) self); |
---|
[f826349] | 115 | } |
---|
| 116 | |
---|
[21e8408] | 117 | static PyObject * |
---|
[a52d3ae] | 118 | Py_filter_do(Py_filter * self, PyObject * args) |
---|
[f826349] | 119 | { |
---|
| 120 | PyObject *input; |
---|
| 121 | |
---|
| 122 | if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) { |
---|
| 123 | return NULL; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | if (input == NULL) { |
---|
| 127 | return NULL; |
---|
| 128 | } |
---|
| 129 | |
---|
[569b363] | 130 | if (!PyAubio_ArrayToCFvec(input, &(self->vec))) { |
---|
[f826349] | 131 | return NULL; |
---|
| 132 | } |
---|
| 133 | |
---|
[21e8408] | 134 | // initialize output now |
---|
| 135 | if (self->out == NULL) { |
---|
| 136 | self->out = new_py_fvec(self->vec.length); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | Py_INCREF(self->out); |
---|
| 140 | if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out)) ) { |
---|
| 141 | return NULL; |
---|
[7c785e6] | 142 | } |
---|
[f826349] | 143 | // compute the function |
---|
[21e8408] | 144 | aubio_filter_do_outplace (self->o, &(self->vec), &(self->c_out)); |
---|
| 145 | return self->out; |
---|
[f826349] | 146 | } |
---|
| 147 | |
---|
[21e8408] | 148 | static PyObject * |
---|
[615ac7d] | 149 | Py_filter_set_c_weighting (Py_filter * self, PyObject *args) |
---|
[f826349] | 150 | { |
---|
[615ac7d] | 151 | uint_t err = 0; |
---|
| 152 | uint_t samplerate; |
---|
| 153 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
| 154 | return NULL; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | err = aubio_filter_set_c_weighting (self->o, samplerate); |
---|
[f826349] | 158 | if (err > 0) { |
---|
[8bfef30] | 159 | if (PyErr_Occurred() == NULL) { |
---|
| 160 | PyErr_SetString (PyExc_ValueError, |
---|
| 161 | "error when setting filter to C-weighting"); |
---|
[11c46c8] | 162 | } else { |
---|
| 163 | // change the RuntimeError into ValueError |
---|
| 164 | PyObject *type, *value, *traceback; |
---|
| 165 | PyErr_Fetch(&type, &value, &traceback); |
---|
[673d7e3] | 166 | Py_XDECREF(type); |
---|
| 167 | type = PyExc_ValueError; |
---|
| 168 | Py_XINCREF(type); |
---|
| 169 | PyErr_Restore(type, value, traceback); |
---|
[8bfef30] | 170 | } |
---|
[f826349] | 171 | return NULL; |
---|
| 172 | } |
---|
[9e6695d] | 173 | Py_RETURN_NONE; |
---|
[f826349] | 174 | } |
---|
| 175 | |
---|
[21e8408] | 176 | static PyObject * |
---|
[615ac7d] | 177 | Py_filter_set_a_weighting (Py_filter * self, PyObject *args) |
---|
[f826349] | 178 | { |
---|
[615ac7d] | 179 | uint_t err = 0; |
---|
| 180 | uint_t samplerate; |
---|
| 181 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
| 182 | return NULL; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | err = aubio_filter_set_a_weighting (self->o, samplerate); |
---|
[f826349] | 186 | if (err > 0) { |
---|
[8bfef30] | 187 | if (PyErr_Occurred() == NULL) { |
---|
| 188 | PyErr_SetString (PyExc_ValueError, |
---|
| 189 | "error when setting filter to A-weighting"); |
---|
[11c46c8] | 190 | } else { |
---|
| 191 | // change the RuntimeError into ValueError |
---|
| 192 | PyObject *type, *value, *traceback; |
---|
| 193 | PyErr_Fetch(&type, &value, &traceback); |
---|
[673d7e3] | 194 | Py_XDECREF(type); |
---|
| 195 | type = PyExc_ValueError; |
---|
| 196 | Py_XINCREF(type); |
---|
| 197 | PyErr_Restore(type, value, traceback); |
---|
[8bfef30] | 198 | } |
---|
[f826349] | 199 | return NULL; |
---|
| 200 | } |
---|
[9e6695d] | 201 | Py_RETURN_NONE; |
---|
[f826349] | 202 | } |
---|
| 203 | |
---|
[a81c8cd] | 204 | static PyObject * |
---|
| 205 | Py_filter_set_biquad(Py_filter * self, PyObject *args) |
---|
| 206 | { |
---|
| 207 | uint_t err = 0; |
---|
| 208 | lsmp_t b0, b1, b2, a1, a2; |
---|
| 209 | if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) { |
---|
| 210 | return NULL; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2); |
---|
| 214 | if (err > 0) { |
---|
[8bfef30] | 215 | if (PyErr_Occurred() == NULL) { |
---|
| 216 | PyErr_SetString (PyExc_ValueError, |
---|
| 217 | "error when setting filter with biquad coefficients"); |
---|
[11c46c8] | 218 | } else { |
---|
| 219 | // change the RuntimeError into ValueError |
---|
| 220 | PyObject *type, *value, *traceback; |
---|
| 221 | PyErr_Fetch(&type, &value, &traceback); |
---|
[673d7e3] | 222 | Py_XDECREF(type); |
---|
| 223 | type = PyExc_ValueError; |
---|
| 224 | Py_XINCREF(type); |
---|
| 225 | PyErr_Restore(type, value, traceback); |
---|
[8bfef30] | 226 | } |
---|
[a81c8cd] | 227 | return NULL; |
---|
| 228 | } |
---|
[9e6695d] | 229 | Py_RETURN_NONE; |
---|
[a81c8cd] | 230 | } |
---|
| 231 | |
---|
[f826349] | 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 */ |
---|
| 237 | }; |
---|
| 238 | |
---|
| 239 | static PyMethodDef Py_filter_methods[] = { |
---|
[07bf04e6] | 240 | {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS, |
---|
[1030a7b] | 241 | Py_filter_set_c_weighting_doc}, |
---|
[07bf04e6] | 242 | {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS, |
---|
[1030a7b] | 243 | Py_filter_set_a_weighting_doc}, |
---|
[a81c8cd] | 244 | {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS, |
---|
[1030a7b] | 245 | Py_filter_set_biquad_doc}, |
---|
[f826349] | 246 | {NULL} |
---|
| 247 | }; |
---|
| 248 | |
---|
| 249 | PyTypeObject Py_filterType = { |
---|
[5c1200a] | 250 | PyVarObject_HEAD_INIT(NULL, 0) |
---|
[f826349] | 251 | "aubio.digital_filter", /* tp_name */ |
---|
| 252 | sizeof (Py_filter), /* tp_basicsize */ |
---|
| 253 | 0, /* tp_itemsize */ |
---|
| 254 | (destructor) Py_filter_del, /* tp_dealloc */ |
---|
| 255 | 0, /* tp_print */ |
---|
| 256 | 0, /* tp_getattr */ |
---|
| 257 | 0, /* tp_setattr */ |
---|
| 258 | 0, /* tp_compare */ |
---|
| 259 | 0, //(reprfunc) Py_filter_repr, /* tp_repr */ |
---|
| 260 | 0, /* tp_as_number */ |
---|
| 261 | 0, /* tp_as_sequence */ |
---|
| 262 | 0, /* tp_as_mapping */ |
---|
| 263 | 0, /* tp_hash */ |
---|
| 264 | (ternaryfunc)Py_filter_do, /* tp_call */ |
---|
| 265 | 0, /* tp_str */ |
---|
| 266 | 0, /* tp_getattro */ |
---|
| 267 | 0, /* tp_setattro */ |
---|
| 268 | 0, /* tp_as_buffer */ |
---|
| 269 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
| 270 | Py_filter_doc, /* tp_doc */ |
---|
| 271 | 0, /* tp_traverse */ |
---|
| 272 | 0, /* tp_clear */ |
---|
| 273 | 0, /* tp_richcompare */ |
---|
| 274 | 0, /* tp_weaklistoffset */ |
---|
| 275 | 0, /* tp_iter */ |
---|
| 276 | 0, /* tp_iternext */ |
---|
| 277 | Py_filter_methods, /* tp_methods */ |
---|
| 278 | Py_filter_members, /* tp_members */ |
---|
| 279 | 0, /* tp_getset */ |
---|
| 280 | 0, /* tp_base */ |
---|
| 281 | 0, /* tp_dict */ |
---|
| 282 | 0, /* tp_descr_get */ |
---|
| 283 | 0, /* tp_descr_set */ |
---|
| 284 | 0, /* tp_dictoffset */ |
---|
| 285 | (initproc) Py_filter_init, /* tp_init */ |
---|
| 286 | 0, /* tp_alloc */ |
---|
| 287 | Py_filter_new, /* tp_new */ |
---|
[0e70ef9] | 288 | 0, |
---|
| 289 | 0, |
---|
| 290 | 0, |
---|
| 291 | 0, |
---|
| 292 | 0, |
---|
| 293 | 0, |
---|
| 294 | 0, |
---|
| 295 | 0, |
---|
| 296 | 0, |
---|
[f826349] | 297 | }; |
---|