source: python/ext/aubiomodule.c @ 72d08ae

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

[py] wrap long lines in aubiomodule.c

  • Property mode set to 100644
File size: 9.3 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
[72d08ae]120  if (!PyArg_ParseTuple (args,
121        "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
122        &input, &samplerate, &fftsize)) {
[6a50b9e]123    return NULL;
124  }
125
126  output = aubio_bintomidi (input, samplerate, fftsize);
127
128  return (PyObject *)PyFloat_FromDouble (output);
129}
130
131static PyObject *
132Py_miditobin (PyObject * self, PyObject * args)
133{
134  smpl_t input, samplerate, fftsize;
135  smpl_t output;
136
[72d08ae]137  if (!PyArg_ParseTuple (args,
138        "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
139        &input, &samplerate, &fftsize)) {
[6a50b9e]140    return NULL;
141  }
142
143  output = aubio_miditobin (input, samplerate, fftsize);
144
145  return (PyObject *)PyFloat_FromDouble (output);
146}
147
148static PyObject *
149Py_bintofreq (PyObject * self, PyObject * args)
150{
151  smpl_t input, samplerate, fftsize;
152  smpl_t output;
153
[72d08ae]154  if (!PyArg_ParseTuple (args,
155        "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
156        &input, &samplerate, &fftsize)) {
[6a50b9e]157    return NULL;
158  }
159
160  output = aubio_bintofreq (input, samplerate, fftsize);
161
162  return (PyObject *)PyFloat_FromDouble (output);
163}
164
165static PyObject *
166Py_freqtobin (PyObject * self, PyObject * args)
167{
168  smpl_t input, samplerate, fftsize;
169  smpl_t output;
170
[72d08ae]171  if (!PyArg_ParseTuple (args,
172        "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
173        &input, &samplerate, &fftsize)) {
[6a50b9e]174    return NULL;
175  }
176
177  output = aubio_freqtobin (input, samplerate, fftsize);
178
179  return (PyObject *)PyFloat_FromDouble (output);
180}
181
[207ed19]182static PyObject *
183Py_zero_crossing_rate (PyObject * self, PyObject * args)
184{
185  PyObject *input;
[569b363]186  fvec_t vec;
[207ed19]187  PyObject *result;
188
189  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
190    return NULL;
191  }
192
193  if (input == NULL) {
194    return NULL;
195  }
196
[569b363]197  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[207ed19]198    return NULL;
199  }
200
201  // compute the function
[c6388f4]202  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
[207ed19]203  if (result == NULL) {
204    return NULL;
205  }
206
207  return result;
208}
209
[ce4bfe3]210static PyObject *
[207ed19]211Py_min_removal(PyObject * self, PyObject * args)
212{
213  PyObject *input;
[569b363]214  fvec_t vec;
[207ed19]215
[ccf8b77]216  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
[207ed19]217    return NULL;
218  }
219
220  if (input == NULL) {
221    return NULL;
222  }
223
[569b363]224  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[207ed19]225    return NULL;
226  }
227
228  // compute the function
[569b363]229  fvec_min_removal (&vec);
[615ac7d]230
[207ed19]231  // since this function does not return, we could return None
[9e6695d]232  //Py_RETURN_NONE;
[ce4bfe3]233  // however it is convenient to return the modified vector
[569b363]234  return (PyObject *) PyAubio_CFvecToArray(&vec);
[207ed19]235  // or even without converting it back to an array
[9b23eb31]236  //Py_INCREF(vec);
237  //return (PyObject *)vec;
[207ed19]238}
239
[0a6c211]240static PyMethodDef aubio_methods[] = {
[6a50b9e]241  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
242  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
243  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
244  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
[0a6c211]245  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
[6a50b9e]246  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
[207ed19]247  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
[5a7e2c3]248  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
[4615886a]249  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
[31a09d2]250  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
[9c8c8a6]251  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
[913a7f1]252  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
[b532275]253  {"shift", Py_aubio_shift, METH_VARARGS, Py_aubio_shift_doc},
254  {"ishift", Py_aubio_ishift, METH_VARARGS, Py_aubio_ishift_doc},
[84fad5a]255  {NULL, NULL, 0, NULL} /* Sentinel */
[0a6c211]256};
257
[2e4ae1d]258#if PY_MAJOR_VERSION >= 3
259// Python3 module definition
260static struct PyModuleDef moduledef = {
261   PyModuleDef_HEAD_INIT,
262   "_aubio",          /* m_name */
263   aubio_module_doc,  /* m_doc */
264   -1,                /* m_size */
265   aubio_methods,     /* m_methods */
266   NULL,              /* m_reload */
267   NULL,              /* m_traverse */
268   NULL,              /* m_clear */
269   NULL,              /* m_free */
270};
271#endif
272
[67537d7]273void
274aubio_log_function(int level, const char *message, void *data)
275{
276  // remove trailing \n
277  char *pos;
278  if ((pos=strchr(message, '\n')) != NULL) {
279        *pos = '\0';
280  }
281  // warning or error
282  if (level == AUBIO_LOG_ERR) {
283    PyErr_Format(PyExc_RuntimeError, "%s", message);
284  } else {
285    PyErr_WarnEx(PyExc_UserWarning, message, 1);
286  }
287}
288
[2e4ae1d]289static PyObject *
290initaubio (void)
[0a6c211]291{
[2e4ae1d]292  PyObject *m = NULL;
[0a6c211]293  int err;
294
[7a6521d]295  // fvec is defined in __init__.py
[ce4bfe3]296  if (   (PyType_Ready (&Py_cvecType) < 0)
297      || (PyType_Ready (&Py_filterType) < 0)
298      || (PyType_Ready (&Py_filterbankType) < 0)
299      || (PyType_Ready (&Py_fftType) < 0)
300      || (PyType_Ready (&Py_pvocType) < 0)
[d27634d]301      || (PyType_Ready (&Py_sourceType) < 0)
[f1100a4]302      || (PyType_Ready (&Py_sinkType) < 0)
[449bff6]303      // generated objects
304      || (generated_types_ready() < 0 )
[615ac7d]305  ) {
[2e4ae1d]306    return m;
[0a6c211]307  }
308
[2e4ae1d]309#if PY_MAJOR_VERSION >= 3
310  m = PyModule_Create(&moduledef);
311#else
[1458de5]312  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
[2e4ae1d]313#endif
[1458de5]314
315  if (m == NULL) {
[2e4ae1d]316    return m;
[1458de5]317  }
318
[0a6c211]319  err = _import_array ();
320  if (err != 0) {
321    fprintf (stderr,
[ad5203c]322        "Unable to import Numpy array from aubio module (error %d)\n", err);
[0a6c211]323  }
324
[9b23eb31]325  Py_INCREF (&Py_cvecType);
326  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
327  Py_INCREF (&Py_filterType);
328  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
[615ac7d]329  Py_INCREF (&Py_filterbankType);
330  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
331  Py_INCREF (&Py_fftType);
332  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
333  Py_INCREF (&Py_pvocType);
334  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
[d27634d]335  Py_INCREF (&Py_sourceType);
336  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
[f1100a4]337  Py_INCREF (&Py_sinkType);
338  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
[449bff6]339
[c6388f4]340  PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
[f7e30e8]341  PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
[c6388f4]342
[7a6521d]343  // add generated objects
[449bff6]344  add_generated_objects(m);
[0d222ee]345
346  // add ufunc
347  add_ufuncs(m);
[2e4ae1d]348
[67537d7]349  aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL);
350  aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL);
[2e4ae1d]351  return m;
[0a6c211]352}
[2e4ae1d]353
354#if PY_MAJOR_VERSION >= 3
355    // Python3 init
356    PyMODINIT_FUNC PyInit__aubio(void)
357    {
358        return initaubio();
359    }
360#else
361    // Python 2 init
362    PyMODINIT_FUNC init_aubio(void)
363    {
364        initaubio();
365    }
366#endif
Note: See TracBrowser for help on using the repository browser.