Changeset 7a7b00f
- Timestamp:
- Dec 5, 2009, 1:42:46 AM (15 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:
- 96fe713
- Parents:
- f7b05d4
- Location:
- interfaces/python
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
interfaces/python/py-cvec.c
rf7b05d4 r7a7b00f 4 4 5 5 class cvec(): 6 def __init__(self, length = 1024 , channels = 1):6 def __init__(self, length = 1024): 7 7 self.length = length 8 self. channels = channels9 self. norm = array(length, channels)10 self.phas = array(length, channels) 8 self.norm = array(length) 9 self.phas = array(length) 10 11 11 */ 12 13 12 14 13 static char Py_cvec_doc[] = "cvec object"; … … 17 16 Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) 18 17 { 19 int length= 0 , channels = 0;18 int length= 0; 20 19 Py_cvec *self; 21 static char *kwlist[] = { "length", "channels",NULL };22 23 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I I", kwlist,24 &length , &channels)) {20 static char *kwlist[] = { "length", NULL }; 21 22 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, 23 &length)) { 25 24 return NULL; 26 25 } … … 30 29 31 30 self->length = Py_default_vector_length / 2 + 1; 32 self->channels = Py_default_vector_channels;33 31 34 32 if (self == NULL) { … … 44 42 } 45 43 46 if (channels > 0) {47 self->channels = channels;48 } else if (channels < 0) {49 PyErr_SetString (PyExc_ValueError,50 "can not use negative number of channels");51 return NULL;52 }53 54 44 return (PyObject *) self; 55 45 } … … 58 48 Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) 59 49 { 60 self->o = new_cvec ((self->length - 1) * 2 , self->channels);50 self->o = new_cvec ((self->length - 1) * 2); 61 51 if (self->o == NULL) { 62 52 return -1; … … 80 70 PyObject *result = NULL; 81 71 82 format = PyString_FromString ("aubio cvec of %d elements with %d channels");72 format = PyString_FromString ("aubio cvec of %d elements"); 83 73 if (format == NULL) { 84 74 goto fail; 85 75 } 86 76 87 args = Py_BuildValue ("I I", self->length, self->channels);77 args = Py_BuildValue ("I", self->length); 88 78 if (args == NULL) { 89 79 goto fail; 90 80 } 91 //cvec_print ( self->o );81 cvec_print ( self->o ); 92 82 93 83 result = PyString_Format (format, args); … … 104 94 PyObject *array; 105 95 Py_cvec *vec; 106 uint_t i; 96 if (input == NULL) { 97 PyErr_SetString (PyExc_ValueError, "input array is not a python object"); 98 goto fail; 99 } 107 100 // parsing input object into a Py_cvec 108 101 if (PyObject_TypeCheck (input, &Py_cvecType)) { … … 115 108 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 116 109 goto fail; 110 } else if (PyArray_NDIM (input) > 1) { 111 PyErr_SetString (PyExc_ValueError, 112 "input array has more than one dimensions"); 113 goto fail; 114 } 115 116 if (!PyArray_ISFLOAT (input)) { 117 PyErr_SetString (PyExc_ValueError, "input array should be float"); 118 goto fail; 119 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 120 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 121 goto fail; 122 } else { 123 // input data type is float32, nothing else to do 124 array = input; 125 } 126 127 // create a new cvec object 128 vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); 129 if (PyArray_NDIM (array) != 2) { 130 PyErr_SetString (PyExc_ValueError, 131 "input array should be have exactly two rows for norm and phas"); 132 goto fail; 133 } else { 134 vec->length = PyArray_SIZE (array); 135 } 136 137 // no need to really allocate cvec, just its struct member 138 // vec->o = new_cvec (vec->length); 139 vec->o = (cvec_t *)malloc(sizeof(cvec_t)); 140 vec->o->length = vec->length; 141 // have norm and phas point to array rows 142 vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); 143 vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 1); 144 145 } else { 146 PyErr_SetString (PyExc_ValueError, "can only accept array or cvec as input"); 147 return NULL; 148 } 149 150 return vec; 151 152 fail: 153 return NULL; 154 } 155 156 PyObject * 157 PyAubio_CvecToArray (Py_cvec * self) 158 { 159 PyObject *array = NULL; 160 npy_intp dims[] = { self->o->length, 1 }; 161 PyObject *concat = PyList_New (0), *tmp = NULL; 162 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm); 163 PyList_Append (concat, tmp); 164 Py_DECREF (tmp); 165 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas); 166 PyList_Append (concat, tmp); 167 Py_DECREF (tmp); 168 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2); 169 Py_DECREF (concat); 170 return array; 171 } 172 173 PyObject * 174 PyAubio_CvecNormToArray (Py_cvec * self) 175 { 176 npy_intp dims[] = { self->o->length, 1 }; 177 return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm); 178 } 179 180 181 PyObject * 182 PyAubio_CvecPhasToArray (Py_cvec * self) 183 { 184 npy_intp dims[] = { self->o->length, 1 }; 185 return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas); 186 } 187 188 PyObject * 189 PyAubio_ArrayToCvecPhas (PyObject * self) 190 { 191 return NULL; 192 } 193 194 PyObject * 195 Py_cvec_get_norm (Py_cvec * self, void *closure) 196 { 197 return PyAubio_CvecNormToArray(self); 198 } 199 200 PyObject * 201 Py_cvec_get_phas (Py_cvec * self, void *closure) 202 { 203 return PyAubio_CvecPhasToArray(self); 204 } 205 206 static int 207 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) 208 { 209 PyObject * array; 210 if (input == NULL) { 211 PyErr_SetString (PyExc_ValueError, "input array is not a python object"); 212 goto fail; 213 } 214 if (PyArray_Check(input)) { 215 216 // we got an array, convert it to a cvec.norm 217 if (PyArray_NDIM (input) == 0) { 218 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 219 goto fail; 117 220 } else if (PyArray_NDIM (input) > 2) { 118 221 PyErr_SetString (PyExc_ValueError, … … 124 227 PyErr_SetString (PyExc_ValueError, "input array should be float"); 125 228 goto fail; 126 #if AUBIO_DO_CASTING127 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {128 // input data type is not float32, casting129 array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL);130 if (array == NULL) {131 PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");132 goto fail;133 }134 #else135 229 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 136 230 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 137 231 goto fail; 138 #endif 232 } 233 array = input; 234 235 // check input array dimensions 236 if (PyArray_NDIM (array) != 1) { 237 PyErr_Format (PyExc_ValueError, 238 "input array has %d dimensions, not 1", 239 PyArray_NDIM (array)); 240 goto fail; 139 241 } else { 140 // input data type is float32, nothing else to do141 array = input;142 }143 144 // create a new cvec object145 vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);146 if (PyArray_NDIM (array) == 1) {147 PyErr_SetString (PyExc_ValueError,148 "input array should be have at least two rows for norm and phas");149 goto fail;150 } else if (PyArray_NDIM (array) == 2) {151 vec->channels = 1;152 vec->length = PyArray_SIZE (array);153 } else {154 vec->channels = PyArray_DIM (array, 0) / 2;155 vec->length = PyArray_DIM (array, 1);156 }157 158 // no need to really allocate cvec, just its struct member159 // vec->o = new_cvec (vec->length, vec->channels);160 vec->o = (cvec_t *)malloc(sizeof(cvec_t));161 vec->o->length = vec->length; vec->o->channels = vec->channels;162 vec->o->norm = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));163 vec->o->phas = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));164 // hat data[i] point to array line165 for (i = 0; i < vec->channels; i+=2) {166 vec->o->norm[i] = (smpl_t *) PyArray_GETPTR1 (array, i);167 vec->o->phas[i] = (smpl_t *) PyArray_GETPTR1 (array, i+1);168 }169 170 } else {171 PyErr_SetString (PyExc_ValueError, "can only accept array or cvec as input");172 return NULL;173 }174 175 return vec;176 177 fail:178 return NULL;179 }180 181 PyObject *182 PyAubio_CvecToArray (Py_cvec * self)183 {184 PyObject *array = NULL;185 uint_t i;186 npy_intp dims[] = { self->o->length, 1 };187 PyObject *concat = PyList_New (0), *tmp = NULL;188 for (i = 0; i < self->channels; i++) {189 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);190 PyList_Append (concat, tmp);191 Py_DECREF (tmp);192 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]);193 PyList_Append (concat, tmp);194 Py_DECREF (tmp);195 }196 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);197 Py_DECREF (concat);198 return array;199 }200 201 PyObject *202 PyAubio_CvecNormToArray (Py_cvec * self)203 {204 PyObject *array = NULL;205 uint_t i;206 npy_intp dims[] = { self->o->length, 1 };207 PyObject *concat = PyList_New (0), *tmp = NULL;208 for (i = 0; i < self->channels; i++) {209 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);210 PyList_Append (concat, tmp);211 Py_DECREF (tmp);212 }213 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);214 Py_DECREF (concat);215 return array;216 }217 218 219 PyObject *220 PyAubio_CvecPhasToArray (Py_cvec * self)221 {222 PyObject *array = NULL;223 uint_t i;224 npy_intp dims[] = { self->o->length, 1 };225 PyObject *concat = PyList_New (0), *tmp = NULL;226 for (i = 0; i < self->channels; i++) {227 tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]);228 PyList_Append (concat, tmp);229 Py_DECREF (tmp);230 }231 array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);232 Py_DECREF (concat);233 return array;234 }235 236 PyObject *237 PyAubio_ArrayToCvecPhas (PyObject * self)238 {239 return NULL;240 }241 242 PyObject *243 Py_cvec_get_norm (Py_cvec * self, void *closure)244 {245 return PyAubio_CvecNormToArray(self);246 }247 248 PyObject *249 Py_cvec_get_phas (Py_cvec * self, void *closure)250 {251 return PyAubio_CvecPhasToArray(self);252 }253 254 static int255 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)256 {257 uint_t i;258 PyObject * array;259 if (input == NULL) {260 PyErr_SetString (PyExc_ValueError, "input array is not a python object");261 goto fail;262 }263 if (PyArray_Check(input)) {264 265 // we got an array, convert it to a cvec.norm266 if (PyArray_NDIM (input) == 0) {267 PyErr_SetString (PyExc_ValueError, "input array is a scalar");268 goto fail;269 } else if (PyArray_NDIM (input) > 2) {270 PyErr_SetString (PyExc_ValueError,271 "input array has more than two dimensions");272 goto fail;273 }274 275 if (!PyArray_ISFLOAT (input)) {276 PyErr_SetString (PyExc_ValueError, "input array should be float");277 goto fail;278 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {279 PyErr_SetString (PyExc_ValueError, "input array should be float32");280 goto fail;281 }282 array = input;283 284 // check input array dimensions285 if (PyArray_NDIM (array) == 1) {286 if (vec->channels != 1) {287 PyErr_SetString (PyExc_ValueError,288 "input array should have more than one channel");289 goto fail;290 }291 242 if (vec->o->length != PyArray_SIZE (array)) { 292 243 PyErr_Format (PyExc_ValueError, … … 295 246 goto fail; 296 247 } 297 } else { 298 if (vec->channels != PyArray_DIM (array, 0)) { 299 PyErr_Format (PyExc_ValueError, 300 "input array has %d channels, but vector has %d channels", 301 PyArray_DIM (array, 0), vec->channels); 302 goto fail; 303 } 304 if (vec->o->length != PyArray_DIM (array, 1)) { 305 PyErr_Format (PyExc_ValueError, 306 "input array has length %d, but vector has length %d", 307 PyArray_DIM (array, 1), vec->o->length); 308 goto fail; 309 } 310 } 311 312 for (i = 0; i < vec->channels; i++) { 313 vec->o->norm[i] = (smpl_t *) PyArray_GETPTR1 (array, i); 314 } 248 } 249 250 vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0); 315 251 316 252 } else { … … 329 265 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) 330 266 { 331 uint_t i;332 267 PyObject * array; 333 268 if (input == NULL) { … … 357 292 358 293 // check input array dimensions 359 if (PyArray_NDIM (array) == 1) {360 if (vec->channels != 1) {361 PyErr_SetString (PyExc_ValueError,362 "input array should have more than one channel");363 364 }294 if (PyArray_NDIM (array) != 1) { 295 PyErr_Format (PyExc_ValueError, 296 "input array has %d dimensions, not 1", 297 PyArray_NDIM (array)); 298 goto fail; 299 } else { 365 300 if (vec->o->length != PyArray_SIZE (array)) { 366 301 PyErr_Format (PyExc_ValueError, … … 369 304 goto fail; 370 305 } 371 } else { 372 if (vec->channels != PyArray_DIM (array, 0)) { 373 PyErr_Format (PyExc_ValueError, 374 "input array has %d channels, but vector has %d channels", 375 PyArray_DIM (array, 0), vec->channels); 376 goto fail; 377 } 378 if (vec->o->length != PyArray_DIM (array, 1)) { 379 PyErr_Format (PyExc_ValueError, 380 "input array has length %d, but vector has length %d", 381 PyArray_DIM (array, 1), vec->o->length); 382 goto fail; 383 } 384 } 385 386 for (i = 0; i < vec->channels; i++) { 387 vec->o->phas[i] = (smpl_t *) PyArray_GETPTR1 (array, i); 388 } 306 } 307 308 vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0); 389 309 390 310 } else { … … 398 318 fail: 399 319 return 1; 400 }401 402 static Py_ssize_t403 Py_cvec_getchannels (Py_cvec * self)404 {405 return self->channels;406 }407 408 static PyObject *409 Py_cvec_getitem (Py_cvec * self, Py_ssize_t index)410 {411 PyObject *array;412 413 if (index < 0 || index >= self->channels) {414 PyErr_SetString (PyExc_IndexError, "no such channel");415 return NULL;416 }417 418 npy_intp dims[] = { self->length, 1 };419 array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[index]);420 return array;421 }422 423 static int424 Py_cvec_setitem (Py_cvec * self, Py_ssize_t index, PyObject * o)425 {426 PyObject *array;427 428 if (index < 0 || index >= self->channels) {429 PyErr_SetString (PyExc_IndexError, "no such channel");430 return -1;431 }432 433 array = PyArray_FROM_OT (o, NPY_FLOAT);434 if (array == NULL) {435 PyErr_SetString (PyExc_ValueError, "should be an array of float");436 goto fail;437 }438 439 if (PyArray_NDIM (array) != 1) {440 PyErr_SetString (PyExc_ValueError, "should be a one-dimensional array");441 goto fail;442 }443 444 if (PyArray_SIZE (array) != self->length) {445 PyErr_SetString (PyExc_ValueError,446 "should be an array of same length as target cvec");447 goto fail;448 }449 450 self->o->norm[index] = (smpl_t *) PyArray_GETPTR1 (array, 0);451 452 return 0;453 454 fail:455 return -1;456 320 } 457 321 … … 460 324 {"length", T_INT, offsetof (Py_cvec, length), READONLY, 461 325 "length attribute"}, 462 {"channels", T_INT, offsetof (Py_cvec, channels), READONLY,463 "channels attribute"},464 326 {NULL} /* Sentinel */ 465 327 }; … … 480 342 {NULL} /* sentinel */ 481 343 }; 482 483 static PySequenceMethods Py_cvec_tp_as_sequence = {484 (lenfunc) Py_cvec_getchannels, /* sq_length */485 0, /* sq_concat */486 0, /* sq_repeat */487 (ssizeargfunc) Py_cvec_getitem, /* sq_item */488 0, /* sq_slice */489 (ssizeobjargproc) Py_cvec_setitem, /* sq_ass_item */490 0, /* sq_ass_slice */491 0, /* sq_contains */492 0, /* sq_inplace_concat */493 0, /* sq_inplace_repeat */494 };495 496 344 497 345 PyTypeObject Py_cvecType = { … … 508 356 (reprfunc) Py_cvec_repr, /* tp_repr */ 509 357 0, /* tp_as_number */ 510 &Py_cvec_tp_as_sequence, /* tp_as_sequence */358 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ 511 359 0, /* tp_as_mapping */ 512 360 0, /* tp_hash */ -
interfaces/python/py-fvec.c
rf7b05d4 r7a7b00f 4 4 5 5 class fvec(): 6 def __init__(self, length = 1024 , channels = 1):6 def __init__(self, length = 1024): 7 7 self.length = length 8 self.channels = channels 9 self.data = array(length, channels) 8 self.data = array(length) 10 9 11 10 */ … … 16 15 Py_fvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) 17 16 { 18 int length= 0 , channels = 0;17 int length= 0; 19 18 Py_fvec *self; 20 static char *kwlist[] = { "length", "channels", NULL }; 21 22 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist, 23 &length, &channels)) { 24 return NULL; 25 } 26 19 static char *kwlist[] = { "length", NULL }; 20 21 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, 22 &length)) { 23 return NULL; 24 } 27 25 28 26 self = (Py_fvec *) type->tp_alloc (type, 0); 29 27 30 28 self->length = Py_default_vector_length; 31 self->channels = Py_default_vector_channels;32 29 33 30 if (self == NULL) { … … 43 40 } 44 41 45 if (channels > 0) {46 self->channels = channels;47 } else if (channels < 0) {48 PyErr_SetString (PyExc_ValueError,49 "can not use negative number of channels");50 return NULL;51 }52 53 42 return (PyObject *) self; 54 43 } … … 57 46 Py_fvec_init (Py_fvec * self, PyObject * args, PyObject * kwds) 58 47 { 59 self->o = new_fvec (self->length , self->channels);48 self->o = new_fvec (self->length); 60 49 if (self->o == NULL) { 61 50 return -1; … … 75 64 Py_fvec_repr (Py_fvec * self, PyObject * unused) 76 65 { 66 #if 0 77 67 PyObject *format = NULL; 78 68 PyObject *args = NULL; 79 69 PyObject *result = NULL; 80 70 81 format = PyString_FromString ("aubio fvec of %d elements with %d channels");71 format = PyString_FromString ("aubio fvec of %d elements"); 82 72 if (format == NULL) { 83 73 goto fail; 84 74 } 85 75 86 args = Py_BuildValue ("I I", self->length, self->channels);76 args = Py_BuildValue ("I", self->length); 87 77 if (args == NULL) { 88 78 goto fail; … … 92 82 result = PyString_Format (format, args); 93 83 84 fail: 85 Py_XDECREF (format); 86 Py_XDECREF (args); 87 88 return result; 89 #endif 90 PyObject *format = NULL; 91 PyObject *args = NULL; 92 PyObject *result = NULL; 93 94 format = PyString_FromString ("%s"); 95 if (format == NULL) { 96 goto fail; 97 } 98 99 args = Py_BuildValue ("O", PyAubio_FvecToArray (self)); 100 if (args == NULL) { 101 goto fail; 102 } 103 104 result = PyString_Format (format, args); 94 105 fail: 95 106 Py_XDECREF (format); … … 103 114 PyObject *array; 104 115 Py_fvec *vec; 105 uint_t i;106 116 if (input == NULL) { 107 117 PyErr_SetString (PyExc_ValueError, "input array is not a python object"); … … 118 128 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 119 129 goto fail; 120 } else if (PyArray_NDIM (input) > 2) {130 } else if (PyArray_NDIM (input) > 1) { 121 131 PyErr_SetString (PyExc_ValueError, 122 "input array has more than twodimensions");132 "input array has more than one dimensions"); 123 133 goto fail; 124 134 } … … 127 137 PyErr_SetString (PyExc_ValueError, "input array should be float"); 128 138 goto fail; 129 #if AUBIO_DO_CASTING130 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {131 // input data type is not float32, casting132 array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL);133 if (array == NULL) {134 PyErr_SetString (PyExc_IndexError, "failed converting to AUBIO_NPY_SMPL");135 goto fail;136 }137 #else138 139 } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) { 139 140 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 140 141 goto fail; 141 #endif142 142 } else { 143 143 // input data type is float32, nothing else to do … … 147 147 // create a new fvec object 148 148 vec = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType); 149 if (PyArray_NDIM (array) == 1) { 150 vec->channels = 1; 151 vec->length = PyArray_SIZE (array); 152 } else { 153 vec->channels = PyArray_DIM (array, 0); 154 vec->length = PyArray_DIM (array, 1); 155 } 149 vec->length = PyArray_SIZE (array); 156 150 157 151 // no need to really allocate fvec, just its struct member 158 // vec->o = new_fvec (vec->length , vec->channels);152 // vec->o = new_fvec (vec->length); 159 153 vec->o = (fvec_t *)malloc(sizeof(fvec_t)); 160 vec->o->length = vec->length; vec->o->channels = vec->channels; 161 vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*)); 162 // hat data[i] point to array line 163 for (i = 0; i < vec->channels; i++) { 164 vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i); 165 } 166 154 vec->o->length = vec->length; 155 vec->o->data = (smpl_t *) PyArray_GETPTR1 (array, 0); 156 157 } else if (PyObject_TypeCheck (input, &PyList_Type)) { 158 PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); 159 return NULL; 167 160 } else { 168 PyErr_SetString (PyExc_ValueError, "can only accept arrayor fvec as input");161 PyErr_SetString (PyExc_ValueError, "can only accept vector or fvec as input"); 169 162 return NULL; 170 163 } … … 179 172 PyAubio_CFvecToArray (fvec_t * self) 180 173 { 181 PyObject *array = NULL;182 uint_t i;183 174 npy_intp dims[] = { self->length, 1 }; 184 PyObject *concat = PyList_New (0), *tmp = NULL; 185 for (i = 0; i < self->channels; i++) { 186 tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data[i]); 187 PyList_Append (concat, tmp); 188 Py_DECREF (tmp); 189 } 190 array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2); 191 Py_DECREF (concat); 192 return array; 175 return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data); 193 176 } 194 177 … … 197 180 { 198 181 PyObject *array = NULL; 199 if (self->channels == 1) { 200 npy_intp dims[] = { self->length, 1 }; 201 array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[0]); 202 } else { 203 uint_t i; 204 npy_intp dims[] = { self->length, 1 }; 205 PyObject *concat = PyList_New (0), *tmp = NULL; 206 for (i = 0; i < self->channels; i++) { 207 tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[i]); 208 PyList_Append (concat, tmp); 209 Py_DECREF (tmp); 210 } 211 array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2); 212 Py_DECREF (concat); 213 } 182 npy_intp dims[] = { self->length, 1 }; 183 array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data); 214 184 return array; 215 }216 217 static Py_ssize_t218 Py_fvec_getchannels (Py_fvec * self)219 {220 return self->channels;221 185 } 222 186 … … 224 188 Py_fvec_getitem (Py_fvec * self, Py_ssize_t index) 225 189 { 226 PyObject *array; 227 228 if (index < 0 || index >= self->channels) { 229 PyErr_SetString (PyExc_IndexError, "no such channel"); 230 return NULL; 231 } 232 233 npy_intp dims[] = { self->length, 1 }; 234 array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[index]); 235 return array; 190 if (index < 0 || index >= self->length) { 191 PyErr_SetString (PyExc_IndexError, "no such element"); 192 return NULL; 193 } 194 195 return PyFloat_FromDouble (self->o->data[index]); 236 196 } 237 197 … … 239 199 Py_fvec_setitem (Py_fvec * self, Py_ssize_t index, PyObject * o) 240 200 { 241 PyObject *array; 242 243 if (index < 0 || index >= self->channels) { 244 PyErr_SetString (PyExc_IndexError, "no such channel"); 245 return -1; 246 } 247 248 array = PyArray_FROM_OT (o, AUBIO_NPY_SMPL); 249 if (array == NULL) { 250 PyErr_SetString (PyExc_ValueError, "should be an array of float"); 251 goto fail; 252 } 253 254 if (PyArray_NDIM (array) != 1) { 255 PyErr_SetString (PyExc_ValueError, "should be a one-dimensional array"); 256 goto fail; 257 } 258 259 if (PyArray_SIZE (array) != self->length) { 260 PyErr_SetString (PyExc_ValueError, 261 "should be an array of same length as target fvec"); 262 goto fail; 263 } 264 265 self->o->data[index] = (smpl_t *) PyArray_GETPTR1 (array, 0); 201 202 if (index < 0 || index >= self->length) { 203 PyErr_SetString (PyExc_IndexError, "no such element"); 204 goto fail; 205 } 206 207 if (PyFloat_Check (o)) { 208 PyErr_SetString (PyExc_ValueError, "should be a float"); 209 goto fail; 210 } 211 212 self->o->data[index] = (smpl_t) PyFloat_AsDouble(o); 266 213 267 214 return 0; … … 269 216 fail: 270 217 return -1; 218 } 219 220 int 221 Py_fvec_get_length (Py_fvec * self) 222 { 223 return self->length; 271 224 } 272 225 … … 275 228 {"length", T_INT, offsetof (Py_fvec, length), READONLY, 276 229 "length attribute"}, 277 {"channels", T_INT, offsetof (Py_fvec, channels), READONLY,278 "channels attribute"},279 230 {NULL} /* Sentinel */ 280 231 }; … … 287 238 288 239 static PySequenceMethods Py_fvec_tp_as_sequence = { 289 (lenfunc) Py_fvec_get channels, /* sq_length */240 (lenfunc) Py_fvec_get_length, /* sq_length */ 290 241 0, /* sq_concat */ 291 242 0, /* sq_repeat */
Note: See TracChangeset
for help on using the changeset viewer.