source: python/aubiomodule.c @ 6514bb6

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

aubiomodule.c: add unwrap2pi

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