source: python/ext/aubiomodule.c @ 0d222ee

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

ext/ufuncs.c: add first ufunc, unwrap2pi

  • Property mode set to 100644
File size: 6.4 KB
Line 
1#define PY_AUBIO_MODULE_MAIN
2#include "aubio-types.h"
3#include "generated/aubio-generated.h"
4
5static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
6
7static PyObject *
8Py_alpha_norm (PyObject * self, PyObject * args)
9{
10  PyObject *input;
11  fvec_t *vec;
12  smpl_t alpha;
13  PyObject *result;
14
15  if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
16    return NULL;
17  }
18
19  if (input == NULL) {
20    return NULL;
21  }
22
23  vec = PyAubio_ArrayToCFvec (input);
24
25  if (vec == NULL) {
26    return NULL;
27  }
28
29  // compute the function
30  result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha));
31  if (result == NULL) {
32    return NULL;
33  }
34
35  return result;
36}
37
38static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]";
39
40static PyObject *
41Py_unwrap2pi (PyObject * self, PyObject * args)
42{
43  smpl_t input;
44  smpl_t output;
45
46  if (!PyArg_ParseTuple (args, "|f", &input)) {
47    return NULL;
48  }
49
50  output = aubio_unwrap2pi (input);
51
52  return (PyObject *)PyFloat_FromDouble (output);
53}
54
55static char Py_bintomidi_doc[] = "convert bin to midi";
56
57static PyObject *
58Py_bintomidi (PyObject * self, PyObject * args)
59{
60  smpl_t input, samplerate, fftsize;
61  smpl_t output;
62
63  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
64    return NULL;
65  }
66
67  output = aubio_bintomidi (input, samplerate, fftsize);
68
69  return (PyObject *)PyFloat_FromDouble (output);
70}
71
72static char Py_miditobin_doc[] = "convert midi to bin";
73
74static PyObject *
75Py_miditobin (PyObject * self, PyObject * args)
76{
77  smpl_t input, samplerate, fftsize;
78  smpl_t output;
79
80  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
81    return NULL;
82  }
83
84  output = aubio_miditobin (input, samplerate, fftsize);
85
86  return (PyObject *)PyFloat_FromDouble (output);
87}
88
89static char Py_bintofreq_doc[] = "convert bin to freq";
90
91static PyObject *
92Py_bintofreq (PyObject * self, PyObject * args)
93{
94  smpl_t input, samplerate, fftsize;
95  smpl_t output;
96
97  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
98    return NULL;
99  }
100
101  output = aubio_bintofreq (input, samplerate, fftsize);
102
103  return (PyObject *)PyFloat_FromDouble (output);
104}
105
106static char Py_freqtobin_doc[] = "convert freq to bin";
107
108static PyObject *
109Py_freqtobin (PyObject * self, PyObject * args)
110{
111  smpl_t input, samplerate, fftsize;
112  smpl_t output;
113
114  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
115    return NULL;
116  }
117
118  output = aubio_freqtobin (input, samplerate, fftsize);
119
120  return (PyObject *)PyFloat_FromDouble (output);
121}
122
123static char Py_freqtomidi_doc[] = "convert freq to midi";
124
125static PyObject *
126Py_freqtomidi (PyObject * self, PyObject * args)
127{
128  smpl_t input;
129  smpl_t output;
130
131  if (!PyArg_ParseTuple (args, "|f", &input)) {
132    return NULL;
133  }
134
135  output = aubio_freqtomidi (input);
136
137  return (PyObject *)PyFloat_FromDouble (output);
138}
139
140static char Py_miditofreq_doc[] = "convert midi to freq";
141
142static PyObject *
143Py_miditofreq (PyObject * self, PyObject * args)
144{
145  smpl_t input;
146  smpl_t output;
147
148  if (!PyArg_ParseTuple (args, "|f", &input)) {
149    return NULL;
150  }
151
152  output = aubio_miditofreq (input);
153
154  return (PyObject *)PyFloat_FromDouble (output);
155}
156
157static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
158
159static PyObject *
160Py_zero_crossing_rate (PyObject * self, PyObject * args)
161{
162  PyObject *input;
163  fvec_t *vec;
164  PyObject *result;
165
166  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
167    return NULL;
168  }
169
170  if (input == NULL) {
171    return NULL;
172  }
173
174  vec = PyAubio_ArrayToCFvec (input);
175
176  if (vec == NULL) {
177    return NULL;
178  }
179
180  // compute the function
181  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
182  if (result == NULL) {
183    return NULL;
184  }
185
186  return result;
187}
188
189static char Py_min_removal_doc[] = "compute zero crossing rate";
190
191static PyObject *
192Py_min_removal(PyObject * self, PyObject * args)
193{
194  PyObject *input;
195  fvec_t *vec;
196
197  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
198    return NULL;
199  }
200
201  if (input == NULL) {
202    return NULL;
203  }
204
205  vec = PyAubio_ArrayToCFvec (input);
206
207  if (vec == NULL) {
208    return NULL;
209  }
210
211  // compute the function
212  fvec_min_removal (vec);
213
214  // since this function does not return, we could return None
215  //return Py_None;
216  // however it is convenient to return the modified vector
217  return (PyObject *) PyAubio_CFvecToArray(vec);
218  // or even without converting it back to an array
219  //Py_INCREF(vec);
220  //return (PyObject *)vec;
221}
222
223static PyMethodDef aubio_methods[] = {
224  //{"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc},
225  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
226  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
227  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
228  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
229  {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc},
230  {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc},
231  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
232  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
233  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
234  {NULL, NULL} /* Sentinel */
235};
236
237static char aubio_module_doc[] = "Python module for the aubio library";
238
239PyMODINIT_FUNC
240init_aubio (void)
241{
242  PyObject *m;
243  int err;
244
245  // fvec is defined in __init__.py
246  if (   (PyType_Ready (&Py_cvecType) < 0)
247      || (PyType_Ready (&Py_filterType) < 0)
248      || (PyType_Ready (&Py_filterbankType) < 0)
249      || (PyType_Ready (&Py_fftType) < 0)
250      || (PyType_Ready (&Py_pvocType) < 0)
251      // generated objects
252      || (generated_types_ready() < 0 )
253  ) {
254    return;
255  }
256
257  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
258
259  if (m == NULL) {
260    return;
261  }
262
263  err = _import_array ();
264
265  if (err != 0) {
266    fprintf (stderr,
267        "Unable to import Numpy C API from aubio module (error %d)\n", err);
268  }
269
270  Py_INCREF (&Py_cvecType);
271  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
272  Py_INCREF (&Py_filterType);
273  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
274  Py_INCREF (&Py_filterbankType);
275  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
276  Py_INCREF (&Py_fftType);
277  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
278  Py_INCREF (&Py_pvocType);
279  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
280
281  // add generated objects
282  add_generated_objects(m);
283
284  // add ufunc
285  add_ufuncs(m);
286}
Note: See TracBrowser for help on using the repository browser.