source: python/ext/aubiomodule.c @ feb694b

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since feb694b was 2e4ae1d, checked in by Paul Brossier <piem@piem.org>, 9 years ago

ext/aubiomodule.c: prepare for python 3, see #33

  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[7a6521d]1#define PY_AUBIO_MODULE_MAIN
[ae6e15c]2#include "aubio-types.h"
[ad5203c]3#include "aubio-generated.h"
[913a7f1]4#include "py-musicutils.h"
[ad5203c]5
[1ba8e60]6static char aubio_module_doc[] = "Python module for the aubio library";
7
8static char Py_alpha_norm_doc[] = ""
9"alpha_norm(fvec, integer) -> float\n"
10"\n"
11"Compute alpha normalisation factor on vector, given alpha\n"
12"\n"
13"Example\n"
14"-------\n"
15"\n"
16">>> b = alpha_norm(a, 9)";
17
18static char Py_bintomidi_doc[] = ""
19"bintomidi(float, samplerate = integer, fftsize = integer) -> float\n"
20"\n"
21"Convert bin (float) to midi (float), given the sampling rate and the FFT size\n"
22"\n"
23"Example\n"
24"-------\n"
25"\n"
26">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)";
27
28static char Py_miditobin_doc[] = ""
29"miditobin(float, samplerate = integer, fftsize = integer) -> float\n"
30"\n"
31"Convert midi (float) to bin (float), given the sampling rate and the FFT size\n"
32"\n"
33"Example\n"
34"-------\n"
35"\n"
36">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)";
37
38static char Py_bintofreq_doc[] = ""
39"bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
40"\n"
41"Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
42"\n"
43"Example\n"
44"-------\n"
45"\n"
46">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
47
48static char Py_freqtobin_doc[] = ""
49"freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
50"\n"
51"Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
52"\n"
53"Example\n"
54"-------\n"
55"\n"
56">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
57
58static char Py_zero_crossing_rate_doc[] = ""
59"zero_crossing_rate(fvec) -> float\n"
60"\n"
61"Compute Zero crossing rate of a vector\n"
62"\n"
63"Example\n"
64"-------\n"
65"\n"
66">>> z = zero_crossing_rate(a)";
67
68static char Py_min_removal_doc[] = ""
69"min_removal(fvec) -> float\n"
70"\n"
71"Remove the minimum value of a vector, in-place modification\n"
72"\n"
73"Example\n"
74"-------\n"
75"\n"
76">>> min_removal(a)";
77
[ad5203c]78extern void add_generated_objects ( PyObject *m );
79extern void add_ufuncs ( PyObject *m );
[d75c900]80extern int generated_types_ready(void);
[0a6c211]81
[6b1aafc]82static PyObject *
83Py_alpha_norm (PyObject * self, PyObject * args)
84{
85  PyObject *input;
[ce4bfe3]86  fvec_t *vec;
[6b1aafc]87  smpl_t alpha;
88  PyObject *result;
89
90  if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
91    return NULL;
92  }
93
94  if (input == NULL) {
95    return NULL;
96  }
97
[ce4bfe3]98  vec = PyAubio_ArrayToCFvec (input);
[6b1aafc]99
100  if (vec == NULL) {
101    return NULL;
102  }
103
[ae6e15c]104  // compute the function
[ce4bfe3]105  result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha));
[0a6c211]106  if (result == NULL) {
107    return NULL;
108  }
109
110  return result;
111}
112
[6a50b9e]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, "|fff", &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, "|fff", &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, "|fff", &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, "|fff", &input, &samplerate, &fftsize)) {
165    return NULL;
166  }
167
168  output = aubio_freqtobin (input, samplerate, fftsize);
169
170  return (PyObject *)PyFloat_FromDouble (output);
171}
172
[207ed19]173static PyObject *
174Py_zero_crossing_rate (PyObject * self, PyObject * args)
175{
176  PyObject *input;
[ce4bfe3]177  fvec_t *vec;
[207ed19]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
[ce4bfe3]188  vec = PyAubio_ArrayToCFvec (input);
[207ed19]189
190  if (vec == NULL) {
191    return NULL;
192  }
193
194  // compute the function
[ce4bfe3]195  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
[207ed19]196  if (result == NULL) {
197    return NULL;
198  }
199
200  return result;
201}
202
[ce4bfe3]203static PyObject *
[207ed19]204Py_min_removal(PyObject * self, PyObject * args)
205{
206  PyObject *input;
[ce4bfe3]207  fvec_t *vec;
[207ed19]208
[ccf8b77]209  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
[207ed19]210    return NULL;
211  }
212
213  if (input == NULL) {
214    return NULL;
215  }
216
[ce4bfe3]217  vec = PyAubio_ArrayToCFvec (input);
[207ed19]218
219  if (vec == NULL) {
220    return NULL;
221  }
222
223  // compute the function
[ce4bfe3]224  fvec_min_removal (vec);
[615ac7d]225
[207ed19]226  // since this function does not return, we could return None
[9e6695d]227  //Py_RETURN_NONE;
[ce4bfe3]228  // however it is convenient to return the modified vector
229  return (PyObject *) PyAubio_CFvecToArray(vec);
[207ed19]230  // or even without converting it back to an array
[9b23eb31]231  //Py_INCREF(vec);
232  //return (PyObject *)vec;
[207ed19]233}
234
[0a6c211]235static PyMethodDef aubio_methods[] = {
[6a50b9e]236  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
237  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
238  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
239  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
[0a6c211]240  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
[6a50b9e]241  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
[207ed19]242  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
[5a7e2c3]243  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
[4615886a]244  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
[31a09d2]245  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
[9c8c8a6]246  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
[913a7f1]247  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
[ccf8b77]248  {NULL, NULL} /* Sentinel */
[0a6c211]249};
250
[2e4ae1d]251#if PY_MAJOR_VERSION >= 3
252// Python3 module definition
253static struct PyModuleDef moduledef = {
254   PyModuleDef_HEAD_INIT,
255   "_aubio",          /* m_name */
256   aubio_module_doc,  /* m_doc */
257   -1,                /* m_size */
258   aubio_methods,     /* m_methods */
259   NULL,              /* m_reload */
260   NULL,              /* m_traverse */
261   NULL,              /* m_clear */
262   NULL,              /* m_free */
263};
264#endif
265
266static PyObject *
267initaubio (void)
[0a6c211]268{
[2e4ae1d]269  PyObject *m = NULL;
[0a6c211]270  int err;
271
[7a6521d]272  // fvec is defined in __init__.py
[ce4bfe3]273  if (   (PyType_Ready (&Py_cvecType) < 0)
274      || (PyType_Ready (&Py_filterType) < 0)
275      || (PyType_Ready (&Py_filterbankType) < 0)
276      || (PyType_Ready (&Py_fftType) < 0)
277      || (PyType_Ready (&Py_pvocType) < 0)
[d27634d]278      || (PyType_Ready (&Py_sourceType) < 0)
[f1100a4]279      || (PyType_Ready (&Py_sinkType) < 0)
[449bff6]280      // generated objects
281      || (generated_types_ready() < 0 )
[615ac7d]282  ) {
[2e4ae1d]283    return m;
[0a6c211]284  }
285
[2e4ae1d]286#if PY_MAJOR_VERSION >= 3
287  m = PyModule_Create(&moduledef);
288#else
[1458de5]289  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
[2e4ae1d]290#endif
[1458de5]291
292  if (m == NULL) {
[2e4ae1d]293    return m;
[1458de5]294  }
295
[0a6c211]296  err = _import_array ();
297  if (err != 0) {
298    fprintf (stderr,
[ad5203c]299        "Unable to import Numpy array from aubio module (error %d)\n", err);
[0a6c211]300  }
301
[9b23eb31]302  Py_INCREF (&Py_cvecType);
303  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
304  Py_INCREF (&Py_filterType);
305  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
[615ac7d]306  Py_INCREF (&Py_filterbankType);
307  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
308  Py_INCREF (&Py_fftType);
309  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
310  Py_INCREF (&Py_pvocType);
311  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
[d27634d]312  Py_INCREF (&Py_sourceType);
313  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
[f1100a4]314  Py_INCREF (&Py_sinkType);
315  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
[449bff6]316
[7a6521d]317  // add generated objects
[449bff6]318  add_generated_objects(m);
[0d222ee]319
320  // add ufunc
321  add_ufuncs(m);
[2e4ae1d]322
323  return m;
[0a6c211]324}
[2e4ae1d]325
326#if PY_MAJOR_VERSION >= 3
327    // Python3 init
328    PyMODINIT_FUNC PyInit__aubio(void)
329    {
330        return initaubio();
331    }
332#else
333    // Python 2 init
334    PyMODINIT_FUNC init_aubio(void)
335    {
336        initaubio();
337    }
338#endif
Note: See TracBrowser for help on using the repository browser.