[7395ec5] | 1 | #include "aubio-types.h" |
---|
| 2 | |
---|
[bfe8256] | 3 | PyObject * |
---|
| 4 | PyAubio_CFvecToArray (fvec_t * self) |
---|
| 5 | { |
---|
| 6 | npy_intp dims[] = { self->length, 1 }; |
---|
| 7 | return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data); |
---|
| 8 | } |
---|
| 9 | |
---|
| 10 | int |
---|
| 11 | PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { |
---|
[7395ec5] | 12 | if (input == NULL) { |
---|
| 13 | PyErr_SetString (PyExc_ValueError, "input array is not a python object"); |
---|
[bfe8256] | 14 | return 0; |
---|
[7395ec5] | 15 | } |
---|
| 16 | // parsing input object into a Py_fvec |
---|
| 17 | if (PyArray_Check(input)) { |
---|
| 18 | |
---|
[93004d5] | 19 | // we got an array, convert it to an fvec |
---|
[1458de5] | 20 | if (PyArray_NDIM ((PyArrayObject *)input) == 0) { |
---|
[7395ec5] | 21 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
[bfe8256] | 22 | return 0; |
---|
[1458de5] | 23 | } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) { |
---|
[7395ec5] | 24 | PyErr_SetString (PyExc_ValueError, |
---|
| 25 | "input array has more than one dimensions"); |
---|
[bfe8256] | 26 | return 0; |
---|
[7395ec5] | 27 | } |
---|
| 28 | |
---|
[1458de5] | 29 | if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { |
---|
[7395ec5] | 30 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
[bfe8256] | 31 | return 0; |
---|
[1458de5] | 32 | } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { |
---|
[c6388f4] | 33 | PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR); |
---|
[bfe8256] | 34 | return 0; |
---|
[7395ec5] | 35 | } |
---|
| 36 | |
---|
| 37 | // vec = new_fvec (vec->length); |
---|
[93004d5] | 38 | // no need to really allocate fvec, just its struct member |
---|
[bfe8256] | 39 | long length = PyArray_SIZE ((PyArrayObject *)input); |
---|
| 40 | if (length <= 0) { |
---|
[3194dca1] | 41 | PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0"); |
---|
[bfe8256] | 42 | return 0; |
---|
[3194dca1] | 43 | } |
---|
[7395ec5] | 44 | |
---|
| 45 | } else if (PyObject_TypeCheck (input, &PyList_Type)) { |
---|
| 46 | PyErr_SetString (PyExc_ValueError, "does not convert from list yet"); |
---|
[bfe8256] | 47 | return 0; |
---|
[7395ec5] | 48 | } else { |
---|
| 49 | PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input"); |
---|
[bfe8256] | 50 | return 0; |
---|
[7395ec5] | 51 | } |
---|
| 52 | |
---|
[bfe8256] | 53 | out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input); |
---|
| 54 | out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0); |
---|
| 55 | return 1; |
---|
[7395ec5] | 56 | } |
---|
| 57 | |
---|
| 58 | PyObject * |
---|
| 59 | PyAubio_CFmatToArray (fmat_t * input) |
---|
| 60 | { |
---|
| 61 | PyObject *array = NULL; |
---|
| 62 | uint_t i; |
---|
| 63 | npy_intp dims[] = { input->length, 1 }; |
---|
| 64 | PyObject *concat = PyList_New (0), *tmp = NULL; |
---|
| 65 | for (i = 0; i < input->height; i++) { |
---|
| 66 | tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->data[i]); |
---|
| 67 | PyList_Append (concat, tmp); |
---|
| 68 | Py_DECREF (tmp); |
---|
| 69 | } |
---|
| 70 | array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2); |
---|
| 71 | Py_DECREF (concat); |
---|
| 72 | return array; |
---|
| 73 | } |
---|
| 74 | |
---|
[bfe8256] | 75 | int |
---|
| 76 | PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) { |
---|
[93004d5] | 77 | uint_t i; |
---|
| 78 | if (input == NULL) { |
---|
| 79 | PyErr_SetString (PyExc_ValueError, "input array is not a python object"); |
---|
[bfe8256] | 80 | return 0; |
---|
[93004d5] | 81 | } |
---|
| 82 | // parsing input object into a Py_fvec |
---|
| 83 | if (PyArray_Check(input)) { |
---|
| 84 | |
---|
| 85 | // we got an array, convert it to an fvec |
---|
| 86 | if (PyArray_NDIM ((PyArrayObject *)input) == 0) { |
---|
| 87 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
[bfe8256] | 88 | return 0; |
---|
[93004d5] | 89 | } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { |
---|
| 90 | PyErr_SetString (PyExc_ValueError, |
---|
| 91 | "input array has more than two dimensions"); |
---|
[bfe8256] | 92 | return 0; |
---|
[93004d5] | 93 | } |
---|
| 94 | |
---|
| 95 | if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { |
---|
| 96 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
[bfe8256] | 97 | return 0; |
---|
[93004d5] | 98 | } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { |
---|
[c6388f4] | 99 | PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR); |
---|
[bfe8256] | 100 | return 0; |
---|
[93004d5] | 101 | } |
---|
| 102 | |
---|
| 103 | // no need to really allocate fvec, just its struct member |
---|
[bfe8256] | 104 | long length = PyArray_DIM ((PyArrayObject *)input, 1); |
---|
| 105 | if (length <= 0) { |
---|
[3194dca1] | 106 | PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0"); |
---|
[bfe8256] | 107 | return 0; |
---|
[3194dca1] | 108 | } |
---|
[bfe8256] | 109 | long height = PyArray_DIM ((PyArrayObject *)input, 0); |
---|
| 110 | if (height <= 0) { |
---|
[3194dca1] | 111 | PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0"); |
---|
[bfe8256] | 112 | return 0; |
---|
[93004d5] | 113 | } |
---|
| 114 | |
---|
| 115 | } else if (PyObject_TypeCheck (input, &PyList_Type)) { |
---|
| 116 | PyErr_SetString (PyExc_ValueError, "can not convert list to fmat"); |
---|
[bfe8256] | 117 | return 0; |
---|
[93004d5] | 118 | } else { |
---|
| 119 | PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input"); |
---|
[bfe8256] | 120 | return 0; |
---|
[93004d5] | 121 | } |
---|
| 122 | |
---|
[569b363] | 123 | uint_t new_height = (uint_t)PyArray_DIM ((PyArrayObject *)input, 0); |
---|
| 124 | if (mat->height != new_height) { |
---|
| 125 | if (mat->data) { |
---|
| 126 | free(mat->data); |
---|
| 127 | } |
---|
| 128 | mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * new_height); |
---|
[bfe8256] | 129 | } |
---|
[93004d5] | 130 | |
---|
[569b363] | 131 | mat->height = new_height; |
---|
[bfe8256] | 132 | mat->length = (uint_t)PyArray_DIM ((PyArrayObject *)input, 1); |
---|
| 133 | for (i=0; i< mat->height; i++) { |
---|
| 134 | mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)input, i); |
---|
| 135 | } |
---|
| 136 | return 1; |
---|
[7395ec5] | 137 | } |
---|