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>, 5 years ago

[python] add docstrings to bintofreq and freqtobin

  • Property mode set to 100644
File size: 10.2 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[] = ""
[57ce9b2]23"bintomidi(fftbin, samplerate, fftsize)\n"
[1ba8e60]24"\n"
[57ce9b2]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"
[1ba8e60]41"\n"
42"Example\n"
43"-------\n"
44"\n"
[57ce9b2]45">>> aubio.bintomidi(10, 44100, 1024)\n"
46"68.62871551513672\n"
47"";
[1ba8e60]48
49static char Py_miditobin_doc[] = ""
[57ce9b2]50"miditobin(midi, samplerate, fftsize)\n"
[1ba8e60]51"\n"
[57ce9b2]52"Convert frequency in midi note to FFT bin, given the sampling rate\n"
53"and the size of the FFT.\n"
[1ba8e60]54"\n"
[57ce9b2]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"
[1ba8e60]65"-------\n"
[57ce9b2]66"float\n"
67"   Frequency converted to FFT bin.\n"
68"\n"
69"Examples\n"
70"--------\n"
[1ba8e60]71"\n"
[57ce9b2]72">>> aubio.miditobin(69, 44100, 1024)\n"
73"10.216779708862305\n"
74">>> aubio.miditobin(75.08, 32000, 512)\n"
75"10.002175331115723\n"
76"";
[1ba8e60]77
78static char Py_bintofreq_doc[] = ""
[7df8df7]79"bintofreq(fftbin, samplerate, fftsize)\n"
[1ba8e60]80"\n"
[7df8df7]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"
[1ba8e60]97"\n"
98"Example\n"
99"-------\n"
100"\n"
[7df8df7]101">>> aubio.bintofreq(10, 44100, 1024)\n"
102"430.6640625\n"
103"";
[1ba8e60]104
105static char Py_freqtobin_doc[] = ""
[7df8df7]106"freqtobin(freq, samplerate, fftsize)\n"
[1ba8e60]107"\n"
[7df8df7]108"Convert frequency in Hz to FFT bin, given the sampling rate\n"
109"and the size of the FFT.\n"
[1ba8e60]110"\n"
[7df8df7]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"
[1ba8e60]121"-------\n"
[7df8df7]122"float\n"
123"   Frequency converted to FFT bin.\n"
124"\n"
125"Examples\n"
126"--------\n"
[1ba8e60]127"\n"
[7df8df7]128">>> aubio.freqtobin(440, 44100, 1024)\n"
129"10.216779708862305\n"
130"";
[1ba8e60]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
[b6230d8]152extern void add_ufuncs ( PyObject *m );
153extern int generated_types_ready(void);
[0a6c211]154
[6b1aafc]155static PyObject *
156Py_alpha_norm (PyObject * self, PyObject * args)
157{
158  PyObject *input;
[569b363]159  fvec_t vec;
[6b1aafc]160  smpl_t alpha;
161  PyObject *result;
162
[c6388f4]163  if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) {
[6b1aafc]164    return NULL;
165  }
166
167  if (input == NULL) {
168    return NULL;
169  }
170
[569b363]171  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[6b1aafc]172    return NULL;
173  }
174
[ae6e15c]175  // compute the function
[c6388f4]176  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha));
[0a6c211]177  if (result == NULL) {
178    return NULL;
179  }
180
181  return result;
182}
183
[6a50b9e]184static PyObject *
185Py_bintomidi (PyObject * self, PyObject * args)
186{
187  smpl_t input, samplerate, fftsize;
188  smpl_t output;
189
[c6388f4]190  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
[6a50b9e]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
[c6388f4]205  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) {
[6a50b9e]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
[c6388f4]220  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
[6a50b9e]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
[c6388f4]235  if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) {
[6a50b9e]236    return NULL;
237  }
238
239  output = aubio_freqtobin (input, samplerate, fftsize);
240
241  return (PyObject *)PyFloat_FromDouble (output);
242}
243
[207ed19]244static PyObject *
245Py_zero_crossing_rate (PyObject * self, PyObject * args)
246{
247  PyObject *input;
[569b363]248  fvec_t vec;
[207ed19]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
[569b363]259  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[207ed19]260    return NULL;
261  }
262
263  // compute the function
[c6388f4]264  result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec));
[207ed19]265  if (result == NULL) {
266    return NULL;
267  }
268
269  return result;
270}
271
[ce4bfe3]272static PyObject *
[207ed19]273Py_min_removal(PyObject * self, PyObject * args)
274{
275  PyObject *input;
[569b363]276  fvec_t vec;
[207ed19]277
[ccf8b77]278  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
[207ed19]279    return NULL;
280  }
281
282  if (input == NULL) {
283    return NULL;
284  }
285
[569b363]286  if (!PyAubio_ArrayToCFvec(input, &vec)) {
[207ed19]287    return NULL;
288  }
289
290  // compute the function
[569b363]291  fvec_min_removal (&vec);
[615ac7d]292
[207ed19]293  // since this function does not return, we could return None
[9e6695d]294  //Py_RETURN_NONE;
[ce4bfe3]295  // however it is convenient to return the modified vector
[569b363]296  return (PyObject *) PyAubio_CFvecToArray(&vec);
[207ed19]297  // or even without converting it back to an array
[9b23eb31]298  //Py_INCREF(vec);
299  //return (PyObject *)vec;
[207ed19]300}
301
[0a6c211]302static PyMethodDef aubio_methods[] = {
[6a50b9e]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},
[0a6c211]307  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
[6a50b9e]308  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
[207ed19]309  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
[5a7e2c3]310  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
[4615886a]311  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
[31a09d2]312  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
[9c8c8a6]313  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
[913a7f1]314  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
[b532275]315  {"shift", Py_aubio_shift, METH_VARARGS, Py_aubio_shift_doc},
316  {"ishift", Py_aubio_ishift, METH_VARARGS, Py_aubio_ishift_doc},
[84fad5a]317  {NULL, NULL, 0, NULL} /* Sentinel */
[0a6c211]318};
319
[2e4ae1d]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
[67537d7]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
[2e4ae1d]351static PyObject *
352initaubio (void)
[0a6c211]353{
[2e4ae1d]354  PyObject *m = NULL;
[0a6c211]355  int err;
356
[7a6521d]357  // fvec is defined in __init__.py
[ce4bfe3]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)
[d27634d]363      || (PyType_Ready (&Py_sourceType) < 0)
[f1100a4]364      || (PyType_Ready (&Py_sinkType) < 0)
[449bff6]365      // generated objects
366      || (generated_types_ready() < 0 )
[615ac7d]367  ) {
[2e4ae1d]368    return m;
[0a6c211]369  }
370
[2e4ae1d]371#if PY_MAJOR_VERSION >= 3
372  m = PyModule_Create(&moduledef);
373#else
[1458de5]374  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
[2e4ae1d]375#endif
[1458de5]376
377  if (m == NULL) {
[2e4ae1d]378    return m;
[1458de5]379  }
380
[0a6c211]381  err = _import_array ();
382  if (err != 0) {
383    fprintf (stderr,
[ad5203c]384        "Unable to import Numpy array from aubio module (error %d)\n", err);
[0a6c211]385  }
386
[9b23eb31]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);
[615ac7d]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);
[d27634d]397  Py_INCREF (&Py_sourceType);
398  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
[f1100a4]399  Py_INCREF (&Py_sinkType);
400  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
[449bff6]401
[c6388f4]402  PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
[f7e30e8]403  PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));
[c6388f4]404
[7a6521d]405  // add generated objects
[449bff6]406  add_generated_objects(m);
[0d222ee]407
408  // add ufunc
409  add_ufuncs(m);
[2e4ae1d]410
[67537d7]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);
[2e4ae1d]413  return m;
[0a6c211]414}
[2e4ae1d]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.