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 | Py_fvec * |
---|
8 | PyAubio_ArrayToFvec (PyObject *input) { |
---|
9 | PyObject *array; |
---|
10 | Py_fvec *vec; |
---|
11 | uint_t i; |
---|
12 | // parsing input object into a Py_fvec |
---|
13 | if (PyObject_TypeCheck (input, &Py_fvecType)) { |
---|
14 | // input is an fvec, nothing else to do |
---|
15 | vec = (Py_fvec *) input; |
---|
16 | } else if (PyArray_Check(input)) { |
---|
17 | |
---|
18 | // we got an array, convert it to an fvec |
---|
19 | if (PyArray_NDIM (input) == 0) { |
---|
20 | PyErr_SetString (PyExc_ValueError, "input array is a scalar"); |
---|
21 | goto fail; |
---|
22 | } else if (PyArray_NDIM (input) > 2) { |
---|
23 | PyErr_SetString (PyExc_ValueError, |
---|
24 | "input array has more than two dimensions"); |
---|
25 | goto fail; |
---|
26 | } |
---|
27 | |
---|
28 | if (!PyArray_ISFLOAT (input)) { |
---|
29 | PyErr_SetString (PyExc_ValueError, "input array should be float"); |
---|
30 | goto fail; |
---|
31 | #if AUBIO_DO_CASTING |
---|
32 | } else if (PyArray_TYPE (input) != AUBIO_FLOAT) { |
---|
33 | // input data type is not float32, casting |
---|
34 | array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT); |
---|
35 | if (array == NULL) { |
---|
36 | PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT"); |
---|
37 | goto fail; |
---|
38 | } |
---|
39 | #else |
---|
40 | } else if (PyArray_TYPE (input) != AUBIO_FLOAT) { |
---|
41 | PyErr_SetString (PyExc_ValueError, "input array should be float32"); |
---|
42 | goto fail; |
---|
43 | #endif |
---|
44 | } else { |
---|
45 | // input data type is float32, nothing else to do |
---|
46 | array = input; |
---|
47 | } |
---|
48 | |
---|
49 | // create a new fvec object |
---|
50 | vec = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType); |
---|
51 | if (PyArray_NDIM (array) == 1) { |
---|
52 | vec->channels = 1; |
---|
53 | vec->length = PyArray_SIZE (array); |
---|
54 | } else { |
---|
55 | vec->channels = PyArray_DIM (array, 0); |
---|
56 | vec->length = PyArray_DIM (array, 1); |
---|
57 | } |
---|
58 | |
---|
59 | // no need to really allocate fvec, just its struct member |
---|
60 | // vec->o = new_fvec (vec->length, vec->channels); |
---|
61 | vec->o = (fvec_t *)malloc(sizeof(fvec_t)); |
---|
62 | vec->o->length = vec->length; vec->o->channels = vec->channels; |
---|
63 | vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*)); |
---|
64 | // hat data[i] point to array line |
---|
65 | for (i = 0; i < vec->channels; i++) { |
---|
66 | vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i); |
---|
67 | } |
---|
68 | |
---|
69 | } else { |
---|
70 | PyErr_SetString (PyExc_ValueError, "can only accept array or fvec as input"); |
---|
71 | return NULL; |
---|
72 | } |
---|
73 | |
---|
74 | return vec; |
---|
75 | |
---|
76 | fail: |
---|
77 | return NULL; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | static char Py_alpha_norm_doc[] = "compute alpha normalisation factor"; |
---|
83 | |
---|
84 | static PyObject * |
---|
85 | Py_alpha_norm (PyObject * self, PyObject * args) |
---|
86 | { |
---|
87 | PyObject *input; |
---|
88 | Py_fvec *vec; |
---|
89 | smpl_t alpha; |
---|
90 | PyObject *result; |
---|
91 | |
---|
92 | if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) { |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | if (input == NULL) { |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | |
---|
100 | vec = PyAubio_ArrayToFvec (input); |
---|
101 | |
---|
102 | if (vec == NULL) { |
---|
103 | return NULL; |
---|
104 | } |
---|
105 | |
---|
106 | // compute the function |
---|
107 | result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha)); |
---|
108 | if (result == NULL) { |
---|
109 | return NULL; |
---|
110 | } |
---|
111 | |
---|
112 | return result; |
---|
113 | } |
---|
114 | |
---|
115 | static PyMethodDef aubio_methods[] = { |
---|
116 | {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, |
---|
117 | {NULL, NULL} /* Sentinel */ |
---|
118 | }; |
---|
119 | |
---|
120 | static char aubio_module_doc[] = "Python module for the aubio library"; |
---|
121 | |
---|
122 | PyMODINIT_FUNC |
---|
123 | init_aubio (void) |
---|
124 | { |
---|
125 | PyObject *m; |
---|
126 | int err; |
---|
127 | |
---|
128 | if (PyType_Ready (&Py_fvecType) < 0) { |
---|
129 | return; |
---|
130 | } |
---|
131 | |
---|
132 | err = _import_array (); |
---|
133 | |
---|
134 | if (err != 0) { |
---|
135 | fprintf (stderr, |
---|
136 | "Unable to import Numpy C API from aubio module (error %d)\n", err); |
---|
137 | } |
---|
138 | |
---|
139 | m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc); |
---|
140 | |
---|
141 | if (m == NULL) { |
---|
142 | return; |
---|
143 | } |
---|
144 | |
---|
145 | Py_INCREF (&Py_fvecType); |
---|
146 | PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType); |
---|
147 | } |
---|