source: python/ext/aubiomodule.c @ 55b6260

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

[python] add docstrings to alpha_norm and zero_crossing_rate

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