From: Paul Brossier Date: Wed, 6 Mar 2013 16:26:49 +0000 (-0500) Subject: ext/aubiomodule.c: more ufuncs X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4a67c129189a19fa97c92ca96770868e836ebdd1;p=aubio.git ext/aubiomodule.c: more ufuncs --- diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index 76afd1e6..3f3ee277 100644 --- a/python/ext/aubiomodule.c +++ b/python/ext/aubiomodule.c @@ -38,23 +38,6 @@ Py_alpha_norm (PyObject * self, PyObject * args) return result; } -static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]"; - -static PyObject * -Py_unwrap2pi (PyObject * self, PyObject * args) -{ - smpl_t input; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|f", &input)) { - return NULL; - } - - output = aubio_unwrap2pi (input); - - return (PyObject *)PyFloat_FromDouble (output); -} - static char Py_bintomidi_doc[] = "convert bin to midi"; static PyObject * @@ -123,40 +106,6 @@ Py_freqtobin (PyObject * self, PyObject * args) return (PyObject *)PyFloat_FromDouble (output); } -static char Py_freqtomidi_doc[] = "convert freq to midi"; - -static PyObject * -Py_freqtomidi (PyObject * self, PyObject * args) -{ - smpl_t input; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|f", &input)) { - return NULL; - } - - output = aubio_freqtomidi (input); - - return (PyObject *)PyFloat_FromDouble (output); -} - -static char Py_miditofreq_doc[] = "convert midi to freq"; - -static PyObject * -Py_miditofreq (PyObject * self, PyObject * args) -{ - smpl_t input; - smpl_t output; - - if (!PyArg_ParseTuple (args, "|f", &input)) { - return NULL; - } - - output = aubio_miditofreq (input); - - return (PyObject *)PyFloat_FromDouble (output); -} - static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; static PyObject * @@ -224,13 +173,10 @@ Py_min_removal(PyObject * self, PyObject * args) } static PyMethodDef aubio_methods[] = { - //{"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc}, {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, - {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc}, - {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc}, {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, diff --git a/python/ext/ufuncs.c b/python/ext/ufuncs.c index 9eb1a83e..455247b9 100644 --- a/python/ext/ufuncs.c +++ b/python/ext/ufuncs.c @@ -1,17 +1,20 @@ #define PY_AUBIO_MODULE_UFUNC #include "aubio-types.h" -static void unwrap2pi(char **args, npy_intp *dimensions, +typedef smpl_t (*aubio_unary_func_t)(smpl_t input); + +static void aubio_PyUFunc_d_d(char **args, npy_intp *dimensions, npy_intp* steps, void* data) { npy_intp i; npy_intp n = dimensions[0]; char *in = args[0], *out = args[1]; npy_intp in_step = steps[0], out_step = steps[1]; + aubio_unary_func_t func = (aubio_unary_func_t)(data); for (i = 0; i < n; i++) { /*BEGIN main ufunc computation*/ - *((double *)out) = aubio_unwrap2pi(*(double *)in); + *((double *)out) = func(*(double *)in); /*END main ufunc computation*/ in += in_step; @@ -19,17 +22,18 @@ static void unwrap2pi(char **args, npy_intp *dimensions, } } -static void unwrap2pif(char **args, npy_intp *dimensions, +static void aubio_PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions, npy_intp* steps, void* data) { npy_intp i; npy_intp n = dimensions[0]; char *in = args[0], *out = args[1]; npy_intp in_step = steps[0], out_step = steps[1]; + aubio_unary_func_t func = (aubio_unary_func_t)(data); for (i = 0; i < n; i++) { /*BEGIN main ufunc computation*/ - *((float *)out) = aubio_unwrap2pi(*(float *)in); + *((float *)out) = func(*(float *)in); /*END main ufunc computation*/ in += in_step; @@ -37,26 +41,44 @@ static void unwrap2pif(char **args, npy_intp *dimensions, } } -static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi["; - -PyUFuncGenericFunction unwrap2pi_functions[] = { - &unwrap2pif, &unwrap2pi, +static int Py_aubio_unary_n_types = 2; +static int Py_aubio_unary_n_inputs = 1; +static int Py_aubio_unary_n_outputs = 1; +PyUFuncGenericFunction Py_aubio_unary_functions[] = { + &aubio_PyUFunc_f_f_As_d_d, + &aubio_PyUFunc_d_d, //PyUFunc_f_f_As_d_d, PyUFunc_d_d, //PyUFunc_g_g, PyUFunc_OO_O_method, }; -static void* unwrap2pi_data[] = { +static char Py_aubio_unary_types[] = { + NPY_FLOAT, NPY_FLOAT, + NPY_DOUBLE, NPY_DOUBLE, + //NPY_LONGDOUBLE, NPY_LONGDOUBLE, + //NPY_OBJECT, NPY_OBJECT, +}; + +static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi["; + +static void* Py_unwrap2pi_data[] = { (void *)aubio_unwrap2pi, (void *)aubio_unwrap2pi, //(void *)unwrap2pil, //(void *)unwrap2pio, }; -static char unwrap2pi_types[] = { - NPY_FLOAT, NPY_FLOAT, - NPY_DOUBLE, NPY_DOUBLE, - //NPY_LONGDOUBLE, NPY_LONGDOUBLE, - //NPY_OBJECT, NPY_OBJECT, +static char Py_freqtomidi_doc[] = "convert frequency to midi"; + +static void* Py_freqtomidi_data[] = { + (void *)aubio_freqtomidi, + (void *)aubio_freqtomidi, +}; + +static char Py_miditofreq_doc[] = "convert midi to frequency"; + +static void* Py_miditofreq_data[] = { + (void *)aubio_miditofreq, + (void *)aubio_miditofreq, }; void add_ufuncs ( PyObject *m ) @@ -71,11 +93,24 @@ void add_ufuncs ( PyObject *m ) PyObject *f, *dict; dict = PyModule_GetDict(m); - f = PyUFunc_FromFuncAndData(unwrap2pi_functions, - unwrap2pi_data, unwrap2pi_types, 2, 1, 1, + f = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_unwrap2pi_data, Py_aubio_unary_types, + Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0); PyDict_SetItemString(dict, "unwrap2pi", f); Py_DECREF(f); + PyObject *g; + g = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_freqtomidi_data, Py_aubio_unary_types, + Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, + PyUFunc_None, "freqtomidi", Py_freqtomidi_doc, 0); + PyDict_SetItemString(dict, "freqtomidi", g); + Py_DECREF(g); + + PyObject *h; + h = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_miditofreq_data, Py_aubio_unary_types, + Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs, + PyUFunc_None, "miditofreq", Py_miditofreq_doc, 0); + PyDict_SetItemString(dict, "miditofreq", h); + Py_DECREF(h); return; }