[7a6521d] | 1 | #define PY_AUBIO_MODULE_MAIN |
---|
[ae6e15c] | 2 | #include "aubio-types.h" |
---|
[ad5203c] | 3 | #include "aubio-generated.h" |
---|
[913a7f1] | 4 | #include "py-musicutils.h" |
---|
[ad5203c] | 5 | |
---|
[1ba8e60] | 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 | |
---|
[ad5203c] | 78 | extern void add_generated_objects ( PyObject *m ); |
---|
| 79 | extern void add_ufuncs ( PyObject *m ); |
---|
[d75c900] | 80 | extern int generated_types_ready(void); |
---|
[0a6c211] | 81 | |
---|
[6b1aafc] | 82 | static PyObject * |
---|
| 83 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
| 84 | { |
---|
| 85 | PyObject *input; |
---|
[ce4bfe3] | 86 | fvec_t *vec; |
---|
[6b1aafc] | 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 | |
---|
[ce4bfe3] | 98 | vec = PyAubio_ArrayToCFvec (input); |
---|
[6b1aafc] | 99 | |
---|
| 100 | if (vec == NULL) { |
---|
| 101 | return NULL; |
---|
| 102 | } |
---|
| 103 | |
---|
[ae6e15c] | 104 | // compute the function |
---|
[ce4bfe3] | 105 | result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha)); |
---|
[0a6c211] | 106 | if (result == NULL) { |
---|
| 107 | return NULL; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | return result; |
---|
| 111 | } |
---|
| 112 | |
---|
[6a50b9e] | 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 | |
---|
[207ed19] | 173 | static PyObject * |
---|
| 174 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 175 | { |
---|
| 176 | PyObject *input; |
---|
[ce4bfe3] | 177 | fvec_t *vec; |
---|
[207ed19] | 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 | |
---|
[ce4bfe3] | 188 | vec = PyAubio_ArrayToCFvec (input); |
---|
[207ed19] | 189 | |
---|
| 190 | if (vec == NULL) { |
---|
| 191 | return NULL; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | // compute the function |
---|
[ce4bfe3] | 195 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec)); |
---|
[207ed19] | 196 | if (result == NULL) { |
---|
| 197 | return NULL; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | return result; |
---|
| 201 | } |
---|
| 202 | |
---|
[ce4bfe3] | 203 | static PyObject * |
---|
[207ed19] | 204 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 205 | { |
---|
| 206 | PyObject *input; |
---|
[ce4bfe3] | 207 | fvec_t *vec; |
---|
[207ed19] | 208 | |
---|
[ccf8b77] | 209 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
[207ed19] | 210 | return NULL; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if (input == NULL) { |
---|
| 214 | return NULL; |
---|
| 215 | } |
---|
| 216 | |
---|
[ce4bfe3] | 217 | vec = PyAubio_ArrayToCFvec (input); |
---|
[207ed19] | 218 | |
---|
| 219 | if (vec == NULL) { |
---|
| 220 | return NULL; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // compute the function |
---|
[ce4bfe3] | 224 | fvec_min_removal (vec); |
---|
[615ac7d] | 225 | |
---|
[207ed19] | 226 | // since this function does not return, we could return None |
---|
[9e6695d] | 227 | //Py_RETURN_NONE; |
---|
[ce4bfe3] | 228 | // however it is convenient to return the modified vector |
---|
| 229 | return (PyObject *) PyAubio_CFvecToArray(vec); |
---|
[207ed19] | 230 | // or even without converting it back to an array |
---|
[9b23eb31] | 231 | //Py_INCREF(vec); |
---|
| 232 | //return (PyObject *)vec; |
---|
[207ed19] | 233 | } |
---|
| 234 | |
---|
[0a6c211] | 235 | static PyMethodDef aubio_methods[] = { |
---|
[6a50b9e] | 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}, |
---|
[0a6c211] | 240 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[6a50b9e] | 241 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
---|
[207ed19] | 242 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[5a7e2c3] | 243 | {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc}, |
---|
[4615886a] | 244 | {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc}, |
---|
[31a09d2] | 245 | {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc}, |
---|
[9c8c8a6] | 246 | {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc}, |
---|
[913a7f1] | 247 | {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, |
---|
[ccf8b77] | 248 | {NULL, NULL} /* Sentinel */ |
---|
[0a6c211] | 249 | }; |
---|
| 250 | |
---|
| 251 | PyMODINIT_FUNC |
---|
| 252 | init_aubio (void) |
---|
| 253 | { |
---|
| 254 | PyObject *m; |
---|
| 255 | int err; |
---|
| 256 | |
---|
[7a6521d] | 257 | // fvec is defined in __init__.py |
---|
[ce4bfe3] | 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) |
---|
[d27634d] | 263 | || (PyType_Ready (&Py_sourceType) < 0) |
---|
[f1100a4] | 264 | || (PyType_Ready (&Py_sinkType) < 0) |
---|
[449bff6] | 265 | // generated objects |
---|
| 266 | || (generated_types_ready() < 0 ) |
---|
[615ac7d] | 267 | ) { |
---|
[0a6c211] | 268 | return; |
---|
| 269 | } |
---|
| 270 | |
---|
[1458de5] | 271 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
| 272 | |
---|
| 273 | if (m == NULL) { |
---|
| 274 | return; |
---|
| 275 | } |
---|
| 276 | |
---|
[0a6c211] | 277 | err = _import_array (); |
---|
| 278 | if (err != 0) { |
---|
| 279 | fprintf (stderr, |
---|
[ad5203c] | 280 | "Unable to import Numpy array from aubio module (error %d)\n", err); |
---|
[0a6c211] | 281 | } |
---|
| 282 | |
---|
[9b23eb31] | 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); |
---|
[615ac7d] | 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); |
---|
[d27634d] | 293 | Py_INCREF (&Py_sourceType); |
---|
| 294 | PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType); |
---|
[f1100a4] | 295 | Py_INCREF (&Py_sinkType); |
---|
| 296 | PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType); |
---|
[449bff6] | 297 | |
---|
[7a6521d] | 298 | // add generated objects |
---|
[449bff6] | 299 | add_generated_objects(m); |
---|
[0d222ee] | 300 | |
---|
| 301 | // add ufunc |
---|
| 302 | add_ufuncs(m); |
---|
[0a6c211] | 303 | } |
---|