python/ext/py-source.c: not generated, modified init to get samplerate from aubio_sou...
[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 extern int generated_types_ready(void);
8
9 static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
10
11 static PyObject *
12 Py_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
42 static char Py_bintomidi_doc[] = "convert bin to midi";
43
44 static PyObject *
45 Py_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
59 static char Py_miditobin_doc[] = "convert midi to bin";
60
61 static PyObject *
62 Py_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
76 static char Py_bintofreq_doc[] = "convert bin to freq";
77
78 static PyObject *
79 Py_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
93 static char Py_freqtobin_doc[] = "convert freq to bin";
94
95 static PyObject *
96 Py_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
110 static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
111
112 static PyObject *
113 Py_zero_crossing_rate (PyObject * self, PyObject * args)
114 {
115   PyObject *input;
116   fvec_t *vec;
117   PyObject *result;
118
119   if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
120     return NULL;
121   }
122
123   if (input == NULL) {
124     return NULL;
125   }
126
127   vec = PyAubio_ArrayToCFvec (input);
128
129   if (vec == NULL) {
130     return NULL;
131   }
132
133   // compute the function
134   result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
135   if (result == NULL) {
136     return NULL;
137   }
138
139   return result;
140 }
141
142 static char Py_min_removal_doc[] = "compute zero crossing rate";
143
144 static PyObject *
145 Py_min_removal(PyObject * self, PyObject * args)
146 {
147   PyObject *input;
148   fvec_t *vec;
149
150   if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
151     return NULL;
152   }
153
154   if (input == NULL) {
155     return NULL;
156   }
157
158   vec = PyAubio_ArrayToCFvec (input);
159
160   if (vec == NULL) {
161     return NULL;
162   }
163
164   // compute the function
165   fvec_min_removal (vec);
166
167   // since this function does not return, we could return None
168   //Py_RETURN_NONE;
169   // however it is convenient to return the modified vector
170   return (PyObject *) PyAubio_CFvecToArray(vec);
171   // or even without converting it back to an array
172   //Py_INCREF(vec);
173   //return (PyObject *)vec;
174 }
175
176 static PyMethodDef aubio_methods[] = {
177   {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
178   {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
179   {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
180   {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
181   {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
182   {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
183   {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
184   {NULL, NULL} /* Sentinel */
185 };
186
187 static char aubio_module_doc[] = "Python module for the aubio library";
188
189 PyMODINIT_FUNC
190 init_aubio (void)
191 {
192   PyObject *m;
193   int err;
194
195   // fvec is defined in __init__.py
196   if (   (PyType_Ready (&Py_cvecType) < 0)
197       || (PyType_Ready (&Py_filterType) < 0)
198       || (PyType_Ready (&Py_filterbankType) < 0)
199       || (PyType_Ready (&Py_fftType) < 0)
200       || (PyType_Ready (&Py_pvocType) < 0)
201       || (PyType_Ready (&Py_sourceType) < 0)
202       // generated objects
203       || (generated_types_ready() < 0 )
204   ) {
205     return;
206   }
207
208   m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
209
210   if (m == NULL) {
211     return;
212   }
213
214   err = _import_array ();
215   if (err != 0) {
216     fprintf (stderr,
217         "Unable to import Numpy array from aubio module (error %d)\n", err);
218   }
219
220   Py_INCREF (&Py_cvecType);
221   PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
222   Py_INCREF (&Py_filterType);
223   PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
224   Py_INCREF (&Py_filterbankType);
225   PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
226   Py_INCREF (&Py_fftType);
227   PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
228   Py_INCREF (&Py_pvocType);
229   PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
230   Py_INCREF (&Py_sourceType);
231   PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
232
233   // add generated objects
234   add_generated_objects(m);
235
236   // add ufunc
237   add_ufuncs(m);
238 }