[f826349] | 1 | #include "aubio-types.h" |
---|
| 2 | |
---|
[92a8800] | 3 | /* cvec type definition |
---|
[f826349] | 4 | |
---|
| 5 | class cvec(): |
---|
[92a8800] | 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) |
---|
[f826349] | 10 | |
---|
[7a7b00f] | 11 | */ |
---|
[f826349] | 12 | |
---|
[92a8800] | 13 | // special python type for cvec |
---|
| 14 | typedef struct |
---|
| 15 | { |
---|
| 16 | PyObject_HEAD |
---|
| 17 | PyObject *norm; |
---|
| 18 | PyObject *phas; |
---|
| 19 | uint_t length; |
---|
| 20 | } Py_cvec; |
---|
| 21 | |
---|
[92948ab] | 22 | static char Py_cvec_doc[] = "" |
---|
| 23 | "cvec(size)\n" |
---|
| 24 | "\n" |
---|
| 25 | "Data structure to hold spectral vectors.\n" |
---|
| 26 | "\n" |
---|
| 27 | "A vector holding spectral data in two vectors, :attr:`phas`\n" |
---|
| 28 | "and :attr:`norm`. Each vector is a :class:`numpy.ndarray`\n" |
---|
| 29 | "of shape `(length,)`, where `length = size // 2 + 1`.\n" |
---|
| 30 | "\n" |
---|
| 31 | "Parameters\n" |
---|
| 32 | "----------\n" |
---|
| 33 | "size: int\n" |
---|
| 34 | " Size of spectrum to create.\n" |
---|
| 35 | "\n" |
---|
| 36 | "Examples\n" |
---|
| 37 | "--------\n" |
---|
| 38 | ">>> c = aubio.cvec(1024)\n" |
---|
| 39 | ">>> c\n" |
---|
| 40 | "aubio cvec of 513 elements\n" |
---|
| 41 | ">>> c.length\n" |
---|
| 42 | "513\n" |
---|
| 43 | ">>> c.norm.dtype, c.phas.dtype\n" |
---|
| 44 | "(dtype('float32'), dtype('float32'))\n" |
---|
| 45 | ">>> c.norm.shape, c.phas.shape\n" |
---|
| 46 | "((513,), (513,))\n" |
---|
| 47 | "\n" |
---|
| 48 | "See Also\n" |
---|
| 49 | "--------\n" |
---|
| 50 | "fft, pvoc\n" |
---|
| 51 | ""; |
---|
[f826349] | 52 | |
---|
[ede5d38] | 53 | |
---|
| 54 | PyObject * |
---|
| 55 | new_py_cvec(uint_t length) { |
---|
| 56 | Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); |
---|
| 57 | npy_intp dims[] = { length / 2 + 1, 1 }; |
---|
| 58 | vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
| 59 | vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
| 60 | vec->length = length / 2 + 1; |
---|
| 61 | return (PyObject*)vec; |
---|
| 62 | } |
---|
| 63 | |
---|
[92a8800] | 64 | int |
---|
| 65 | PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) { |
---|
| 66 | if (PyObject_TypeCheck (input, &Py_cvecType)) { |
---|
| 67 | Py_cvec * in = (Py_cvec *)input; |
---|
| 68 | i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0); |
---|
| 69 | i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0); |
---|
| 70 | i->length = ((Py_cvec*)input)->length; |
---|
| 71 | return 1; |
---|
| 72 | } else { |
---|
| 73 | PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec"); |
---|
| 74 | return 0; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
[f826349] | 78 | static PyObject * |
---|
| 79 | Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
| 80 | { |
---|
[7a7b00f] | 81 | int length= 0; |
---|
[f826349] | 82 | Py_cvec *self; |
---|
[7a7b00f] | 83 | static char *kwlist[] = { "length", NULL }; |
---|
[f826349] | 84 | |
---|
[7a7b00f] | 85 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
| 86 | &length)) { |
---|
[f826349] | 87 | return NULL; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | self = (Py_cvec *) type->tp_alloc (type, 0); |
---|
| 91 | |
---|
[eb93592] | 92 | self->length = Py_default_vector_length / 2 + 1; |
---|
[f826349] | 93 | |
---|
| 94 | if (self == NULL) { |
---|
| 95 | return NULL; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | if (length > 0) { |
---|
[eb93592] | 99 | self->length = length / 2 + 1; |
---|
[f826349] | 100 | } else if (length < 0) { |
---|
| 101 | PyErr_SetString (PyExc_ValueError, |
---|
| 102 | "can not use negative number of elements"); |
---|
| 103 | return NULL; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return (PyObject *) self; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | static int |
---|
| 110 | Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) |
---|
| 111 | { |
---|
[ede5d38] | 112 | npy_intp dims[] = { self->length, 1 }; |
---|
| 113 | self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
| 114 | self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
[f826349] | 115 | return 0; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | static void |
---|
| 119 | Py_cvec_del (Py_cvec * self) |
---|
| 120 | { |
---|
[ede5d38] | 121 | Py_DECREF(self->norm); |
---|
| 122 | Py_DECREF(self->phas); |
---|
[770b9e7] | 123 | Py_TYPE(self)->tp_free ((PyObject *) self); |
---|
[f826349] | 124 | } |
---|
| 125 | |
---|
| 126 | static PyObject * |
---|
| 127 | Py_cvec_repr (Py_cvec * self, PyObject * unused) |
---|
| 128 | { |
---|
| 129 | PyObject *format = NULL; |
---|
| 130 | PyObject *args = NULL; |
---|
| 131 | PyObject *result = NULL; |
---|
| 132 | |
---|
[5c1200a] | 133 | format = PyUnicode_FromString ("aubio cvec of %d elements"); |
---|
[f826349] | 134 | if (format == NULL) { |
---|
| 135 | goto fail; |
---|
| 136 | } |
---|
| 137 | |
---|
[7a7b00f] | 138 | args = Py_BuildValue ("I", self->length); |
---|
[f826349] | 139 | if (args == NULL) { |
---|
| 140 | goto fail; |
---|
| 141 | } |
---|
[92a8800] | 142 | // hide actual norm / phas content |
---|
[f826349] | 143 | |
---|
[5c1200a] | 144 | result = PyUnicode_Format (format, args); |
---|
[f826349] | 145 | |
---|
| 146 | fail: |
---|
| 147 | Py_XDECREF (format); |
---|
| 148 | Py_XDECREF (args); |
---|
| 149 | |
---|
| 150 | return result; |
---|
| 151 | } |
---|
| 152 | |
---|
[615ac7d] | 153 | PyObject * |
---|
| 154 | Py_cvec_get_norm (Py_cvec * self, void *closure) |
---|
| 155 | { |
---|
[ede5d38] | 156 | // we want self->norm to still exist after our caller return it |
---|
[92a8800] | 157 | Py_INCREF(self->norm); |
---|
| 158 | return (PyObject*)(self->norm); |
---|
[615ac7d] | 159 | } |
---|
| 160 | |
---|
| 161 | PyObject * |
---|
| 162 | Py_cvec_get_phas (Py_cvec * self, void *closure) |
---|
| 163 | { |
---|
[ede5d38] | 164 | // we want self->phas to still exist after our caller return it |
---|
[92a8800] | 165 | Py_INCREF(self->phas); |
---|
| 166 | return (PyObject *)(self->phas); |
---|
[615ac7d] | 167 | } |
---|
| 168 | |
---|
| 169 | static int |
---|
[a8aaef3] | 170 | Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) |
---|
[615ac7d] | 171 | { |
---|
[a138975] | 172 | npy_intp length; |
---|
[34d0c25] | 173 | if (!PyAubio_IsValidVector(input)) { |
---|
[4deb255] | 174 | return -1; |
---|
[a8aaef3] | 175 | } |
---|
[a138975] | 176 | length = PyArray_SIZE ((PyArrayObject *)input); |
---|
[34d0c25] | 177 | if (length != vec->length) { |
---|
| 178 | PyErr_Format (PyExc_ValueError, |
---|
[a203d0e] | 179 | "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length, |
---|
[34d0c25] | 180 | vec->length); |
---|
[4deb255] | 181 | return -1; |
---|
[a8aaef3] | 182 | } |
---|
| 183 | |
---|
[34d0c25] | 184 | Py_XDECREF(vec->norm); |
---|
| 185 | vec->norm = input; |
---|
| 186 | Py_INCREF(vec->norm); |
---|
[615ac7d] | 187 | return 0; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | static int |
---|
[a8aaef3] | 191 | Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) |
---|
[615ac7d] | 192 | { |
---|
[a138975] | 193 | npy_intp length; |
---|
[34d0c25] | 194 | if (!PyAubio_IsValidVector(input)) { |
---|
[4deb255] | 195 | return -1; |
---|
[a8aaef3] | 196 | } |
---|
[a138975] | 197 | length = PyArray_SIZE ((PyArrayObject *)input); |
---|
[34d0c25] | 198 | if (length != vec->length) { |
---|
| 199 | PyErr_Format (PyExc_ValueError, |
---|
[a203d0e] | 200 | "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length, |
---|
[34d0c25] | 201 | vec->length); |
---|
[4deb255] | 202 | return -1; |
---|
[a8aaef3] | 203 | } |
---|
| 204 | |
---|
[34d0c25] | 205 | Py_XDECREF(vec->phas); |
---|
| 206 | vec->phas = input; |
---|
| 207 | Py_INCREF(vec->phas); |
---|
[615ac7d] | 208 | return 0; |
---|
| 209 | } |
---|
| 210 | |
---|
[f826349] | 211 | static PyMemberDef Py_cvec_members[] = { |
---|
| 212 | // TODO remove READONLY flag and define getter/setter |
---|
| 213 | {"length", T_INT, offsetof (Py_cvec, length), READONLY, |
---|
[92948ab] | 214 | "int: Length of `norm` and `phas` vectors."}, |
---|
[f826349] | 215 | {NULL} /* Sentinel */ |
---|
| 216 | }; |
---|
| 217 | |
---|
| 218 | static PyMethodDef Py_cvec_methods[] = { |
---|
| 219 | {NULL} |
---|
| 220 | }; |
---|
| 221 | |
---|
[615ac7d] | 222 | static PyGetSetDef Py_cvec_getseters[] = { |
---|
[92948ab] | 223 | {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, |
---|
| 224 | "numpy.ndarray: Vector of shape `(length,)` containing the magnitude.", |
---|
[615ac7d] | 225 | NULL}, |
---|
[92948ab] | 226 | {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, |
---|
| 227 | "numpy.ndarray: Vector of shape `(length,)` containing the phase.", |
---|
[615ac7d] | 228 | NULL}, |
---|
| 229 | {NULL} /* sentinel */ |
---|
| 230 | }; |
---|
| 231 | |
---|
[f826349] | 232 | PyTypeObject Py_cvecType = { |
---|
[5c1200a] | 233 | PyVarObject_HEAD_INIT(NULL, 0) |
---|
[f826349] | 234 | "aubio.cvec", /* tp_name */ |
---|
| 235 | sizeof (Py_cvec), /* tp_basicsize */ |
---|
| 236 | 0, /* tp_itemsize */ |
---|
| 237 | (destructor) Py_cvec_del, /* tp_dealloc */ |
---|
| 238 | 0, /* tp_print */ |
---|
| 239 | 0, /* tp_getattr */ |
---|
| 240 | 0, /* tp_setattr */ |
---|
| 241 | 0, /* tp_compare */ |
---|
| 242 | (reprfunc) Py_cvec_repr, /* tp_repr */ |
---|
| 243 | 0, /* tp_as_number */ |
---|
[f2ce0fc] | 244 | 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ |
---|
[f826349] | 245 | 0, /* tp_as_mapping */ |
---|
| 246 | 0, /* tp_hash */ |
---|
| 247 | 0, /* tp_call */ |
---|
| 248 | 0, /* tp_str */ |
---|
| 249 | 0, /* tp_getattro */ |
---|
| 250 | 0, /* tp_setattro */ |
---|
| 251 | 0, /* tp_as_buffer */ |
---|
| 252 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
| 253 | Py_cvec_doc, /* tp_doc */ |
---|
| 254 | 0, /* tp_traverse */ |
---|
| 255 | 0, /* tp_clear */ |
---|
| 256 | 0, /* tp_richcompare */ |
---|
| 257 | 0, /* tp_weaklistoffset */ |
---|
| 258 | 0, /* tp_iter */ |
---|
| 259 | 0, /* tp_iternext */ |
---|
| 260 | Py_cvec_methods, /* tp_methods */ |
---|
| 261 | Py_cvec_members, /* tp_members */ |
---|
[615ac7d] | 262 | Py_cvec_getseters, /* tp_getset */ |
---|
[f826349] | 263 | 0, /* tp_base */ |
---|
| 264 | 0, /* tp_dict */ |
---|
| 265 | 0, /* tp_descr_get */ |
---|
| 266 | 0, /* tp_descr_set */ |
---|
| 267 | 0, /* tp_dictoffset */ |
---|
| 268 | (initproc) Py_cvec_init, /* tp_init */ |
---|
| 269 | 0, /* tp_alloc */ |
---|
| 270 | Py_cvec_new, /* tp_new */ |
---|
[0e70ef9] | 271 | 0, |
---|
| 272 | 0, |
---|
| 273 | 0, |
---|
| 274 | 0, |
---|
| 275 | 0, |
---|
| 276 | 0, |
---|
| 277 | 0, |
---|
| 278 | 0, |
---|
| 279 | 0, |
---|
[f826349] | 280 | }; |
---|