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_generated_objects ( PyObject *m );
78 extern void add_ufuncs ( PyObject *m );
79 extern int generated_types_ready(void);
82 Py_alpha_norm (PyObject * self, PyObject * args)
89 if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
97 if (!PyAubio_ArrayToCFvec(input, &vec)) {
101 // compute the function
102 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
103 if (result == NULL) {
111 Py_bintomidi (PyObject * self, PyObject * args)
113 smpl_t input, samplerate, fftsize;
116 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
120 output = aubio_bintomidi (input, samplerate, fftsize);
122 return (PyObject *)PyFloat_FromDouble (output);
126 Py_miditobin (PyObject * self, PyObject * args)
128 smpl_t input, samplerate, fftsize;
131 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
135 output = aubio_miditobin (input, samplerate, fftsize);
137 return (PyObject *)PyFloat_FromDouble (output);
141 Py_bintofreq (PyObject * self, PyObject * args)
143 smpl_t input, samplerate, fftsize;
146 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
150 output = aubio_bintofreq (input, samplerate, fftsize);
152 return (PyObject *)PyFloat_FromDouble (output);
156 Py_freqtobin (PyObject * self, PyObject * args)
158 smpl_t input, samplerate, fftsize;
161 if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
165 output = aubio_freqtobin (input, samplerate, fftsize);
167 return (PyObject *)PyFloat_FromDouble (output);
171 Py_zero_crossing_rate (PyObject * self, PyObject * args)
177 if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
185 if (!PyAubio_ArrayToCFvec(input, &vec)) {
189 // compute the function
190 result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
191 if (result == NULL) {
199 Py_min_removal(PyObject * self, PyObject * args)
204 if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
212 if (!PyAubio_ArrayToCFvec(input, &vec)) {
216 // compute the function
217 fvec_min_removal (&vec);
219 // since this function does not return, we could return None
221 // however it is convenient to return the modified vector
222 return (PyObject *) PyAubio_CFvecToArray(&vec);
223 // or even without converting it back to an array
225 //return (PyObject *)vec;
228 static PyMethodDef aubio_methods[] = {
229 {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
230 {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
231 {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
232 {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
233 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
234 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
235 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
236 {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
237 {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
238 {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
239 {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
240 {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
241 {NULL, NULL, 0, NULL} /* Sentinel */
244 #if PY_MAJOR_VERSION >= 3
245 // Python3 module definition
246 static struct PyModuleDef moduledef = {
247 PyModuleDef_HEAD_INIT,
248 "_aubio", /* m_name */
249 aubio_module_doc, /* m_doc */
251 aubio_methods, /* m_methods */
253 NULL, /* m_traverse */
265 // fvec is defined in __init__.py
266 if ( (PyType_Ready (&Py_cvecType) < 0)
267 || (PyType_Ready (&Py_filterType) < 0)
268 || (PyType_Ready (&Py_filterbankType) < 0)
269 || (PyType_Ready (&Py_fftType) < 0)
270 || (PyType_Ready (&Py_pvocType) < 0)
271 || (PyType_Ready (&Py_sourceType) < 0)
272 || (PyType_Ready (&Py_sinkType) < 0)
274 || (generated_types_ready() < 0 )
279 #if PY_MAJOR_VERSION >= 3
280 m = PyModule_Create(&moduledef);
282 m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
289 err = _import_array ();
292 "Unable to import Numpy array from aubio module (error %d)\n", err);
295 Py_INCREF (&Py_cvecType);
296 PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
297 Py_INCREF (&Py_filterType);
298 PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
299 Py_INCREF (&Py_filterbankType);
300 PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
301 Py_INCREF (&Py_fftType);
302 PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
303 Py_INCREF (&Py_pvocType);
304 PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
305 Py_INCREF (&Py_sourceType);
306 PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
307 Py_INCREF (&Py_sinkType);
308 PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
310 PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
312 // add generated objects
313 add_generated_objects(m);
321 #if PY_MAJOR_VERSION >= 3
323 PyMODINIT_FUNC PyInit__aubio(void)
329 PyMODINIT_FUNC init_aubio(void)