[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 | |
---|
[207ed19] | 115 | static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; |
---|
| 116 | |
---|
| 117 | static PyObject * |
---|
| 118 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 119 | { |
---|
| 120 | PyObject *input; |
---|
| 121 | Py_fvec *vec; |
---|
| 122 | PyObject *result; |
---|
| 123 | |
---|
| 124 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 125 | return NULL; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | if (input == NULL) { |
---|
| 129 | return NULL; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | vec = PyAubio_ArrayToFvec (input); |
---|
| 133 | |
---|
| 134 | if (vec == NULL) { |
---|
| 135 | return NULL; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // compute the function |
---|
| 139 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec->o)); |
---|
| 140 | if (result == NULL) { |
---|
| 141 | return NULL; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | return result; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | static char Py_min_removal_doc[] = "compute zero crossing rate"; |
---|
| 148 | |
---|
| 149 | static PyObject * |
---|
| 150 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 151 | { |
---|
| 152 | PyObject *input; |
---|
| 153 | Py_fvec *vec; |
---|
| 154 | |
---|
| 155 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 156 | return NULL; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | if (input == NULL) { |
---|
| 160 | return NULL; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | vec = PyAubio_ArrayToFvec (input); |
---|
| 164 | |
---|
| 165 | if (vec == NULL) { |
---|
| 166 | return NULL; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // compute the function |
---|
| 170 | fvec_min_removal (vec->o); |
---|
| 171 | // since this function does not return, we could return None |
---|
| 172 | //return Py_None; |
---|
| 173 | // however it is convenient to return the modified vector |
---|
| 174 | //return (PyObject *) PyAubio_FvecToArray(vec); |
---|
| 175 | // or even without converting it back to an array |
---|
| 176 | Py_INCREF(vec); |
---|
| 177 | return (PyObject *)vec; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
[0a6c211] | 183 | static PyMethodDef aubio_methods[] = { |
---|
| 184 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[207ed19] | 185 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, |
---|
| 186 | Py_zero_crossing_rate_doc}, |
---|
| 187 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[0a6c211] | 188 | {NULL, NULL} /* Sentinel */ |
---|
| 189 | }; |
---|
| 190 | |
---|
[ae6e15c] | 191 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
| 192 | |
---|
[0a6c211] | 193 | PyMODINIT_FUNC |
---|
| 194 | init_aubio (void) |
---|
| 195 | { |
---|
| 196 | PyObject *m; |
---|
| 197 | int err; |
---|
| 198 | |
---|
| 199 | if (PyType_Ready (&Py_fvecType) < 0) { |
---|
| 200 | return; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | err = _import_array (); |
---|
| 204 | |
---|
| 205 | if (err != 0) { |
---|
| 206 | fprintf (stderr, |
---|
| 207 | "Unable to import Numpy C API from aubio module (error %d)\n", err); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
| 211 | |
---|
| 212 | if (m == NULL) { |
---|
| 213 | return; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | Py_INCREF (&Py_fvecType); |
---|
| 217 | PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType); |
---|
| 218 | } |
---|