Changeset 92a8800 for python/ext/py-cvec.c
- Timestamp:
- Apr 28, 2016, 6:59:55 PM (8 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 6937842
- Parents:
- 1f4d932
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/ext/py-cvec.c
r1f4d932 r92a8800 1 1 #include "aubio-types.h" 2 2 3 /* cvec type definition 3 /* cvec type definition 4 4 5 5 class cvec(): 6 def __ init__(self, length = 1024):7 self.length = length 8 self.norm = array(length)9 self.phas = array(length)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) 10 10 11 11 */ 12 12 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 13 22 static char Py_cvec_doc[] = "cvec object"; 23 24 PyObject * 25 PyAubio_CCvecToPyCvec (cvec_t * input) { 26 if (input == NULL) { 27 PyErr_SetString (PyExc_ValueError, "PyAubio_CCvecToPyCvec got a null cvec!"); 28 return NULL; 29 } 30 Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); 31 npy_intp dims[] = { input->length, 1 }; 32 vec->norm = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->norm); 33 vec->phas = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->phas); 34 vec->length = input->length; 35 return (PyObject *)vec; 36 } 37 38 int 39 PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) { 40 if (PyObject_TypeCheck (input, &Py_cvecType)) { 41 Py_cvec * in = (Py_cvec *)input; 42 if (in->norm == NULL) { 43 npy_intp dims[] = { in->length, 1 }; 44 in->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); 45 } 46 if (in->phas == NULL) { 47 npy_intp dims[] = { in->length, 1 }; 48 in->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); 49 } 50 i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0); 51 i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0); 52 i->length = ((Py_cvec*)input)->length; 53 return 1; 54 } else { 55 PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec"); 56 return 0; 57 } 58 } 14 59 15 60 static PyObject * … … 24 69 return NULL; 25 70 } 26 27 71 28 72 self = (Py_cvec *) type->tp_alloc (type, 0); … … 48 92 Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) 49 93 { 50 self->o = new_cvec ((self->length - 1) * 2); 51 if (self->o == NULL) { 52 return -1; 53 } 54 94 self->norm = NULL; 95 self->phas = NULL; 55 96 return 0; 56 97 } … … 59 100 Py_cvec_del (Py_cvec * self) 60 101 { 61 del_cvec (self->o); 102 Py_XDECREF(self->norm); 103 Py_XDECREF(self->phas); 62 104 Py_TYPE(self)->tp_free ((PyObject *) self); 63 105 } … … 79 121 goto fail; 80 122 } 81 cvec_print ( self->o );123 // hide actual norm / phas content 82 124 83 125 result = PyUnicode_Format (format, args); … … 91 133 92 134 PyObject * 93 PyAubio_CvecNormToArray (Py_cvec * self)94 {95 npy_intp dims[] = { self->o->length, 1 };96 return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->norm);97 }98 99 100 PyObject *101 PyAubio_CvecPhasToArray (Py_cvec * self)102 {103 npy_intp dims[] = { self->o->length, 1 };104 return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->phas);105 }106 107 PyObject *108 PyAubio_ArrayToCvecPhas (PyObject * self)109 {110 return NULL;111 }112 113 PyObject *114 135 Py_cvec_get_norm (Py_cvec * self, void *closure) 115 136 { 116 return PyAubio_CvecNormToArray(self); 137 // if it norm hasn't been created, create it now 138 if (self->norm == NULL) { 139 npy_intp dims[] = { self->length, 1 }; 140 self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); 141 } 142 Py_INCREF(self->norm); 143 return (PyObject*)(self->norm); 117 144 } 118 145 … … 120 147 Py_cvec_get_phas (Py_cvec * self, void *closure) 121 148 { 122 return PyAubio_CvecPhasToArray(self); 149 // if it phas hasn't been created, create it now 150 if (self->phas == NULL) { 151 npy_intp dims[] = { self->length, 1 }; 152 self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); 153 } 154 Py_INCREF(self->phas); 155 return (PyObject *)(self->phas); 123 156 } 124 157 … … 132 165 } 133 166 if (PyArray_Check(input)) { 134 135 // we got an array, convert it to a cvec.norm 167 // we got an array, convert it to a cvec.norm 136 168 if (PyArray_NDIM ((PyArrayObject *)input) == 0) { 137 169 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); … … 159 191 goto fail; 160 192 } else { 161 if (vec-> o->length != PyArray_SIZE (array)) {193 if (vec->length != PyArray_SIZE (array)) { 162 194 PyErr_Format (PyExc_ValueError, 163 195 "input array has length %d, but cvec has length %d", 164 (int)PyArray_SIZE (array), vec-> o->length);196 (int)PyArray_SIZE (array), vec->length); 165 197 goto fail; 166 198 } 167 199 } 168 200 169 vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); 201 Py_XDECREF(vec->norm); 202 vec->norm = input; 203 Py_INCREF(vec->norm); 170 204 171 205 } else { … … 174 208 } 175 209 176 Py_INCREF(array);177 210 return 0; 178 211 … … 217 250 goto fail; 218 251 } else { 219 if (vec-> o->length != PyArray_SIZE (array)) {252 if (vec->length != PyArray_SIZE (array)) { 220 253 PyErr_Format (PyExc_ValueError, 221 254 "input array has length %d, but cvec has length %d", 222 (int)PyArray_SIZE (array), vec-> o->length);255 (int)PyArray_SIZE (array), vec->length); 223 256 goto fail; 224 257 } 225 258 } 226 259 227 vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); 260 Py_XDECREF(vec->phas); 261 vec->phas = input; 262 Py_INCREF(vec->phas); 228 263 229 264 } else { … … 232 267 } 233 268 234 Py_INCREF(array);235 269 return 0; 236 270
Note: See TracChangeset
for help on using the changeset viewer.