source: python/ext/aubiomodule.c @ fc359e0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since fc359e0 was a159628, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/ext/aubiomodule.c: more hack to avoid msvc issues

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[7a6521d]1#define PY_AUBIO_MODULE_MAIN
[ae6e15c]2#include "aubio-types.h"
[913a7f1]3#include "py-musicutils.h"
[ad5203c]4
[f7e30e8]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.
[a159628]7#define REDEFINESTRING(x) #x
8#define DEFINEDSTRING(x) REDEFINESTRING(x)
[f7e30e8]9
[1ba8e60]10static char aubio_module_doc[] = "Python module for the aubio library";
11
12static char Py_alpha_norm_doc[] = ""
13"alpha_norm(fvec, integer) -> float\n"
14"\n"
15"Compute alpha normalisation factor on vector, given alpha\n"
16"\n"
17"Example\n"
18"-------\n"
19"\n"
20">>> b = alpha_norm(a, 9)";
21
22static char Py_bintomidi_doc[] = ""
23"bintomidi(float, samplerate = integer, fftsize = integer) -> float\n"
24"\n"
25"Convert bin (float) to midi (float), given the sampling rate and the FFT size\n"
26"\n"
27"Example\n"
28"-------\n"
29"\n"
30">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)";
31
32static char Py_miditobin_doc[] = ""
33"miditobin(float, samplerate = integer, fftsize = integer) -> float\n"
34"\n"
35"Convert midi (float) to bin (float), given the sampling rate and the FFT size\n"
36"\n"
37"Example\n"
38"-------\n"
39"\n"
40">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)";
41
42static char Py_bintofreq_doc[] = ""
43"bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
44"\n"
45"Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
46"\n"
47"Example\n"
48"-------\n"
49"\n"
50">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
51
52static char Py_freqtobin_doc[] = ""
53"freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
54"\n"
55"Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
56"\n"
57"Example\n"
58"-------\n"
59"\n"
60">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
61
62static char Py_zero_crossing_rate_doc[] = ""
63"zero_crossing_rate(fvec) -> float\n"
64"\n"
65"Compute Zero crossing rate of a vector\n"
66"\n"
67"Example\n"
68"-------\n"
69"\n"
70">>> z = zero_crossing_rate(a)";
71
72static char Py_min_removal_doc[] = ""
73"min_removal(fvec) -> float\n"
74"\n"
75"Remove the minimum value of a vector, in-place modification\n"
76"\n"
77"Example\n"
78"-------\n"
79"\n"
80">>> min_removal(a)";
81
[b6230d8]82extern void add_ufuncs ( PyObject *m );
83extern int generated_types_ready(void);
[0a6c211]84
[6b1aafc]85static PyObject *
86Py_alpha_norm (PyObject * self, PyObject * args)
87{
88  PyObject *input;
[569b363]89  fvec_t vec;
[6b1aafc]90  smpl_t alpha;
91  PyObject *result;
92
[c6388f4]93  if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
[6b1aafc]94    return NULL;
95  }
96
97  if (input == NULL) {
98    return NULL;
99  }
100
[569b363]101  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[6b1aafc]102    return NULL;
103  }
104
[ae6e15c]105  // compute the function
[c6388f4]106  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
[0a6c211]107  if (result == NULL) {
108    return NULL;
109  }
110
111  return result;
112}
113
[6a50b9e]114static PyObject *
115Py_bintomidi (PyObject * self, PyObject * args)
116{
117  smpl_t input, samplerate, fftsize;
118  smpl_t output;
119
[c6388f4]120  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
[6a50b9e]121    return NULL;
122  }
123
124  output = aubio_bintomidi (input, samplerate, fftsize);
125
126  return (PyObject *)PyFloat_FromDouble (output);
127}
128
129static PyObject *
130Py_miditobin (PyObject * self, PyObject * args)
131{
132  smpl_t input, samplerate, fftsize;
133  smpl_t output;
134
[c6388f4]135  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
[6a50b9e]136    return NULL;
137  }
138
139  output = aubio_miditobin (input, samplerate, fftsize);
140
141  return (PyObject *)PyFloat_FromDouble (output);
142}
143
144static PyObject *
145Py_bintofreq (PyObject * self, PyObject * args)
146{
147  smpl_t input, samplerate, fftsize;
148  smpl_t output;
149
[c6388f4]150  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
[6a50b9e]151    return NULL;
152  }
153
154  output = aubio_bintofreq (input, samplerate, fftsize);
155
156  return (PyObject *)PyFloat_FromDouble (output);
157}
158
159static PyObject *
160Py_freqtobin (PyObject * self, PyObject * args)
161{
162  smpl_t input, samplerate, fftsize;
163  smpl_t output;
164
[c6388f4]165  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
[6a50b9e]166    return NULL;
167  }
168
169  output = aubio_freqtobin (input, samplerate, fftsize);
170
171  return (PyObject *)PyFloat_FromDouble (output);
172}
173
[207ed19]174static PyObject *
175Py_zero_crossing_rate (PyObject * self, PyObject * args)
176{
177  PyObject *input;
[569b363]178  fvec_t vec;
[207ed19]179  PyObject *result;
180
181  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
182    return NULL;
183  }
184
185  if (input == NULL) {
186    return NULL;
187  }
188
[569b363]189  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[207ed19]190    return NULL;
191  }
192
193  // compute the function
[c6388f4]194  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
[207ed19]195  if (result == NULL) {
196    return NULL;
197  }
198
199  return result;
200}
201
[ce4bfe3]202static PyObject *
[207ed19]203Py_min_removal(PyObject * self, PyObject * args)
204{
205  PyObject *input;
[569b363]206  fvec_t vec;
[207ed19]207
[ccf8b77]208  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
[207ed19]209    return NULL;
210  }
211
212  if (input == NULL) {
213    return NULL;
214  }
215
[569b363]216  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[207ed19]217    return NULL;
218  }
219
220  // compute the function
[569b363]221  fvec_min_removal (&vec);
[615ac7d]222
[207ed19]223  // since this function does not return, we could return None
[9e6695d]224  //Py_RETURN_NONE;
[ce4bfe3]225  // however it is convenient to return the modified vector
[569b363]226  return (PyObject *) PyAubio_CFvecToArray(&vec);
[207ed19]227  // or even without converting it back to an array
[9b23eb31]228  //Py_INCREF(vec);
229  //return (PyObject *)vec;
[207ed19]230}
231
[0a6c211]232static PyMethodDef aubio_methods[] = {
[6a50b9e]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},
[0a6c211]237  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
[6a50b9e]238  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
[207ed19]239  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
[5a7e2c3]240  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
[4615886a]241  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
[31a09d2]242  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
[9c8c8a6]243  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
[913a7f1]244  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
[84fad5a]245  {NULL, NULL, 0, NULL} /* Sentinel */
[0a6c211]246};
247
[2e4ae1d]248#if PY_MAJOR_VERSION >= 3
249// Python3 module definition
250static struct PyModuleDef moduledef = {
251   PyModuleDef_HEAD_INIT,
252   "_aubio",          /* m_name */
253   aubio_module_doc,  /* m_doc */
254   -1,                /* m_size */
255   aubio_methods,     /* m_methods */
256   NULL,              /* m_reload */
257   NULL,              /* m_traverse */
258   NULL,              /* m_clear */
259   NULL,              /* m_free */
260};
261#endif
262
[67537d7]263void
264aubio_log_function(int level, const char *message, void *data)
265{
266  // remove trailing \n
267  char *pos;
268  if ((pos=strchr(message, '\n')) != NULL) {
269        *pos = '\0';
270  }
271  // warning or error
272  if (level == AUBIO_LOG_ERR) {
273    PyErr_Format(PyExc_RuntimeError, "%s", message);
274  } else {
275    PyErr_WarnEx(PyExc_UserWarning, message, 1);
276  }
277}
278
[2e4ae1d]279static PyObject *
280initaubio (void)
[0a6c211]281{
[2e4ae1d]282  PyObject *m = NULL;
[0a6c211]283  int err;
284
[7a6521d]285  // fvec is defined in __init__.py
[ce4bfe3]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)
[d27634d]291      || (PyType_Ready (&Py_sourceType) < 0)
[f1100a4]292      || (PyType_Ready (&Py_sinkType) < 0)
[449bff6]293      // generated objects
294      || (generated_types_ready() < 0 )
[615ac7d]295  ) {
[2e4ae1d]296    return m;
[0a6c211]297  }
298
[2e4ae1d]299#if PY_MAJOR_VERSION >= 3
300  m = PyModule_Create(&moduledef);
301#else
[1458de5]302  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
[2e4ae1d]303#endif
[1458de5]304
305  if (m == NULL) {
[2e4ae1d]306    return m;
[1458de5]307  }
308
[0a6c211]309  err = _import_array ();
310  if (err != 0) {
311    fprintf (stderr,
[ad5203c]312        "Unable to import Numpy array from aubio module (error %d)\n", err);
[0a6c211]313  }
314
[9b23eb31]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);
[615ac7d]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);
[d27634d]325  Py_INCREF (&Py_sourceType);
326  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
[f1100a4]327  Py_INCREF (&Py_sinkType);
328  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
[449bff6]329
[c6388f4]330  PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
[f7e30e8]331  PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
[c6388f4]332
[7a6521d]333  // add generated objects
[449bff6]334  add_generated_objects(m);
[0d222ee]335
336  // add ufunc
337  add_ufuncs(m);
[2e4ae1d]338
[67537d7]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);
[2e4ae1d]341  return m;
[0a6c211]342}
[2e4ae1d]343
344#if PY_MAJOR_VERSION >= 3
345    // Python3 init
346    PyMODINIT_FUNC PyInit__aubio(void)
347    {
348        return initaubio();
349    }
350#else
351    // Python 2 init
352    PyMODINIT_FUNC init_aubio(void)
353    {
354        initaubio();
355    }
356#endif
Note: See TracBrowser for help on using the repository browser.