Changeset f264b17 for python/ext/py-cvec.c
- Timestamp:
- Jun 22, 2016, 1:00:10 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:
- 4b9443c4
- Parents:
- 60fc05b (diff), 6769586 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/ext/py-cvec.c
r60fc05b rf264b17 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 25 PyObject * 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; 33 } 34 35 int 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; 42 return 1; 43 } else { 44 PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec"); 45 return 0; 46 } 47 } 14 48 15 49 static PyObject * … … 24 58 return NULL; 25 59 } 26 27 60 28 61 self = (Py_cvec *) type->tp_alloc (type, 0); … … 48 81 Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) 49 82 { 50 self->o = new_cvec ((self->length - 1) * 2); 51 if (self->o == NULL) { 52 return -1; 53 } 54 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); 55 86 return 0; 56 87 } … … 59 90 Py_cvec_del (Py_cvec * self) 60 91 { 61 del_cvec (self->o); 62 self->ob_type->tp_free ((PyObject *) self); 92 Py_DECREF(self->norm); 93 Py_DECREF(self->phas); 94 Py_TYPE(self)->tp_free ((PyObject *) self); 63 95 } 64 96 … … 70 102 PyObject *result = NULL; 71 103 72 format = Py String_FromString ("aubio cvec of %d elements");104 format = PyUnicode_FromString ("aubio cvec of %d elements"); 73 105 if (format == NULL) { 74 106 goto fail; … … 79 111 goto fail; 80 112 } 81 cvec_print ( self->o );82 83 result = Py String_Format (format, args);113 // hide actual norm / phas content 114 115 result = PyUnicode_Format (format, args); 84 116 85 117 fail: … … 91 123 92 124 PyObject * 93 PyAubio_CvecNormToArray (Py_cvec * self)94 {95 npy_intp dims[] = { self->o->length, 1 };96 return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, 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, NPY_FLOAT, self->o->phas);105 }106 107 PyObject *108 PyAubio_ArrayToCvecPhas (PyObject * self)109 {110 return NULL;111 }112 113 PyObject *114 125 Py_cvec_get_norm (Py_cvec * self, void *closure) 115 126 { 116 return PyAubio_CvecNormToArray(self); 127 // we want self->norm to still exist after our caller return it 128 Py_INCREF(self->norm); 129 return (PyObject*)(self->norm); 117 130 } 118 131 … … 120 133 Py_cvec_get_phas (Py_cvec * self, void *closure) 121 134 { 122 return PyAubio_CvecPhasToArray(self); 135 // we want self->phas to still exist after our caller return it 136 Py_INCREF(self->phas); 137 return (PyObject *)(self->phas); 123 138 } 124 139 … … 126 141 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) 127 142 { 128 PyArrayObject * array; 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 136 if (PyArray_NDIM ((PyArrayObject *)input) == 0) { 137 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 138 goto fail; 139 } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { 140 PyErr_SetString (PyExc_ValueError, 141 "input array has more than two dimensions"); 142 goto fail; 143 } 144 145 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { 146 PyErr_SetString (PyExc_ValueError, "input array should be float"); 147 goto fail; 148 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { 149 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 150 goto fail; 151 } 152 array = (PyArrayObject *)input; 153 154 // check input array dimensions 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 { 161 if (vec->o->length != PyArray_SIZE (array)) { 162 PyErr_Format (PyExc_ValueError, 163 "input array has length %d, but cvec has length %d", 164 (int)PyArray_SIZE (array), vec->o->length); 165 goto fail; 166 } 167 } 168 169 vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); 170 171 } else { 172 PyErr_SetString (PyExc_ValueError, "can only accept array as input"); 173 return 1; 174 } 175 176 Py_INCREF(array); 143 npy_intp length; 144 if (!PyAubio_IsValidVector(input)) { 145 return 1; 146 } 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, 151 vec->length); 152 return 1; 153 } 154 155 Py_XDECREF(vec->norm); 156 vec->norm = input; 157 Py_INCREF(vec->norm); 177 158 return 0; 178 179 fail:180 return 1;181 159 } 182 160 … … 184 162 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) 185 163 { 186 PyArrayObject * array; 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 194 if (PyArray_NDIM ((PyArrayObject *)input) == 0) { 195 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 196 goto fail; 197 } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { 198 PyErr_SetString (PyExc_ValueError, 199 "input array has more than two dimensions"); 200 goto fail; 201 } 202 203 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { 204 PyErr_SetString (PyExc_ValueError, "input array should be float"); 205 goto fail; 206 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { 207 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 208 goto fail; 209 } 210 array = (PyArrayObject *)input; 211 212 // check input array dimensions 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 { 219 if (vec->o->length != PyArray_SIZE (array)) { 220 PyErr_Format (PyExc_ValueError, 221 "input array has length %d, but cvec has length %d", 222 (int)PyArray_SIZE (array), vec->o->length); 223 goto fail; 224 } 225 } 226 227 vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); 228 229 } else { 230 PyErr_SetString (PyExc_ValueError, "can only accept array as input"); 231 return 1; 232 } 233 234 Py_INCREF(array); 164 npy_intp length; 165 if (!PyAubio_IsValidVector(input)) { 166 return 1; 167 } 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, 172 vec->length); 173 return 1; 174 } 175 176 Py_XDECREF(vec->phas); 177 vec->phas = input; 178 Py_INCREF(vec->phas); 235 179 return 0; 236 237 fail:238 return 1;239 180 } 240 181 … … 261 202 262 203 PyTypeObject Py_cvecType = { 263 PyObject_HEAD_INIT (NULL) 264 0, /* ob_size */ 204 PyVarObject_HEAD_INIT(NULL, 0) 265 205 "aubio.cvec", /* tp_name */ 266 206 sizeof (Py_cvec), /* tp_basicsize */ … … 273 213 (reprfunc) Py_cvec_repr, /* tp_repr */ 274 214 0, /* tp_as_number */ 275 0, //&Py_cvec_tp_as_sequence, 215 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ 276 216 0, /* tp_as_mapping */ 277 217 0, /* tp_hash */ … … 300 240 0, /* tp_alloc */ 301 241 Py_cvec_new, /* tp_new */ 302 }; 242 0, 243 0, 244 0, 245 0, 246 0, 247 0, 248 0, 249 0, 250 0, 251 };
Note: See TracChangeset
for help on using the changeset viewer.