source: python/ext/aubiomodule.c @ 01e7be0

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

python/: use Py_RETURN_NONE, fixing a memory bug triggered after opening many sinks

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