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[] = ""
25 "A container holding spectral data.\n"
27 "Create one `cvec` to store the spectral information of a window\n"
28 "of `size` points. The data will be stored in two vectors,\n"
29 ":attr:`phas` and :attr:`norm`, each of shape (:attr:`length`,),\n"
30 "with `length = size // 2 + 1`.\n"
35 " Size of spectrum to create.\n"
39 ">>> c = aubio.cvec(1024)\n"
41 "aubio cvec of 513 elements\n"
44 ">>> c.norm.dtype, c.phas.dtype\n"
45 "(dtype('float32'), dtype('float32'))\n"
46 ">>> c.norm.shape, c.phas.shape\n"
56 new_py_cvec(uint_t length) {
57 Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
58 npy_intp dims[] = { length / 2 + 1, 1 };
59 vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
60 vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
61 vec->length = length / 2 + 1;
62 return (PyObject*)vec;
66 PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) {
67 if (PyObject_TypeCheck (input, &Py_cvecType)) {
68 Py_cvec * in = (Py_cvec *)input;
69 i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0);
70 i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0);
71 i->length = ((Py_cvec*)input)->length;
74 PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec");
80 Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
84 static char *kwlist[] = { "length", NULL };
86 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
91 self = (Py_cvec *) type->tp_alloc (type, 0);
93 self->length = Py_default_vector_length / 2 + 1;
100 self->length = length / 2 + 1;
101 } else if (length < 0) {
102 PyErr_SetString (PyExc_ValueError,
103 "can not use negative number of elements");
107 return (PyObject *) self;
111 Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
113 npy_intp dims[] = { self->length, 1 };
114 self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
115 self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
120 Py_cvec_del (Py_cvec * self)
122 Py_DECREF(self->norm);
123 Py_DECREF(self->phas);
124 Py_TYPE(self)->tp_free ((PyObject *) self);
128 Py_cvec_repr (Py_cvec * self, PyObject * unused)
130 PyObject *format = NULL;
131 PyObject *args = NULL;
132 PyObject *result = NULL;
134 format = PyUnicode_FromString ("aubio cvec of %d elements");
135 if (format == NULL) {
139 args = Py_BuildValue ("I", self->length);
143 // hide actual norm / phas content
145 result = PyUnicode_Format (format, args);
155 Py_cvec_get_norm (Py_cvec * self, void *closure)
157 // we want self->norm to still exist after our caller return it
158 Py_INCREF(self->norm);
159 return (PyObject*)(self->norm);
163 Py_cvec_get_phas (Py_cvec * self, void *closure)
165 // we want self->phas to still exist after our caller return it
166 Py_INCREF(self->phas);
167 return (PyObject *)(self->phas);
171 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
174 if (!PyAubio_IsValidVector(input)) {
177 length = PyArray_SIZE ((PyArrayObject *)input);
178 if (length != vec->length) {
179 PyErr_Format (PyExc_ValueError,
180 "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length,
185 Py_XDECREF(vec->norm);
187 Py_INCREF(vec->norm);
192 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
195 if (!PyAubio_IsValidVector(input)) {
198 length = PyArray_SIZE ((PyArrayObject *)input);
199 if (length != vec->length) {
200 PyErr_Format (PyExc_ValueError,
201 "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length,
206 Py_XDECREF(vec->phas);
208 Py_INCREF(vec->phas);
212 static PyMemberDef Py_cvec_members[] = {
213 // TODO remove READONLY flag and define getter/setter
214 {"length", T_INT, offsetof (Py_cvec, length), READONLY,
215 "int: Length of `norm` and `phas` vectors."},
216 {NULL} /* Sentinel */
219 static PyMethodDef Py_cvec_methods[] = {
223 static PyGetSetDef Py_cvec_getseters[] = {
224 {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm,
225 "numpy.ndarray: Vector of shape `(length,)` containing the magnitude.",
227 {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas,
228 "numpy.ndarray: Vector of shape `(length,)` containing the phase.",
230 {NULL} /* sentinel */
233 PyTypeObject Py_cvecType = {
234 PyVarObject_HEAD_INIT(NULL, 0)
235 "aubio.cvec", /* tp_name */
236 sizeof (Py_cvec), /* tp_basicsize */
238 (destructor) Py_cvec_del, /* tp_dealloc */
243 (reprfunc) Py_cvec_repr, /* tp_repr */
244 0, /* tp_as_number */
245 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */
246 0, /* tp_as_mapping */
252 0, /* tp_as_buffer */
253 Py_TPFLAGS_DEFAULT, /* tp_flags */
254 Py_cvec_doc, /* tp_doc */
257 0, /* tp_richcompare */
258 0, /* tp_weaklistoffset */
261 Py_cvec_methods, /* tp_methods */
262 Py_cvec_members, /* tp_members */
263 Py_cvec_getseters, /* tp_getset */
266 0, /* tp_descr_get */
267 0, /* tp_descr_set */
268 0, /* tp_dictoffset */
269 (initproc) Py_cvec_init, /* tp_init */
271 Py_cvec_new, /* tp_new */