1 #include "aubio-types.h"
3 /* cvec type definition
6 def __new__(self, length = 1024):
7 self.length = length / 2 + 1
8 self.norm = np.zeros(length / 2 + 1)
9 self.phas = np.zeros(length / 2 + 1)
13 // special python type for cvec
22 static char Py_cvec_doc[] = "cvec object";
26 new_py_cvec(uint_t length) {
27 Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
28 npy_intp dims[] = { length / 2 + 1, 1 };
29 vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
30 vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
31 vec->length = length / 2 + 1;
32 return (PyObject*)vec;
36 PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) {
37 if (PyObject_TypeCheck (input, &Py_cvecType)) {
38 Py_cvec * in = (Py_cvec *)input;
39 i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0);
40 i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0);
41 i->length = ((Py_cvec*)input)->length;
44 PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec");
50 Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
54 static char *kwlist[] = { "length", NULL };
56 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
61 self = (Py_cvec *) type->tp_alloc (type, 0);
63 self->length = Py_default_vector_length / 2 + 1;
70 self->length = length / 2 + 1;
71 } else if (length < 0) {
72 PyErr_SetString (PyExc_ValueError,
73 "can not use negative number of elements");
77 return (PyObject *) self;
81 Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
83 npy_intp dims[] = { self->length, 1 };
84 self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
85 self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
90 Py_cvec_del (Py_cvec * self)
92 Py_DECREF(self->norm);
93 Py_DECREF(self->phas);
94 Py_TYPE(self)->tp_free ((PyObject *) self);
98 Py_cvec_repr (Py_cvec * self, PyObject * unused)
100 PyObject *format = NULL;
101 PyObject *args = NULL;
102 PyObject *result = NULL;
104 format = PyUnicode_FromString ("aubio cvec of %d elements");
105 if (format == NULL) {
109 args = Py_BuildValue ("I", self->length);
113 // hide actual norm / phas content
115 result = PyUnicode_Format (format, args);
125 Py_cvec_get_norm (Py_cvec * self, void *closure)
127 // we want self->norm to still exist after our caller return it
128 Py_INCREF(self->norm);
129 return (PyObject*)(self->norm);
133 Py_cvec_get_phas (Py_cvec * self, void *closure)
135 // we want self->phas to still exist after our caller return it
136 Py_INCREF(self->phas);
137 return (PyObject *)(self->phas);
141 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
144 if (!PyAubio_IsValidVector(input)) {
147 length = PyArray_SIZE ((PyArrayObject *)input);
148 if (length != vec->length) {
149 PyErr_Format (PyExc_ValueError,
150 "input array has length %ld, but cvec has length %d", length,
155 Py_XDECREF(vec->norm);
157 Py_INCREF(vec->norm);
162 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
165 if (!PyAubio_IsValidVector(input)) {
168 length = PyArray_SIZE ((PyArrayObject *)input);
169 if (length != vec->length) {
170 PyErr_Format (PyExc_ValueError,
171 "input array has length %ld, but cvec has length %d", length,
176 Py_XDECREF(vec->phas);
178 Py_INCREF(vec->phas);
182 static PyMemberDef Py_cvec_members[] = {
183 // TODO remove READONLY flag and define getter/setter
184 {"length", T_INT, offsetof (Py_cvec, length), READONLY,
186 {NULL} /* Sentinel */
189 static PyMethodDef Py_cvec_methods[] = {
193 static PyGetSetDef Py_cvec_getseters[] = {
194 {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm,
195 "Numpy vector of shape (length,) containing the magnitude",
197 {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas,
198 "Numpy vector of shape (length,) containing the phase",
200 {NULL} /* sentinel */
203 PyTypeObject Py_cvecType = {
204 PyVarObject_HEAD_INIT(NULL, 0)
205 "aubio.cvec", /* tp_name */
206 sizeof (Py_cvec), /* tp_basicsize */
208 (destructor) Py_cvec_del, /* tp_dealloc */
213 (reprfunc) Py_cvec_repr, /* tp_repr */
214 0, /* tp_as_number */
215 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */
216 0, /* tp_as_mapping */
222 0, /* tp_as_buffer */
223 Py_TPFLAGS_DEFAULT, /* tp_flags */
224 Py_cvec_doc, /* tp_doc */
227 0, /* tp_richcompare */
228 0, /* tp_weaklistoffset */
231 Py_cvec_methods, /* tp_methods */
232 Py_cvec_members, /* tp_members */
233 Py_cvec_getseters, /* tp_getset */
236 0, /* tp_descr_get */
237 0, /* tp_descr_set */
238 0, /* tp_dictoffset */
239 (initproc) Py_cvec_init, /* tp_init */
241 Py_cvec_new, /* tp_new */