python/: improve build
[aubio.git] / python / ext / aubiomodule.c
1 #define PY_AUBIO_MODULE_MAIN
2 #include "aubio-types.h"
3 #include "aubio-generated.h"
4
5 extern void add_generated_objects ( PyObject *m );
6 extern void add_ufuncs ( PyObject *m );
7
8 static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
9
10 static PyObject *
11 Py_alpha_norm (PyObject * self, PyObject * args)
12 {
13   PyObject *input;
14   fvec_t *vec;
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
26   vec = PyAubio_ArrayToCFvec (input);
27
28   if (vec == NULL) {
29     return NULL;
30   }
31
32   // compute the function
33   result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha));
34   if (result == NULL) {
35     return NULL;
36   }
37
38   return result;
39 }
40
41 static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]";
42
43 static PyObject *
44 Py_unwrap2pi (PyObject * self, PyObject * args)
45 {
46   smpl_t input;
47   smpl_t output;
48
49   if (!PyArg_ParseTuple (args, "|f", &input)) {
50     return NULL;
51   }
52
53   output = aubio_unwrap2pi (input);
54
55   return (PyObject *)PyFloat_FromDouble (output);
56 }
57
58 static char Py_bintomidi_doc[] = "convert bin to midi";
59
60 static PyObject *
61 Py_bintomidi (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_bintomidi (input, samplerate, fftsize);
71
72   return (PyObject *)PyFloat_FromDouble (output);
73 }
74
75 static char Py_miditobin_doc[] = "convert midi to bin";
76
77 static PyObject *
78 Py_miditobin (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_miditobin (input, samplerate, fftsize);
88
89   return (PyObject *)PyFloat_FromDouble (output);
90 }
91
92 static char Py_bintofreq_doc[] = "convert bin to freq";
93
94 static PyObject *
95 Py_bintofreq (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_bintofreq (input, samplerate, fftsize);
105
106   return (PyObject *)PyFloat_FromDouble (output);
107 }
108
109 static char Py_freqtobin_doc[] = "convert freq to bin";
110
111 static PyObject *
112 Py_freqtobin (PyObject * self, PyObject * args)
113 {
114   smpl_t input, samplerate, fftsize;
115   smpl_t output;
116
117   if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
118     return NULL;
119   }
120
121   output = aubio_freqtobin (input, samplerate, fftsize);
122
123   return (PyObject *)PyFloat_FromDouble (output);
124 }
125
126 static char Py_freqtomidi_doc[] = "convert freq to midi";
127
128 static PyObject *
129 Py_freqtomidi (PyObject * self, PyObject * args)
130 {
131   smpl_t input;
132   smpl_t output;
133
134   if (!PyArg_ParseTuple (args, "|f", &input)) {
135     return NULL;
136   }
137
138   output = aubio_freqtomidi (input);
139
140   return (PyObject *)PyFloat_FromDouble (output);
141 }
142
143 static char Py_miditofreq_doc[] = "convert midi to freq";
144
145 static PyObject *
146 Py_miditofreq (PyObject * self, PyObject * args)
147 {
148   smpl_t input;
149   smpl_t output;
150
151   if (!PyArg_ParseTuple (args, "|f", &input)) {
152     return NULL;
153   }
154
155   output = aubio_miditofreq (input);
156
157   return (PyObject *)PyFloat_FromDouble (output);
158 }
159
160 static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
161
162 static PyObject *
163 Py_zero_crossing_rate (PyObject * self, PyObject * args)
164 {
165   PyObject *input;
166   fvec_t *vec;
167   PyObject *result;
168
169   if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
170     return NULL;
171   }
172
173   if (input == NULL) {
174     return NULL;
175   }
176
177   vec = PyAubio_ArrayToCFvec (input);
178
179   if (vec == NULL) {
180     return NULL;
181   }
182
183   // compute the function
184   result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
185   if (result == NULL) {
186     return NULL;
187   }
188
189   return result;
190 }
191
192 static char Py_min_removal_doc[] = "compute zero crossing rate";
193
194 static PyObject *
195 Py_min_removal(PyObject * self, PyObject * args)
196 {
197   PyObject *input;
198   fvec_t *vec;
199
200   if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
201     return NULL;
202   }
203
204   if (input == NULL) {
205     return NULL;
206   }
207
208   vec = PyAubio_ArrayToCFvec (input);
209
210   if (vec == NULL) {
211     return NULL;
212   }
213
214   // compute the function
215   fvec_min_removal (vec);
216
217   // since this function does not return, we could return None
218   //return Py_None;
219   // however it is convenient to return the modified vector
220   return (PyObject *) PyAubio_CFvecToArray(vec);
221   // or even without converting it back to an array
222   //Py_INCREF(vec);
223   //return (PyObject *)vec;
224 }
225
226 static PyMethodDef aubio_methods[] = {
227   //{"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc},
228   {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
229   {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
230   {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
231   {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
232   {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc},
233   {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc},
234   {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
235   {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
236   {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
237   {NULL, NULL} /* Sentinel */
238 };
239
240 static char aubio_module_doc[] = "Python module for the aubio library";
241
242 PyMODINIT_FUNC
243 init_aubio (void)
244 {
245   PyObject *m;
246   int err;
247
248   // fvec is defined in __init__.py
249   if (   (PyType_Ready (&Py_cvecType) < 0)
250       || (PyType_Ready (&Py_filterType) < 0)
251       || (PyType_Ready (&Py_filterbankType) < 0)
252       || (PyType_Ready (&Py_fftType) < 0)
253       || (PyType_Ready (&Py_pvocType) < 0)
254       // generated objects
255       || (generated_types_ready() < 0 )
256   ) {
257     return;
258   }
259
260   m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
261
262   if (m == NULL) {
263     return;
264   }
265
266   err = _import_array ();
267   if (err != 0) {
268     fprintf (stderr,
269         "Unable to import Numpy array from aubio module (error %d)\n", err);
270   }
271
272   Py_INCREF (&Py_cvecType);
273   PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
274   Py_INCREF (&Py_filterType);
275   PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
276   Py_INCREF (&Py_filterbankType);
277   PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
278   Py_INCREF (&Py_fftType);
279   PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
280   Py_INCREF (&Py_pvocType);
281   PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
282
283   // add generated objects
284   add_generated_objects(m);
285
286   // add ufunc
287   add_ufuncs(m);
288 }