Changeset 4a67c12 for python


Ignore:
Timestamp:
Mar 6, 2013, 5:26:49 PM (12 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
b583c099
Parents:
7606d23
Message:

ext/aubiomodule.c: more ufuncs

Location:
python/ext
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/ext/aubiomodule.c

    r7606d23 r4a67c12  
    3939}
    4040
    41 static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]";
    42 
    43 static PyObject *
    44 Py_unwrap2pi (PyObject * self, PyObject * args)
    45 {
    46   smpl_t input;
    47   smpl_t output;
    48 
    49   if (!PyArg_ParseTuple (args, "|f", &input)) {
    50     return NULL;
    51   }
    52 
    53   output = aubio_unwrap2pi (input);
    54 
    55   return (PyObject *)PyFloat_FromDouble (output);
    56 }
    57 
    5841static char Py_bintomidi_doc[] = "convert bin to midi";
    5942
     
    120103
    121104  output = aubio_freqtobin (input, samplerate, fftsize);
    122 
    123   return (PyObject *)PyFloat_FromDouble (output);
    124 }
    125 
    126 static char Py_freqtomidi_doc[] = "convert freq to midi";
    127 
    128 static PyObject *
    129 Py_freqtomidi (PyObject * self, PyObject * args)
    130 {
    131   smpl_t input;
    132   smpl_t output;
    133 
    134   if (!PyArg_ParseTuple (args, "|f", &input)) {
    135     return NULL;
    136   }
    137 
    138   output = aubio_freqtomidi (input);
    139 
    140   return (PyObject *)PyFloat_FromDouble (output);
    141 }
    142 
    143 static char Py_miditofreq_doc[] = "convert midi to freq";
    144 
    145 static PyObject *
    146 Py_miditofreq (PyObject * self, PyObject * args)
    147 {
    148   smpl_t input;
    149   smpl_t output;
    150 
    151   if (!PyArg_ParseTuple (args, "|f", &input)) {
    152     return NULL;
    153   }
    154 
    155   output = aubio_miditofreq (input);
    156105
    157106  return (PyObject *)PyFloat_FromDouble (output);
     
    225174
    226175static PyMethodDef aubio_methods[] = {
    227   //{"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc},
    228176  {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
    229177  {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
    230178  {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
    231179  {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
    232   {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc},
    233   {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc},
    234180  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
    235181  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
  • python/ext/ufuncs.c

    r7606d23 r4a67c12  
    22#include "aubio-types.h"
    33
    4 static void unwrap2pi(char **args, npy_intp *dimensions,
     4typedef smpl_t (*aubio_unary_func_t)(smpl_t input);
     5
     6static void aubio_PyUFunc_d_d(char **args, npy_intp *dimensions,
    57                            npy_intp* steps, void* data)
    68{
     
    911    char *in = args[0], *out = args[1];
    1012    npy_intp in_step = steps[0], out_step = steps[1];
     13    aubio_unary_func_t func = (aubio_unary_func_t)(data);
    1114
    1215    for (i = 0; i < n; i++) {
    1316        /*BEGIN main ufunc computation*/
    14         *((double *)out) = aubio_unwrap2pi(*(double *)in);
     17        *((double *)out) = func(*(double *)in);
    1518        /*END main ufunc computation*/
    1619
     
    2023}
    2124
    22 static void unwrap2pif(char **args, npy_intp *dimensions,
     25static void aubio_PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions,
    2326                            npy_intp* steps, void* data)
    2427{
     
    2730    char *in = args[0], *out = args[1];
    2831    npy_intp in_step = steps[0], out_step = steps[1];
     32    aubio_unary_func_t func = (aubio_unary_func_t)(data);
    2933
    3034    for (i = 0; i < n; i++) {
    3135        /*BEGIN main ufunc computation*/
    32         *((float *)out) = aubio_unwrap2pi(*(float *)in);
     36        *((float *)out) = func(*(float *)in);
    3337        /*END main ufunc computation*/
    3438
     
    3842}
    3943
    40 static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi[";
    41 
    42 PyUFuncGenericFunction unwrap2pi_functions[] = {
    43   &unwrap2pif, &unwrap2pi,
     44static int Py_aubio_unary_n_types = 2;
     45static int Py_aubio_unary_n_inputs = 1;
     46static int Py_aubio_unary_n_outputs = 1;
     47PyUFuncGenericFunction Py_aubio_unary_functions[] = {
     48  &aubio_PyUFunc_f_f_As_d_d,
     49  &aubio_PyUFunc_d_d,
    4450  //PyUFunc_f_f_As_d_d, PyUFunc_d_d,
    4551  //PyUFunc_g_g, PyUFunc_OO_O_method,
    4652};
    4753
    48 static void* unwrap2pi_data[] = {
     54static char Py_aubio_unary_types[] = {
     55  NPY_FLOAT, NPY_FLOAT,
     56  NPY_DOUBLE, NPY_DOUBLE,
     57  //NPY_LONGDOUBLE, NPY_LONGDOUBLE,
     58  //NPY_OBJECT, NPY_OBJECT,
     59};
     60
     61static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi[";
     62
     63static void* Py_unwrap2pi_data[] = {
    4964  (void *)aubio_unwrap2pi,
    5065  (void *)aubio_unwrap2pi,
     
    5368};
    5469
    55 static char unwrap2pi_types[] = {
    56   NPY_FLOAT, NPY_FLOAT,
    57   NPY_DOUBLE, NPY_DOUBLE,
    58   //NPY_LONGDOUBLE, NPY_LONGDOUBLE,
    59   //NPY_OBJECT, NPY_OBJECT,
     70static char Py_freqtomidi_doc[] = "convert frequency to midi";
     71
     72static void* Py_freqtomidi_data[] = {
     73  (void *)aubio_freqtomidi,
     74  (void *)aubio_freqtomidi,
     75};
     76
     77static char Py_miditofreq_doc[] = "convert midi to frequency";
     78
     79static void* Py_miditofreq_data[] = {
     80  (void *)aubio_miditofreq,
     81  (void *)aubio_miditofreq,
    6082};
    6183
     
    7294  PyObject *f, *dict;
    7395  dict = PyModule_GetDict(m);
    74   f = PyUFunc_FromFuncAndData(unwrap2pi_functions,
    75           unwrap2pi_data, unwrap2pi_types, 2, 1, 1,
     96  f = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_unwrap2pi_data, Py_aubio_unary_types,
     97          Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs,
    7698          PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0);
    7799  PyDict_SetItemString(dict, "unwrap2pi", f);
    78100  Py_DECREF(f);
    79101
     102  PyObject *g;
     103  g = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_freqtomidi_data, Py_aubio_unary_types,
     104          Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs,
     105          PyUFunc_None, "freqtomidi", Py_freqtomidi_doc, 0);
     106  PyDict_SetItemString(dict, "freqtomidi", g);
     107  Py_DECREF(g);
     108
     109  PyObject *h;
     110  h = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_miditofreq_data, Py_aubio_unary_types,
     111          Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs,
     112          PyUFunc_None, "miditofreq", Py_miditofreq_doc, 0);
     113  PyDict_SetItemString(dict, "miditofreq", h);
     114  Py_DECREF(h);
    80115  return;
    81116}
Note: See TracChangeset for help on using the changeset viewer.