source: python/ext/ufuncs.c @ 633400d

feature/cnnfeature/crepefeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 633400d was e3c4d00, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[python] add docstrings to ufuncs

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#define PY_AUBIO_MODULE_UFUNC
2#include "aubio-types.h"
3
4typedef smpl_t (*aubio_unary_func_t)(smpl_t input);
5
6static void aubio_PyUFunc_d_d(char **args, npy_intp *dimensions,
7                            npy_intp* steps, void* data)
8{
9    npy_intp i;
10    npy_intp n = dimensions[0];
11    char *in = args[0], *out = args[1];
12    npy_intp in_step = steps[0], out_step = steps[1];
13    aubio_unary_func_t func = (aubio_unary_func_t)(data);
14
15    for (i = 0; i < n; i++) {
16        /*BEGIN main ufunc computation*/
17        *((double *)out) = func(*(double *)in);
18        /*END main ufunc computation*/
19
20        in += in_step;
21        out += out_step;
22    }
23}
24
25static void aubio_PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions,
26                            npy_intp* steps, void* data)
27{
28    npy_intp i;
29    npy_intp n = dimensions[0];
30    char *in = args[0], *out = args[1];
31    npy_intp in_step = steps[0], out_step = steps[1];
32    aubio_unary_func_t func = (aubio_unary_func_t)(data);
33
34    for (i = 0; i < n; i++) {
35        /*BEGIN main ufunc computation*/
36        *((float *)out) = func(*(float *)in);
37        /*END main ufunc computation*/
38
39        in += in_step;
40        out += out_step;
41    }
42}
43
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,
50  //PyUFunc_f_f_As_d_d, PyUFunc_d_d,
51  //PyUFunc_g_g, PyUFunc_OO_O_method,
52};
53
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
61// Note: these docstrings should *not* include the function signatures
62
63static char Py_unwrap2pi_doc[] = ""
64"\n"
65"Map angle to unit circle :math:`[-\\pi, \\pi[`.\n"
66"\n"
67"Parameters\n"
68"----------\n"
69"x : numpy.ndarray\n"
70"   input array\n"
71"\n"
72"Returns\n"
73"-------\n"
74"numpy.ndarray\n"
75"   values clamped to the unit circle :math:`[-\\pi, \\pi[`\n"
76"";
77
78static void* Py_unwrap2pi_data[] = {
79  (void *)aubio_unwrap2pi,
80  (void *)aubio_unwrap2pi,
81  //(void *)unwrap2pil,
82  //(void *)unwrap2pio,
83};
84
85static char Py_freqtomidi_doc[] = ""
86"\n"
87"Convert frequency `[0; 23000[` to midi `[0; 128[`.\n"
88"\n"
89"Parameters\n"
90"----------\n"
91"x : numpy.ndarray\n"
92"    Array of frequencies, in Hz.\n"
93"\n"
94"Returns\n"
95"-------\n"
96"numpy.ndarray\n"
97"    Converted frequencies, in midi note.\n"
98"";
99
100static void* Py_freqtomidi_data[] = {
101  (void *)aubio_freqtomidi,
102  (void *)aubio_freqtomidi,
103};
104
105static char Py_miditofreq_doc[] = ""
106"\n"
107"Convert midi `[0; 128[` to frequency `[0, 23000]`.\n"
108"\n"
109"Parameters\n"
110"----------\n"
111"x : numpy.ndarray\n"
112"    Array of frequencies, in midi note.\n"
113"\n"
114"Returns\n"
115"-------\n"
116"numpy.ndarray\n"
117"    Converted frequencies, in Hz\n"
118"";
119
120static void* Py_miditofreq_data[] = {
121  (void *)aubio_miditofreq,
122  (void *)aubio_miditofreq,
123};
124
125void add_ufuncs ( PyObject *m )
126{
127  int err = 0;
128  PyObject *dict, *f, *g, *h;
129
130  err = _import_umath ();
131  if (err != 0) {
132    fprintf (stderr,
133        "Unable to import Numpy umath from aubio module (error %d)\n", err);
134  }
135
136  dict = PyModule_GetDict(m);
137  f = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_unwrap2pi_data, Py_aubio_unary_types,
138          Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs,
139          PyUFunc_None, "unwrap2pi", Py_unwrap2pi_doc, 0);
140  PyDict_SetItemString(dict, "unwrap2pi", f);
141  Py_DECREF(f);
142
143  g = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_freqtomidi_data, Py_aubio_unary_types,
144          Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs,
145          PyUFunc_None, "freqtomidi", Py_freqtomidi_doc, 0);
146  PyDict_SetItemString(dict, "freqtomidi", g);
147  Py_DECREF(g);
148
149  h = PyUFunc_FromFuncAndData(Py_aubio_unary_functions, Py_miditofreq_data, Py_aubio_unary_types,
150          Py_aubio_unary_n_types, Py_aubio_unary_n_inputs, Py_aubio_unary_n_outputs,
151          PyUFunc_None, "miditofreq", Py_miditofreq_doc, 0);
152  PyDict_SetItemString(dict, "miditofreq", h);
153  Py_DECREF(h);
154  return;
155}
Note: See TracBrowser for help on using the repository browser.