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