source: python/ext/aubiomodule.c @ ceb884d

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

python/setup.py: add command 'generate' with option '--enable-double'

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