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