1 | #include "aubio-types.h" |
---|
2 | |
---|
3 | /* cvec type definition |
---|
4 | |
---|
5 | class cvec(): |
---|
6 | def __new__(self, length = 1024): |
---|
7 | self.length = length / 2 + 1 |
---|
8 | self.norm = np.zeros(length / 2 + 1) |
---|
9 | self.phas = np.zeros(length / 2 + 1) |
---|
10 | |
---|
11 | */ |
---|
12 | |
---|
13 | // special python type for cvec |
---|
14 | typedef struct |
---|
15 | { |
---|
16 | PyObject_HEAD |
---|
17 | PyObject *norm; |
---|
18 | PyObject *phas; |
---|
19 | uint_t length; |
---|
20 | } Py_cvec; |
---|
21 | |
---|
22 | static char Py_cvec_doc[] = "" |
---|
23 | "cvec(size)\n" |
---|
24 | "\n" |
---|
25 | "Data structure to hold spectral vectors.\n" |
---|
26 | "\n" |
---|
27 | "A vector holding spectral data in two vectors, :attr:`phas`\n" |
---|
28 | "and :attr:`norm`. Each vector is a :class:`numpy.ndarray`\n" |
---|
29 | "of shape `(length,)`, where `length = size // 2 + 1`.\n" |
---|
30 | "\n" |
---|
31 | "Parameters\n" |
---|
32 | "----------\n" |
---|
33 | "size: int\n" |
---|
34 | " Size of spectrum to create.\n" |
---|
35 | "\n" |
---|
36 | "Examples\n" |
---|
37 | "--------\n" |
---|
38 | ">>> c = aubio.cvec(1024)\n" |
---|
39 | ">>> c\n" |
---|
40 | "aubio cvec of 513 elements\n" |
---|
41 | ">>> c.length\n" |
---|
42 | "513\n" |
---|
43 | ">>> c.norm.dtype, c.phas.dtype\n" |
---|
44 | "(dtype('float32'), dtype('float32'))\n" |
---|
45 | ">>> c.norm.shape, c.phas.shape\n" |
---|
46 | "((513,), (513,))\n" |
---|
47 | "\n" |
---|
48 | "See Also\n" |
---|
49 | "--------\n" |
---|
50 | "fft, pvoc\n" |
---|
51 | ""; |
---|
52 | |
---|
53 | |
---|
54 | PyObject * |
---|
55 | new_py_cvec(uint_t length) { |
---|
56 | Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); |
---|
57 | npy_intp dims[] = { length / 2 + 1, 1 }; |
---|
58 | vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
59 | vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
60 | vec->length = length / 2 + 1; |
---|
61 | return (PyObject*)vec; |
---|
62 | } |
---|
63 | |
---|
64 | int |
---|
65 | PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) { |
---|
66 | if (PyObject_TypeCheck (input, &Py_cvecType)) { |
---|
67 | Py_cvec * in = (Py_cvec *)input; |
---|
68 | i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0); |
---|
69 | i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0); |
---|
70 | i->length = ((Py_cvec*)input)->length; |
---|
71 | return 1; |
---|
72 | } else { |
---|
73 | PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec"); |
---|
74 | return 0; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | static PyObject * |
---|
79 | Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) |
---|
80 | { |
---|
81 | int length= 0; |
---|
82 | Py_cvec *self; |
---|
83 | static char *kwlist[] = { "length", NULL }; |
---|
84 | |
---|
85 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist, |
---|
86 | &length)) { |
---|
87 | return NULL; |
---|
88 | } |
---|
89 | |
---|
90 | self = (Py_cvec *) type->tp_alloc (type, 0); |
---|
91 | |
---|
92 | self->length = Py_default_vector_length / 2 + 1; |
---|
93 | |
---|
94 | if (self == NULL) { |
---|
95 | return NULL; |
---|
96 | } |
---|
97 | |
---|
98 | if (length > 0) { |
---|
99 | self->length = length / 2 + 1; |
---|
100 | } else if (length < 0) { |
---|
101 | PyErr_SetString (PyExc_ValueError, |
---|
102 | "can not use negative number of elements"); |
---|
103 | return NULL; |
---|
104 | } |
---|
105 | |
---|
106 | return (PyObject *) self; |
---|
107 | } |
---|
108 | |
---|
109 | static int |
---|
110 | Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds) |
---|
111 | { |
---|
112 | npy_intp dims[] = { self->length, 1 }; |
---|
113 | self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
114 | self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0); |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | static void |
---|
119 | Py_cvec_del (Py_cvec * self) |
---|
120 | { |
---|
121 | Py_DECREF(self->norm); |
---|
122 | Py_DECREF(self->phas); |
---|
123 | Py_TYPE(self)->tp_free ((PyObject *) self); |
---|
124 | } |
---|
125 | |
---|
126 | static PyObject * |
---|
127 | Py_cvec_repr (Py_cvec * self, PyObject * unused) |
---|
128 | { |
---|
129 | PyObject *format = NULL; |
---|
130 | PyObject *args = NULL; |
---|
131 | PyObject *result = NULL; |
---|
132 | |
---|
133 | format = PyUnicode_FromString ("aubio cvec of %d elements"); |
---|
134 | if (format == NULL) { |
---|
135 | goto fail; |
---|
136 | } |
---|
137 | |
---|
138 | args = Py_BuildValue ("I", self->length); |
---|
139 | if (args == NULL) { |
---|
140 | goto fail; |
---|
141 | } |
---|
142 | // hide actual norm / phas content |
---|
143 | |
---|
144 | result = PyUnicode_Format (format, args); |
---|
145 | |
---|
146 | fail: |
---|
147 | Py_XDECREF (format); |
---|
148 | Py_XDECREF (args); |
---|
149 | |
---|
150 | return result; |
---|
151 | } |
---|
152 | |
---|
153 | PyObject * |
---|
154 | Py_cvec_get_norm (Py_cvec * self, void *closure) |
---|
155 | { |
---|
156 | // we want self->norm to still exist after our caller return it |
---|
157 | Py_INCREF(self->norm); |
---|
158 | return (PyObject*)(self->norm); |
---|
159 | } |
---|
160 | |
---|
161 | PyObject * |
---|
162 | Py_cvec_get_phas (Py_cvec * self, void *closure) |
---|
163 | { |
---|
164 | // we want self->phas to still exist after our caller return it |
---|
165 | Py_INCREF(self->phas); |
---|
166 | return (PyObject *)(self->phas); |
---|
167 | } |
---|
168 | |
---|
169 | static int |
---|
170 | Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) |
---|
171 | { |
---|
172 | npy_intp length; |
---|
173 | if (!PyAubio_IsValidVector(input)) { |
---|
174 | return -1; |
---|
175 | } |
---|
176 | length = PyArray_SIZE ((PyArrayObject *)input); |
---|
177 | if (length != vec->length) { |
---|
178 | PyErr_Format (PyExc_ValueError, |
---|
179 | "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length, |
---|
180 | vec->length); |
---|
181 | return -1; |
---|
182 | } |
---|
183 | |
---|
184 | Py_XDECREF(vec->norm); |
---|
185 | vec->norm = input; |
---|
186 | Py_INCREF(vec->norm); |
---|
187 | return 0; |
---|
188 | } |
---|
189 | |
---|
190 | static int |
---|
191 | Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) |
---|
192 | { |
---|
193 | npy_intp length; |
---|
194 | if (!PyAubio_IsValidVector(input)) { |
---|
195 | return -1; |
---|
196 | } |
---|
197 | length = PyArray_SIZE ((PyArrayObject *)input); |
---|
198 | if (length != vec->length) { |
---|
199 | PyErr_Format (PyExc_ValueError, |
---|
200 | "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length, |
---|
201 | vec->length); |
---|
202 | return -1; |
---|
203 | } |
---|
204 | |
---|
205 | Py_XDECREF(vec->phas); |
---|
206 | vec->phas = input; |
---|
207 | Py_INCREF(vec->phas); |
---|
208 | return 0; |
---|
209 | } |
---|
210 | |
---|
211 | static PyMemberDef Py_cvec_members[] = { |
---|
212 | // TODO remove READONLY flag and define getter/setter |
---|
213 | {"length", T_INT, offsetof (Py_cvec, length), READONLY, |
---|
214 | "int: Length of `norm` and `phas` vectors."}, |
---|
215 | {NULL} /* Sentinel */ |
---|
216 | }; |
---|
217 | |
---|
218 | static PyMethodDef Py_cvec_methods[] = { |
---|
219 | {NULL} |
---|
220 | }; |
---|
221 | |
---|
222 | static PyGetSetDef Py_cvec_getseters[] = { |
---|
223 | {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, |
---|
224 | "numpy.ndarray: Vector of shape `(length,)` containing the magnitude.", |
---|
225 | NULL}, |
---|
226 | {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, |
---|
227 | "numpy.ndarray: Vector of shape `(length,)` containing the phase.", |
---|
228 | NULL}, |
---|
229 | {NULL} /* sentinel */ |
---|
230 | }; |
---|
231 | |
---|
232 | PyTypeObject Py_cvecType = { |
---|
233 | PyVarObject_HEAD_INIT(NULL, 0) |
---|
234 | "aubio.cvec", /* tp_name */ |
---|
235 | sizeof (Py_cvec), /* tp_basicsize */ |
---|
236 | 0, /* tp_itemsize */ |
---|
237 | (destructor) Py_cvec_del, /* tp_dealloc */ |
---|
238 | 0, /* tp_print */ |
---|
239 | 0, /* tp_getattr */ |
---|
240 | 0, /* tp_setattr */ |
---|
241 | 0, /* tp_compare */ |
---|
242 | (reprfunc) Py_cvec_repr, /* tp_repr */ |
---|
243 | 0, /* tp_as_number */ |
---|
244 | 0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence */ |
---|
245 | 0, /* tp_as_mapping */ |
---|
246 | 0, /* tp_hash */ |
---|
247 | 0, /* tp_call */ |
---|
248 | 0, /* tp_str */ |
---|
249 | 0, /* tp_getattro */ |
---|
250 | 0, /* tp_setattro */ |
---|
251 | 0, /* tp_as_buffer */ |
---|
252 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
253 | Py_cvec_doc, /* tp_doc */ |
---|
254 | 0, /* tp_traverse */ |
---|
255 | 0, /* tp_clear */ |
---|
256 | 0, /* tp_richcompare */ |
---|
257 | 0, /* tp_weaklistoffset */ |
---|
258 | 0, /* tp_iter */ |
---|
259 | 0, /* tp_iternext */ |
---|
260 | Py_cvec_methods, /* tp_methods */ |
---|
261 | Py_cvec_members, /* tp_members */ |
---|
262 | Py_cvec_getseters, /* tp_getset */ |
---|
263 | 0, /* tp_base */ |
---|
264 | 0, /* tp_dict */ |
---|
265 | 0, /* tp_descr_get */ |
---|
266 | 0, /* tp_descr_set */ |
---|
267 | 0, /* tp_dictoffset */ |
---|
268 | (initproc) Py_cvec_init, /* tp_init */ |
---|
269 | 0, /* tp_alloc */ |
---|
270 | Py_cvec_new, /* tp_new */ |
---|
271 | 0, |
---|
272 | 0, |
---|
273 | 0, |
---|
274 | 0, |
---|
275 | 0, |
---|
276 | 0, |
---|
277 | 0, |
---|
278 | 0, |
---|
279 | 0, |
---|
280 | }; |
---|