Changes in python/ext/aubioproxy.c [3194dca1:911c22f]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/ext/aubioproxy.c
r3194dca1 r911c22f 1 1 #include "aubio-types.h" 2 2 3 fvec_t * 4 PyAubio_ArrayToCFvec (PyObject *input) { 5 PyObject *array; 6 fvec_t *vec; 7 if (input == NULL) { 8 PyErr_SetString (PyExc_ValueError, "input array is not a python object"); 9 goto fail; 10 } 11 // parsing input object into a Py_fvec 12 if (PyArray_Check(input)) { 3 PyObject * 4 new_py_fvec(uint_t length) { 5 npy_intp dims[] = { length, 1 }; 6 return PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); 7 } 13 8 14 // we got an array, convert it to an fvec 15 if (PyArray_NDIM ((PyArrayObject *)input) == 0) { 16 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 17 goto fail; 18 } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) { 19 PyErr_SetString (PyExc_ValueError, 20 "input array has more than one dimensions"); 21 goto fail; 22 } 23 24 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { 25 PyErr_SetString (PyExc_ValueError, "input array should be float"); 26 goto fail; 27 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { 28 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 29 goto fail; 30 } else { 31 // input data type is float32, nothing else to do 32 array = input; 33 } 34 35 // vec = new_fvec (vec->length); 36 // no need to really allocate fvec, just its struct member 37 vec = (fvec_t *)malloc(sizeof(fvec_t)); 38 long length = PyArray_SIZE ((PyArrayObject *)array); 39 if (length > 0) { 40 vec->length = (uint_t)length; 41 } else { 42 PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0"); 43 goto fail; 44 } 45 vec->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)array, 0); 46 47 } else if (PyObject_TypeCheck (input, &PyList_Type)) { 48 PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); 49 return NULL; 50 } else { 51 PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input"); 52 return NULL; 53 } 54 55 return vec; 56 57 fail: 58 return NULL; 9 PyObject * 10 new_py_fmat(uint_t height, uint_t length) { 11 npy_intp dims[] = { height, length, 1 }; 12 return PyArray_ZEROS(2, dims, AUBIO_NPY_SMPL, 0); 59 13 } 60 14 … … 66 20 } 67 21 68 Py_cvec * 69 PyAubio_CCvecToPyCvec (cvec_t * input) { 70 Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); 71 vec->length = input->length; 72 vec->o = input; 73 Py_INCREF(vec); 74 return vec; 22 int 23 PyAubio_IsValidVector (PyObject * input) { 24 npy_intp length; 25 if (input == NULL) { 26 PyErr_SetString (PyExc_ValueError, "input array is not a python object"); 27 return 0; 28 } 29 // parsing input object into a Py_fvec 30 if (PyArray_Check(input)) { 31 32 // we got an array, convert it to an fvec 33 if (PyArray_NDIM ((PyArrayObject *)input) == 0) { 34 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 35 return 0; 36 } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) { 37 PyErr_SetString (PyExc_ValueError, 38 "input array has more than one dimensions"); 39 return 0; 40 } 41 42 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { 43 PyErr_SetString (PyExc_ValueError, "input array should be float"); 44 return 0; 45 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { 46 PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR); 47 return 0; 48 } 49 50 length = PyArray_SIZE ((PyArrayObject *)input); 51 if (length <= 0) { 52 PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0"); 53 return 0; 54 } 55 56 } else if (PyObject_TypeCheck (input, &PyList_Type)) { 57 PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); 58 return 0; 59 } else { 60 PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input"); 61 return 0; 62 } 63 return 1; 75 64 } 76 65 77 cvec_t * 78 PyAubio_ArrayToCCvec (PyObject *input) { 79 if (PyObject_TypeCheck (input, &Py_cvecType)) { 80 return ((Py_cvec*)input)->o; 81 } else { 82 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 83 return NULL; 66 int 67 PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { 68 69 if (!PyAubio_IsValidVector(input)){ 70 return 0; 84 71 } 72 73 out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input); 74 out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0); 75 return 1; 85 76 } 86 77 … … 102 93 } 103 94 104 fmat_t * 105 PyAubio_ArrayToCFmat (PyObject *input) { 106 PyObject *array; 107 fmat_t *mat; 108 uint_t i; 95 int 96 PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) { 97 uint_t i, new_height; 98 npy_intp length, height; 109 99 if (input == NULL) { 110 100 PyErr_SetString (PyExc_ValueError, "input array is not a python object"); 111 goto fail;101 return 0; 112 102 } 113 103 // parsing input object into a Py_fvec … … 117 107 if (PyArray_NDIM ((PyArrayObject *)input) == 0) { 118 108 PyErr_SetString (PyExc_ValueError, "input array is a scalar"); 119 goto fail;109 return 0; 120 110 } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { 121 111 PyErr_SetString (PyExc_ValueError, 122 112 "input array has more than two dimensions"); 123 goto fail;113 return 0; 124 114 } 125 115 126 116 if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { 127 117 PyErr_SetString (PyExc_ValueError, "input array should be float"); 128 goto fail;118 return 0; 129 119 } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { 130 PyErr_SetString (PyExc_ValueError, "input array should be float32"); 131 goto fail; 132 } else { 133 // input data type is float32, nothing else to do 134 array = input; 120 PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR); 121 return 0; 135 122 } 136 123 137 124 // no need to really allocate fvec, just its struct member 138 mat = (fmat_t *)malloc(sizeof(fmat_t)); 139 long length = PyArray_DIM ((PyArrayObject *)array, 1); 140 if (length > 0) { 141 mat->length = (uint_t)length; 142 } else { 125 length = PyArray_DIM ((PyArrayObject *)input, 1); 126 if (length <= 0) { 143 127 PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0"); 144 goto fail;128 return 0; 145 129 } 146 long height = PyArray_DIM ((PyArrayObject *)array, 0); 147 if (height > 0) { 148 mat->height = (uint_t)height; 149 } else { 130 height = PyArray_DIM ((PyArrayObject *)input, 0); 131 if (height <= 0) { 150 132 PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0"); 151 goto fail; 152 } 153 mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height); 154 for (i=0; i< mat->height; i++) { 155 mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i); 133 return 0; 156 134 } 157 135 158 136 } else if (PyObject_TypeCheck (input, &PyList_Type)) { 159 137 PyErr_SetString (PyExc_ValueError, "can not convert list to fmat"); 160 return NULL;138 return 0; 161 139 } else { 162 140 PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input"); 163 return NULL;141 return 0; 164 142 } 165 143 166 return mat; 144 new_height = (uint_t)PyArray_DIM ((PyArrayObject *)input, 0); 145 if (mat->height != new_height) { 146 if (mat->data) { 147 free(mat->data); 148 } 149 mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * new_height); 150 } 167 151 168 fail: 169 return NULL; 152 mat->height = new_height; 153 mat->length = (uint_t)PyArray_DIM ((PyArrayObject *)input, 1); 154 for (i=0; i< mat->height; i++) { 155 mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)input, i); 156 } 157 return 1; 170 158 } 171
Note: See TracChangeset
for help on using the changeset viewer.