[f826349] | 1 | #include "aubio-types.h" |
---|
| 2 | |
---|
| 3 | typedef struct |
---|
| 4 | { |
---|
| 5 | PyObject_HEAD |
---|
| 6 | aubio_filter_t * o; |
---|
| 7 | uint_t order; |
---|
| 8 | } Py_filter; |
---|
| 9 | |
---|
| 10 | static char Py_filter_doc[] = "filter object"; |
---|
| 11 | |
---|
| 12 | static PyObject * |
---|
| 13 | Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 14 | { |
---|
[96fe713] | 15 | int order= 0; |
---|
[f826349] | 16 | Py_filter *self; |
---|
[96fe713] | 17 | static char *kwlist[] = { "order", NULL }; |
---|
[f826349] | 18 | |
---|
[a52d3ae] | 19 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
[96fe713] | 20 | &order)) { |
---|
[f826349] | 21 | return NULL; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | self = (Py_filter *) type->tp_alloc (type, 0); |
---|
| 25 | |
---|
| 26 | if (self == NULL) { |
---|
| 27 | return NULL; |
---|
| 28 | } |
---|
| 29 | |
---|
[615ac7d] | 30 | self->order = 7; |
---|
[f826349] | 31 | |
---|
| 32 | if (order > 0) { |
---|
| 33 | self->order = order; |
---|
| 34 | } else if (order < 0) { |
---|
| 35 | PyErr_SetString (PyExc_ValueError, |
---|
| 36 | "can not use negative order"); |
---|
| 37 | return NULL; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | return (PyObject *) self; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | static int |
---|
| 44 | Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) |
---|
| 45 | { |
---|
[96fe713] | 46 | self->o = new_aubio_filter (self->order); |
---|
[f826349] | 47 | if (self->o == NULL) { |
---|
| 48 | return -1; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | return 0; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | static void |
---|
| 55 | Py_filter_del (Py_filter * self) |
---|
| 56 | { |
---|
| 57 | del_aubio_filter (self->o); |
---|
| 58 | self->ob_type->tp_free ((PyObject *) self); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | static PyObject * |
---|
[a52d3ae] | 62 | Py_filter_do(Py_filter * self, PyObject * args) |
---|
[f826349] | 63 | { |
---|
| 64 | PyObject *input; |
---|
[a52d3ae] | 65 | fvec_t *vec; |
---|
[f826349] | 66 | |
---|
| 67 | if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) { |
---|
| 68 | return NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (input == NULL) { |
---|
| 72 | return NULL; |
---|
| 73 | } |
---|
| 74 | |
---|
[a52d3ae] | 75 | vec = PyAubio_ArrayToCFvec (input); |
---|
[f826349] | 76 | |
---|
| 77 | if (vec == NULL) { |
---|
| 78 | return NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // compute the function |
---|
[a52d3ae] | 82 | fvec_t * out = new_fvec(vec->length); |
---|
| 83 | aubio_filter_do_outplace (self->o, vec, out); |
---|
| 84 | return PyAubio_CFvecToArray(out); |
---|
[f826349] | 85 | } |
---|
| 86 | |
---|
| 87 | static PyObject * |
---|
[615ac7d] | 88 | Py_filter_set_c_weighting (Py_filter * self, PyObject *args) |
---|
[f826349] | 89 | { |
---|
[615ac7d] | 90 | uint_t err = 0; |
---|
| 91 | uint_t samplerate; |
---|
| 92 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
| 93 | return NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | err = aubio_filter_set_c_weighting (self->o, samplerate); |
---|
[f826349] | 97 | if (err > 0) { |
---|
| 98 | PyErr_SetString (PyExc_ValueError, |
---|
[7d50c5a] | 99 | "error when setting filter to C-weighting"); |
---|
[f826349] | 100 | return NULL; |
---|
| 101 | } |
---|
| 102 | return Py_None; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | static PyObject * |
---|
[615ac7d] | 106 | Py_filter_set_a_weighting (Py_filter * self, PyObject *args) |
---|
[f826349] | 107 | { |
---|
[615ac7d] | 108 | uint_t err = 0; |
---|
| 109 | uint_t samplerate; |
---|
| 110 | if (!PyArg_ParseTuple (args, "I", &samplerate)) { |
---|
| 111 | return NULL; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | err = aubio_filter_set_a_weighting (self->o, samplerate); |
---|
[f826349] | 115 | if (err > 0) { |
---|
| 116 | PyErr_SetString (PyExc_ValueError, |
---|
| 117 | "error when setting filter to A-weighting"); |
---|
| 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | return Py_None; |
---|
| 121 | } |
---|
| 122 | |
---|
[a81c8cd] | 123 | static PyObject * |
---|
| 124 | Py_filter_set_biquad(Py_filter * self, PyObject *args) |
---|
| 125 | { |
---|
| 126 | uint_t err = 0; |
---|
| 127 | lsmp_t b0, b1, b2, a1, a2; |
---|
| 128 | if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) { |
---|
| 129 | return NULL; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2); |
---|
| 133 | if (err > 0) { |
---|
| 134 | PyErr_SetString (PyExc_ValueError, |
---|
| 135 | "error when setting filter with biquad coefficients"); |
---|
| 136 | return NULL; |
---|
| 137 | } |
---|
| 138 | return Py_None; |
---|
| 139 | } |
---|
| 140 | |
---|
[f826349] | 141 | static PyMemberDef Py_filter_members[] = { |
---|
| 142 | // TODO remove READONLY flag and define getter/setter |
---|
| 143 | {"order", T_INT, offsetof (Py_filter, order), READONLY, |
---|
| 144 | "order of the filter"}, |
---|
| 145 | {NULL} /* Sentinel */ |
---|
| 146 | }; |
---|
| 147 | |
---|
| 148 | static PyMethodDef Py_filter_methods[] = { |
---|
[07bf04e6] | 149 | {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS, |
---|
[f826349] | 150 | "set filter coefficients to C-weighting"}, |
---|
[07bf04e6] | 151 | {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS, |
---|
[f826349] | 152 | "set filter coefficients to A-weighting"}, |
---|
[a81c8cd] | 153 | {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS, |
---|
| 154 | "set b0, b1, b2, a1, a2 biquad coefficients"}, |
---|
[f826349] | 155 | {NULL} |
---|
| 156 | }; |
---|
| 157 | |
---|
| 158 | PyTypeObject Py_filterType = { |
---|
| 159 | PyObject_HEAD_INIT (NULL) |
---|
| 160 | 0, /* ob_size */ |
---|
| 161 | "aubio.digital_filter", /* tp_name */ |
---|
| 162 | sizeof (Py_filter), /* tp_basicsize */ |
---|
| 163 | 0, /* tp_itemsize */ |
---|
| 164 | (destructor) Py_filter_del, /* tp_dealloc */ |
---|
| 165 | 0, /* tp_print */ |
---|
| 166 | 0, /* tp_getattr */ |
---|
| 167 | 0, /* tp_setattr */ |
---|
| 168 | 0, /* tp_compare */ |
---|
| 169 | 0, //(reprfunc) Py_filter_repr, /* tp_repr */ |
---|
| 170 | 0, /* tp_as_number */ |
---|
| 171 | 0, /* tp_as_sequence */ |
---|
| 172 | 0, /* tp_as_mapping */ |
---|
| 173 | 0, /* tp_hash */ |
---|
| 174 | (ternaryfunc)Py_filter_do, /* tp_call */ |
---|
| 175 | 0, /* tp_str */ |
---|
| 176 | 0, /* tp_getattro */ |
---|
| 177 | 0, /* tp_setattro */ |
---|
| 178 | 0, /* tp_as_buffer */ |
---|
| 179 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
| 180 | Py_filter_doc, /* tp_doc */ |
---|
| 181 | 0, /* tp_traverse */ |
---|
| 182 | 0, /* tp_clear */ |
---|
| 183 | 0, /* tp_richcompare */ |
---|
| 184 | 0, /* tp_weaklistoffset */ |
---|
| 185 | 0, /* tp_iter */ |
---|
| 186 | 0, /* tp_iternext */ |
---|
| 187 | Py_filter_methods, /* tp_methods */ |
---|
| 188 | Py_filter_members, /* tp_members */ |
---|
| 189 | 0, /* tp_getset */ |
---|
| 190 | 0, /* tp_base */ |
---|
| 191 | 0, /* tp_dict */ |
---|
| 192 | 0, /* tp_descr_get */ |
---|
| 193 | 0, /* tp_descr_set */ |
---|
| 194 | 0, /* tp_dictoffset */ |
---|
| 195 | (initproc) Py_filter_init, /* tp_init */ |
---|
| 196 | 0, /* tp_alloc */ |
---|
| 197 | Py_filter_new, /* tp_new */ |
---|
| 198 | }; |
---|