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(fvec, integer) -> float\n"
15 "Compute alpha normalisation factor on vector, given alpha\n"
20 ">>> b = alpha_norm(a, 9)";
22 static char Py_bintomidi_doc[] = ""
23 "bintomidi(float, samplerate = integer, fftsize = integer) -> float\n"
25 "Convert bin (float) to midi (float), given the sampling rate and the FFT size\n"
30 ">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)";
32 static char Py_miditobin_doc[] = ""
33 "miditobin(float, samplerate = integer, fftsize = integer) -> float\n"
35 "Convert midi (float) to bin (float), given the sampling rate and the FFT size\n"
40 ">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)";
42 static char Py_bintofreq_doc[] = ""
43 "bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
45 "Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
50 ">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
52 static char Py_freqtobin_doc[] = ""
53 "freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
55 "Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
60 ">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
62 static char Py_zero_crossing_rate_doc[] = ""
63 "zero_crossing_rate(fvec) -> float\n"
65 "Compute Zero crossing rate of a vector\n"
70 ">>> z = zero_crossing_rate(a)";
72 static char Py_min_removal_doc[] = ""
73 "min_removal(fvec) -> float\n"
75 "Remove the minimum value of a vector, in-place modification\n"
82 extern void add_ufuncs ( PyObject *m );
83 extern int generated_types_ready(void);
86 Py_alpha_norm (PyObject * self, PyObject * args)
93 if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
101 if (!PyAubio_ArrayToCFvec(input, &vec)) {
105 // compute the function
106 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
107 if (result == NULL) {
115 Py_bintomidi (PyObject * self, PyObject * args)
117 smpl_t input, samplerate, fftsize;
120 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
124 output = aubio_bintomidi (input, samplerate, fftsize);
126 return (PyObject *)PyFloat_FromDouble (output);
130 Py_miditobin (PyObject * self, PyObject * args)
132 smpl_t input, samplerate, fftsize;
135 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
139 output = aubio_miditobin (input, samplerate, fftsize);
141 return (PyObject *)PyFloat_FromDouble (output);
145 Py_bintofreq (PyObject * self, PyObject * args)
147 smpl_t input, samplerate, fftsize;
150 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
154 output = aubio_bintofreq (input, samplerate, fftsize);
156 return (PyObject *)PyFloat_FromDouble (output);
160 Py_freqtobin (PyObject * self, PyObject * args)
162 smpl_t input, samplerate, fftsize;
165 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
169 output = aubio_freqtobin (input, samplerate, fftsize);
171 return (PyObject *)PyFloat_FromDouble (output);
175 Py_zero_crossing_rate (PyObject * self, PyObject * args)
181 if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
189 if (!PyAubio_ArrayToCFvec(input, &vec)) {
193 // compute the function
194 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
195 if (result == NULL) {
203 Py_min_removal(PyObject * self, PyObject * args)
208 if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
216 if (!PyAubio_ArrayToCFvec(input, &vec)) {
220 // compute the function
221 fvec_min_removal (&vec);
223 // since this function does not return, we could return None
225 // however it is convenient to return the modified vector
226 return (PyObject *) PyAubio_CFvecToArray(&vec);
227 // or even without converting it back to an array
229 //return (PyObject *)vec;
232 static PyMethodDef aubio_methods[] = {
233 {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
234 {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
235 {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
236 {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
237 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
238 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
239 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
240 {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
241 {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
242 {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
243 {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
244 {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
245 {NULL, NULL, 0, NULL} /* Sentinel */
248 #if PY_MAJOR_VERSION >= 3
249 // Python3 module definition
250 static struct PyModuleDef moduledef = {
251 PyModuleDef_HEAD_INIT,
252 "_aubio", /* m_name */
253 aubio_module_doc, /* m_doc */
255 aubio_methods, /* m_methods */
257 NULL, /* m_traverse */
264 aubio_log_function(int level, const char *message, void *data)
266 // remove trailing \n
268 if ((pos=strchr(message, '\n')) != NULL) {
272 if (level == AUBIO_LOG_ERR) {
273 PyErr_Format(PyExc_RuntimeError, "%s", message);
275 PyErr_WarnEx(PyExc_UserWarning, message, 1);
285 // fvec is defined in __init__.py
286 if ( (PyType_Ready (&Py_cvecType) < 0)
287 || (PyType_Ready (&Py_filterType) < 0)
288 || (PyType_Ready (&Py_filterbankType) < 0)
289 || (PyType_Ready (&Py_fftType) < 0)
290 || (PyType_Ready (&Py_pvocType) < 0)
291 || (PyType_Ready (&Py_sourceType) < 0)
292 || (PyType_Ready (&Py_sinkType) < 0)
294 || (generated_types_ready() < 0 )
299 #if PY_MAJOR_VERSION >= 3
300 m = PyModule_Create(&moduledef);
302 m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
309 err = _import_array ();
312 "Unable to import Numpy array from aubio module (error %d)\n", err);
315 Py_INCREF (&Py_cvecType);
316 PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
317 Py_INCREF (&Py_filterType);
318 PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
319 Py_INCREF (&Py_filterbankType);
320 PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
321 Py_INCREF (&Py_fftType);
322 PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
323 Py_INCREF (&Py_pvocType);
324 PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
325 Py_INCREF (&Py_sourceType);
326 PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
327 Py_INCREF (&Py_sinkType);
328 PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
330 PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
331 PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
333 // add generated objects
334 add_generated_objects(m);
339 aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL);
340 aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL);
344 #if PY_MAJOR_VERSION >= 3
346 PyMODINIT_FUNC PyInit__aubio(void)
352 PyMODINIT_FUNC init_aubio(void)