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 | { |
---|
15 | int order= 0; |
---|
16 | Py_filter *self; |
---|
17 | static char *kwlist[] = { "order", NULL }; |
---|
18 | |
---|
19 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
20 | &order)) { |
---|
21 | return NULL; |
---|
22 | } |
---|
23 | |
---|
24 | self = (Py_filter *) type->tp_alloc (type, 0); |
---|
25 | |
---|
26 | if (self == NULL) { |
---|
27 | return NULL; |
---|
28 | } |
---|
29 | |
---|
30 | self->order = 7; |
---|
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 | { |
---|
46 | self->o = new_aubio_filter (self->order); |
---|
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 * |
---|
62 | Py_filter_do(Py_filter * self, PyObject * args) |
---|
63 | { |
---|
64 | PyObject *input; |
---|
65 | fvec_t *vec; |
---|
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 | |
---|
75 | vec = PyAubio_ArrayToCFvec (input); |
---|
76 | |
---|
77 | if (vec == NULL) { |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | |
---|
81 | // compute the function |
---|
82 | fvec_t * out = new_fvec(vec->length); |
---|
83 | aubio_filter_do_outplace (self->o, vec, out); |
---|
84 | return PyAubio_CFvecToArray(out); |
---|
85 | } |
---|
86 | |
---|
87 | static PyObject * |
---|
88 | Py_filter_set_c_weighting (Py_filter * self, PyObject *args) |
---|
89 | { |
---|
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); |
---|
97 | if (err > 0) { |
---|
98 | PyErr_SetString (PyExc_ValueError, |
---|
99 | "error when setting filter to A-weighting"); |
---|
100 | return NULL; |
---|
101 | } |
---|
102 | return Py_None; |
---|
103 | } |
---|
104 | |
---|
105 | static PyObject * |
---|
106 | Py_filter_set_a_weighting (Py_filter * self, PyObject *args) |
---|
107 | { |
---|
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); |
---|
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 | |
---|
123 | static PyMemberDef Py_filter_members[] = { |
---|
124 | // TODO remove READONLY flag and define getter/setter |
---|
125 | {"order", T_INT, offsetof (Py_filter, order), READONLY, |
---|
126 | "order of the filter"}, |
---|
127 | {NULL} /* Sentinel */ |
---|
128 | }; |
---|
129 | |
---|
130 | static PyMethodDef Py_filter_methods[] = { |
---|
131 | {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS, |
---|
132 | "set filter coefficients to C-weighting"}, |
---|
133 | {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS, |
---|
134 | "set filter coefficients to A-weighting"}, |
---|
135 | {NULL} |
---|
136 | }; |
---|
137 | |
---|
138 | PyTypeObject Py_filterType = { |
---|
139 | PyObject_HEAD_INIT (NULL) |
---|
140 | 0, /* ob_size */ |
---|
141 | "aubio.digital_filter", /* tp_name */ |
---|
142 | sizeof (Py_filter), /* tp_basicsize */ |
---|
143 | 0, /* tp_itemsize */ |
---|
144 | (destructor) Py_filter_del, /* tp_dealloc */ |
---|
145 | 0, /* tp_print */ |
---|
146 | 0, /* tp_getattr */ |
---|
147 | 0, /* tp_setattr */ |
---|
148 | 0, /* tp_compare */ |
---|
149 | 0, //(reprfunc) Py_filter_repr, /* tp_repr */ |
---|
150 | 0, /* tp_as_number */ |
---|
151 | 0, /* tp_as_sequence */ |
---|
152 | 0, /* tp_as_mapping */ |
---|
153 | 0, /* tp_hash */ |
---|
154 | (ternaryfunc)Py_filter_do, /* tp_call */ |
---|
155 | 0, /* tp_str */ |
---|
156 | 0, /* tp_getattro */ |
---|
157 | 0, /* tp_setattro */ |
---|
158 | 0, /* tp_as_buffer */ |
---|
159 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
160 | Py_filter_doc, /* tp_doc */ |
---|
161 | 0, /* tp_traverse */ |
---|
162 | 0, /* tp_clear */ |
---|
163 | 0, /* tp_richcompare */ |
---|
164 | 0, /* tp_weaklistoffset */ |
---|
165 | 0, /* tp_iter */ |
---|
166 | 0, /* tp_iternext */ |
---|
167 | Py_filter_methods, /* tp_methods */ |
---|
168 | Py_filter_members, /* tp_members */ |
---|
169 | 0, /* tp_getset */ |
---|
170 | 0, /* tp_base */ |
---|
171 | 0, /* tp_dict */ |
---|
172 | 0, /* tp_descr_get */ |
---|
173 | 0, /* tp_descr_set */ |
---|
174 | 0, /* tp_dictoffset */ |
---|
175 | (initproc) Py_filter_init, /* tp_init */ |
---|
176 | 0, /* tp_alloc */ |
---|
177 | Py_filter_new, /* tp_new */ |
---|
178 | }; |
---|