source: python/ext/aubiomodule.c @ aad1235

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

ext/: use new proxy functions

  • Property mode set to 100644
File size: 8.3 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 = (fvec_t *)malloc(sizeof(fvec_t));
99  if (!PyAubio_ArrayToCFvec(input, vec)) {
100    free(vec);
101    return NULL;
102  }
103
104  // compute the function
105  result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha));
106  free(vec);
107  if (result == NULL) {
108    return NULL;
109  }
110
111  return result;
112}
113
114static PyObject *
115Py_bintomidi (PyObject * self, PyObject * args)
116{
117  smpl_t input, samplerate, fftsize;
118  smpl_t output;
119
120  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
121    return NULL;
122  }
123
124  output = aubio_bintomidi (input, samplerate, fftsize);
125
126  return (PyObject *)PyFloat_FromDouble (output);
127}
128
129static PyObject *
130Py_miditobin (PyObject * self, PyObject * args)
131{
132  smpl_t input, samplerate, fftsize;
133  smpl_t output;
134
135  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
136    return NULL;
137  }
138
139  output = aubio_miditobin (input, samplerate, fftsize);
140
141  return (PyObject *)PyFloat_FromDouble (output);
142}
143
144static PyObject *
145Py_bintofreq (PyObject * self, PyObject * args)
146{
147  smpl_t input, samplerate, fftsize;
148  smpl_t output;
149
150  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
151    return NULL;
152  }
153
154  output = aubio_bintofreq (input, samplerate, fftsize);
155
156  return (PyObject *)PyFloat_FromDouble (output);
157}
158
159static PyObject *
160Py_freqtobin (PyObject * self, PyObject * args)
161{
162  smpl_t input, samplerate, fftsize;
163  smpl_t output;
164
165  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
166    return NULL;
167  }
168
169  output = aubio_freqtobin (input, samplerate, fftsize);
170
171  return (PyObject *)PyFloat_FromDouble (output);
172}
173
174static PyObject *
175Py_zero_crossing_rate (PyObject * self, PyObject * args)
176{
177  PyObject *input;
178  fvec_t *vec;
179  PyObject *result;
180
181  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
182    return NULL;
183  }
184
185  if (input == NULL) {
186    return NULL;
187  }
188
189  vec = (fvec_t *)malloc(sizeof(fvec_t));
190  if (!PyAubio_ArrayToCFvec(input, vec)) {
191    free(vec);
192    return NULL;
193  }
194
195  // compute the function
196  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
197  free(vec);
198  if (result == NULL) {
199    return NULL;
200  }
201
202  return result;
203}
204
205static PyObject *
206Py_min_removal(PyObject * self, PyObject * args)
207{
208  PyObject *input;
209  fvec_t *vec;
210
211  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
212    return NULL;
213  }
214
215  if (input == NULL) {
216    return NULL;
217  }
218
219  vec = (fvec_t *)malloc(sizeof(fvec_t));
220  if (!PyAubio_ArrayToCFvec(input, vec)) {
221    free(vec);
222    return NULL;
223  }
224
225  // compute the function
226  fvec_min_removal (vec);
227
228  // since this function does not return, we could return None
229  //Py_RETURN_NONE;
230  // however it is convenient to return the modified vector
231  return (PyObject *) PyAubio_CFvecToArray(vec);
232  // or even without converting it back to an array
233  //Py_INCREF(vec);
234  //return (PyObject *)vec;
235}
236
237static PyMethodDef aubio_methods[] = {
238  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
239  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
240  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
241  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
242  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
243  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
244  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
245  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
246  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
247  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
248  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
249  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
250  {NULL, NULL} /* Sentinel */
251};
252
253#if PY_MAJOR_VERSION >= 3
254// Python3 module definition
255static struct PyModuleDef moduledef = {
256   PyModuleDef_HEAD_INIT,
257   "_aubio",          /* m_name */
258   aubio_module_doc,  /* m_doc */
259   -1,                /* m_size */
260   aubio_methods,     /* m_methods */
261   NULL,              /* m_reload */
262   NULL,              /* m_traverse */
263   NULL,              /* m_clear */
264   NULL,              /* m_free */
265};
266#endif
267
268static PyObject *
269initaubio (void)
270{
271  PyObject *m = NULL;
272  int err;
273
274  // fvec is defined in __init__.py
275  if (   (PyType_Ready (&Py_cvecType) < 0)
276      || (PyType_Ready (&Py_filterType) < 0)
277      || (PyType_Ready (&Py_filterbankType) < 0)
278      || (PyType_Ready (&Py_fftType) < 0)
279      || (PyType_Ready (&Py_pvocType) < 0)
280      || (PyType_Ready (&Py_sourceType) < 0)
281      || (PyType_Ready (&Py_sinkType) < 0)
282      // generated objects
283      || (generated_types_ready() < 0 )
284  ) {
285    return m;
286  }
287
288#if PY_MAJOR_VERSION >= 3
289  m = PyModule_Create(&moduledef);
290#else
291  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
292#endif
293
294  if (m == NULL) {
295    return m;
296  }
297
298  err = _import_array ();
299  if (err != 0) {
300    fprintf (stderr,
301        "Unable to import Numpy array from aubio module (error %d)\n", err);
302  }
303
304  Py_INCREF (&Py_cvecType);
305  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
306  Py_INCREF (&Py_filterType);
307  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
308  Py_INCREF (&Py_filterbankType);
309  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
310  Py_INCREF (&Py_fftType);
311  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
312  Py_INCREF (&Py_pvocType);
313  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
314  Py_INCREF (&Py_sourceType);
315  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
316  Py_INCREF (&Py_sinkType);
317  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
318
319  // add generated objects
320  add_generated_objects(m);
321
322  // add ufunc
323  add_ufuncs(m);
324
325  return m;
326}
327
328#if PY_MAJOR_VERSION >= 3
329    // Python3 init
330    PyMODINIT_FUNC PyInit__aubio(void)
331    {
332        return initaubio();
333    }
334#else
335    // Python 2 init
336    PyMODINIT_FUNC init_aubio(void)
337    {
338        initaubio();
339    }
340#endif
Note: See TracBrowser for help on using the repository browser.