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