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