1 #define PY_AUBIO_MODULE_MAIN
2 #include "aubio-types.h"
3 #include "py-musicutils.h"
5 static char aubio_module_doc[] = "Python module for the aubio library";
7 static char Py_alpha_norm_doc[] = ""
8 "alpha_norm(fvec, integer) -> float\n"
10 "Compute alpha normalisation factor on vector, given alpha\n"
15 ">>> b = alpha_norm(a, 9)";
17 static char Py_bintomidi_doc[] = ""
18 "bintomidi(float, samplerate = integer, fftsize = integer) -> float\n"
20 "Convert bin (float) to midi (float), given the sampling rate and the FFT size\n"
25 ">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)";
27 static char Py_miditobin_doc[] = ""
28 "miditobin(float, samplerate = integer, fftsize = integer) -> float\n"
30 "Convert midi (float) to bin (float), given the sampling rate and the FFT size\n"
35 ">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)";
37 static char Py_bintofreq_doc[] = ""
38 "bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
40 "Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
45 ">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
47 static char Py_freqtobin_doc[] = ""
48 "freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
50 "Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
55 ">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
57 static char Py_zero_crossing_rate_doc[] = ""
58 "zero_crossing_rate(fvec) -> float\n"
60 "Compute Zero crossing rate of a vector\n"
65 ">>> z = zero_crossing_rate(a)";
67 static char Py_min_removal_doc[] = ""
68 "min_removal(fvec) -> float\n"
70 "Remove the minimum value of a vector, in-place modification\n"
77 extern void add_ufuncs ( PyObject *m );
78 extern int generated_types_ready(void);
81 Py_alpha_norm (PyObject * self, PyObject * args)
88 if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
96 if (!PyAubio_ArrayToCFvec(input, &vec)) {
100 // compute the function
101 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
102 if (result == NULL) {
110 Py_bintomidi (PyObject * self, PyObject * args)
112 smpl_t input, samplerate, fftsize;
115 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
119 output = aubio_bintomidi (input, samplerate, fftsize);
121 return (PyObject *)PyFloat_FromDouble (output);
125 Py_miditobin (PyObject * self, PyObject * args)
127 smpl_t input, samplerate, fftsize;
130 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
134 output = aubio_miditobin (input, samplerate, fftsize);
136 return (PyObject *)PyFloat_FromDouble (output);
140 Py_bintofreq (PyObject * self, PyObject * args)
142 smpl_t input, samplerate, fftsize;
145 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
149 output = aubio_bintofreq (input, samplerate, fftsize);
151 return (PyObject *)PyFloat_FromDouble (output);
155 Py_freqtobin (PyObject * self, PyObject * args)
157 smpl_t input, samplerate, fftsize;
160 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
164 output = aubio_freqtobin (input, samplerate, fftsize);
166 return (PyObject *)PyFloat_FromDouble (output);
170 Py_zero_crossing_rate (PyObject * self, PyObject * args)
176 if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
184 if (!PyAubio_ArrayToCFvec(input, &vec)) {
188 // compute the function
189 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
190 if (result == NULL) {
198 Py_min_removal(PyObject * self, PyObject * args)
203 if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
211 if (!PyAubio_ArrayToCFvec(input, &vec)) {
215 // compute the function
216 fvec_min_removal (&vec);
218 // since this function does not return, we could return None
220 // however it is convenient to return the modified vector
221 return (PyObject *) PyAubio_CFvecToArray(&vec);
222 // or even without converting it back to an array
224 //return (PyObject *)vec;
227 static PyMethodDef aubio_methods[] = {
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 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
233 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
234 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
235 {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
236 {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
237 {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
238 {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
239 {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
240 {NULL, NULL, 0, NULL} /* Sentinel */
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 */
250 aubio_methods, /* m_methods */
252 NULL, /* m_traverse */
259 aubio_log_function(int level, const char *message, void *data)
261 // remove trailing \n
263 if ((pos=strchr(message, '\n')) != NULL) {
267 if (level == AUBIO_LOG_ERR) {
268 PyErr_Format(PyExc_RuntimeError, "%s", message);
270 PyErr_WarnEx(PyExc_UserWarning, message, 1);
280 // fvec is defined in __init__.py
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)
286 || (PyType_Ready (&Py_sourceType) < 0)
287 || (PyType_Ready (&Py_sinkType) < 0)
289 || (generated_types_ready() < 0 )
294 #if PY_MAJOR_VERSION >= 3
295 m = PyModule_Create(&moduledef);
297 m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
304 err = _import_array ();
307 "Unable to import Numpy array from aubio module (error %d)\n", err);
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);
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);
320 Py_INCREF (&Py_sourceType);
321 PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
322 Py_INCREF (&Py_sinkType);
323 PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
325 PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
326 PyModule_AddStringConstant(m, "__version__", AUBIO_VERSION);
328 // add generated objects
329 add_generated_objects(m);
334 aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL);
335 aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL);
339 #if PY_MAJOR_VERSION >= 3
341 PyMODINIT_FUNC PyInit__aubio(void)
347 PyMODINIT_FUNC init_aubio(void)