[7a6521d] | 1 | #define PY_AUBIO_MODULE_MAIN |
---|
[ae6e15c] | 2 | #include "aubio-types.h" |
---|
[ad5203c] | 3 | #include "aubio-generated.h" |
---|
| 4 | |
---|
| 5 | extern void add_generated_objects ( PyObject *m ); |
---|
| 6 | extern void add_ufuncs ( PyObject *m ); |
---|
[d75c900] | 7 | extern int generated_types_ready(void); |
---|
[0a6c211] | 8 | |
---|
[6b1aafc] | 9 | static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; |
---|
| 10 | |
---|
| 11 | static PyObject * |
---|
| 12 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
| 13 | { |
---|
| 14 | PyObject *input; |
---|
[ce4bfe3] | 15 | fvec_t *vec; |
---|
[6b1aafc] | 16 | smpl_t alpha; |
---|
| 17 | PyObject *result; |
---|
| 18 | |
---|
| 19 | if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { |
---|
| 20 | return NULL; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | if (input == NULL) { |
---|
| 24 | return NULL; |
---|
| 25 | } |
---|
| 26 | |
---|
[ce4bfe3] | 27 | vec = PyAubio_ArrayToCFvec (input); |
---|
[6b1aafc] | 28 | |
---|
| 29 | if (vec == NULL) { |
---|
| 30 | return NULL; |
---|
| 31 | } |
---|
| 32 | |
---|
[ae6e15c] | 33 | // compute the function |
---|
[ce4bfe3] | 34 | result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); |
---|
[0a6c211] | 35 | if (result == NULL) { |
---|
| 36 | return NULL; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | return result; |
---|
| 40 | } |
---|
| 41 | |
---|
[6a50b9e] | 42 | static char Py_bintomidi_doc[] = "convert bin to midi"; |
---|
| 43 | |
---|
| 44 | static PyObject * |
---|
| 45 | Py_bintomidi (PyObject * self, PyObject * args) |
---|
| 46 | { |
---|
| 47 | smpl_t input, samplerate, fftsize; |
---|
| 48 | smpl_t output; |
---|
| 49 | |
---|
| 50 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 51 | return NULL; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | output = aubio_bintomidi (input, samplerate, fftsize); |
---|
| 55 | |
---|
| 56 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | static char Py_miditobin_doc[] = "convert midi to bin"; |
---|
| 60 | |
---|
| 61 | static PyObject * |
---|
| 62 | Py_miditobin (PyObject * self, PyObject * args) |
---|
| 63 | { |
---|
| 64 | smpl_t input, samplerate, fftsize; |
---|
| 65 | smpl_t output; |
---|
| 66 | |
---|
| 67 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 68 | return NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | output = aubio_miditobin (input, samplerate, fftsize); |
---|
| 72 | |
---|
| 73 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static char Py_bintofreq_doc[] = "convert bin to freq"; |
---|
| 77 | |
---|
| 78 | static PyObject * |
---|
| 79 | Py_bintofreq (PyObject * self, PyObject * args) |
---|
| 80 | { |
---|
| 81 | smpl_t input, samplerate, fftsize; |
---|
| 82 | smpl_t output; |
---|
| 83 | |
---|
| 84 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 85 | return NULL; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | output = aubio_bintofreq (input, samplerate, fftsize); |
---|
| 89 | |
---|
| 90 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | static char Py_freqtobin_doc[] = "convert freq to bin"; |
---|
| 94 | |
---|
| 95 | static PyObject * |
---|
| 96 | Py_freqtobin (PyObject * self, PyObject * args) |
---|
| 97 | { |
---|
| 98 | smpl_t input, samplerate, fftsize; |
---|
| 99 | smpl_t output; |
---|
| 100 | |
---|
| 101 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 102 | return NULL; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | output = aubio_freqtobin (input, samplerate, fftsize); |
---|
| 106 | |
---|
| 107 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 108 | } |
---|
| 109 | |
---|
[207ed19] | 110 | static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; |
---|
| 111 | |
---|
| 112 | static PyObject * |
---|
| 113 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 114 | { |
---|
| 115 | PyObject *input; |
---|
[ce4bfe3] | 116 | fvec_t *vec; |
---|
[207ed19] | 117 | PyObject *result; |
---|
| 118 | |
---|
| 119 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 120 | return NULL; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if (input == NULL) { |
---|
| 124 | return NULL; |
---|
| 125 | } |
---|
| 126 | |
---|
[ce4bfe3] | 127 | vec = PyAubio_ArrayToCFvec (input); |
---|
[207ed19] | 128 | |
---|
| 129 | if (vec == NULL) { |
---|
| 130 | return NULL; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | // compute the function |
---|
[ce4bfe3] | 134 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); |
---|
[207ed19] | 135 | if (result == NULL) { |
---|
| 136 | return NULL; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | return result; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | static char Py_min_removal_doc[] = "compute zero crossing rate"; |
---|
| 143 | |
---|
[ce4bfe3] | 144 | static PyObject * |
---|
[207ed19] | 145 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 146 | { |
---|
| 147 | PyObject *input; |
---|
[ce4bfe3] | 148 | fvec_t *vec; |
---|
[207ed19] | 149 | |
---|
[ccf8b77] | 150 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
[207ed19] | 151 | return NULL; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | if (input == NULL) { |
---|
| 155 | return NULL; |
---|
| 156 | } |
---|
| 157 | |
---|
[ce4bfe3] | 158 | vec = PyAubio_ArrayToCFvec (input); |
---|
[207ed19] | 159 | |
---|
| 160 | if (vec == NULL) { |
---|
| 161 | return NULL; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // compute the function |
---|
[ce4bfe3] | 165 | fvec_min_removal (vec); |
---|
[615ac7d] | 166 | |
---|
[207ed19] | 167 | // since this function does not return, we could return None |
---|
[9e6695d] | 168 | //Py_RETURN_NONE; |
---|
[ce4bfe3] | 169 | // however it is convenient to return the modified vector |
---|
| 170 | return (PyObject *) PyAubio_CFvecToArray(vec); |
---|
[207ed19] | 171 | // or even without converting it back to an array |
---|
[9b23eb31] | 172 | //Py_INCREF(vec); |
---|
| 173 | //return (PyObject *)vec; |
---|
[207ed19] | 174 | } |
---|
| 175 | |
---|
[0a6c211] | 176 | static PyMethodDef aubio_methods[] = { |
---|
[6a50b9e] | 177 | {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, |
---|
| 178 | {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, |
---|
| 179 | {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, |
---|
| 180 | {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, |
---|
[0a6c211] | 181 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[6a50b9e] | 182 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
---|
[207ed19] | 183 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[ccf8b77] | 184 | {NULL, NULL} /* Sentinel */ |
---|
[0a6c211] | 185 | }; |
---|
| 186 | |
---|
[ae6e15c] | 187 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
| 188 | |
---|
[0a6c211] | 189 | PyMODINIT_FUNC |
---|
| 190 | init_aubio (void) |
---|
| 191 | { |
---|
| 192 | PyObject *m; |
---|
| 193 | int err; |
---|
| 194 | |
---|
[7a6521d] | 195 | // fvec is defined in __init__.py |
---|
[ce4bfe3] | 196 | if ( (PyType_Ready (&Py_cvecType) < 0) |
---|
| 197 | || (PyType_Ready (&Py_filterType) < 0) |
---|
| 198 | || (PyType_Ready (&Py_filterbankType) < 0) |
---|
| 199 | || (PyType_Ready (&Py_fftType) < 0) |
---|
| 200 | || (PyType_Ready (&Py_pvocType) < 0) |
---|
[449bff6] | 201 | // generated objects |
---|
| 202 | || (generated_types_ready() < 0 ) |
---|
[615ac7d] | 203 | ) { |
---|
[0a6c211] | 204 | return; |
---|
| 205 | } |
---|
| 206 | |
---|
[1458de5] | 207 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
| 208 | |
---|
| 209 | if (m == NULL) { |
---|
| 210 | return; |
---|
| 211 | } |
---|
| 212 | |
---|
[0a6c211] | 213 | err = _import_array (); |
---|
| 214 | if (err != 0) { |
---|
| 215 | fprintf (stderr, |
---|
[ad5203c] | 216 | "Unable to import Numpy array from aubio module (error %d)\n", err); |
---|
[0a6c211] | 217 | } |
---|
| 218 | |
---|
[9b23eb31] | 219 | Py_INCREF (&Py_cvecType); |
---|
| 220 | PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); |
---|
| 221 | Py_INCREF (&Py_filterType); |
---|
| 222 | PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); |
---|
[615ac7d] | 223 | Py_INCREF (&Py_filterbankType); |
---|
| 224 | PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); |
---|
| 225 | Py_INCREF (&Py_fftType); |
---|
| 226 | PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); |
---|
| 227 | Py_INCREF (&Py_pvocType); |
---|
| 228 | PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); |
---|
[449bff6] | 229 | |
---|
[7a6521d] | 230 | // add generated objects |
---|
[449bff6] | 231 | add_generated_objects(m); |
---|
[0d222ee] | 232 | |
---|
| 233 | // add ufunc |
---|
| 234 | add_ufuncs(m); |
---|
[0a6c211] | 235 | } |
---|