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