1 | #include <Python.h> |
---|
2 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API |
---|
3 | #include <numpy/arrayobject.h> |
---|
4 | |
---|
5 | #include "aubio-types.h" |
---|
6 | |
---|
7 | static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; |
---|
8 | |
---|
9 | static PyObject * |
---|
10 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
11 | { |
---|
12 | PyObject *input; |
---|
13 | Py_fvec *vec; |
---|
14 | smpl_t alpha; |
---|
15 | PyObject *result; |
---|
16 | |
---|
17 | if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { |
---|
18 | return NULL; |
---|
19 | } |
---|
20 | |
---|
21 | if (input == NULL) { |
---|
22 | return NULL; |
---|
23 | } |
---|
24 | |
---|
25 | vec = PyAubio_ArrayToFvec (input); |
---|
26 | |
---|
27 | if (vec == NULL) { |
---|
28 | return NULL; |
---|
29 | } |
---|
30 | |
---|
31 | // compute the function |
---|
32 | result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha)); |
---|
33 | if (result == NULL) { |
---|
34 | return NULL; |
---|
35 | } |
---|
36 | |
---|
37 | return result; |
---|
38 | } |
---|
39 | |
---|
40 | static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; |
---|
41 | |
---|
42 | static PyObject * |
---|
43 | Py_zero_crossing_rate (PyObject * self, PyObject * args) |
---|
44 | { |
---|
45 | PyObject *input; |
---|
46 | Py_fvec *vec; |
---|
47 | PyObject *result; |
---|
48 | |
---|
49 | if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) { |
---|
50 | return NULL; |
---|
51 | } |
---|
52 | |
---|
53 | if (input == NULL) { |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | |
---|
57 | vec = PyAubio_ArrayToFvec (input); |
---|
58 | |
---|
59 | if (vec == NULL) { |
---|
60 | return NULL; |
---|
61 | } |
---|
62 | |
---|
63 | // compute the function |
---|
64 | result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec->o)); |
---|
65 | if (result == NULL) { |
---|
66 | return NULL; |
---|
67 | } |
---|
68 | |
---|
69 | return result; |
---|
70 | } |
---|
71 | |
---|
72 | static char Py_min_removal_doc[] = "compute zero crossing rate"; |
---|
73 | |
---|
74 | static PyObject * |
---|
75 | Py_min_removal(PyObject * self, PyObject * args) |
---|
76 | { |
---|
77 | PyObject *input; |
---|
78 | Py_fvec *vec; |
---|
79 | |
---|
80 | if (!PyArg_ParseTuple (args, "O:min_removal", &input)) { |
---|
81 | return NULL; |
---|
82 | } |
---|
83 | |
---|
84 | if (input == NULL) { |
---|
85 | return NULL; |
---|
86 | } |
---|
87 | |
---|
88 | vec = PyAubio_ArrayToFvec (input); |
---|
89 | |
---|
90 | if (vec == NULL) { |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | // compute the function |
---|
95 | fvec_min_removal (vec->o); |
---|
96 | // since this function does not return, we could return None |
---|
97 | //return Py_None; |
---|
98 | // however it is convenient to return the modified vector |
---|
99 | return (PyObject *) PyAubio_FvecToArray(vec); |
---|
100 | // or even without converting it back to an array |
---|
101 | //Py_INCREF(vec); |
---|
102 | //return (PyObject *)vec; |
---|
103 | } |
---|
104 | |
---|
105 | static PyMethodDef aubio_methods[] = { |
---|
106 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
107 | {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, |
---|
108 | Py_zero_crossing_rate_doc}, |
---|
109 | {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, |
---|
110 | {NULL, NULL} /* Sentinel */ |
---|
111 | }; |
---|
112 | |
---|
113 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
114 | |
---|
115 | PyMODINIT_FUNC |
---|
116 | init_aubio (void) |
---|
117 | { |
---|
118 | PyObject *m; |
---|
119 | int err; |
---|
120 | |
---|
121 | if ((PyType_Ready (&Py_fvecType) < 0) || |
---|
122 | (PyType_Ready (&Py_cvecType) < 0) || |
---|
123 | (PyType_Ready (&Py_filterType) < 0)) { |
---|
124 | return; |
---|
125 | } |
---|
126 | |
---|
127 | err = _import_array (); |
---|
128 | |
---|
129 | if (err != 0) { |
---|
130 | fprintf (stderr, |
---|
131 | "Unable to import Numpy C API from aubio module (error %d)\n", err); |
---|
132 | } |
---|
133 | |
---|
134 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
135 | |
---|
136 | if (m == NULL) { |
---|
137 | return; |
---|
138 | } |
---|
139 | |
---|
140 | Py_INCREF (&Py_fvecType); |
---|
141 | PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType); |
---|
142 | Py_INCREF (&Py_cvecType); |
---|
143 | PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); |
---|
144 | Py_INCREF (&Py_filterType); |
---|
145 | PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); |
---|
146 | } |
---|