1 | #include "aubio-types.h" |
---|
2 | |
---|
3 | /* fvec type definition |
---|
4 | |
---|
5 | class fvec(): |
---|
6 | def __init__(self, length = 1024): |
---|
7 | self.length = length |
---|
8 | self.data = array(length) |
---|
9 | |
---|
10 | */ |
---|
11 | |
---|
12 | static char Py_fvec_doc[] = "fvec object"; |
---|
13 | |
---|
14 | static PyObject * |
---|
15 | Py_fvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
16 | { |
---|
17 | int length= 0; |
---|
18 | Py_fvec *self; |
---|
19 | static char *kwlist[] = { "length", NULL }; |
---|
20 | |
---|
21 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
22 | &length)) { |
---|
23 | return NULL; |
---|
24 | } |
---|
25 | |
---|
26 | self = (Py_fvec *) type->tp_alloc (type, 0); |
---|
27 | |
---|
28 | self->length = Py_default_vector_length; |
---|
29 | |
---|
30 | if (self == NULL) { |
---|
31 | return NULL; |
---|
32 | } |
---|
33 | |
---|
34 | if (length > 0) { |
---|
35 | self->length = length; |
---|
36 | } else if (length < 0) { |
---|
37 | PyErr_SetString (PyExc_ValueError, |
---|
38 | "can not use negative number of elements"); |
---|
39 | return NULL; |
---|
40 | } |
---|
41 | |
---|
42 | return (PyObject *) self; |
---|
43 | } |
---|
44 | |
---|
45 | static int |
---|
46 | Py_fvec_init (Py_fvec * self, PyObject * args, PyObject * kwds) |
---|
47 | { |
---|
48 | self->o = new_fvec (self->length); |
---|
49 | if (self->o == NULL) { |
---|
50 | return -1; |
---|
51 | } |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | static void |
---|
57 | Py_fvec_del (Py_fvec * self) |
---|
58 | { |
---|
59 | del_fvec (self->o); |
---|
60 | self->ob_type->tp_free ((PyObject *) self); |
---|
61 | } |
---|
62 | |
---|
63 | static PyObject * |
---|
64 | Py_fvec_repr (Py_fvec * self, PyObject * unused) |
---|
65 | { |
---|
66 | #if 0 |
---|
67 | PyObject *format = NULL; |
---|
68 | PyObject *args = NULL; |
---|
69 | PyObject *result = NULL; |
---|
70 | |
---|
71 | format = PyString_FromString ("aubio fvec of %d elements"); |
---|
72 | if (format == NULL) { |
---|
73 | goto fail; |
---|
74 | } |
---|
75 | |
---|
76 | args = Py_BuildValue ("I", self->length); |
---|
77 | if (args == NULL) { |
---|
78 | goto fail; |
---|
79 | } |
---|
80 | fvec_print ( self->o ); |
---|
81 | |
---|
82 | result = PyString_Format (format, args); |
---|
83 | |
---|
84 | fail: |
---|
85 | Py_XDECREF (format); |
---|
86 | Py_XDECREF (args); |
---|
87 | |
---|
88 | return result; |
---|
89 | #endif |
---|
90 | PyObject *format = NULL; |
---|
91 | PyObject *args = NULL; |
---|
92 | PyObject *result = NULL; |
---|
93 | |
---|
94 | format = PyString_FromString ("%s"); |
---|
95 | if (format == NULL) { |
---|
96 | goto fail; |
---|
97 | } |
---|
98 | |
---|
99 | args = Py_BuildValue ("O", PyAubio_FvecToArray (self)); |
---|
100 | if (args == NULL) { |
---|
101 | goto fail; |
---|
102 | } |
---|
103 | |
---|
104 | result = PyString_Format (format, args); |
---|
105 | fail: |
---|
106 | Py_XDECREF (format); |
---|
107 | Py_XDECREF (args); |
---|
108 | |
---|
109 | return result; |
---|
110 | } |
---|
111 | |
---|
112 | Py_fvec * |
---|
113 | PyAubio_ArrayToFvec (PyObject *input) { |
---|
114 | PyObject *array; |
---|
115 | Py_fvec *vec; |
---|
116 | if (input == NULL) { |
---|
117 | PyErr_SetString (PyExc_ValueError, "input array is not a python object"); |
---|
118 | goto fail; |
---|
119 | } |
---|
120 | // parsing input object into a Py_fvec |
---|
121 | if (PyObject_TypeCheck (input, &Py_fvecType)) { |
---|
122 | // input is an fvec, nothing else to do |
---|
123 | vec = (Py_fvec *) input; |
---|
124 | } else if (PyArray_Check(input)) { |
---|
125 | |
---|
126 | // we got an array, convert it to an fvec |
---|
127 | if (PyArray_NDIM (input) == 0) { |
---|
128 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
129 | goto fail; |
---|
130 | } else if (PyArray_NDIM (input) > 1) { |
---|
131 | PyErr_SetString (PyExc_ValueError, |
---|
132 | "input array has more than one dimensions"); |
---|
133 | goto fail; |
---|
134 | } |
---|
135 | |
---|
136 | if (!PyArray_ISFLOAT (input)) { |
---|
137 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
138 | goto fail; |
---|
139 | } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { |
---|
140 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
141 | goto fail; |
---|
142 | } else { |
---|
143 | // input data type is float32, nothing else to do |
---|
144 | array = input; |
---|
145 | } |
---|
146 | |
---|
147 | // create a new fvec object |
---|
148 | vec = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType); |
---|
149 | vec->length = PyArray_SIZE (array); |
---|
150 | |
---|
151 | // no need to really allocate fvec, just its struct member |
---|
152 | // vec->o = new_fvec (vec->length); |
---|
153 | vec->o = (fvec_t *)malloc(sizeof(fvec_t)); |
---|
154 | vec->o->length = vec->length; |
---|
155 | vec->o->data = (smpl_t *) PyArray_GETPTR1 (array, 0); |
---|
156 | |
---|
157 | } else if (PyObject_TypeCheck (input, &PyList_Type)) { |
---|
158 | PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); |
---|
159 | return NULL; |
---|
160 | } else { |
---|
161 | PyErr_SetString (PyExc_ValueError, "can only accept vector or fvec as input"); |
---|
162 | return NULL; |
---|
163 | } |
---|
164 | |
---|
165 | return vec; |
---|
166 | |
---|
167 | fail: |
---|
168 | return NULL; |
---|
169 | } |
---|
170 | |
---|
171 | PyObject * |
---|
172 | PyAubio_CFvecToArray (fvec_t * self) |
---|
173 | { |
---|
174 | npy_intp dims[] = { self->length, 1 }; |
---|
175 | return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data); |
---|
176 | } |
---|
177 | |
---|
178 | PyObject * |
---|
179 | PyAubio_FvecToArray (Py_fvec * self) |
---|
180 | { |
---|
181 | PyObject *array = NULL; |
---|
182 | npy_intp dims[] = { self->length, 1 }; |
---|
183 | array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data); |
---|
184 | return array; |
---|
185 | } |
---|
186 | |
---|
187 | static PyObject * |
---|
188 | Py_fvec_getitem (Py_fvec * self, Py_ssize_t index) |
---|
189 | { |
---|
190 | if (index < 0 || index >= self->length) { |
---|
191 | PyErr_SetString (PyExc_IndexError, "no such element"); |
---|
192 | return NULL; |
---|
193 | } |
---|
194 | |
---|
195 | return PyFloat_FromDouble (self->o->data[index]); |
---|
196 | } |
---|
197 | |
---|
198 | static int |
---|
199 | Py_fvec_setitem (Py_fvec * self, Py_ssize_t index, PyObject * o) |
---|
200 | { |
---|
201 | |
---|
202 | if (index < 0 || index >= self->length) { |
---|
203 | PyErr_SetString (PyExc_IndexError, "no such element"); |
---|
204 | goto fail; |
---|
205 | } |
---|
206 | |
---|
207 | if (PyFloat_Check (o)) { |
---|
208 | PyErr_SetString (PyExc_ValueError, "should be a float"); |
---|
209 | goto fail; |
---|
210 | } |
---|
211 | |
---|
212 | self->o->data[index] = (smpl_t) PyFloat_AsDouble(o); |
---|
213 | |
---|
214 | return 0; |
---|
215 | |
---|
216 | fail: |
---|
217 | return -1; |
---|
218 | } |
---|
219 | |
---|
220 | int |
---|
221 | Py_fvec_get_length (Py_fvec * self) |
---|
222 | { |
---|
223 | return self->length; |
---|
224 | } |
---|
225 | |
---|
226 | static PyMemberDef Py_fvec_members[] = { |
---|
227 | // TODO remove READONLY flag and define getter/setter |
---|
228 | {"length", T_INT, offsetof (Py_fvec, length), READONLY, |
---|
229 | "length attribute"}, |
---|
230 | {NULL} /* Sentinel */ |
---|
231 | }; |
---|
232 | |
---|
233 | static PyMethodDef Py_fvec_methods[] = { |
---|
234 | {"__array__", (PyCFunction) PyAubio_FvecToArray, METH_NOARGS, |
---|
235 | "Returns the vector as a numpy array."}, |
---|
236 | {NULL} |
---|
237 | }; |
---|
238 | |
---|
239 | static PySequenceMethods Py_fvec_tp_as_sequence = { |
---|
240 | (lenfunc) Py_fvec_get_length, /* sq_length */ |
---|
241 | 0, /* sq_concat */ |
---|
242 | 0, /* sq_repeat */ |
---|
243 | (ssizeargfunc) Py_fvec_getitem, /* sq_item */ |
---|
244 | 0, /* sq_slice */ |
---|
245 | (ssizeobjargproc) Py_fvec_setitem, /* sq_ass_item */ |
---|
246 | 0, /* sq_ass_slice */ |
---|
247 | 0, /* sq_contains */ |
---|
248 | 0, /* sq_inplace_concat */ |
---|
249 | 0, /* sq_inplace_repeat */ |
---|
250 | }; |
---|
251 | |
---|
252 | |
---|
253 | PyTypeObject Py_fvecType = { |
---|
254 | PyObject_HEAD_INIT (NULL) |
---|
255 | 0, /* ob_size */ |
---|
256 | "aubio.fvec", /* tp_name */ |
---|
257 | sizeof (Py_fvec), /* tp_basicsize */ |
---|
258 | 0, /* tp_itemsize */ |
---|
259 | (destructor) Py_fvec_del, /* tp_dealloc */ |
---|
260 | 0, /* tp_print */ |
---|
261 | 0, /* tp_getattr */ |
---|
262 | 0, /* tp_setattr */ |
---|
263 | 0, /* tp_compare */ |
---|
264 | (reprfunc) Py_fvec_repr, /* tp_repr */ |
---|
265 | 0, /* tp_as_number */ |
---|
266 | &Py_fvec_tp_as_sequence, /* tp_as_sequence */ |
---|
267 | 0, /* tp_as_mapping */ |
---|
268 | 0, /* tp_hash */ |
---|
269 | 0, /* tp_call */ |
---|
270 | 0, /* tp_str */ |
---|
271 | 0, /* tp_getattro */ |
---|
272 | 0, /* tp_setattro */ |
---|
273 | 0, /* tp_as_buffer */ |
---|
274 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
275 | Py_fvec_doc, /* tp_doc */ |
---|
276 | 0, /* tp_traverse */ |
---|
277 | 0, /* tp_clear */ |
---|
278 | 0, /* tp_richcompare */ |
---|
279 | 0, /* tp_weaklistoffset */ |
---|
280 | 0, /* tp_iter */ |
---|
281 | 0, /* tp_iternext */ |
---|
282 | Py_fvec_methods, /* tp_methods */ |
---|
283 | Py_fvec_members, /* tp_members */ |
---|
284 | 0, /* tp_getset */ |
---|
285 | 0, /* tp_base */ |
---|
286 | 0, /* tp_dict */ |
---|
287 | 0, /* tp_descr_get */ |
---|
288 | 0, /* tp_descr_set */ |
---|
289 | 0, /* tp_dictoffset */ |
---|
290 | (initproc) Py_fvec_init, /* tp_init */ |
---|
291 | 0, /* tp_alloc */ |
---|
292 | Py_fvec_new, /* tp_new */ |
---|
293 | }; |
---|