source: python/ext/aubiomodule.c @ a599a1b

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

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

  • Property mode set to 100644
File size: 8.1 KB
Line 
1#define PY_AUBIO_MODULE_MAIN
2#include "aubio-types.h"
3#include "aubio-generated.h"
4#include "py-musicutils.h"
5
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
78extern void add_generated_objects ( PyObject *m );
79extern void add_ufuncs ( PyObject *m );
80extern int generated_types_ready(void);
81
82static PyObject *
83Py_alpha_norm (PyObject * self, PyObject * args)
84{
85  PyObject *input;
86  fvec_t *vec;
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
98  vec = PyAubio_ArrayToCFvec (input);
99
100  if (vec == NULL) {
101    return NULL;
102  }
103
104  // compute the function
105  result = Py_BuildValue ("f", 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, "|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
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  vec = PyAubio_ArrayToCFvec (input);
189
190  if (vec == NULL) {
191    return NULL;
192  }
193
194  // compute the function
195  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
196  if (result == NULL) {
197    return NULL;
198  }
199
200  return result;
201}
202
203static PyObject *
204Py_min_removal(PyObject * self, PyObject * args)
205{
206  PyObject *input;
207  fvec_t *vec;
208
209  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
210    return NULL;
211  }
212
213  if (input == NULL) {
214    return NULL;
215  }
216
217  vec = PyAubio_ArrayToCFvec (input);
218
219  if (vec == NULL) {
220    return NULL;
221  }
222
223  // compute the function
224  fvec_min_removal (vec);
225
226  // since this function does not return, we could return None
227  //Py_RETURN_NONE;
228  // however it is convenient to return the modified vector
229  return (PyObject *) PyAubio_CFvecToArray(vec);
230  // or even without converting it back to an array
231  //Py_INCREF(vec);
232  //return (PyObject *)vec;
233}
234
235static PyMethodDef aubio_methods[] = {
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},
240  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
241  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
242  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
243  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
244  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
245  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
246  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
247  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
248  {NULL, NULL} /* Sentinel */
249};
250
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)
268{
269  PyObject *m = NULL;
270  int err;
271
272  // fvec is defined in __init__.py
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)
278      || (PyType_Ready (&Py_sourceType) < 0)
279      || (PyType_Ready (&Py_sinkType) < 0)
280      // generated objects
281      || (generated_types_ready() < 0 )
282  ) {
283    return m;
284  }
285
286#if PY_MAJOR_VERSION >= 3
287  m = PyModule_Create(&moduledef);
288#else
289  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
290#endif
291
292  if (m == NULL) {
293    return m;
294  }
295
296  err = _import_array ();
297  if (err != 0) {
298    fprintf (stderr,
299        "Unable to import Numpy array from aubio module (error %d)\n", err);
300  }
301
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);
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);
312  Py_INCREF (&Py_sourceType);
313  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
314  Py_INCREF (&Py_sinkType);
315  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
316
317  // add generated objects
318  add_generated_objects(m);
319
320  // add ufunc
321  add_ufuncs(m);
322
323  return m;
324}
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.