1 #define PY_AUBIO_MODULE_MAIN
2 #include "aubio-types.h"
3 #include "py-musicutils.h"
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 REDEFINESTRING(x) #x
8 #define DEFINEDSTRING(x) REDEFINESTRING(x)
10 static char aubio_module_doc[] = "Python module for the aubio library";
12 static char Py_alpha_norm_doc[] = ""
13 "alpha_norm(vec, alpha)\n"
15 "Compute `alpha` normalisation factor of vector `vec`.\n"
27 " p-norm of the input vector, where `p=alpha`\n"
32 ">>> a = aubio.fvec(np.arange(10)); alpha = 2\n"
33 ">>> aubio.alpha_norm(a, alpha), (sum(a**alpha)/len(a))**(1./alpha)\n"
34 "(5.338539123535156, 5.338539126015656)\n"
42 " \\|\\frac{\\sum_{n=0}^{N-1}{{x_n}^{\\alpha}}}{N}\\|^{1/\\alpha}\n"
45 static char Py_bintomidi_doc[] = ""
46 "bintomidi(fftbin, samplerate, fftsize)\n"
48 "Convert FFT bin to frequency in midi note, given the sampling rate\n"
49 "and the size of the FFT.\n"
54 " input frequency bin\n"
55 "samplerate : float\n"
56 " sampling rate of the signal\n"
63 " Frequency converted to midi note.\n"
68 ">>> aubio.bintomidi(10, 44100, 1024)\n"
72 static char Py_miditobin_doc[] = ""
73 "miditobin(midi, samplerate, fftsize)\n"
75 "Convert frequency in midi note to FFT bin, given the sampling rate\n"
76 "and the size of the FFT.\n"
81 " input frequency, in midi note\n"
82 "samplerate : float\n"
83 " sampling rate of the signal\n"
90 " Frequency converted to FFT bin.\n"
95 ">>> aubio.miditobin(69, 44100, 1024)\n"
96 "10.216779708862305\n"
97 ">>> aubio.miditobin(75.08, 32000, 512)\n"
98 "10.002175331115723\n"
101 static char Py_bintofreq_doc[] = ""
102 "bintofreq(fftbin, samplerate, fftsize)\n"
104 "Convert FFT bin to frequency in Hz, given the sampling rate\n"
105 "and the size of the FFT.\n"
110 " input frequency bin\n"
111 "samplerate : float\n"
112 " sampling rate of the signal\n"
119 " Frequency converted to Hz.\n"
124 ">>> aubio.bintofreq(10, 44100, 1024)\n"
128 static char Py_freqtobin_doc[] = ""
129 "freqtobin(freq, samplerate, fftsize)\n"
131 "Convert frequency in Hz to FFT bin, given the sampling rate\n"
132 "and the size of the FFT.\n"
137 " input frequency, in midi note\n"
138 "samplerate : float\n"
139 " sampling rate of the signal\n"
146 " Frequency converted to FFT bin.\n"
151 ">>> aubio.freqtobin(440, 44100, 1024)\n"
152 "10.216779708862305\n"
155 static char Py_zero_crossing_rate_doc[] = ""
156 "zero_crossing_rate(vec)\n"
158 "Compute zero-crossing rate of `vec`.\n"
168 " Zero-crossing rate.\n"
173 ">>> a = np.linspace(-1., 1., 1000, dtype=aubio.float_type)\n"
174 ">>> aubio.zero_crossing_rate(a), 1/1000\n"
175 "(0.0010000000474974513, 0.001)\n"
178 static char Py_min_removal_doc[] = ""
181 "Remove the minimum value of a vector to each of its element.\n"
183 "Modifies the input vector in-place and returns a reference to it.\n"
193 " modified input vector\n"
198 ">>> aubio.min_removal(aubio.fvec(np.arange(1,4)))\n"
199 "array([0., 1., 2.], dtype=" AUBIO_NPY_SMPL_STR ")\n"
202 extern void add_ufuncs ( PyObject *m );
203 extern int generated_types_ready(void);
206 Py_alpha_norm (PyObject * self, PyObject * args)
213 if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
221 if (!PyAubio_ArrayToCFvec(input, &vec)) {
225 // compute the function
226 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
227 if (result == NULL) {
235 Py_bintomidi (PyObject * self, PyObject * args)
237 smpl_t input, samplerate, fftsize;
240 if (!PyArg_ParseTuple (args,
241 "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
242 &input, &samplerate, &fftsize)) {
246 output = aubio_bintomidi (input, samplerate, fftsize);
248 return (PyObject *)PyFloat_FromDouble (output);
252 Py_miditobin (PyObject * self, PyObject * args)
254 smpl_t input, samplerate, fftsize;
257 if (!PyArg_ParseTuple (args,
258 "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
259 &input, &samplerate, &fftsize)) {
263 output = aubio_miditobin (input, samplerate, fftsize);
265 return (PyObject *)PyFloat_FromDouble (output);
269 Py_bintofreq (PyObject * self, PyObject * args)
271 smpl_t input, samplerate, fftsize;
274 if (!PyArg_ParseTuple (args,
275 "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
276 &input, &samplerate, &fftsize)) {
280 output = aubio_bintofreq (input, samplerate, fftsize);
282 return (PyObject *)PyFloat_FromDouble (output);
286 Py_freqtobin (PyObject * self, PyObject * args)
288 smpl_t input, samplerate, fftsize;
291 if (!PyArg_ParseTuple (args,
292 "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
293 &input, &samplerate, &fftsize)) {
297 output = aubio_freqtobin (input, samplerate, fftsize);
299 return (PyObject *)PyFloat_FromDouble (output);
303 Py_zero_crossing_rate (PyObject * self, PyObject * args)
309 if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
317 if (!PyAubio_ArrayToCFvec(input, &vec)) {
321 // compute the function
322 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
323 if (result == NULL) {
331 Py_min_removal(PyObject * self, PyObject * args)
336 if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
344 if (!PyAubio_ArrayToCFvec(input, &vec)) {
348 // compute the function
349 fvec_min_removal (&vec);
351 // since this function does not return, we could return None
353 // however it is convenient to return the modified vector
354 return (PyObject *) PyAubio_CFvecToArray(&vec);
355 // or even without converting it back to an array
357 //return (PyObject *)vec;
360 static PyMethodDef aubio_methods[] = {
361 {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
362 {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
363 {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
364 {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
365 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
366 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
367 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
368 {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
369 {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
370 {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
371 {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
372 {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
373 {"shift", Py_aubio_shift, METH_VARARGS, Py_aubio_shift_doc},
374 {"ishift", Py_aubio_ishift, METH_VARARGS, Py_aubio_ishift_doc},
375 {NULL, NULL, 0, NULL} /* Sentinel */
378 #if PY_MAJOR_VERSION >= 3
379 // Python3 module definition
380 static struct PyModuleDef moduledef = {
381 PyModuleDef_HEAD_INIT,
382 "_aubio", /* m_name */
383 aubio_module_doc, /* m_doc */
385 aubio_methods, /* m_methods */
387 NULL, /* m_traverse */
394 aubio_log_function(int level, const char *message, void *data)
396 // remove trailing \n
398 if ((pos=strchr(message, '\n')) != NULL) {
402 if (level == AUBIO_LOG_ERR) {
403 PyErr_Format(PyExc_RuntimeError, "%s", message);
405 PyErr_WarnEx(PyExc_UserWarning, message, 1);
415 // fvec is defined in __init__.py
416 if ( (PyType_Ready (&Py_cvecType) < 0)
417 || (PyType_Ready (&Py_filterType) < 0)
418 || (PyType_Ready (&Py_filterbankType) < 0)
419 || (PyType_Ready (&Py_fftType) < 0)
420 || (PyType_Ready (&Py_pvocType) < 0)
421 || (PyType_Ready (&Py_sourceType) < 0)
422 || (PyType_Ready (&Py_sinkType) < 0)
424 || (generated_types_ready() < 0 )
429 #if PY_MAJOR_VERSION >= 3
430 m = PyModule_Create(&moduledef);
432 m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
439 err = _import_array ();
442 "Unable to import Numpy array from aubio module (error %d)\n", err);
445 Py_INCREF (&Py_cvecType);
446 PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
447 Py_INCREF (&Py_filterType);
448 PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
449 Py_INCREF (&Py_filterbankType);
450 PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
451 Py_INCREF (&Py_fftType);
452 PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
453 Py_INCREF (&Py_pvocType);
454 PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
455 Py_INCREF (&Py_sourceType);
456 PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
457 Py_INCREF (&Py_sinkType);
458 PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
460 PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
461 PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
463 // add generated objects
464 add_generated_objects(m);
469 aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL);
470 aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL);
474 #if PY_MAJOR_VERSION >= 3
476 PyMODINIT_FUNC PyInit__aubio(void)
482 PyMODINIT_FUNC init_aubio(void)