[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 | |
---|
[e3c4d00] | 61 | // Note: these docstrings should *not* include the function signatures |
---|
| 62 | |
---|
| 63 | static char Py_unwrap2pi_doc[] = "" |
---|
| 64 | "\n" |
---|
| 65 | "Map angle to unit circle :math:`[-\\pi, \\pi[`.\n" |
---|
| 66 | "\n" |
---|
| 67 | "Parameters\n" |
---|
| 68 | "----------\n" |
---|
| 69 | "x : numpy.ndarray\n" |
---|
| 70 | " input array\n" |
---|
| 71 | "\n" |
---|
| 72 | "Returns\n" |
---|
| 73 | "-------\n" |
---|
| 74 | "numpy.ndarray\n" |
---|
| 75 | " values clamped to the unit circle :math:`[-\\pi, \\pi[`\n" |
---|
| 76 | ""; |
---|
[4a67c12] | 77 | |
---|
| 78 | static void* Py_unwrap2pi_data[] = { |
---|
[0d222ee] | 79 | (void *)aubio_unwrap2pi, |
---|
| 80 | (void *)aubio_unwrap2pi, |
---|
| 81 | //(void *)unwrap2pil, |
---|
| 82 | //(void *)unwrap2pio, |
---|
| 83 | }; |
---|
| 84 | |
---|
[e3c4d00] | 85 | static char Py_freqtomidi_doc[] = "" |
---|
| 86 | "\n" |
---|
| 87 | "Convert frequency `[0; 23000[` to midi `[0; 128[`.\n" |
---|
| 88 | "\n" |
---|
| 89 | "Parameters\n" |
---|
| 90 | "----------\n" |
---|
| 91 | "x : numpy.ndarray\n" |
---|
| 92 | " Array of frequencies, in Hz.\n" |
---|
| 93 | "\n" |
---|
| 94 | "Returns\n" |
---|
| 95 | "-------\n" |
---|
| 96 | "numpy.ndarray\n" |
---|
| 97 | " Converted frequencies, in midi note.\n" |
---|
| 98 | ""; |
---|
[4a67c12] | 99 | |
---|
| 100 | static void* Py_freqtomidi_data[] = { |
---|
| 101 | (void *)aubio_freqtomidi, |
---|
| 102 | (void *)aubio_freqtomidi, |
---|
| 103 | }; |
---|
| 104 | |
---|
[e3c4d00] | 105 | static char Py_miditofreq_doc[] = "" |
---|
| 106 | "\n" |
---|
| 107 | "Convert midi `[0; 128[` to frequency `[0, 23000]`.\n" |
---|
| 108 | "\n" |
---|
| 109 | "Parameters\n" |
---|
| 110 | "----------\n" |
---|
| 111 | "x : numpy.ndarray\n" |
---|
| 112 | " Array of frequencies, in midi note.\n" |
---|
| 113 | "\n" |
---|
| 114 | "Returns\n" |
---|
| 115 | "-------\n" |
---|
| 116 | "numpy.ndarray\n" |
---|
| 117 | " Converted frequencies, in Hz\n" |
---|
| 118 | ""; |
---|
[4a67c12] | 119 | |
---|
| 120 | static void* Py_miditofreq_data[] = { |
---|
| 121 | (void *)aubio_miditofreq, |
---|
| 122 | (void *)aubio_miditofreq, |
---|
[0d222ee] | 123 | }; |
---|
| 124 | |
---|
| 125 | void add_ufuncs ( PyObject *m ) |
---|
| 126 | { |
---|
[ad5203c] | 127 | int err = 0; |
---|
[a138975] | 128 | PyObject *dict, *f, *g, *h; |
---|
[0d222ee] | 129 | |
---|
[ad5203c] | 130 | err = _import_umath (); |
---|
[0d222ee] | 131 | if (err != 0) { |
---|
| 132 | fprintf (stderr, |
---|
[ad5203c] | 133 | "Unable to import Numpy umath from aubio module (error %d)\n", err); |
---|
[0d222ee] | 134 | } |
---|
| 135 | |
---|
| 136 | dict = PyModule_GetDict(m); |
---|
[4a67c12] | 137 | f = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_unwrap2pi_data, Py_aubio_unary_types, |
---|
| 138 | Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, |
---|
[0d222ee] | 139 | PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0); |
---|
| 140 | PyDict_SetItemString(dict, "unwrap2pi", f); |
---|
| 141 | Py_DECREF(f); |
---|
| 142 | |
---|
[4a67c12] | 143 | g = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_freqtomidi_data, Py_aubio_unary_types, |
---|
| 144 | Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, |
---|
| 145 | PyUFunc_None, "freqtomidi", Py_freqtomidi_doc, 0); |
---|
| 146 | PyDict_SetItemString(dict, "freqtomidi", g); |
---|
| 147 | Py_DECREF(g); |
---|
| 148 | |
---|
| 149 | h = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_miditofreq_data, Py_aubio_unary_types, |
---|
| 150 | Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, |
---|
| 151 | PyUFunc_None, "miditofreq", Py_miditofreq_doc, 0); |
---|
| 152 | PyDict_SetItemString(dict, "miditofreq", h); |
---|
| 153 | Py_DECREF(h); |
---|
[0d222ee] | 154 | return; |
---|
| 155 | } |
---|