[7a6521d] | 1 | #define PY_AUBIO_MODULE_MAIN |
---|
[ae6e15c] | 2 | #include "aubio-types.h" |
---|
[913a7f1] | 3 | #include "py-musicutils.h" |
---|
[ad5203c] | 4 | |
---|
[1ba8e60] | 5 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
| 6 | |
---|
| 7 | static char Py_alpha_norm_doc[] = "" |
---|
| 8 | "alpha_norm(fvec, integer) -> float\n" |
---|
| 9 | "\n" |
---|
| 10 | "Compute alpha normalisation factor on vector, given alpha\n" |
---|
| 11 | "\n" |
---|
| 12 | "Example\n" |
---|
| 13 | "-------\n" |
---|
| 14 | "\n" |
---|
| 15 | ">>> b = alpha_norm(a, 9)"; |
---|
| 16 | |
---|
| 17 | static char Py_bintomidi_doc[] = "" |
---|
| 18 | "bintomidi(float, samplerate = integer, fftsize = integer) -> float\n" |
---|
| 19 | "\n" |
---|
| 20 | "Convert bin (float) to midi (float), given the sampling rate and the FFT size\n" |
---|
| 21 | "\n" |
---|
| 22 | "Example\n" |
---|
| 23 | "-------\n" |
---|
| 24 | "\n" |
---|
| 25 | ">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)"; |
---|
| 26 | |
---|
| 27 | static char Py_miditobin_doc[] = "" |
---|
| 28 | "miditobin(float, samplerate = integer, fftsize = integer) -> float\n" |
---|
| 29 | "\n" |
---|
| 30 | "Convert midi (float) to bin (float), given the sampling rate and the FFT size\n" |
---|
| 31 | "\n" |
---|
| 32 | "Example\n" |
---|
| 33 | "-------\n" |
---|
| 34 | "\n" |
---|
| 35 | ">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)"; |
---|
| 36 | |
---|
| 37 | static char Py_bintofreq_doc[] = "" |
---|
| 38 | "bintofreq(float, samplerate = integer, fftsize = integer) -> float\n" |
---|
| 39 | "\n" |
---|
| 40 | "Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n" |
---|
| 41 | "\n" |
---|
| 42 | "Example\n" |
---|
| 43 | "-------\n" |
---|
| 44 | "\n" |
---|
| 45 | ">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)"; |
---|
| 46 | |
---|
| 47 | static char Py_freqtobin_doc[] = "" |
---|
| 48 | "freqtobin(float, samplerate = integer, fftsize = integer) -> float\n" |
---|
| 49 | "\n" |
---|
| 50 | "Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n" |
---|
| 51 | "\n" |
---|
| 52 | "Example\n" |
---|
| 53 | "-------\n" |
---|
| 54 | "\n" |
---|
| 55 | ">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)"; |
---|
| 56 | |
---|
| 57 | static char Py_zero_crossing_rate_doc[] = "" |
---|
| 58 | "zero_crossing_rate(fvec) -> float\n" |
---|
| 59 | "\n" |
---|
| 60 | "Compute Zero crossing rate of a vector\n" |
---|
| 61 | "\n" |
---|
| 62 | "Example\n" |
---|
| 63 | "-------\n" |
---|
| 64 | "\n" |
---|
| 65 | ">>> z = zero_crossing_rate(a)"; |
---|
| 66 | |
---|
| 67 | static char Py_min_removal_doc[] = "" |
---|
| 68 | "min_removal(fvec) -> float\n" |
---|
| 69 | "\n" |
---|
| 70 | "Remove the minimum value of a vector, in-place modification\n" |
---|
| 71 | "\n" |
---|
| 72 | "Example\n" |
---|
| 73 | "-------\n" |
---|
| 74 | "\n" |
---|
| 75 | ">>> min_removal(a)"; |
---|
| 76 | |
---|
[b6230d8] | 77 | extern void add_ufuncs ( PyObject *m ); |
---|
| 78 | extern int generated_types_ready(void); |
---|
[0a6c211] | 79 | |
---|
[6b1aafc] | 80 | static PyObject * |
---|
| 81 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
| 82 | { |
---|
| 83 | PyObject *input; |
---|
[569b363] | 84 | fvec_t vec; |
---|
[6b1aafc] | 85 | smpl_t alpha; |
---|
| 86 | PyObject *result; |
---|
| 87 | |
---|
[c6388f4] | 88 | if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) { |
---|
[6b1aafc] | 89 | return NULL; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if (input == NULL) { |
---|
| 93 | return NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
[569b363] | 96 | if (!PyAubio_ArrayToCFvec(input, &vec)) { |
---|
[6b1aafc] | 97 | return NULL; |
---|
| 98 | } |
---|
| 99 | |
---|
[ae6e15c] | 100 | // compute the function |
---|
[c6388f4] | 101 | result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha)); |
---|
[0a6c211] | 102 | if (result == NULL) { |
---|
| 103 | return NULL; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return result; |
---|
| 107 | } |
---|
| 108 | |
---|
[6a50b9e] | 109 | static PyObject * |
---|
| 110 | Py_bintomidi (PyObject * self, PyObject * args) |
---|
| 111 | { |
---|
| 112 | smpl_t input, samplerate, fftsize; |
---|
| 113 | smpl_t output; |
---|
| 114 | |
---|
[c6388f4] | 115 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 116 | return NULL; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | output = aubio_bintomidi (input, samplerate, fftsize); |
---|
| 120 | |
---|
| 121 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | static PyObject * |
---|
| 125 | Py_miditobin (PyObject * self, PyObject * args) |
---|
| 126 | { |
---|
| 127 | smpl_t input, samplerate, fftsize; |
---|
| 128 | smpl_t output; |
---|
| 129 | |
---|
[c6388f4] | 130 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 131 | return NULL; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | output = aubio_miditobin (input, samplerate, fftsize); |
---|
| 135 | |
---|
| 136 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | static PyObject * |
---|
| 140 | Py_bintofreq (PyObject * self, PyObject * args) |
---|
| 141 | { |
---|
| 142 | smpl_t input, samplerate, fftsize; |
---|
| 143 | smpl_t output; |
---|
| 144 | |
---|
[c6388f4] | 145 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 146 | return NULL; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | output = aubio_bintofreq (input, samplerate, fftsize); |
---|
| 150 | |
---|
| 151 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | static PyObject * |
---|
| 155 | Py_freqtobin (PyObject * self, PyObject * args) |
---|
| 156 | { |
---|
| 157 | smpl_t input, samplerate, fftsize; |
---|
| 158 | smpl_t output; |
---|
| 159 | |
---|
[c6388f4] | 160 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 161 | return NULL; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | output = aubio_freqtobin (input, samplerate, fftsize); |
---|
| 165 | |
---|
| 166 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 167 | } |
---|
| 168 | |
---|
[207ed19] | 169 | static PyObject * |
---|
| 170 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 171 | { |
---|
| 172 | PyObject *input; |
---|
[569b363] | 173 | fvec_t vec; |
---|
[207ed19] | 174 | PyObject *result; |
---|
| 175 | |
---|
| 176 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 177 | return NULL; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if (input == NULL) { |
---|
| 181 | return NULL; |
---|
| 182 | } |
---|
| 183 | |
---|
[569b363] | 184 | if (!PyAubio_ArrayToCFvec(input, &vec)) { |
---|
[207ed19] | 185 | return NULL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // compute the function |
---|
[c6388f4] | 189 | result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec)); |
---|
[207ed19] | 190 | if (result == NULL) { |
---|
| 191 | return NULL; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | return result; |
---|
| 195 | } |
---|
| 196 | |
---|
[ce4bfe3] | 197 | static PyObject * |
---|
[207ed19] | 198 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 199 | { |
---|
| 200 | PyObject *input; |
---|
[569b363] | 201 | fvec_t vec; |
---|
[207ed19] | 202 | |
---|
[ccf8b77] | 203 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
[207ed19] | 204 | return NULL; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | if (input == NULL) { |
---|
| 208 | return NULL; |
---|
| 209 | } |
---|
| 210 | |
---|
[569b363] | 211 | if (!PyAubio_ArrayToCFvec(input, &vec)) { |
---|
[207ed19] | 212 | return NULL; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | // compute the function |
---|
[569b363] | 216 | fvec_min_removal (&vec); |
---|
[615ac7d] | 217 | |
---|
[207ed19] | 218 | // since this function does not return, we could return None |
---|
[9e6695d] | 219 | //Py_RETURN_NONE; |
---|
[ce4bfe3] | 220 | // however it is convenient to return the modified vector |
---|
[569b363] | 221 | return (PyObject *) PyAubio_CFvecToArray(&vec); |
---|
[207ed19] | 222 | // or even without converting it back to an array |
---|
[9b23eb31] | 223 | //Py_INCREF(vec); |
---|
| 224 | //return (PyObject *)vec; |
---|
[207ed19] | 225 | } |
---|
| 226 | |
---|
[0a6c211] | 227 | static PyMethodDef aubio_methods[] = { |
---|
[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}, |
---|
[0a6c211] | 232 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[6a50b9e] | 233 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
---|
[207ed19] | 234 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[5a7e2c3] | 235 | {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc}, |
---|
[4615886a] | 236 | {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc}, |
---|
[31a09d2] | 237 | {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc}, |
---|
[9c8c8a6] | 238 | {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc}, |
---|
[913a7f1] | 239 | {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, |
---|
[84fad5a] | 240 | {NULL, NULL, 0, NULL} /* Sentinel */ |
---|
[0a6c211] | 241 | }; |
---|
| 242 | |
---|
[2e4ae1d] | 243 | #if PY_MAJOR_VERSION >= 3 |
---|
| 244 | // Python3 module definition |
---|
| 245 | static struct PyModuleDef moduledef = { |
---|
| 246 | PyModuleDef_HEAD_INIT, |
---|
| 247 | "_aubio", /* m_name */ |
---|
| 248 | aubio_module_doc, /* m_doc */ |
---|
| 249 | -1, /* m_size */ |
---|
| 250 | aubio_methods, /* m_methods */ |
---|
| 251 | NULL, /* m_reload */ |
---|
| 252 | NULL, /* m_traverse */ |
---|
| 253 | NULL, /* m_clear */ |
---|
| 254 | NULL, /* m_free */ |
---|
| 255 | }; |
---|
| 256 | #endif |
---|
| 257 | |
---|
[67537d7] | 258 | void |
---|
| 259 | aubio_log_function(int level, const char *message, void *data) |
---|
| 260 | { |
---|
| 261 | // remove trailing \n |
---|
| 262 | char *pos; |
---|
| 263 | if ((pos=strchr(message, '\n')) != NULL) { |
---|
| 264 | *pos = '\0'; |
---|
| 265 | } |
---|
| 266 | // warning or error |
---|
| 267 | if (level == AUBIO_LOG_ERR) { |
---|
| 268 | PyErr_Format(PyExc_RuntimeError, "%s", message); |
---|
| 269 | } else { |
---|
| 270 | PyErr_WarnEx(PyExc_UserWarning, message, 1); |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
[2e4ae1d] | 274 | static PyObject * |
---|
| 275 | initaubio (void) |
---|
[0a6c211] | 276 | { |
---|
[2e4ae1d] | 277 | PyObject *m = NULL; |
---|
[0a6c211] | 278 | int err; |
---|
| 279 | |
---|
[7a6521d] | 280 | // fvec is defined in __init__.py |
---|
[ce4bfe3] | 281 | if ( (PyType_Ready (&Py_cvecType) < 0) |
---|
| 282 | || (PyType_Ready (&Py_filterType) < 0) |
---|
| 283 | || (PyType_Ready (&Py_filterbankType) < 0) |
---|
| 284 | || (PyType_Ready (&Py_fftType) < 0) |
---|
| 285 | || (PyType_Ready (&Py_pvocType) < 0) |
---|
[d27634d] | 286 | || (PyType_Ready (&Py_sourceType) < 0) |
---|
[f1100a4] | 287 | || (PyType_Ready (&Py_sinkType) < 0) |
---|
[449bff6] | 288 | // generated objects |
---|
| 289 | || (generated_types_ready() < 0 ) |
---|
[615ac7d] | 290 | ) { |
---|
[2e4ae1d] | 291 | return m; |
---|
[0a6c211] | 292 | } |
---|
| 293 | |
---|
[2e4ae1d] | 294 | #if PY_MAJOR_VERSION >= 3 |
---|
| 295 | m = PyModule_Create(&moduledef); |
---|
| 296 | #else |
---|
[1458de5] | 297 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
[2e4ae1d] | 298 | #endif |
---|
[1458de5] | 299 | |
---|
| 300 | if (m == NULL) { |
---|
[2e4ae1d] | 301 | return m; |
---|
[1458de5] | 302 | } |
---|
| 303 | |
---|
[0a6c211] | 304 | err = _import_array (); |
---|
| 305 | if (err != 0) { |
---|
| 306 | fprintf (stderr, |
---|
[ad5203c] | 307 | "Unable to import Numpy array from aubio module (error %d)\n", err); |
---|
[0a6c211] | 308 | } |
---|
| 309 | |
---|
[9b23eb31] | 310 | Py_INCREF (&Py_cvecType); |
---|
| 311 | PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); |
---|
| 312 | Py_INCREF (&Py_filterType); |
---|
| 313 | PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); |
---|
[615ac7d] | 314 | Py_INCREF (&Py_filterbankType); |
---|
| 315 | PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); |
---|
| 316 | Py_INCREF (&Py_fftType); |
---|
| 317 | PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); |
---|
| 318 | Py_INCREF (&Py_pvocType); |
---|
| 319 | PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); |
---|
[d27634d] | 320 | Py_INCREF (&Py_sourceType); |
---|
| 321 | PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType); |
---|
[f1100a4] | 322 | Py_INCREF (&Py_sinkType); |
---|
| 323 | PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType); |
---|
[449bff6] | 324 | |
---|
[c6388f4] | 325 | PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR); |
---|
| 326 | |
---|
[7a6521d] | 327 | // add generated objects |
---|
[449bff6] | 328 | add_generated_objects(m); |
---|
[0d222ee] | 329 | |
---|
| 330 | // add ufunc |
---|
| 331 | add_ufuncs(m); |
---|
[2e4ae1d] | 332 | |
---|
[67537d7] | 333 | aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL); |
---|
| 334 | aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL); |
---|
[2e4ae1d] | 335 | return m; |
---|
[0a6c211] | 336 | } |
---|
[2e4ae1d] | 337 | |
---|
| 338 | #if PY_MAJOR_VERSION >= 3 |
---|
| 339 | // Python3 init |
---|
| 340 | PyMODINIT_FUNC PyInit__aubio(void) |
---|
| 341 | { |
---|
| 342 | return initaubio(); |
---|
| 343 | } |
---|
| 344 | #else |
---|
| 345 | // Python 2 init |
---|
| 346 | PyMODINIT_FUNC init_aubio(void) |
---|
| 347 | { |
---|
| 348 | initaubio(); |
---|
| 349 | } |
---|
| 350 | #endif |
---|