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