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 DEFINEDSTRING(x) #x
9 static char aubio_module_doc[] = "Python module for the aubio library";
11 static char Py_alpha_norm_doc[] = ""
12 "alpha_norm(fvec, integer) -> float\n"
14 "Compute alpha normalisation factor on vector, given alpha\n"
19 ">>> b = alpha_norm(a, 9)";
21 static char Py_bintomidi_doc[] = ""
22 "bintomidi(float, samplerate = integer, fftsize = integer) -> float\n"
24 "Convert bin (float) to midi (float), given the sampling rate and the FFT size\n"
29 ">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)";
31 static char Py_miditobin_doc[] = ""
32 "miditobin(float, samplerate = integer, fftsize = integer) -> float\n"
34 "Convert midi (float) to bin (float), given the sampling rate and the FFT size\n"
39 ">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)";
41 static char Py_bintofreq_doc[] = ""
42 "bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
44 "Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
49 ">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
51 static char Py_freqtobin_doc[] = ""
52 "freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
54 "Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
59 ">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
61 static char Py_zero_crossing_rate_doc[] = ""
62 "zero_crossing_rate(fvec) -> float\n"
64 "Compute Zero crossing rate of a vector\n"
69 ">>> z = zero_crossing_rate(a)";
71 static char Py_min_removal_doc[] = ""
72 "min_removal(fvec) -> float\n"
74 "Remove the minimum value of a vector, in-place modification\n"
81 extern void add_ufuncs ( PyObject *m );
82 extern int generated_types_ready(void);
85 Py_alpha_norm (PyObject * self, PyObject * args)
92 if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
100 if (!PyAubio_ArrayToCFvec(input, &vec)) {
104 // compute the function
105 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
106 if (result == NULL) {
114 Py_bintomidi (PyObject * self, PyObject * args)
116 smpl_t input, samplerate, fftsize;
119 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
123 output = aubio_bintomidi (input, samplerate, fftsize);
125 return (PyObject *)PyFloat_FromDouble (output);
129 Py_miditobin (PyObject * self, PyObject * args)
131 smpl_t input, samplerate, fftsize;
134 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
138 output = aubio_miditobin (input, samplerate, fftsize);
140 return (PyObject *)PyFloat_FromDouble (output);
144 Py_bintofreq (PyObject * self, PyObject * args)
146 smpl_t input, samplerate, fftsize;
149 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
153 output = aubio_bintofreq (input, samplerate, fftsize);
155 return (PyObject *)PyFloat_FromDouble (output);
159 Py_freqtobin (PyObject * self, PyObject * args)
161 smpl_t input, samplerate, fftsize;
164 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
168 output = aubio_freqtobin (input, samplerate, fftsize);
170 return (PyObject *)PyFloat_FromDouble (output);
174 Py_zero_crossing_rate (PyObject * self, PyObject * args)
180 if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
188 if (!PyAubio_ArrayToCFvec(input, &vec)) {
192 // compute the function
193 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
194 if (result == NULL) {
202 Py_min_removal(PyObject * self, PyObject * args)
207 if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
215 if (!PyAubio_ArrayToCFvec(input, &vec)) {
219 // compute the function
220 fvec_min_removal (&vec);
222 // since this function does not return, we could return None
224 // however it is convenient to return the modified vector
225 return (PyObject *) PyAubio_CFvecToArray(&vec);
226 // or even without converting it back to an array
228 //return (PyObject *)vec;
231 static PyMethodDef aubio_methods[] = {
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},
236 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
237 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
238 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
239 {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
240 {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
241 {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
242 {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
243 {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
244 {NULL, NULL, 0, NULL} /* Sentinel */
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 */
254 aubio_methods, /* m_methods */
256 NULL, /* m_traverse */
263 aubio_log_function(int level, const char *message, void *data)
265 // remove trailing \n
267 if ((pos=strchr(message, '\n')) != NULL) {
271 if (level == AUBIO_LOG_ERR) {
272 PyErr_Format(PyExc_RuntimeError, "%s", message);
274 PyErr_WarnEx(PyExc_UserWarning, message, 1);
284 // fvec is defined in __init__.py
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)
290 || (PyType_Ready (&Py_sourceType) < 0)
291 || (PyType_Ready (&Py_sinkType) < 0)
293 || (generated_types_ready() < 0 )
298 #if PY_MAJOR_VERSION >= 3
299 m = PyModule_Create(&moduledef);
301 m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
308 err = _import_array ();
311 "Unable to import Numpy array from aubio module (error %d)\n", err);
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);
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);
324 Py_INCREF (&Py_sourceType);
325 PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
326 Py_INCREF (&Py_sinkType);
327 PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
329 PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
330 PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
332 // add generated objects
333 add_generated_objects(m);
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);
343 #if PY_MAJOR_VERSION >= 3
345 PyMODINIT_FUNC PyInit__aubio(void)
351 PyMODINIT_FUNC init_aubio(void)