From 0d222ee8b82a3bb852b652a083712b20c0b06519 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Mon, 4 Mar 2013 22:50:50 -0500 Subject: [PATCH] ext/ufuncs.c: add first ufunc, unwrap2pi --- python/ext/aubiomodule.c | 5 ++- python/ext/ufuncs.c | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 python/ext/ufuncs.c diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index 293758ab..74ad7cb8 100644 --- a/python/ext/aubiomodule.c +++ b/python/ext/aubiomodule.c @@ -221,7 +221,7 @@ Py_min_removal(PyObject * self, PyObject * args) } static PyMethodDef aubio_methods[] = { - {"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc}, + //{"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}, @@ -280,4 +280,7 @@ init_aubio (void) // add generated objects add_generated_objects(m); + + // add ufunc + add_ufuncs(m); } diff --git a/python/ext/ufuncs.c b/python/ext/ufuncs.c new file mode 100644 index 00000000..63b11460 --- /dev/null +++ b/python/ext/ufuncs.c @@ -0,0 +1,79 @@ +#include "aubio-types.h" + +static void unwrap2pi(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]; + + for (i = 0; i < n; i++) { + /*BEGIN main ufunc computation*/ + *((double *)out) = aubio_unwrap2pi(*(double *)in); + /*END main ufunc computation*/ + + in += in_step; + out += out_step; + } +} + +static void unwrap2pif(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]; + + for (i = 0; i < n; i++) { + /*BEGIN main ufunc computation*/ + *((float *)out) = aubio_unwrap2pi(*(float *)in); + /*END main ufunc computation*/ + + in += in_step; + out += out_step; + } +} + +static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi["; + +PyUFuncGenericFunction unwrap2pi_functions[] = { + &unwrap2pif, &unwrap2pi, + //PyUFunc_f_f_As_d_d, PyUFunc_d_d, + //PyUFunc_g_g, PyUFunc_OO_O_method, +}; + +static void* 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, +}; + +void add_ufuncs ( PyObject *m ) +{ + int err = _import_umath(); + + if (err != 0) { + fprintf (stderr, + "Unable to import Numpy C API Ufunc from aubio module (error %d)\n", err); + } + + PyObject *f, *dict; + dict = PyModule_GetDict(m); + f = PyUFunc_FromFuncAndData(unwrap2pi_functions, + unwrap2pi_data, unwrap2pi_types, 2, 1, 1, + PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0); + PyDict_SetItemString(dict, "unwrap2pi", f); + Py_DECREF(f); + + return; +} -- 2.26.2