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