[f826349] | 1 | #include "aubio-types.h" |
---|
| 2 | |
---|
| 3 | /* cvec type definition |
---|
| 4 | |
---|
| 5 | class cvec(): |
---|
[7a7b00f] | 6 | def __init__(self, length = 1024): |
---|
[f826349] | 7 | self.length = length |
---|
[7a7b00f] | 8 | self.norm = array(length) |
---|
| 9 | self.phas = array(length) |
---|
[f826349] | 10 | |
---|
[7a7b00f] | 11 | */ |
---|
[f826349] | 12 | |
---|
| 13 | static char Py_cvec_doc[] = "cvec object"; |
---|
| 14 | |
---|
| 15 | static PyObject * |
---|
| 16 | Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 17 | { |
---|
[7a7b00f] | 18 | int length= 0; |
---|
[f826349] | 19 | Py_cvec *self; |
---|
[7a7b00f] | 20 | static char *kwlist[] = { "length", NULL }; |
---|
[f826349] | 21 | |
---|
[7a7b00f] | 22 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
| 23 | &length)) { |
---|
[f826349] | 24 | return NULL; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | self = (Py_cvec *) type->tp_alloc (type, 0); |
---|
| 29 | |
---|
[eb93592] | 30 | self->length = Py_default_vector_length / 2 + 1; |
---|
[f826349] | 31 | |
---|
| 32 | if (self == NULL) { |
---|
| 33 | return NULL; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | if (length > 0) { |
---|
[eb93592] | 37 | self->length = length / 2 + 1; |
---|
[f826349] | 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 | { |
---|
[7a7b00f] | 50 | self->o = new_cvec ((self->length - 1) * 2); |
---|
[f826349] | 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 | self->ob_type->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 | |
---|
[7a7b00f] | 72 | format = PyString_FromString ("aubio cvec of %d elements"); |
---|
[f826349] | 73 | if (format == NULL) { |
---|
| 74 | goto fail; |
---|
| 75 | } |
---|
| 76 | |
---|
[7a7b00f] | 77 | args = Py_BuildValue ("I", self->length); |
---|
[f826349] | 78 | if (args == NULL) { |
---|
| 79 | goto fail; |
---|
| 80 | } |
---|
[7a7b00f] | 81 | cvec_print ( self->o ); |
---|
[f826349] | 82 | |
---|
| 83 | result = PyString_Format (format, args); |
---|
| 84 | |
---|
| 85 | fail: |
---|
| 86 | Py_XDECREF (format); |
---|
| 87 | Py_XDECREF (args); |
---|
| 88 | |
---|
| 89 | return result; |
---|
| 90 | } |
---|
| 91 | |
---|
[615ac7d] | 92 | PyObject * |
---|
| 93 | PyAubio_CvecNormToArray (Py_cvec * self) |
---|
| 94 | { |
---|
| 95 | npy_intp dims[] = { self->o->length, 1 }; |
---|
[7a7b00f] | 96 | return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm); |
---|
[615ac7d] | 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | PyObject * |
---|
| 101 | PyAubio_CvecPhasToArray (Py_cvec * self) |
---|
| 102 | { |
---|
| 103 | npy_intp dims[] = { self->o->length, 1 }; |
---|
[7a7b00f] | 104 | return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas); |
---|
[f826349] | 105 | } |
---|
| 106 | |
---|
[615ac7d] | 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 |
---|
[a8aaef3] | 126 | Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) |
---|
[615ac7d] | 127 | { |
---|
[1458de5] | 128 | PyArrayObject * array; |
---|
[a8aaef3] | 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 |
---|
[1458de5] | 136 | if (PyArray_NDIM ((PyArrayObject *)input) == 0) { |
---|
[a8aaef3] | 137 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
| 138 | goto fail; |
---|
[1458de5] | 139 | } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { |
---|
[a8aaef3] | 140 | PyErr_SetString (PyExc_ValueError, |
---|
| 141 | "input array has more than two dimensions"); |
---|
| 142 | goto fail; |
---|
| 143 | } |
---|
| 144 | |
---|
[1458de5] | 145 | if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { |
---|
[a8aaef3] | 146 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
| 147 | goto fail; |
---|
[1458de5] | 148 | } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { |
---|
[a8aaef3] | 149 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
| 150 | goto fail; |
---|
| 151 | } |
---|
[1458de5] | 152 | array = (PyArrayObject *)input; |
---|
[a8aaef3] | 153 | |
---|
| 154 | // check input array dimensions |
---|
[7a7b00f] | 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 { |
---|
[a8aaef3] | 161 | if (vec->o->length != PyArray_SIZE (array)) { |
---|
| 162 | PyErr_Format (PyExc_ValueError, |
---|
| 163 | "input array has length %d, but cvec has length %d", |
---|
[1458de5] | 164 | (int)PyArray_SIZE (array), vec->o->length); |
---|
[a8aaef3] | 165 | goto fail; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[7a7b00f] | 169 | vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); |
---|
[a8aaef3] | 170 | |
---|
| 171 | } else { |
---|
| 172 | PyErr_SetString (PyExc_ValueError, "can only accept array as input"); |
---|
| 173 | return 1; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | Py_INCREF(array); |
---|
[615ac7d] | 177 | return 0; |
---|
[a8aaef3] | 178 | |
---|
| 179 | fail: |
---|
| 180 | return 1; |
---|
[615ac7d] | 181 | } |
---|
| 182 | |
---|
| 183 | static int |
---|
[a8aaef3] | 184 | Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) |
---|
[615ac7d] | 185 | { |
---|
[1458de5] | 186 | PyArrayObject * array; |
---|
[a8aaef3] | 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 |
---|
[1458de5] | 194 | if (PyArray_NDIM ((PyArrayObject *)input) == 0) { |
---|
[a8aaef3] | 195 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
| 196 | goto fail; |
---|
[1458de5] | 197 | } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { |
---|
[a8aaef3] | 198 | PyErr_SetString (PyExc_ValueError, |
---|
| 199 | "input array has more than two dimensions"); |
---|
| 200 | goto fail; |
---|
| 201 | } |
---|
| 202 | |
---|
[1458de5] | 203 | if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { |
---|
[a8aaef3] | 204 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
| 205 | goto fail; |
---|
[1458de5] | 206 | } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { |
---|
[a8aaef3] | 207 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
| 208 | goto fail; |
---|
| 209 | } |
---|
[1458de5] | 210 | array = (PyArrayObject *)input; |
---|
[a8aaef3] | 211 | |
---|
| 212 | // check input array dimensions |
---|
[7a7b00f] | 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 { |
---|
[a8aaef3] | 219 | if (vec->o->length != PyArray_SIZE (array)) { |
---|
| 220 | PyErr_Format (PyExc_ValueError, |
---|
| 221 | "input array has length %d, but cvec has length %d", |
---|
[1458de5] | 222 | (int)PyArray_SIZE (array), vec->o->length); |
---|
[a8aaef3] | 223 | goto fail; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[7a7b00f] | 227 | vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); |
---|
[a8aaef3] | 228 | |
---|
| 229 | } else { |
---|
| 230 | PyErr_SetString (PyExc_ValueError, "can only accept array as input"); |
---|
| 231 | return 1; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | Py_INCREF(array); |
---|
[615ac7d] | 235 | return 0; |
---|
[a8aaef3] | 236 | |
---|
| 237 | fail: |
---|
| 238 | return 1; |
---|
[615ac7d] | 239 | } |
---|
| 240 | |
---|
[f826349] | 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 | |
---|
[615ac7d] | 252 | static PyGetSetDef Py_cvec_getseters[] = { |
---|
| 253 | {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, |
---|
[474f297] | 254 | "Numpy vector of shape (length,) containing the magnitude", |
---|
[615ac7d] | 255 | NULL}, |
---|
| 256 | {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, |
---|
[474f297] | 257 | "Numpy vector of shape (length,) containing the phase", |
---|
[615ac7d] | 258 | NULL}, |
---|
| 259 | {NULL} /* sentinel */ |
---|
| 260 | }; |
---|
| 261 | |
---|
[f826349] | 262 | PyTypeObject Py_cvecType = { |
---|
| 263 | PyObject_HEAD_INIT (NULL) |
---|
| 264 | 0, /* ob_size */ |
---|
| 265 | "aubio.cvec", /* tp_name */ |
---|
| 266 | sizeof (Py_cvec), /* tp_basicsize */ |
---|
| 267 | 0, /* tp_itemsize */ |
---|
| 268 | (destructor) Py_cvec_del, /* tp_dealloc */ |
---|
| 269 | 0, /* tp_print */ |
---|
| 270 | 0, /* tp_getattr */ |
---|
| 271 | 0, /* tp_setattr */ |
---|
| 272 | 0, /* tp_compare */ |
---|
| 273 | (reprfunc) Py_cvec_repr, /* tp_repr */ |
---|
| 274 | 0, /* tp_as_number */ |
---|
[7a7b00f] | 275 | 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ |
---|
[f826349] | 276 | 0, /* tp_as_mapping */ |
---|
| 277 | 0, /* tp_hash */ |
---|
| 278 | 0, /* tp_call */ |
---|
| 279 | 0, /* tp_str */ |
---|
| 280 | 0, /* tp_getattro */ |
---|
| 281 | 0, /* tp_setattro */ |
---|
| 282 | 0, /* tp_as_buffer */ |
---|
| 283 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
| 284 | Py_cvec_doc, /* tp_doc */ |
---|
| 285 | 0, /* tp_traverse */ |
---|
| 286 | 0, /* tp_clear */ |
---|
| 287 | 0, /* tp_richcompare */ |
---|
| 288 | 0, /* tp_weaklistoffset */ |
---|
| 289 | 0, /* tp_iter */ |
---|
| 290 | 0, /* tp_iternext */ |
---|
| 291 | Py_cvec_methods, /* tp_methods */ |
---|
| 292 | Py_cvec_members, /* tp_members */ |
---|
[615ac7d] | 293 | Py_cvec_getseters, /* tp_getset */ |
---|
[f826349] | 294 | 0, /* tp_base */ |
---|
| 295 | 0, /* tp_dict */ |
---|
| 296 | 0, /* tp_descr_get */ |
---|
| 297 | 0, /* tp_descr_set */ |
---|
| 298 | 0, /* tp_dictoffset */ |
---|
| 299 | (initproc) Py_cvec_init, /* tp_init */ |
---|
| 300 | 0, /* tp_alloc */ |
---|
| 301 | Py_cvec_new, /* tp_new */ |
---|
| 302 | }; |
---|