Showing the changes to python/ext (2 of 2 files). Show all
Unified · Split
| @@ -38,23 +38,6 @@ Py_alpha_norm (PyObject * self, PyObject * args) · 37 unmodified lines | ||
| 38 | 38 | return result; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | −static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]"; | |
| 42 | − | |
| 43 | −static PyObject * | |
| 44 | −Py_unwrap2pi (PyObject * self, PyObject * args) | |
| 45 | −{ | |
| 46 | − smpl_t input; | |
| 47 | − smpl_t output; | |
| 48 | − | |
| 49 | − if (!PyArg_ParseTuple (args, "|f", &input)) { | |
| 50 | − return NULL; | |
| 51 | − } | |
| 52 | − | |
| 53 | − output = aubio_unwrap2pi (input); | |
| 54 | − | |
| 55 | − return (PyObject *)PyFloat_FromDouble (output); | |
| 56 | −} | |
| 57 | − | |
| 58 | 41 | static char Py_bintomidi_doc[] = "convert bin to midi"; |
| 59 | 42 | |
| 60 | 43 | static PyObject * |
| @@ -123,40 +106,6 @@ Py_freqtobin (PyObject * self, PyObject * args) · 62 unmodified lines | ||
| 123 | 106 | return (PyObject *)PyFloat_FromDouble (output); |
| 124 | 107 | } |
| 125 | 108 | |
| 126 | −static char Py_freqtomidi_doc[] = "convert freq to midi"; | |
| 127 | − | |
| 128 | −static PyObject * | |
| 129 | −Py_freqtomidi (PyObject * self, PyObject * args) | |
| 130 | −{ | |
| 131 | − smpl_t input; | |
| 132 | − smpl_t output; | |
| 133 | − | |
| 134 | − if (!PyArg_ParseTuple (args, "|f", &input)) { | |
| 135 | − return NULL; | |
| 136 | − } | |
| 137 | − | |
| 138 | − output = aubio_freqtomidi (input); | |
| 139 | − | |
| 140 | − return (PyObject *)PyFloat_FromDouble (output); | |
| 141 | −} | |
| 142 | − | |
| 143 | −static char Py_miditofreq_doc[] = "convert midi to freq"; | |
| 144 | − | |
| 145 | −static PyObject * | |
| 146 | −Py_miditofreq (PyObject * self, PyObject * args) | |
| 147 | −{ | |
| 148 | − smpl_t input; | |
| 149 | − smpl_t output; | |
| 150 | − | |
| 151 | − if (!PyArg_ParseTuple (args, "|f", &input)) { | |
| 152 | − return NULL; | |
| 153 | − } | |
| 154 | − | |
| 155 | − output = aubio_miditofreq (input); | |
| 156 | − | |
| 157 | − return (PyObject *)PyFloat_FromDouble (output); | |
| 158 | −} | |
| 159 | − | |
| 160 | 109 | static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; |
| 161 | 110 | |
| 162 | 111 | static PyObject * |
| @@ -224,13 +173,10 @@ Py_min_removal(PyObject * self, PyObject * args) · 61 unmodified lines | ||
| 224 | 173 | } |
| 225 | 174 | |
| 226 | 175 | static PyMethodDef aubio_methods[] = { |
| 227 | − //{"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc}, | |
| 228 | 176 | {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, |
| 229 | 177 | {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, |
| 230 | 178 | {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, |
| 231 | 179 | {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, |
| 232 | − {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc}, | |
| 233 | − {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc}, | |
| 234 | 180 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
| 235 | 181 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, |
| 236 | 182 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
| @@ -1,17 +1,20 @@ | ||
| 1 | 1 | #define PY_AUBIO_MODULE_UFUNC |
| 2 | 2 | #include "aubio-types.h" |
| 3 | 3 | |
| 4 | −static void unwrap2pi(char **args, npy_intp *dimensions, | |
| 4 | +typedef smpl_t (*aubio_unary_func_t)(smpl_t input); | |
| 5 | + | |
| 6 | +static void aubio_PyUFunc_d_d(char **args, npy_intp *dimensions, | |
| 5 | 7 | npy_intp* steps, void* data) |
| 6 | 8 | { |
| 7 | 9 | npy_intp i; |
| 8 | 10 | npy_intp n = dimensions[0]; |
| 9 | 11 | char *in = args[0], *out = args[1]; |
| 10 | 12 | npy_intp in_step = steps[0], out_step = steps[1]; |
| 13 | + aubio_unary_func_t func = (aubio_unary_func_t)(data); | |
| 11 | 14 | |
| 12 | 15 | for (i = 0; i < n; i++) { |
| 13 | 16 | /*BEGIN main ufunc computation*/ |
| 14 | − *((double *)out) = aubio_unwrap2pi(*(double *)in); | |
| 17 | + *((double *)out) = func(*(double *)in); | |
| 15 | 18 | /*END main ufunc computation*/ |
| 16 | 19 | |
| 17 | 20 | in += in_step; |
| @@ -19,17 +22,18 @@ static void unwrap2pi(char **args, npy_intp *dimensions, · 1 unmodified line | ||
| 19 | 22 | } |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | −static void unwrap2pif(char **args, npy_intp *dimensions, | |
| 25 | +static void aubio_PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions, | |
| 23 | 26 | npy_intp* steps, void* data) |
| 24 | 27 | { |
| 25 | 28 | npy_intp i; |
| 26 | 29 | npy_intp n = dimensions[0]; |
| 27 | 30 | char *in = args[0], *out = args[1]; |
| 28 | 31 | npy_intp in_step = steps[0], out_step = steps[1]; |
| 32 | + aubio_unary_func_t func = (aubio_unary_func_t)(data); | |
| 29 | 33 | |
| 30 | 34 | for (i = 0; i < n; i++) { |
| 31 | 35 | /*BEGIN main ufunc computation*/ |
| 32 | − *((float *)out) = aubio_unwrap2pi(*(float *)in); | |
| 36 | + *((float *)out) = func(*(float *)in); | |
| 33 | 37 | /*END main ufunc computation*/ |
| 34 | 38 | |
| 35 | 39 | in += in_step; |
| @@ -37,26 +41,44 @@ static void unwrap2pif(char **args, npy_intp *dimensions, · 1 unmodified line | ||
| 37 | 41 | } |
| 38 | 42 | } |
| 39 | 43 | |
| 40 | −static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi["; | |
| 41 | − | |
| 42 | −PyUFuncGenericFunction unwrap2pi_functions[] = { | |
| 43 | − &unwrap2pif, &unwrap2pi, | |
| 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, | |
| 44 | 50 | //PyUFunc_f_f_As_d_d, PyUFunc_d_d, |
| 45 | 51 | //PyUFunc_g_g, PyUFunc_OO_O_method, |
| 46 | 52 | }; |
| 47 | 53 | |
| 48 | −static void* unwrap2pi_data[] = { | |
| 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[] = { | |
| 49 | 64 | (void *)aubio_unwrap2pi, |
| 50 | 65 | (void *)aubio_unwrap2pi, |
| 51 | 66 | //(void *)unwrap2pil, |
| 52 | 67 | //(void *)unwrap2pio, |
| 53 | 68 | }; |
| 54 | 69 | |
| 55 | −static char unwrap2pi_types[] = { | |
| 56 | − NPY_FLOAT, NPY_FLOAT, | |
| 57 | − NPY_DOUBLE, NPY_DOUBLE, | |
| 58 | − //NPY_LONGDOUBLE, NPY_LONGDOUBLE, | |
| 59 | − //NPY_OBJECT, NPY_OBJECT, | |
| 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, | |
| 60 | 82 | }; |
| 61 | 83 | |
| 62 | 84 | void add_ufuncs ( PyObject *m ) |
| @@ -71,11 +93,24 @@ void add_ufuncs ( PyObject *m ) · 8 unmodified lines | ||
| 71 | 93 | |
| 72 | 94 | PyObject *f, *dict; |
| 73 | 95 | dict = PyModule_GetDict(m); |
| 74 | − f = PyUFunc_FromFuncAndData(unwrap2pi_functions, | |
| 75 | − unwrap2pi_data, unwrap2pi_types, 2, 1, 1, | |
| 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, | |
| 76 | 98 | PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0); |
| 77 | 99 | PyDict_SetItemString(dict, "unwrap2pi", f); |
| 78 | 100 | Py_DECREF(f); |
| 79 | 101 | |
| 102 | + PyObject *g; | |
| 103 | + g = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_freqtomidi_data, Py_aubio_unary_types, | |
| 104 | + Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, | |
| 105 | + PyUFunc_None, "freqtomidi", Py_freqtomidi_doc, 0); | |
| 106 | + PyDict_SetItemString(dict, "freqtomidi", g); | |
| 107 | + Py_DECREF(g); | |
| 108 | + | |
| 109 | + PyObject *h; | |
| 110 | + h = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_miditofreq_data, Py_aubio_unary_types, | |
| 111 | + Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, | |
| 112 | + PyUFunc_None, "miditofreq", Py_miditofreq_doc, 0); | |
| 113 | + PyDict_SetItemString(dict, "miditofreq", h); | |
| 114 | + Py_DECREF(h); | |
| 80 | 115 | return; |
| 81 | 116 | } |