[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 | static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; |
---|
| 8 | |
---|
| 9 | static PyObject * |
---|
| 10 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
| 11 | { |
---|
| 12 | PyObject *input; |
---|
| 13 | Py_fvec *vec; |
---|
| 14 | smpl_t alpha; |
---|
| 15 | PyObject *result; |
---|
| 16 | |
---|
| 17 | if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { |
---|
| 18 | return NULL; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | if (input == NULL) { |
---|
| 22 | return NULL; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | vec = PyAubio_ArrayToFvec (input); |
---|
| 26 | |
---|
| 27 | if (vec == NULL) { |
---|
| 28 | return NULL; |
---|
| 29 | } |
---|
| 30 | |
---|
[ae6e15c] | 31 | // compute the function |
---|
[6b1aafc] | 32 | result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha)); |
---|
[0a6c211] | 33 | if (result == NULL) { |
---|
| 34 | return NULL; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | return result; |
---|
| 38 | } |
---|
| 39 | |
---|
[207ed19] | 40 | static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; |
---|
| 41 | |
---|
| 42 | static PyObject * |
---|
| 43 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 44 | { |
---|
| 45 | PyObject *input; |
---|
| 46 | Py_fvec *vec; |
---|
| 47 | PyObject *result; |
---|
| 48 | |
---|
| 49 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 50 | return NULL; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | if (input == NULL) { |
---|
| 54 | return NULL; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | vec = PyAubio_ArrayToFvec (input); |
---|
| 58 | |
---|
| 59 | if (vec == NULL) { |
---|
| 60 | return NULL; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | // compute the function |
---|
| 64 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec->o)); |
---|
| 65 | if (result == NULL) { |
---|
| 66 | return NULL; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | return result; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | static char Py_min_removal_doc[] = "compute zero crossing rate"; |
---|
| 73 | |
---|
| 74 | static PyObject * |
---|
| 75 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 76 | { |
---|
| 77 | PyObject *input; |
---|
| 78 | Py_fvec *vec; |
---|
| 79 | |
---|
[ccf8b77] | 80 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
[207ed19] | 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if (input == NULL) { |
---|
| 85 | return NULL; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | vec = PyAubio_ArrayToFvec (input); |
---|
| 89 | |
---|
| 90 | if (vec == NULL) { |
---|
| 91 | return NULL; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // compute the function |
---|
| 95 | fvec_min_removal (vec->o); |
---|
| 96 | // since this function does not return, we could return None |
---|
| 97 | //return Py_None; |
---|
| 98 | // however it is convenient to return the modified vector |
---|
| 99 | //return (PyObject *) PyAubio_FvecToArray(vec); |
---|
| 100 | // or even without converting it back to an array |
---|
| 101 | Py_INCREF(vec); |
---|
| 102 | return (PyObject *)vec; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
[0a6c211] | 108 | static PyMethodDef aubio_methods[] = { |
---|
| 109 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[207ed19] | 110 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, |
---|
| 111 | Py_zero_crossing_rate_doc}, |
---|
| 112 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[ccf8b77] | 113 | {NULL, NULL} /* Sentinel */ |
---|
[0a6c211] | 114 | }; |
---|
| 115 | |
---|
[ae6e15c] | 116 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
| 117 | |
---|
[0a6c211] | 118 | PyMODINIT_FUNC |
---|
| 119 | init_aubio (void) |
---|
| 120 | { |
---|
| 121 | PyObject *m; |
---|
| 122 | int err; |
---|
| 123 | |
---|
| 124 | if (PyType_Ready (&Py_fvecType) < 0) { |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | err = _import_array (); |
---|
| 129 | |
---|
| 130 | if (err != 0) { |
---|
| 131 | fprintf (stderr, |
---|
| 132 | "Unable to import Numpy C API from aubio module (error %d)\n", err); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
| 136 | |
---|
| 137 | if (m == NULL) { |
---|
| 138 | return; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | Py_INCREF (&Py_fvecType); |
---|
| 142 | PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType); |
---|
| 143 | } |
---|