source: python/ext/aubiomodule.c @ 57ce9b2

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

[python] add docstrings to bintomidi and miditobin

  • Property mode set to 100644
File size: 9.8 KB
Line 
1#define PY_AUBIO_MODULE_MAIN
2#include "aubio-types.h"
3#include "py-musicutils.h"
4
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)
9
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(fftbin, samplerate, fftsize)\n"
24"\n"
25"Convert FFT bin to frequency in midi note, given the sampling rate\n"
26"and the size of the FFT.\n"
27"\n"
28"Parameters\n"
29"----------\n"
30"fftbin : float\n"
31"   input frequency bin\n"
32"samplerate : float\n"
33"   sampling rate of the signal\n"
34"fftsize : float\n"
35"   size of the FFT\n"
36"\n"
37"Returns\n"
38"-------\n"
39"float\n"
40"   Frequency converted to midi note.\n"
41"\n"
42"Example\n"
43"-------\n"
44"\n"
45">>> aubio.bintomidi(10, 44100, 1024)\n"
46"68.62871551513672\n"
47"";
48
49static char Py_miditobin_doc[] = ""
50"miditobin(midi, samplerate, fftsize)\n"
51"\n"
52"Convert frequency in midi note to FFT bin, given the sampling rate\n"
53"and the size of the FFT.\n"
54"\n"
55"Parameters\n"
56"----------\n"
57"midi : float\n"
58"   input frequency, in midi note\n"
59"samplerate : float\n"
60"   sampling rate of the signal\n"
61"fftsize : float\n"
62"   size of the FFT\n"
63"\n"
64"Returns\n"
65"-------\n"
66"float\n"
67"   Frequency converted to FFT bin.\n"
68"\n"
69"Examples\n"
70"--------\n"
71"\n"
72">>> aubio.miditobin(69, 44100, 1024)\n"
73"10.216779708862305\n"
74">>> aubio.miditobin(75.08, 32000, 512)\n"
75"10.002175331115723\n"
76"";
77
78static char Py_bintofreq_doc[] = ""
79"bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
80"\n"
81"Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
82"\n"
83"Example\n"
84"-------\n"
85"\n"
86">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
87
88static char Py_freqtobin_doc[] = ""
89"freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
90"\n"
91"Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
92"\n"
93"Example\n"
94"-------\n"
95"\n"
96">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
97
98static char Py_zero_crossing_rate_doc[] = ""
99"zero_crossing_rate(fvec) -> float\n"
100"\n"
101"Compute Zero crossing rate of a vector\n"
102"\n"
103"Example\n"
104"-------\n"
105"\n"
106">>> z = zero_crossing_rate(a)";
107
108static char Py_min_removal_doc[] = ""
109"min_removal(fvec) -> float\n"
110"\n"
111"Remove the minimum value of a vector, in-place modification\n"
112"\n"
113"Example\n"
114"-------\n"
115"\n"
116">>> min_removal(a)";
117
118extern void add_ufuncs ( PyObject *m );
119extern int generated_types_ready(void);
120
121static PyObject *
122Py_alpha_norm (PyObject * self, PyObject * args)
123{
124  PyObject *input;
125  fvec_t vec;
126  smpl_t alpha;
127  PyObject *result;
128
129  if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
130    return NULL;
131  }
132
133  if (input == NULL) {
134    return NULL;
135  }
136
137  if (!PyAubio_ArrayToCFvec(input, &vec)) {
138    return NULL;
139  }
140
141  // compute the function
142  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
143  if (result == NULL) {
144    return NULL;
145  }
146
147  return result;
148}
149
150static PyObject *
151Py_bintomidi (PyObject * self, PyObject * args)
152{
153  smpl_t input, samplerate, fftsize;
154  smpl_t output;
155
156  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
157    return NULL;
158  }
159
160  output = aubio_bintomidi (input, samplerate, fftsize);
161
162  return (PyObject *)PyFloat_FromDouble (output);
163}
164
165static PyObject *
166Py_miditobin (PyObject * self, PyObject * args)
167{
168  smpl_t input, samplerate, fftsize;
169  smpl_t output;
170
171  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
172    return NULL;
173  }
174
175  output = aubio_miditobin (input, samplerate, fftsize);
176
177  return (PyObject *)PyFloat_FromDouble (output);
178}
179
180static PyObject *
181Py_bintofreq (PyObject * self, PyObject * args)
182{
183  smpl_t input, samplerate, fftsize;
184  smpl_t output;
185
186  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
187    return NULL;
188  }
189
190  output = aubio_bintofreq (input, samplerate, fftsize);
191
192  return (PyObject *)PyFloat_FromDouble (output);
193}
194
195static PyObject *
196Py_freqtobin (PyObject * self, PyObject * args)
197{
198  smpl_t input, samplerate, fftsize;
199  smpl_t output;
200
201  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
202    return NULL;
203  }
204
205  output = aubio_freqtobin (input, samplerate, fftsize);
206
207  return (PyObject *)PyFloat_FromDouble (output);
208}
209
210static PyObject *
211Py_zero_crossing_rate (PyObject * self, PyObject * args)
212{
213  PyObject *input;
214  fvec_t vec;
215  PyObject *result;
216
217  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
218    return NULL;
219  }
220
221  if (input == NULL) {
222    return NULL;
223  }
224
225  if (!PyAubio_ArrayToCFvec(input, &vec)) {
226    return NULL;
227  }
228
229  // compute the function
230  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
231  if (result == NULL) {
232    return NULL;
233  }
234
235  return result;
236}
237
238static PyObject *
239Py_min_removal(PyObject * self, PyObject * args)
240{
241  PyObject *input;
242  fvec_t vec;
243
244  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
245    return NULL;
246  }
247
248  if (input == NULL) {
249    return NULL;
250  }
251
252  if (!PyAubio_ArrayToCFvec(input, &vec)) {
253    return NULL;
254  }
255
256  // compute the function
257  fvec_min_removal (&vec);
258
259  // since this function does not return, we could return None
260  //Py_RETURN_NONE;
261  // however it is convenient to return the modified vector
262  return (PyObject *) PyAubio_CFvecToArray(&vec);
263  // or even without converting it back to an array
264  //Py_INCREF(vec);
265  //return (PyObject *)vec;
266}
267
268static PyMethodDef aubio_methods[] = {
269  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
270  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
271  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
272  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
273  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
274  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
275  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
276  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
277  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
278  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
279  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
280  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
281  {"shift", Py_aubio_shift, METH_VARARGS, Py_aubio_shift_doc},
282  {"ishift", Py_aubio_ishift, METH_VARARGS, Py_aubio_ishift_doc},
283  {NULL, NULL, 0, NULL} /* Sentinel */
284};
285
286#if PY_MAJOR_VERSION >= 3
287// Python3 module definition
288static struct PyModuleDef moduledef = {
289   PyModuleDef_HEAD_INIT,
290   "_aubio",          /* m_name */
291   aubio_module_doc,  /* m_doc */
292   -1,                /* m_size */
293   aubio_methods,     /* m_methods */
294   NULL,              /* m_reload */
295   NULL,              /* m_traverse */
296   NULL,              /* m_clear */
297   NULL,              /* m_free */
298};
299#endif
300
301void
302aubio_log_function(int level, const char *message, void *data)
303{
304  // remove trailing \n
305  char *pos;
306  if ((pos=strchr(message, '\n')) != NULL) {
307        *pos = '\0';
308  }
309  // warning or error
310  if (level == AUBIO_LOG_ERR) {
311    PyErr_Format(PyExc_RuntimeError, "%s", message);
312  } else {
313    PyErr_WarnEx(PyExc_UserWarning, message, 1);
314  }
315}
316
317static PyObject *
318initaubio (void)
319{
320  PyObject *m = NULL;
321  int err;
322
323  // fvec is defined in __init__.py
324  if (   (PyType_Ready (&Py_cvecType) < 0)
325      || (PyType_Ready (&Py_filterType) < 0)
326      || (PyType_Ready (&Py_filterbankType) < 0)
327      || (PyType_Ready (&Py_fftType) < 0)
328      || (PyType_Ready (&Py_pvocType) < 0)
329      || (PyType_Ready (&Py_sourceType) < 0)
330      || (PyType_Ready (&Py_sinkType) < 0)
331      // generated objects
332      || (generated_types_ready() < 0 )
333  ) {
334    return m;
335  }
336
337#if PY_MAJOR_VERSION >= 3
338  m = PyModule_Create(&moduledef);
339#else
340  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
341#endif
342
343  if (m == NULL) {
344    return m;
345  }
346
347  err = _import_array ();
348  if (err != 0) {
349    fprintf (stderr,
350        "Unable to import Numpy array from aubio module (error %d)\n", err);
351  }
352
353  Py_INCREF (&Py_cvecType);
354  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
355  Py_INCREF (&Py_filterType);
356  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
357  Py_INCREF (&Py_filterbankType);
358  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
359  Py_INCREF (&Py_fftType);
360  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
361  Py_INCREF (&Py_pvocType);
362  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
363  Py_INCREF (&Py_sourceType);
364  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
365  Py_INCREF (&Py_sinkType);
366  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
367
368  PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
369  PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
370
371  // add generated objects
372  add_generated_objects(m);
373
374  // add ufunc
375  add_ufuncs(m);
376
377  aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL);
378  aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL);
379  return m;
380}
381
382#if PY_MAJOR_VERSION >= 3
383    // Python3 init
384    PyMODINIT_FUNC PyInit__aubio(void)
385    {
386        return initaubio();
387    }
388#else
389    // Python 2 init
390    PyMODINIT_FUNC init_aubio(void)
391    {
392        initaubio();
393    }
394#endif
Note: See TracBrowser for help on using the repository browser.