source: python/ext/aubiomodule.c @ 7df8df7

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

[python] add docstrings to bintofreq and freqtobin

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