1 | #define PY_AUBIO_MODULE_MAIN |
---|
2 | #include "aubio-types.h" |
---|
3 | #include "aubio-generated.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, "Of:alpha_norm", &input, &alpha)) { |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | |
---|
93 | if (input == NULL) { |
---|
94 | return NULL; |
---|
95 | } |
---|
96 | |
---|
97 | vec = PyAubio_ArrayToCFvec (input); |
---|
98 | |
---|
99 | if (vec == NULL) { |
---|
100 | return NULL; |
---|
101 | } |
---|
102 | |
---|
103 | // compute the function |
---|
104 | result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); |
---|
105 | if (result == NULL) { |
---|
106 | return NULL; |
---|
107 | } |
---|
108 | |
---|
109 | return result; |
---|
110 | } |
---|
111 | |
---|
112 | static PyObject * |
---|
113 | Py_bintomidi (PyObject * self, PyObject * args) |
---|
114 | { |
---|
115 | smpl_t input, samplerate, fftsize; |
---|
116 | smpl_t output; |
---|
117 | |
---|
118 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
119 | return NULL; |
---|
120 | } |
---|
121 | |
---|
122 | output = aubio_bintomidi (input, samplerate, fftsize); |
---|
123 | |
---|
124 | return (PyObject *)PyFloat_FromDouble (output); |
---|
125 | } |
---|
126 | |
---|
127 | static PyObject * |
---|
128 | Py_miditobin (PyObject * self, PyObject * args) |
---|
129 | { |
---|
130 | smpl_t input, samplerate, fftsize; |
---|
131 | smpl_t output; |
---|
132 | |
---|
133 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
134 | return NULL; |
---|
135 | } |
---|
136 | |
---|
137 | output = aubio_miditobin (input, samplerate, fftsize); |
---|
138 | |
---|
139 | return (PyObject *)PyFloat_FromDouble (output); |
---|
140 | } |
---|
141 | |
---|
142 | static PyObject * |
---|
143 | Py_bintofreq (PyObject * self, PyObject * args) |
---|
144 | { |
---|
145 | smpl_t input, samplerate, fftsize; |
---|
146 | smpl_t output; |
---|
147 | |
---|
148 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
149 | return NULL; |
---|
150 | } |
---|
151 | |
---|
152 | output = aubio_bintofreq (input, samplerate, fftsize); |
---|
153 | |
---|
154 | return (PyObject *)PyFloat_FromDouble (output); |
---|
155 | } |
---|
156 | |
---|
157 | static PyObject * |
---|
158 | Py_freqtobin (PyObject * self, PyObject * args) |
---|
159 | { |
---|
160 | smpl_t input, samplerate, fftsize; |
---|
161 | smpl_t output; |
---|
162 | |
---|
163 | if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { |
---|
164 | return NULL; |
---|
165 | } |
---|
166 | |
---|
167 | output = aubio_freqtobin (input, samplerate, fftsize); |
---|
168 | |
---|
169 | return (PyObject *)PyFloat_FromDouble (output); |
---|
170 | } |
---|
171 | |
---|
172 | static PyObject * |
---|
173 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
174 | { |
---|
175 | PyObject *input; |
---|
176 | fvec_t *vec; |
---|
177 | PyObject *result; |
---|
178 | |
---|
179 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
180 | return NULL; |
---|
181 | } |
---|
182 | |
---|
183 | if (input == NULL) { |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | |
---|
187 | vec = PyAubio_ArrayToCFvec (input); |
---|
188 | |
---|
189 | if (vec == NULL) { |
---|
190 | return NULL; |
---|
191 | } |
---|
192 | |
---|
193 | // compute the function |
---|
194 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); |
---|
195 | if (result == NULL) { |
---|
196 | return NULL; |
---|
197 | } |
---|
198 | |
---|
199 | return result; |
---|
200 | } |
---|
201 | |
---|
202 | static PyObject * |
---|
203 | Py_min_removal(PyObject * self, PyObject * args) |
---|
204 | { |
---|
205 | PyObject *input; |
---|
206 | fvec_t *vec; |
---|
207 | |
---|
208 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
209 | return NULL; |
---|
210 | } |
---|
211 | |
---|
212 | if (input == NULL) { |
---|
213 | return NULL; |
---|
214 | } |
---|
215 | |
---|
216 | vec = PyAubio_ArrayToCFvec (input); |
---|
217 | |
---|
218 | if (vec == NULL) { |
---|
219 | return NULL; |
---|
220 | } |
---|
221 | |
---|
222 | // compute the function |
---|
223 | fvec_min_removal (vec); |
---|
224 | |
---|
225 | // since this function does not return, we could return None |
---|
226 | //Py_RETURN_NONE; |
---|
227 | // however it is convenient to return the modified vector |
---|
228 | return (PyObject *) PyAubio_CFvecToArray(vec); |
---|
229 | // or even without converting it back to an array |
---|
230 | //Py_INCREF(vec); |
---|
231 | //return (PyObject *)vec; |
---|
232 | } |
---|
233 | |
---|
234 | static PyMethodDef aubio_methods[] = { |
---|
235 | {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, |
---|
236 | {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, |
---|
237 | {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, |
---|
238 | {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, |
---|
239 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
240 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
---|
241 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
242 | {NULL, NULL} /* Sentinel */ |
---|
243 | }; |
---|
244 | |
---|
245 | PyMODINIT_FUNC |
---|
246 | init_aubio (void) |
---|
247 | { |
---|
248 | PyObject *m; |
---|
249 | int err; |
---|
250 | |
---|
251 | // fvec is defined in __init__.py |
---|
252 | if ( (PyType_Ready (&Py_cvecType) < 0) |
---|
253 | || (PyType_Ready (&Py_filterType) < 0) |
---|
254 | || (PyType_Ready (&Py_filterbankType) < 0) |
---|
255 | || (PyType_Ready (&Py_fftType) < 0) |
---|
256 | || (PyType_Ready (&Py_pvocType) < 0) |
---|
257 | || (PyType_Ready (&Py_sourceType) < 0) |
---|
258 | || (PyType_Ready (&Py_sinkType) < 0) |
---|
259 | // generated objects |
---|
260 | || (generated_types_ready() < 0 ) |
---|
261 | ) { |
---|
262 | return; |
---|
263 | } |
---|
264 | |
---|
265 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
266 | |
---|
267 | if (m == NULL) { |
---|
268 | return; |
---|
269 | } |
---|
270 | |
---|
271 | err = _import_array (); |
---|
272 | if (err != 0) { |
---|
273 | fprintf (stderr, |
---|
274 | "Unable to import Numpy array from aubio module (error %d)\n", err); |
---|
275 | } |
---|
276 | |
---|
277 | Py_INCREF (&Py_cvecType); |
---|
278 | PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); |
---|
279 | Py_INCREF (&Py_filterType); |
---|
280 | PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); |
---|
281 | Py_INCREF (&Py_filterbankType); |
---|
282 | PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); |
---|
283 | Py_INCREF (&Py_fftType); |
---|
284 | PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); |
---|
285 | Py_INCREF (&Py_pvocType); |
---|
286 | PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); |
---|
287 | Py_INCREF (&Py_sourceType); |
---|
288 | PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType); |
---|
289 | Py_INCREF (&Py_sinkType); |
---|
290 | PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType); |
---|
291 | |
---|
292 | // add generated objects |
---|
293 | add_generated_objects(m); |
---|
294 | |
---|
295 | // add ufunc |
---|
296 | add_ufuncs(m); |
---|
297 | } |
---|