[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; |
---|
[569b363] | 86 | fvec_t vec; |
---|
[6b1aafc] | 87 | smpl_t alpha; |
---|
| 88 | PyObject *result; |
---|
| 89 | |
---|
[c6388f4] | 90 | if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR ":alpha_norm", &input, &alpha)) { |
---|
[6b1aafc] | 91 | return NULL; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if (input == NULL) { |
---|
| 95 | return NULL; |
---|
| 96 | } |
---|
| 97 | |
---|
[569b363] | 98 | if (!PyAubio_ArrayToCFvec(input, &vec)) { |
---|
[6b1aafc] | 99 | return NULL; |
---|
| 100 | } |
---|
| 101 | |
---|
[ae6e15c] | 102 | // compute the function |
---|
[c6388f4] | 103 | result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, fvec_alpha_norm (&vec, alpha)); |
---|
[0a6c211] | 104 | if (result == NULL) { |
---|
| 105 | return NULL; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | return result; |
---|
| 109 | } |
---|
| 110 | |
---|
[6a50b9e] | 111 | static PyObject * |
---|
| 112 | Py_bintomidi (PyObject * self, PyObject * args) |
---|
| 113 | { |
---|
| 114 | smpl_t input, samplerate, fftsize; |
---|
| 115 | smpl_t output; |
---|
| 116 | |
---|
[c6388f4] | 117 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | output = aubio_bintomidi (input, samplerate, fftsize); |
---|
| 122 | |
---|
| 123 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | static PyObject * |
---|
| 127 | Py_miditobin (PyObject * self, PyObject * args) |
---|
| 128 | { |
---|
| 129 | smpl_t input, samplerate, fftsize; |
---|
| 130 | smpl_t output; |
---|
| 131 | |
---|
[c6388f4] | 132 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 133 | return NULL; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | output = aubio_miditobin (input, samplerate, fftsize); |
---|
| 137 | |
---|
| 138 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | static PyObject * |
---|
| 142 | Py_bintofreq (PyObject * self, PyObject * args) |
---|
| 143 | { |
---|
| 144 | smpl_t input, samplerate, fftsize; |
---|
| 145 | smpl_t output; |
---|
| 146 | |
---|
[c6388f4] | 147 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 148 | return NULL; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | output = aubio_bintofreq (input, samplerate, fftsize); |
---|
| 152 | |
---|
| 153 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | static PyObject * |
---|
| 157 | Py_freqtobin (PyObject * self, PyObject * args) |
---|
| 158 | { |
---|
| 159 | smpl_t input, samplerate, fftsize; |
---|
| 160 | smpl_t output; |
---|
| 161 | |
---|
[c6388f4] | 162 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { |
---|
[6a50b9e] | 163 | return NULL; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | output = aubio_freqtobin (input, samplerate, fftsize); |
---|
| 167 | |
---|
| 168 | return (PyObject *)PyFloat_FromDouble (output); |
---|
| 169 | } |
---|
| 170 | |
---|
[207ed19] | 171 | static PyObject * |
---|
| 172 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
| 173 | { |
---|
| 174 | PyObject *input; |
---|
[569b363] | 175 | fvec_t vec; |
---|
[207ed19] | 176 | PyObject *result; |
---|
| 177 | |
---|
| 178 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
| 179 | return NULL; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if (input == NULL) { |
---|
| 183 | return NULL; |
---|
| 184 | } |
---|
| 185 | |
---|
[569b363] | 186 | if (!PyAubio_ArrayToCFvec(input, &vec)) { |
---|
[207ed19] | 187 | return NULL; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // compute the function |
---|
[c6388f4] | 191 | result = Py_BuildValue (AUBIO_NPY_SMPL_CHR, aubio_zero_crossing_rate (&vec)); |
---|
[207ed19] | 192 | if (result == NULL) { |
---|
| 193 | return NULL; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | return result; |
---|
| 197 | } |
---|
| 198 | |
---|
[ce4bfe3] | 199 | static PyObject * |
---|
[207ed19] | 200 | Py_min_removal(PyObject * self, PyObject * args) |
---|
| 201 | { |
---|
| 202 | PyObject *input; |
---|
[569b363] | 203 | fvec_t vec; |
---|
[207ed19] | 204 | |
---|
[ccf8b77] | 205 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
[207ed19] | 206 | return NULL; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if (input == NULL) { |
---|
| 210 | return NULL; |
---|
| 211 | } |
---|
| 212 | |
---|
[569b363] | 213 | if (!PyAubio_ArrayToCFvec(input, &vec)) { |
---|
[207ed19] | 214 | return NULL; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | // compute the function |
---|
[569b363] | 218 | fvec_min_removal (&vec); |
---|
[615ac7d] | 219 | |
---|
[207ed19] | 220 | // since this function does not return, we could return None |
---|
[9e6695d] | 221 | //Py_RETURN_NONE; |
---|
[ce4bfe3] | 222 | // however it is convenient to return the modified vector |
---|
[569b363] | 223 | return (PyObject *) PyAubio_CFvecToArray(&vec); |
---|
[207ed19] | 224 | // or even without converting it back to an array |
---|
[9b23eb31] | 225 | //Py_INCREF(vec); |
---|
| 226 | //return (PyObject *)vec; |
---|
[207ed19] | 227 | } |
---|
| 228 | |
---|
[0a6c211] | 229 | static PyMethodDef aubio_methods[] = { |
---|
[6a50b9e] | 230 | {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, |
---|
| 231 | {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, |
---|
| 232 | {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, |
---|
| 233 | {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, |
---|
[0a6c211] | 234 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
[6a50b9e] | 235 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
---|
[207ed19] | 236 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
[5a7e2c3] | 237 | {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc}, |
---|
[4615886a] | 238 | {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc}, |
---|
[31a09d2] | 239 | {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc}, |
---|
[9c8c8a6] | 240 | {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc}, |
---|
[913a7f1] | 241 | {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, |
---|
[ccf8b77] | 242 | {NULL, NULL} /* Sentinel */ |
---|
[0a6c211] | 243 | }; |
---|
| 244 | |
---|
[2e4ae1d] | 245 | #if PY_MAJOR_VERSION >= 3 |
---|
| 246 | // Python3 module definition |
---|
| 247 | static struct PyModuleDef moduledef = { |
---|
| 248 | PyModuleDef_HEAD_INIT, |
---|
| 249 | "_aubio", /* m_name */ |
---|
| 250 | aubio_module_doc, /* m_doc */ |
---|
| 251 | -1, /* m_size */ |
---|
| 252 | aubio_methods, /* m_methods */ |
---|
| 253 | NULL, /* m_reload */ |
---|
| 254 | NULL, /* m_traverse */ |
---|
| 255 | NULL, /* m_clear */ |
---|
| 256 | NULL, /* m_free */ |
---|
| 257 | }; |
---|
| 258 | #endif |
---|
| 259 | |
---|
| 260 | static PyObject * |
---|
| 261 | initaubio (void) |
---|
[0a6c211] | 262 | { |
---|
[2e4ae1d] | 263 | PyObject *m = NULL; |
---|
[0a6c211] | 264 | int err; |
---|
| 265 | |
---|
[7a6521d] | 266 | // fvec is defined in __init__.py |
---|
[ce4bfe3] | 267 | if ( (PyType_Ready (&Py_cvecType) < 0) |
---|
| 268 | || (PyType_Ready (&Py_filterType) < 0) |
---|
| 269 | || (PyType_Ready (&Py_filterbankType) < 0) |
---|
| 270 | || (PyType_Ready (&Py_fftType) < 0) |
---|
| 271 | || (PyType_Ready (&Py_pvocType) < 0) |
---|
[d27634d] | 272 | || (PyType_Ready (&Py_sourceType) < 0) |
---|
[f1100a4] | 273 | || (PyType_Ready (&Py_sinkType) < 0) |
---|
[449bff6] | 274 | // generated objects |
---|
| 275 | || (generated_types_ready() < 0 ) |
---|
[615ac7d] | 276 | ) { |
---|
[2e4ae1d] | 277 | return m; |
---|
[0a6c211] | 278 | } |
---|
| 279 | |
---|
[2e4ae1d] | 280 | #if PY_MAJOR_VERSION >= 3 |
---|
| 281 | m = PyModule_Create(&moduledef); |
---|
| 282 | #else |
---|
[1458de5] | 283 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
[2e4ae1d] | 284 | #endif |
---|
[1458de5] | 285 | |
---|
| 286 | if (m == NULL) { |
---|
[2e4ae1d] | 287 | return m; |
---|
[1458de5] | 288 | } |
---|
| 289 | |
---|
[0a6c211] | 290 | err = _import_array (); |
---|
| 291 | if (err != 0) { |
---|
| 292 | fprintf (stderr, |
---|
[ad5203c] | 293 | "Unable to import Numpy array from aubio module (error %d)\n", err); |
---|
[0a6c211] | 294 | } |
---|
| 295 | |
---|
[9b23eb31] | 296 | Py_INCREF (&Py_cvecType); |
---|
| 297 | PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); |
---|
| 298 | Py_INCREF (&Py_filterType); |
---|
| 299 | PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); |
---|
[615ac7d] | 300 | Py_INCREF (&Py_filterbankType); |
---|
| 301 | PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType); |
---|
| 302 | Py_INCREF (&Py_fftType); |
---|
| 303 | PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); |
---|
| 304 | Py_INCREF (&Py_pvocType); |
---|
| 305 | PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); |
---|
[d27634d] | 306 | Py_INCREF (&Py_sourceType); |
---|
| 307 | PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType); |
---|
[f1100a4] | 308 | Py_INCREF (&Py_sinkType); |
---|
| 309 | PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType); |
---|
[449bff6] | 310 | |
---|
[c6388f4] | 311 | PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR); |
---|
| 312 | |
---|
[7a6521d] | 313 | // add generated objects |
---|
[449bff6] | 314 | add_generated_objects(m); |
---|
[0d222ee] | 315 | |
---|
| 316 | // add ufunc |
---|
| 317 | add_ufuncs(m); |
---|
[2e4ae1d] | 318 | |
---|
| 319 | return m; |
---|
[0a6c211] | 320 | } |
---|
[2e4ae1d] | 321 | |
---|
| 322 | #if PY_MAJOR_VERSION >= 3 |
---|
| 323 | // Python3 init |
---|
| 324 | PyMODINIT_FUNC PyInit__aubio(void) |
---|
| 325 | { |
---|
| 326 | return initaubio(); |
---|
| 327 | } |
---|
| 328 | #else |
---|
| 329 | // Python 2 init |
---|
| 330 | PyMODINIT_FUNC init_aubio(void) |
---|
| 331 | { |
---|
| 332 | initaubio(); |
---|
| 333 | } |
---|
| 334 | #endif |
---|