source: python/ext/aubiomodule.c @ dcde285

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampleryinfft+
Last change on this file since dcde285 was 333a5bb, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/ext/aubiomodule.c: remove duplicate add_generated_objects

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