source: python/aubiomodule.c @ 6a50b9e

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

python/aubiomodule.c: add midi/bin/freq conversion

  • Property mode set to 100644
File size: 6.1 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_bintomidi_doc[] = "convert bin to midi";
43
44static PyObject *
45Py_bintomidi (PyObject * self, PyObject * args)
46{
47  smpl_t input, samplerate, fftsize;
48  smpl_t output;
49
50  if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
51    return NULL;
52  }
53
54  output = aubio_bintomidi (input, samplerate, fftsize);
55
56  return (PyObject *)PyFloat_FromDouble (output);
57}
58
59static char Py_miditobin_doc[] = "convert midi to bin";
60
61static PyObject *
62Py_miditobin (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_miditobin (input, samplerate, fftsize);
72
73  return (PyObject *)PyFloat_FromDouble (output);
74}
75
76static char Py_bintofreq_doc[] = "convert bin to freq";
77
78static PyObject *
79Py_bintofreq (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_bintofreq (input, samplerate, fftsize);
89
90  return (PyObject *)PyFloat_FromDouble (output);
91}
92
93static char Py_freqtobin_doc[] = "convert freq to bin";
94
95static PyObject *
96Py_freqtobin (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_freqtobin (input, samplerate, fftsize);
106
107  return (PyObject *)PyFloat_FromDouble (output);
108}
109
110static char Py_freqtomidi_doc[] = "convert freq to midi";
111
112static PyObject *
113Py_freqtomidi (PyObject * self, PyObject * args)
114{
115  smpl_t input;
116  smpl_t output;
117
118  if (!PyArg_ParseTuple (args, "|f", &input)) {
119    return NULL;
120  }
121
122  output = aubio_freqtomidi (input);
123
124  return (PyObject *)PyFloat_FromDouble (output);
125}
126
127static char Py_miditofreq_doc[] = "convert midi to freq";
128
129static PyObject *
130Py_miditofreq (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_miditofreq (input);
140
141  return (PyObject *)PyFloat_FromDouble (output);
142}
143
144static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
145
146static PyObject *
147Py_zero_crossing_rate (PyObject * self, PyObject * args)
148{
149  PyObject *input;
150  fvec_t *vec;
151  PyObject *result;
152
153  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
154    return NULL;
155  }
156
157  if (input == NULL) {
158    return NULL;
159  }
160
161  vec = PyAubio_ArrayToCFvec (input);
162
163  if (vec == NULL) {
164    return NULL;
165  }
166
167  // compute the function
168  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
169  if (result == NULL) {
170    return NULL;
171  }
172
173  return result;
174}
175
176static char Py_min_removal_doc[] = "compute zero crossing rate";
177
178static PyObject *
179Py_min_removal(PyObject * self, PyObject * args)
180{
181  PyObject *input;
182  fvec_t *vec;
183
184  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
185    return NULL;
186  }
187
188  if (input == NULL) {
189    return NULL;
190  }
191
192  vec = PyAubio_ArrayToCFvec (input);
193
194  if (vec == NULL) {
195    return NULL;
196  }
197
198  // compute the function
199  fvec_min_removal (vec);
200
201  // since this function does not return, we could return None
202  //return Py_None;
203  // however it is convenient to return the modified vector
204  return (PyObject *) PyAubio_CFvecToArray(vec);
205  // or even without converting it back to an array
206  //Py_INCREF(vec);
207  //return (PyObject *)vec;
208}
209
210static PyMethodDef aubio_methods[] = {
211  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
212  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
213  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
214  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
215  {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc},
216  {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc},
217  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
218  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
219  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
220  {NULL, NULL} /* Sentinel */
221};
222
223static char aubio_module_doc[] = "Python module for the aubio library";
224
225PyMODINIT_FUNC
226init_aubio (void)
227{
228  PyObject *m;
229  int err;
230
231  if (   (PyType_Ready (&Py_cvecType) < 0)
232      || (PyType_Ready (&Py_filterType) < 0)
233      || (PyType_Ready (&Py_filterbankType) < 0)
234      || (PyType_Ready (&Py_fftType) < 0)
235      || (PyType_Ready (&Py_pvocType) < 0)
236      // generated objects
237      || (generated_types_ready() < 0 )
238  ) {
239    return;
240  }
241
242  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
243
244  if (m == NULL) {
245    return;
246  }
247
248  err = _import_array ();
249
250  if (err != 0) {
251    fprintf (stderr,
252        "Unable to import Numpy C API from aubio module (error %d)\n", err);
253  }
254
255  Py_INCREF (&Py_cvecType);
256  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
257  Py_INCREF (&Py_filterType);
258  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
259  Py_INCREF (&Py_filterbankType);
260  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
261  Py_INCREF (&Py_fftType);
262  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
263  Py_INCREF (&Py_pvocType);
264  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
265
266  // generated objects
267  add_generated_objects(m);
268}
Note: See TracBrowser for help on using the repository browser.