shithub: aubio

Download patch

ref: 4a67c129189a19fa97c92ca96770868e836ebdd1
parent: 7606d2389698018c0b6ccbab28292d0646fce4c4
author: Paul Brossier <piem@piem.org>
date: Wed Mar 6 06:26:49 EST 2013

ext/aubiomodule.c: more ufuncs

--- a/python/ext/aubiomodule.c
+++ b/python/ext/aubiomodule.c
@@ -38,23 +38,6 @@
   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 @@
   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 @@
 }
 
 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},
--- a/python/ext/ufuncs.c
+++ b/python/ext/ufuncs.c
@@ -1,7 +1,9 @@
 #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;
@@ -8,10 +10,11 @@
     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,7 +22,7 @@
     }
 }
 
-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;
@@ -26,10 +29,11 @@
     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,15 +41,26 @@
     }
 }
 
-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,
@@ -52,13 +67,20 @@
   //(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 )
 {
   int err = 0;
@@ -71,11 +93,24 @@
 
   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;
 }