source: python/ext/aubiomodule.c @ c2e0aef

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since c2e0aef 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
RevLine 
[7a6521d]1#define PY_AUBIO_MODULE_MAIN
[ae6e15c]2#include "aubio-types.h"
[ad5203c]3#include "aubio-generated.h"
4
5extern void add_generated_objects ( PyObject *m );
6extern void add_ufuncs ( PyObject *m );
[0a6c211]7
[6b1aafc]8static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
9
10static PyObject *
11Py_alpha_norm (PyObject * self, PyObject * args)
12{
13  PyObject *input;
[ce4bfe3]14  fvec_t *vec;
[6b1aafc]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
[ce4bfe3]26  vec = PyAubio_ArrayToCFvec (input);
[6b1aafc]27
28  if (vec == NULL) {
29    return NULL;
30  }
31
[ae6e15c]32  // compute the function
[ce4bfe3]33  result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha));
[0a6c211]34  if (result == NULL) {
35    return NULL;
36  }
37
38  return result;
39}
40
[6a50b9e]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
[207ed19]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;
[ce4bfe3]115  fvec_t *vec;
[207ed19]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
[ce4bfe3]126  vec = PyAubio_ArrayToCFvec (input);
[207ed19]127
128  if (vec == NULL) {
129    return NULL;
130  }
131
132  // compute the function
[ce4bfe3]133  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
[207ed19]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
[ce4bfe3]143static PyObject *
[207ed19]144Py_min_removal(PyObject * self, PyObject * args)
145{
146  PyObject *input;
[ce4bfe3]147  fvec_t *vec;
[207ed19]148
[ccf8b77]149  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
[207ed19]150    return NULL;
151  }
152
153  if (input == NULL) {
154    return NULL;
155  }
156
[ce4bfe3]157  vec = PyAubio_ArrayToCFvec (input);
[207ed19]158
159  if (vec == NULL) {
160    return NULL;
161  }
162
163  // compute the function
[ce4bfe3]164  fvec_min_removal (vec);
[615ac7d]165
[207ed19]166  // since this function does not return, we could return None
[9e6695d]167  //Py_RETURN_NONE;
[ce4bfe3]168  // however it is convenient to return the modified vector
169  return (PyObject *) PyAubio_CFvecToArray(vec);
[207ed19]170  // or even without converting it back to an array
[9b23eb31]171  //Py_INCREF(vec);
172  //return (PyObject *)vec;
[207ed19]173}
174
[0a6c211]175static PyMethodDef aubio_methods[] = {
[6a50b9e]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},
[0a6c211]180  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
[6a50b9e]181  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
[207ed19]182  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
[ccf8b77]183  {NULL, NULL} /* Sentinel */
[0a6c211]184};
185
[ae6e15c]186static char aubio_module_doc[] = "Python module for the aubio library";
187
[0a6c211]188PyMODINIT_FUNC
189init_aubio (void)
190{
191  PyObject *m;
192  int err;
193
[7a6521d]194  // fvec is defined in __init__.py
[ce4bfe3]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)
[449bff6]200      // generated objects
201      || (generated_types_ready() < 0 )
[615ac7d]202  ) {
[0a6c211]203    return;
204  }
205
[1458de5]206  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
207
208  if (m == NULL) {
209    return;
210  }
211
[0a6c211]212  err = _import_array ();
213  if (err != 0) {
214    fprintf (stderr,
[ad5203c]215        "Unable to import Numpy array from aubio module (error %d)\n", err);
[0a6c211]216  }
217
[9b23eb31]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);
[615ac7d]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);
[449bff6]228
[7a6521d]229  // add generated objects
[449bff6]230  add_generated_objects(m);
[0d222ee]231
232  // add ufunc
233  add_ufuncs(m);
[0a6c211]234}
Note: See TracBrowser for help on using the repository browser.