shithub: aubio

ref: 134cd3a1b76f29e6e2f888da59da9ce6c68cce44
dir: /python/ext/ufuncs.c/

View raw version
#define PY_AUBIO_MODULE_UFUNC
#include "aubio-types.h"

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) = func(*(double *)in);
        /*END main ufunc computation*/

        in += in_step;
        out += out_step;
    }
}

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) = func(*(float *)in);
        /*END main ufunc computation*/

        in += in_step;
        out += out_step;
    }
}

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 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 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;

  err = _import_umath ();
  if (err != 0) {
    fprintf (stderr,
        "Unable to import Numpy umath from aubio module (error %d)\n", err);
  }

  PyObject *f, *dict;
  dict = PyModule_GetDict(m);
  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;
}