1 #include "aubio-types.h"
3 /* cvec type definition
6 def __init__(self, length = 1024):
8 self.norm = array(length)
9 self.phas = array(length)
13 static char Py_cvec_doc[] = "cvec object";
16 Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
20 static char *kwlist[] = { "length", NULL };
22 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
28 self = (Py_cvec *) type->tp_alloc (type, 0);
30 self->length = Py_default_vector_length / 2 + 1;
37 self->length = length / 2 + 1;
38 } else if (length < 0) {
39 PyErr_SetString (PyExc_ValueError,
40 "can not use negative number of elements");
44 return (PyObject *) self;
48 Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
50 self->o = new_cvec ((self->length - 1) * 2);
51 if (self->o == NULL) {
59 Py_cvec_del (Py_cvec * self)
62 self->ob_type->tp_free ((PyObject *) self);
66 Py_cvec_repr (Py_cvec * self, PyObject * unused)
68 PyObject *format = NULL;
69 PyObject *args = NULL;
70 PyObject *result = NULL;
72 format = PyString_FromString ("aubio cvec of %d elements");
77 args = Py_BuildValue ("I", self->length);
81 cvec_print ( self->o );
83 result = PyString_Format (format, args);
93 PyAubio_CvecNormToArray (Py_cvec * self)
95 npy_intp dims[] = { self->o->length, 1 };
96 return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm);
101 PyAubio_CvecPhasToArray (Py_cvec * self)
103 npy_intp dims[] = { self->o->length, 1 };
104 return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas);
108 PyAubio_ArrayToCvecPhas (PyObject * self)
114 Py_cvec_get_norm (Py_cvec * self, void *closure)
116 return PyAubio_CvecNormToArray(self);
120 Py_cvec_get_phas (Py_cvec * self, void *closure)
122 return PyAubio_CvecPhasToArray(self);
126 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
128 PyArrayObject * array;
130 PyErr_SetString (PyExc_ValueError, "input array is not a python object");
133 if (PyArray_Check(input)) {
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");
139 } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
140 PyErr_SetString (PyExc_ValueError,
141 "input array has more than two dimensions");
145 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
146 PyErr_SetString (PyExc_ValueError, "input array should be float");
148 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
149 PyErr_SetString (PyExc_ValueError, "input array should be float32");
152 array = (PyArrayObject *)input;
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));
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);
169 vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0);
172 PyErr_SetString (PyExc_ValueError, "can only accept array as input");
184 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
186 PyArrayObject * array;
188 PyErr_SetString (PyExc_ValueError, "input array is not a python object");
191 if (PyArray_Check(input)) {
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");
197 } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
198 PyErr_SetString (PyExc_ValueError,
199 "input array has more than two dimensions");
203 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
204 PyErr_SetString (PyExc_ValueError, "input array should be float");
206 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
207 PyErr_SetString (PyExc_ValueError, "input array should be float32");
210 array = (PyArrayObject *)input;
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));
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);
227 vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0);
230 PyErr_SetString (PyExc_ValueError, "can only accept array as input");
241 static PyMemberDef Py_cvec_members[] = {
242 // TODO remove READONLY flag and define getter/setter
243 {"length", T_INT, offsetof (Py_cvec, length), READONLY,
245 {NULL} /* Sentinel */
248 static PyMethodDef Py_cvec_methods[] = {
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",
256 {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas,
257 "Numpy vector of shape (length,) containing the phase",
259 {NULL} /* sentinel */
262 PyTypeObject Py_cvecType = {
263 PyObject_HEAD_INIT (NULL)
265 "aubio.cvec", /* tp_name */
266 sizeof (Py_cvec), /* tp_basicsize */
268 (destructor) Py_cvec_del, /* tp_dealloc */
273 (reprfunc) Py_cvec_repr, /* tp_repr */
274 0, /* tp_as_number */
275 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */
276 0, /* tp_as_mapping */
282 0, /* tp_as_buffer */
283 Py_TPFLAGS_DEFAULT, /* tp_flags */
284 Py_cvec_doc, /* tp_doc */
287 0, /* tp_richcompare */
288 0, /* tp_weaklistoffset */
291 Py_cvec_methods, /* tp_methods */
292 Py_cvec_members, /* tp_members */
293 Py_cvec_getseters, /* tp_getset */
296 0, /* tp_descr_get */
297 0, /* tp_descr_set */
298 0, /* tp_dictoffset */
299 (initproc) Py_cvec_init, /* tp_init */
301 Py_cvec_new, /* tp_new */