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