[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 ); |
---|
[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; |
---|
[ce4bfe3] | 14 | fvec_t *vec; |
---|
[6b1aafc] | 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 | |
---|
[ce4bfe3] | 26 | vec = PyAubio_ArrayToCFvec (input); |
---|
[6b1aafc] | 27 | |
---|
| 28 | if (vec == NULL) { |
---|
| 29 | return NULL; |
---|
| 30 | } |
---|
| 31 | |
---|
[ae6e15c] | 32 | // compute the function |
---|
[ce4bfe3] | 33 | result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); |
---|
[0a6c211] | 34 | if (result == NULL) { |
---|
| 35 | return NULL; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | return result; |
---|
| 39 | } |
---|
| 40 | |
---|
[6514bb6] | 41 | static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]"; |
---|
| 42 | |
---|
| 43 | static PyObject * |
---|
| 44 | Py_unwrap2pi (PyObject * self, PyObject * args) |
---|
| 45 | { |
---|
| 46 | smpl_t input; |
---|
| 47 | smpl_t output; |
---|
| 48 | |
---|
| 49 | if (!PyArg_ParseTuple (args, "|f", &input)) { |
---|
| 50 | return NULL; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | output = aubio_unwrap2pi (input); |
---|
| 54 | |
---|
| 55 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 56 | } |
---|
| 57 | |
---|
[6a50b9e] | 58 | static char Py_bintomidi_doc[] = "convert bin to midi"; |
---|
| 59 | |
---|
| 60 | static PyObject * |
---|
| 61 | Py_bintomidi (PyObject * self, PyObject * args) |
---|
| 62 | { |
---|
| 63 | smpl_t input, samplerate, fftsize; |
---|
| 64 | smpl_t output; |
---|
| 65 | |
---|
| 66 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 67 | return NULL; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | output = aubio_bintomidi (input, samplerate, fftsize); |
---|
| 71 | |
---|
| 72 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | static char Py_miditobin_doc[] = "convert midi to bin"; |
---|
| 76 | |
---|
| 77 | static PyObject * |
---|
| 78 | Py_miditobin (PyObject * self, PyObject * args) |
---|
| 79 | { |
---|
| 80 | smpl_t input, samplerate, fftsize; |
---|
| 81 | smpl_t output; |
---|
| 82 | |
---|
| 83 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 84 | return NULL; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | output = aubio_miditobin (input, samplerate, fftsize); |
---|
| 88 | |
---|
| 89 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | static char Py_bintofreq_doc[] = "convert bin to freq"; |
---|
| 93 | |
---|
| 94 | static PyObject * |
---|
| 95 | Py_bintofreq (PyObject * self, PyObject * args) |
---|
| 96 | { |
---|
| 97 | smpl_t input, samplerate, fftsize; |
---|
| 98 | smpl_t output; |
---|
| 99 | |
---|
| 100 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 101 | return NULL; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | output = aubio_bintofreq (input, samplerate, fftsize); |
---|
| 105 | |
---|
| 106 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | static char Py_freqtobin_doc[] = "convert freq to bin"; |
---|
| 110 | |
---|
| 111 | static PyObject * |
---|
| 112 | Py_freqtobin (PyObject * self, PyObject * args) |
---|
| 113 | { |
---|
| 114 | smpl_t input, samplerate, fftsize; |
---|
| 115 | smpl_t output; |
---|
| 116 | |
---|
| 117 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
| 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | output = aubio_freqtobin (input, samplerate, fftsize); |
---|
| 122 | |
---|
| 123 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | static char Py_freqtomidi_doc[] = "convert freq to midi"; |
---|
| 127 | |
---|
| 128 | static PyObject * |
---|
| 129 | Py_freqtomidi (PyObject * self, PyObject * args) |
---|
| 130 | { |
---|
| 131 | smpl_t input; |
---|
| 132 | smpl_t output; |
---|
| 133 | |
---|
| 134 | if (!PyArg_ParseTuple (args, "|f", &input)) { |
---|
| 135 | return NULL; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | output = aubio_freqtomidi (input); |
---|
| 139 | |
---|
| 140 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | static char Py_miditofreq_doc[] = "convert midi to freq"; |
---|
| 144 | |
---|
| 145 | static PyObject * |
---|
| 146 | Py_miditofreq (PyObject * self, PyObject * args) |
---|
| 147 | { |
---|
| 148 | smpl_t input; |
---|
| 149 | smpl_t output; |
---|
| 150 | |
---|
| 151 | if (!PyArg_ParseTuple (args, "|f", &input)) { |
---|
| 152 | return NULL; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | output = aubio_miditofreq (input); |
---|
| 156 | |
---|
| 157 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 158 | } |
---|
| 159 | |
---|
[207ed19] | 160 | static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; |
---|
| 161 | |
---|
| 162 | static PyObject * |
---|
| 163 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 164 | { |
---|
| 165 | PyObject *input; |
---|
[ce4bfe3] | 166 | fvec_t *vec; |
---|
[207ed19] | 167 | PyObject *result; |
---|
| 168 | |
---|
| 169 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 170 | return NULL; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | if (input == NULL) { |
---|
| 174 | return NULL; |
---|
| 175 | } |
---|
| 176 | |
---|
[ce4bfe3] | 177 | vec = PyAubio_ArrayToCFvec (input); |
---|
[207ed19] | 178 | |
---|
| 179 | if (vec == NULL) { |
---|
| 180 | return NULL; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // compute the function |
---|
[ce4bfe3] | 184 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); |
---|
[207ed19] | 185 | if (result == NULL) { |
---|
| 186 | return NULL; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | return result; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | static char Py_min_removal_doc[] = "compute zero crossing rate"; |
---|
| 193 | |
---|
[ce4bfe3] | 194 | static PyObject * |
---|
[207ed19] | 195 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 196 | { |
---|
| 197 | PyObject *input; |
---|
[ce4bfe3] | 198 | fvec_t *vec; |
---|
[207ed19] | 199 | |
---|
[ccf8b77] | 200 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
[207ed19] | 201 | return NULL; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if (input == NULL) { |
---|
| 205 | return NULL; |
---|
| 206 | } |
---|
| 207 | |
---|
[ce4bfe3] | 208 | vec = PyAubio_ArrayToCFvec (input); |
---|
[207ed19] | 209 | |
---|
| 210 | if (vec == NULL) { |
---|
| 211 | return NULL; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | // compute the function |
---|
[ce4bfe3] | 215 | fvec_min_removal (vec); |
---|
[615ac7d] | 216 | |
---|
[207ed19] | 217 | // since this function does not return, we could return None |
---|
| 218 | //return Py_None; |
---|
[ce4bfe3] | 219 | // however it is convenient to return the modified vector |
---|
| 220 | return (PyObject *) PyAubio_CFvecToArray(vec); |
---|
[207ed19] | 221 | // or even without converting it back to an array |
---|
[9b23eb31] | 222 | //Py_INCREF(vec); |
---|
| 223 | //return (PyObject *)vec; |
---|
[207ed19] | 224 | } |
---|
| 225 | |
---|
[0a6c211] | 226 | static PyMethodDef aubio_methods[] = { |
---|
[0d222ee] | 227 | //{"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc}, |
---|
[6a50b9e] | 228 | {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, |
---|
| 229 | {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, |
---|
| 230 | {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, |
---|
| 231 | {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, |
---|
| 232 | {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc}, |
---|
| 233 | {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc}, |
---|
[0a6c211] | 234 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[6a50b9e] | 235 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
---|
[207ed19] | 236 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[ccf8b77] | 237 | {NULL, NULL} /* Sentinel */ |
---|
[0a6c211] | 238 | }; |
---|
| 239 | |
---|
[ae6e15c] | 240 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
| 241 | |
---|
[0a6c211] | 242 | PyMODINIT_FUNC |
---|
| 243 | init_aubio (void) |
---|
| 244 | { |
---|
| 245 | PyObject *m; |
---|
| 246 | int err; |
---|
| 247 | |
---|
[7a6521d] | 248 | // fvec is defined in __init__.py |
---|
[ce4bfe3] | 249 | if ( (PyType_Ready (&Py_cvecType) < 0) |
---|
| 250 | || (PyType_Ready (&Py_filterType) < 0) |
---|
| 251 | || (PyType_Ready (&Py_filterbankType) < 0) |
---|
| 252 | || (PyType_Ready (&Py_fftType) < 0) |
---|
| 253 | || (PyType_Ready (&Py_pvocType) < 0) |
---|
[449bff6] | 254 | // generated objects |
---|
| 255 | || (generated_types_ready() < 0 ) |
---|
[615ac7d] | 256 | ) { |
---|
[0a6c211] | 257 | return; |
---|
| 258 | } |
---|
| 259 | |
---|
[1458de5] | 260 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
| 261 | |
---|
| 262 | if (m == NULL) { |
---|
| 263 | return; |
---|
| 264 | } |
---|
| 265 | |
---|
[0a6c211] | 266 | err = _import_array (); |
---|
| 267 | if (err != 0) { |
---|
| 268 | fprintf (stderr, |
---|
[ad5203c] | 269 | "Unable to import Numpy array from aubio module (error %d)\n", err); |
---|
[0a6c211] | 270 | } |
---|
| 271 | |
---|
[9b23eb31] | 272 | Py_INCREF (&Py_cvecType); |
---|
| 273 | PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); |
---|
| 274 | Py_INCREF (&Py_filterType); |
---|
| 275 | PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); |
---|
[615ac7d] | 276 | Py_INCREF (&Py_filterbankType); |
---|
| 277 | PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); |
---|
| 278 | Py_INCREF (&Py_fftType); |
---|
| 279 | PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); |
---|
| 280 | Py_INCREF (&Py_pvocType); |
---|
| 281 | PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); |
---|
[449bff6] | 282 | |
---|
[7a6521d] | 283 | // add generated objects |
---|
[449bff6] | 284 | add_generated_objects(m); |
---|
[0d222ee] | 285 | |
---|
| 286 | // add ufunc |
---|
| 287 | add_ufuncs(m); |
---|
[0a6c211] | 288 | } |
---|