[0a6c211] | 1 | #include <Python.h> |
---|
[ae6e15c] | 2 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API |
---|
[0a6c211] | 3 | #include <numpy/arrayobject.h> |
---|
| 4 | |
---|
[ae6e15c] | 5 | #include "aubio-types.h" |
---|
[0a6c211] | 6 | |
---|
[6b1aafc] | 7 | Py_fvec * |
---|
| 8 | PyAubio_ArrayToFvec (PyObject *input) { |
---|
[ae6e15c] | 9 | PyObject *array; |
---|
[6b1aafc] | 10 | Py_fvec *vec; |
---|
[ae6e15c] | 11 | uint_t i; |
---|
| 12 | // parsing input object into a Py_fvec |
---|
| 13 | if (PyObject_TypeCheck (input, &Py_fvecType)) { |
---|
| 14 | // input is an fvec, nothing else to do |
---|
| 15 | vec = (Py_fvec *) input; |
---|
| 16 | } else if (PyArray_Check(input)) { |
---|
[0a6c211] | 17 | |
---|
[ae6e15c] | 18 | // we got an array, convert it to an fvec |
---|
| 19 | if (PyArray_NDIM (input) == 0) { |
---|
| 20 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
| 21 | goto fail; |
---|
| 22 | } else if (PyArray_NDIM (input) > 2) { |
---|
[6b1aafc] | 23 | PyErr_SetString (PyExc_ValueError, |
---|
| 24 | "input array has more than two dimensions"); |
---|
[ae6e15c] | 25 | goto fail; |
---|
[0a6c211] | 26 | } |
---|
| 27 | |
---|
[ae6e15c] | 28 | if (!PyArray_ISFLOAT (input)) { |
---|
| 29 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
| 30 | goto fail; |
---|
[6b1aafc] | 31 | #if AUBIO_DO_CASTING |
---|
| 32 | } else if (PyArray_TYPE (input) != AUBIO_FLOAT) { |
---|
[ae6e15c] | 33 | // input data type is not float32, casting |
---|
[6b1aafc] | 34 | array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT); |
---|
[ae6e15c] | 35 | if (array == NULL) { |
---|
| 36 | PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT"); |
---|
| 37 | goto fail; |
---|
| 38 | } |
---|
[6b1aafc] | 39 | #else |
---|
| 40 | } else if (PyArray_TYPE (input) != AUBIO_FLOAT) { |
---|
| 41 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
| 42 | goto fail; |
---|
| 43 | #endif |
---|
[ae6e15c] | 44 | } else { |
---|
| 45 | // input data type is float32, nothing else to do |
---|
| 46 | array = input; |
---|
| 47 | } |
---|
[0a6c211] | 48 | |
---|
[ae6e15c] | 49 | // create a new fvec object |
---|
| 50 | vec = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType); |
---|
| 51 | if (PyArray_NDIM (array) == 1) { |
---|
| 52 | vec->channels = 1; |
---|
| 53 | vec->length = PyArray_SIZE (array); |
---|
| 54 | } else { |
---|
| 55 | vec->channels = PyArray_DIM (array, 0); |
---|
| 56 | vec->length = PyArray_DIM (array, 1); |
---|
| 57 | } |
---|
[0a6c211] | 58 | |
---|
[6b1aafc] | 59 | // no need to really allocate fvec, just its struct member |
---|
| 60 | // vec->o = new_fvec (vec->length, vec->channels); |
---|
| 61 | vec->o = (fvec_t *)malloc(sizeof(fvec_t)); |
---|
| 62 | vec->o->length = vec->length; vec->o->channels = vec->channels; |
---|
| 63 | vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*)); |
---|
| 64 | // hat data[i] point to array line |
---|
[ae6e15c] | 65 | for (i = 0; i < vec->channels; i++) { |
---|
| 66 | vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i); |
---|
| 67 | } |
---|
[0a6c211] | 68 | |
---|
[ae6e15c] | 69 | } else { |
---|
| 70 | PyErr_SetString (PyExc_ValueError, "can only accept array or fvec as input"); |
---|
[0a6c211] | 71 | return NULL; |
---|
| 72 | } |
---|
| 73 | |
---|
[6b1aafc] | 74 | return vec; |
---|
| 75 | |
---|
| 76 | fail: |
---|
| 77 | return NULL; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; |
---|
| 83 | |
---|
| 84 | static PyObject * |
---|
| 85 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
| 86 | { |
---|
| 87 | PyObject *input; |
---|
| 88 | Py_fvec *vec; |
---|
| 89 | smpl_t alpha; |
---|
| 90 | PyObject *result; |
---|
| 91 | |
---|
| 92 | if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { |
---|
| 93 | return NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if (input == NULL) { |
---|
| 97 | return NULL; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | vec = PyAubio_ArrayToFvec (input); |
---|
| 101 | |
---|
| 102 | if (vec == NULL) { |
---|
| 103 | return NULL; |
---|
| 104 | } |
---|
| 105 | |
---|
[ae6e15c] | 106 | // compute the function |
---|
[6b1aafc] | 107 | result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha)); |
---|
[0a6c211] | 108 | if (result == NULL) { |
---|
| 109 | return NULL; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | return result; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | static PyMethodDef aubio_methods[] = { |
---|
| 116 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
| 117 | {NULL, NULL} /* Sentinel */ |
---|
| 118 | }; |
---|
| 119 | |
---|
[ae6e15c] | 120 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
| 121 | |
---|
[0a6c211] | 122 | PyMODINIT_FUNC |
---|
| 123 | init_aubio (void) |
---|
| 124 | { |
---|
| 125 | PyObject *m; |
---|
| 126 | int err; |
---|
| 127 | |
---|
| 128 | if (PyType_Ready (&Py_fvecType) < 0) { |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | err = _import_array (); |
---|
| 133 | |
---|
| 134 | if (err != 0) { |
---|
| 135 | fprintf (stderr, |
---|
| 136 | "Unable to import Numpy C API from aubio module (error %d)\n", err); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
| 140 | |
---|
| 141 | if (m == NULL) { |
---|
| 142 | return; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | Py_INCREF (&Py_fvecType); |
---|
| 146 | PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType); |
---|
| 147 | } |
---|