1 | #include "aubio-types.h" |
---|
2 | |
---|
3 | /* cvec type definition |
---|
4 | |
---|
5 | class cvec(): |
---|
6 | def __init__(self, length = 1024): |
---|
7 | self.length = length |
---|
8 | self.norm = array(length) |
---|
9 | self.phas = array(length) |
---|
10 | |
---|
11 | */ |
---|
12 | |
---|
13 | static char Py_cvec_doc[] = "cvec object"; |
---|
14 | |
---|
15 | static PyObject * |
---|
16 | Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
17 | { |
---|
18 | int length= 0; |
---|
19 | Py_cvec *self; |
---|
20 | static char *kwlist[] = { "length", NULL }; |
---|
21 | |
---|
22 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
23 | &length)) { |
---|
24 | return NULL; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | self = (Py_cvec *) type->tp_alloc (type, 0); |
---|
29 | |
---|
30 | self->length = Py_default_vector_length / 2 + 1; |
---|
31 | |
---|
32 | if (self == NULL) { |
---|
33 | return NULL; |
---|
34 | } |
---|
35 | |
---|
36 | if (length > 0) { |
---|
37 | self->length = length / 2 + 1; |
---|
38 | } else if (length < 0) { |
---|
39 | PyErr_SetString (PyExc_ValueError, |
---|
40 | "can not use negative number of elements"); |
---|
41 | return NULL; |
---|
42 | } |
---|
43 | |
---|
44 | return (PyObject *) self; |
---|
45 | } |
---|
46 | |
---|
47 | static int |
---|
48 | Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) |
---|
49 | { |
---|
50 | self->o = new_cvec ((self->length - 1) * 2); |
---|
51 | if (self->o == NULL) { |
---|
52 | return -1; |
---|
53 | } |
---|
54 | |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | static void |
---|
59 | Py_cvec_del (Py_cvec * self) |
---|
60 | { |
---|
61 | del_cvec (self->o); |
---|
62 | Py_TYPE(self)->tp_free ((PyObject *) self); |
---|
63 | } |
---|
64 | |
---|
65 | static PyObject * |
---|
66 | Py_cvec_repr (Py_cvec * self, PyObject * unused) |
---|
67 | { |
---|
68 | PyObject *format = NULL; |
---|
69 | PyObject *args = NULL; |
---|
70 | PyObject *result = NULL; |
---|
71 | |
---|
72 | format = PyUnicode_FromString ("aubio cvec of %d elements"); |
---|
73 | if (format == NULL) { |
---|
74 | goto fail; |
---|
75 | } |
---|
76 | |
---|
77 | args = Py_BuildValue ("I", self->length); |
---|
78 | if (args == NULL) { |
---|
79 | goto fail; |
---|
80 | } |
---|
81 | cvec_print ( self->o ); |
---|
82 | |
---|
83 | result = PyUnicode_Format (format, args); |
---|
84 | |
---|
85 | fail: |
---|
86 | Py_XDECREF (format); |
---|
87 | Py_XDECREF (args); |
---|
88 | |
---|
89 | return result; |
---|
90 | } |
---|
91 | |
---|
92 | PyObject * |
---|
93 | PyAubio_CvecNormToArray (Py_cvec * self) |
---|
94 | { |
---|
95 | npy_intp dims[] = { self->o->length, 1 }; |
---|
96 | return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->norm); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | PyObject * |
---|
101 | PyAubio_CvecPhasToArray (Py_cvec * self) |
---|
102 | { |
---|
103 | npy_intp dims[] = { self->o->length, 1 }; |
---|
104 | return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->phas); |
---|
105 | } |
---|
106 | |
---|
107 | PyObject * |
---|
108 | PyAubio_ArrayToCvecPhas (PyObject * self) |
---|
109 | { |
---|
110 | return NULL; |
---|
111 | } |
---|
112 | |
---|
113 | PyObject * |
---|
114 | Py_cvec_get_norm (Py_cvec * self, void *closure) |
---|
115 | { |
---|
116 | return PyAubio_CvecNormToArray(self); |
---|
117 | } |
---|
118 | |
---|
119 | PyObject * |
---|
120 | Py_cvec_get_phas (Py_cvec * self, void *closure) |
---|
121 | { |
---|
122 | return PyAubio_CvecPhasToArray(self); |
---|
123 | } |
---|
124 | |
---|
125 | static int |
---|
126 | Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) |
---|
127 | { |
---|
128 | PyArrayObject * array; |
---|
129 | if (input == NULL) { |
---|
130 | PyErr_SetString (PyExc_ValueError, "input array is not a python object"); |
---|
131 | goto fail; |
---|
132 | } |
---|
133 | if (PyArray_Check(input)) { |
---|
134 | |
---|
135 | // we got an array, convert it to a cvec.norm |
---|
136 | if (PyArray_NDIM ((PyArrayObject *)input) == 0) { |
---|
137 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
138 | goto fail; |
---|
139 | } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { |
---|
140 | PyErr_SetString (PyExc_ValueError, |
---|
141 | "input array has more than two dimensions"); |
---|
142 | goto fail; |
---|
143 | } |
---|
144 | |
---|
145 | if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { |
---|
146 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
147 | goto fail; |
---|
148 | } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { |
---|
149 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
150 | goto fail; |
---|
151 | } |
---|
152 | array = (PyArrayObject *)input; |
---|
153 | |
---|
154 | // check input array dimensions |
---|
155 | if (PyArray_NDIM (array) != 1) { |
---|
156 | PyErr_Format (PyExc_ValueError, |
---|
157 | "input array has %d dimensions, not 1", |
---|
158 | PyArray_NDIM (array)); |
---|
159 | goto fail; |
---|
160 | } else { |
---|
161 | if (vec->o->length != PyArray_SIZE (array)) { |
---|
162 | PyErr_Format (PyExc_ValueError, |
---|
163 | "input array has length %d, but cvec has length %d", |
---|
164 | (int)PyArray_SIZE (array), vec->o->length); |
---|
165 | goto fail; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); |
---|
170 | |
---|
171 | } else { |
---|
172 | PyErr_SetString (PyExc_ValueError, "can only accept array as input"); |
---|
173 | return 1; |
---|
174 | } |
---|
175 | |
---|
176 | Py_INCREF(array); |
---|
177 | return 0; |
---|
178 | |
---|
179 | fail: |
---|
180 | return 1; |
---|
181 | } |
---|
182 | |
---|
183 | static int |
---|
184 | Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) |
---|
185 | { |
---|
186 | PyArrayObject * array; |
---|
187 | if (input == NULL) { |
---|
188 | PyErr_SetString (PyExc_ValueError, "input array is not a python object"); |
---|
189 | goto fail; |
---|
190 | } |
---|
191 | if (PyArray_Check(input)) { |
---|
192 | |
---|
193 | // we got an array, convert it to a cvec.phas |
---|
194 | if (PyArray_NDIM ((PyArrayObject *)input) == 0) { |
---|
195 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
196 | goto fail; |
---|
197 | } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { |
---|
198 | PyErr_SetString (PyExc_ValueError, |
---|
199 | "input array has more than two dimensions"); |
---|
200 | goto fail; |
---|
201 | } |
---|
202 | |
---|
203 | if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { |
---|
204 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
205 | goto fail; |
---|
206 | } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { |
---|
207 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
208 | goto fail; |
---|
209 | } |
---|
210 | array = (PyArrayObject *)input; |
---|
211 | |
---|
212 | // check input array dimensions |
---|
213 | if (PyArray_NDIM (array) != 1) { |
---|
214 | PyErr_Format (PyExc_ValueError, |
---|
215 | "input array has %d dimensions, not 1", |
---|
216 | PyArray_NDIM (array)); |
---|
217 | goto fail; |
---|
218 | } else { |
---|
219 | if (vec->o->length != PyArray_SIZE (array)) { |
---|
220 | PyErr_Format (PyExc_ValueError, |
---|
221 | "input array has length %d, but cvec has length %d", |
---|
222 | (int)PyArray_SIZE (array), vec->o->length); |
---|
223 | goto fail; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); |
---|
228 | |
---|
229 | } else { |
---|
230 | PyErr_SetString (PyExc_ValueError, "can only accept array as input"); |
---|
231 | return 1; |
---|
232 | } |
---|
233 | |
---|
234 | Py_INCREF(array); |
---|
235 | return 0; |
---|
236 | |
---|
237 | fail: |
---|
238 | return 1; |
---|
239 | } |
---|
240 | |
---|
241 | static PyMemberDef Py_cvec_members[] = { |
---|
242 | // TODO remove READONLY flag and define getter/setter |
---|
243 | {"length", T_INT, offsetof (Py_cvec, length), READONLY, |
---|
244 | "length attribute"}, |
---|
245 | {NULL} /* Sentinel */ |
---|
246 | }; |
---|
247 | |
---|
248 | static PyMethodDef Py_cvec_methods[] = { |
---|
249 | {NULL} |
---|
250 | }; |
---|
251 | |
---|
252 | static PyGetSetDef Py_cvec_getseters[] = { |
---|
253 | {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, |
---|
254 | "Numpy vector of shape (length,) containing the magnitude", |
---|
255 | NULL}, |
---|
256 | {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, |
---|
257 | "Numpy vector of shape (length,) containing the phase", |
---|
258 | NULL}, |
---|
259 | {NULL} /* sentinel */ |
---|
260 | }; |
---|
261 | |
---|
262 | PyTypeObject Py_cvecType = { |
---|
263 | PyVarObject_HEAD_INIT(NULL, 0) |
---|
264 | "aubio.cvec", /* tp_name */ |
---|
265 | sizeof (Py_cvec), /* tp_basicsize */ |
---|
266 | 0, /* tp_itemsize */ |
---|
267 | (destructor) Py_cvec_del, /* tp_dealloc */ |
---|
268 | 0, /* tp_print */ |
---|
269 | 0, /* tp_getattr */ |
---|
270 | 0, /* tp_setattr */ |
---|
271 | 0, /* tp_compare */ |
---|
272 | (reprfunc) Py_cvec_repr, /* tp_repr */ |
---|
273 | 0, /* tp_as_number */ |
---|
274 | 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ |
---|
275 | 0, /* tp_as_mapping */ |
---|
276 | 0, /* tp_hash */ |
---|
277 | 0, /* tp_call */ |
---|
278 | 0, /* tp_str */ |
---|
279 | 0, /* tp_getattro */ |
---|
280 | 0, /* tp_setattro */ |
---|
281 | 0, /* tp_as_buffer */ |
---|
282 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
283 | Py_cvec_doc, /* tp_doc */ |
---|
284 | 0, /* tp_traverse */ |
---|
285 | 0, /* tp_clear */ |
---|
286 | 0, /* tp_richcompare */ |
---|
287 | 0, /* tp_weaklistoffset */ |
---|
288 | 0, /* tp_iter */ |
---|
289 | 0, /* tp_iternext */ |
---|
290 | Py_cvec_methods, /* tp_methods */ |
---|
291 | Py_cvec_members, /* tp_members */ |
---|
292 | Py_cvec_getseters, /* tp_getset */ |
---|
293 | 0, /* tp_base */ |
---|
294 | 0, /* tp_dict */ |
---|
295 | 0, /* tp_descr_get */ |
---|
296 | 0, /* tp_descr_set */ |
---|
297 | 0, /* tp_dictoffset */ |
---|
298 | (initproc) Py_cvec_init, /* tp_init */ |
---|
299 | 0, /* tp_alloc */ |
---|
300 | Py_cvec_new, /* tp_new */ |
---|
301 | }; |
---|