History

ext/aubiomodule.c: more ufuncs

Paul Brossier committed 13 years ago

Commit 4a67c121 parent 7606d232 files · +51 −70Browse files

Showing the changes to python (2 of 2 files). Show all

Unified · Split

2 files
  1. python/ext/aubiomodule.c +0 −54
  2. python/ext/ufuncs.c +51 −16
python/ext/aubiomodule.c+0−54
@@ -38,23 +38,6 @@ Py_alpha_norm (PyObject * self, PyObject * args) · 37 unmodified lines
3838 return result;
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−
5841 static char Py_bintomidi_doc[] = "convert bin to midi";
5942
6043 static PyObject *
@@ -123,40 +106,6 @@ Py_freqtobin (PyObject * self, PyObject * args) · 62 unmodified lines
123106 return (PyObject *)PyFloat_FromDouble (output);
124107 }
125108
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);
156−
157− return (PyObject *)PyFloat_FromDouble (output);
158−}
159−
160109 static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
161110
162111 static PyObject *
@@ -224,13 +173,10 @@ Py_min_removal(PyObject * self, PyObject * args) · 61 unmodified lines
224173 }
225174
226175 static 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},
236182 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
python/ext/ufuncs.c+51−16
@@ -1,17 +1,20 @@
11 #define PY_AUBIO_MODULE_UFUNC
22 #include "aubio-types.h"
33
4−static void unwrap2pi(char **args, npy_intp *dimensions,
4+typedef smpl_t (*aubio_unary_func_t)(smpl_t input);
5+
6+static void aubio_PyUFunc_d_d(char **args, npy_intp *dimensions,
57 npy_intp* steps, void* data)
68 {
79 npy_intp i;
810 npy_intp n = dimensions[0];
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
1720 in += in_step;
@@ -19,17 +22,18 @@ static void unwrap2pi(char **args, npy_intp *dimensions, · 1 unmodified line
1922 }
2023 }
2124
22−static void unwrap2pif(char **args, npy_intp *dimensions,
25+static void aubio_PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions,
2326 npy_intp* steps, void* data)
2427 {
2528 npy_intp i;
2629 npy_intp n = dimensions[0];
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
3539 in += in_step;
@@ -37,26 +41,44 @@ static void unwrap2pif(char **args, npy_intp *dimensions, · 1 unmodified line
3741 }
3842 }
3943
40−static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi[";
41−
42−PyUFuncGenericFunction unwrap2pi_functions[] = {
43− &unwrap2pif, &unwrap2pi,
44+static int Py_aubio_unary_n_types = 2;
45+static int Py_aubio_unary_n_inputs = 1;
46+static int Py_aubio_unary_n_outputs = 1;
47+PyUFuncGenericFunction 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[] = {
54+static 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+static char Py_unwrap2pi_doc[] = "map angle to unit circle [-pi, pi[";
62+
63+static void* Py_unwrap2pi_data[] = {
4964 (void *)aubio_unwrap2pi,
5065 (void *)aubio_unwrap2pi,
5166 //(void *)unwrap2pil,
5267 //(void *)unwrap2pio,
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,
70+static char Py_freqtomidi_doc[] = "convert frequency to midi";
71+
72+static void* Py_freqtomidi_data[] = {
73+ (void *)aubio_freqtomidi,
74+ (void *)aubio_freqtomidi,
75+};
76+
77+static char Py_miditofreq_doc[] = "convert midi to frequency";
78+
79+static void* Py_miditofreq_data[] = {
80+ (void *)aubio_miditofreq,
81+ (void *)aubio_miditofreq,
6082 };
6183
6284 void add_ufuncs ( PyObject *m )
@@ -71,11 +93,24 @@ void add_ufuncs ( PyObject *m ) · 8 unmodified lines
7193
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 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t