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