shithub: aubio

Download patch

ref: 0d222ee8b82a3bb852b652a083712b20c0b06519
parent: 118f47f7c1956b1b8cea6d1598ce83e67f0ed7cc
author: Paul Brossier <piem@piem.org>
date: Mon Mar 4 17:50:50 EST 2013

ext/ufuncs.c: add first ufunc, unwrap2pi

--- a/python/ext/aubiomodule.c
+++ b/python/ext/aubiomodule.c
@@ -221,7 +221,7 @@
 }
 
 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 @@
 
   // add generated objects
   add_generated_objects(m);
+
+  // add ufunc
+  add_ufuncs(m);
 }
--- /dev/null
+++ b/python/ext/ufuncs.c
@@ -1,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;
+}