source: python/ext/aubiomodule.c @ f7e30e8

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchsampleryinfft+
Last change on this file since f7e30e8 was f7e30e8, checked in by Paul Brossier <piem@piem.org>, 4 years ago

python/ext/aubiomodule.c: fix version string on windows

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