[ad5203c] | 1 | #define PY_AUBIO_MODULE_UFUNC |
---|
[0d222ee] | 2 | #include "aubio-types.h" |
---|
| 3 | |
---|
[4a67c12] | 4 | typedef smpl_t (*aubio_unary_func_t)(smpl_t input); |
---|
| 5 | |
---|
| 6 | static void aubio_PyUFunc_d_d(char **args, npy_intp *dimensions, |
---|
[0d222ee] | 7 | npy_intp* steps, void* data) |
---|
| 8 | { |
---|
| 9 | npy_intp i; |
---|
| 10 | npy_intp n = dimensions[0]; |
---|
| 11 | char *in = args[0], *out = args[1]; |
---|
| 12 | npy_intp in_step = steps[0], out_step = steps[1]; |
---|
[4a67c12] | 13 | aubio_unary_func_t func = (aubio_unary_func_t)(data); |
---|
[0d222ee] | 14 | |
---|
| 15 | for (i = 0; i < n; i++) { |
---|
| 16 | /*BEGIN main ufunc computation*/ |
---|
[4a67c12] | 17 | *((double *)out) = func(*(double *)in); |
---|
[0d222ee] | 18 | /*END main ufunc computation*/ |
---|
| 19 | |
---|
| 20 | in += in_step; |
---|
| 21 | out += out_step; |
---|
| 22 | } |
---|
| 23 | } |
---|
| 24 | |
---|
[4a67c12] | 25 | static void aubio_PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions, |
---|
[0d222ee] | 26 | npy_intp* steps, void* data) |
---|
| 27 | { |
---|
| 28 | npy_intp i; |
---|
| 29 | npy_intp n = dimensions[0]; |
---|
| 30 | char *in = args[0], *out = args[1]; |
---|
| 31 | npy_intp in_step = steps[0], out_step = steps[1]; |
---|
[4a67c12] | 32 | aubio_unary_func_t func = (aubio_unary_func_t)(data); |
---|
[0d222ee] | 33 | |
---|
| 34 | for (i = 0; i < n; i++) { |
---|
| 35 | /*BEGIN main ufunc computation*/ |
---|
[4a67c12] | 36 | *((float *)out) = func(*(float *)in); |
---|
[0d222ee] | 37 | /*END main ufunc computation*/ |
---|
| 38 | |
---|
| 39 | in += in_step; |
---|
| 40 | out += out_step; |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
[4a67c12] | 44 | static int Py_aubio_unary_n_types = 2; |
---|
| 45 | static int Py_aubio_unary_n_inputs = 1; |
---|
| 46 | static int Py_aubio_unary_n_outputs = 1; |
---|
| 47 | PyUFuncGenericFunction Py_aubio_unary_functions[] = { |
---|
| 48 | &aubio_PyUFunc_f_f_As_d_d, |
---|
| 49 | &aubio_PyUFunc_d_d, |
---|
[0d222ee] | 50 | //PyUFunc_f_f_As_d_d, PyUFunc_d_d, |
---|
| 51 | //PyUFunc_g_g, PyUFunc_OO_O_method, |
---|
| 52 | }; |
---|
| 53 | |
---|
[4a67c12] | 54 | static char Py_aubio_unary_types[] = { |
---|
| 55 | NPY_FLOAT, NPY_FLOAT, |
---|
| 56 | NPY_DOUBLE, NPY_DOUBLE, |
---|
| 57 | //NPY_LONGDOUBLE, NPY_LONGDOUBLE, |
---|
| 58 | //NPY_OBJECT, NPY_OBJECT, |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi["; |
---|
| 62 | |
---|
| 63 | static void* Py_unwrap2pi_data[] = { |
---|
[0d222ee] | 64 | (void *)aubio_unwrap2pi, |
---|
| 65 | (void *)aubio_unwrap2pi, |
---|
| 66 | //(void *)unwrap2pil, |
---|
| 67 | //(void *)unwrap2pio, |
---|
| 68 | }; |
---|
| 69 | |
---|
[4a67c12] | 70 | static char Py_freqtomidi_doc[] = "convert frequency to midi"; |
---|
| 71 | |
---|
| 72 | static void* Py_freqtomidi_data[] = { |
---|
| 73 | (void *)aubio_freqtomidi, |
---|
| 74 | (void *)aubio_freqtomidi, |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | static char Py_miditofreq_doc[] = "convert midi to frequency"; |
---|
| 78 | |
---|
| 79 | static void* Py_miditofreq_data[] = { |
---|
| 80 | (void *)aubio_miditofreq, |
---|
| 81 | (void *)aubio_miditofreq, |
---|
[0d222ee] | 82 | }; |
---|
| 83 | |
---|
| 84 | void add_ufuncs ( PyObject *m ) |
---|
| 85 | { |
---|
[ad5203c] | 86 | int err = 0; |
---|
[a138975] | 87 | PyObject *dict, *f, *g, *h; |
---|
[0d222ee] | 88 | |
---|
[ad5203c] | 89 | err = _import_umath (); |
---|
[0d222ee] | 90 | if (err != 0) { |
---|
| 91 | fprintf (stderr, |
---|
[ad5203c] | 92 | "Unable to import Numpy umath from aubio module (error %d)\n", err); |
---|
[0d222ee] | 93 | } |
---|
| 94 | |
---|
| 95 | dict = PyModule_GetDict(m); |
---|
[4a67c12] | 96 | f = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_unwrap2pi_data, Py_aubio_unary_types, |
---|
| 97 | Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, |
---|
[0d222ee] | 98 | PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0); |
---|
| 99 | PyDict_SetItemString(dict, "unwrap2pi", f); |
---|
| 100 | Py_DECREF(f); |
---|
| 101 | |
---|
[4a67c12] | 102 | g = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_freqtomidi_data, Py_aubio_unary_types, |
---|
| 103 | Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, |
---|
| 104 | PyUFunc_None, "freqtomidi", Py_freqtomidi_doc, 0); |
---|
| 105 | PyDict_SetItemString(dict, "freqtomidi", g); |
---|
| 106 | Py_DECREF(g); |
---|
| 107 | |
---|
| 108 | h = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_miditofreq_data, Py_aubio_unary_types, |
---|
| 109 | Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, |
---|
| 110 | PyUFunc_None, "miditofreq", Py_miditofreq_doc, 0); |
---|
| 111 | PyDict_SetItemString(dict, "miditofreq", h); |
---|
| 112 | Py_DECREF(h); |
---|
[0d222ee] | 113 | return; |
---|
| 114 | } |
---|