source: python/py-cvec.c @ 25c9f9a

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 25c9f9a was 25c9f9a, checked in by Paul Brossier <piem@piem.org>, 11 years ago

move new python module to the top

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include "aubio-types.h"
2
3/* cvec type definition
4
5class cvec():
6    def __init__(self, length = 1024):
7        self.length = length
8        self.norm = array(length)
9        self.phas = array(length)
10
11*/
12
13static char Py_cvec_doc[] = "cvec object";
14
15static PyObject *
16Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
17{
18  int length= 0;
19  Py_cvec *self;
20  static char *kwlist[] = { "length", NULL };
21
22  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
23          &length)) {
24    return NULL;
25  }
26
27
28  self = (Py_cvec *) type->tp_alloc (type, 0);
29
30  self->length = Py_default_vector_length / 2 + 1;
31
32  if (self == NULL) {
33    return NULL;
34  }
35
36  if (length > 0) {
37    self->length = length / 2 + 1;
38  } else if (length < 0) {
39    PyErr_SetString (PyExc_ValueError,
40        "can not use negative number of elements");
41    return NULL;
42  }
43
44  return (PyObject *) self;
45}
46
47static int
48Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
49{
50  self->o = new_cvec ((self->length - 1) * 2);
51  if (self->o == NULL) {
52    return -1;
53  }
54
55  return 0;
56}
57
58static void
59Py_cvec_del (Py_cvec * self)
60{
61  del_cvec (self->o);
62  self->ob_type->tp_free ((PyObject *) self);
63}
64
65static PyObject *
66Py_cvec_repr (Py_cvec * self, PyObject * unused)
67{
68  PyObject *format = NULL;
69  PyObject *args = NULL;
70  PyObject *result = NULL;
71
72  format = PyString_FromString ("aubio cvec of %d elements");
73  if (format == NULL) {
74    goto fail;
75  }
76
77  args = Py_BuildValue ("I", self->length);
78  if (args == NULL) {
79    goto fail;
80  }
81  cvec_print ( self->o );
82
83  result = PyString_Format (format, args);
84
85fail:
86  Py_XDECREF (format);
87  Py_XDECREF (args);
88
89  return result;
90}
91
92PyObject *
93PyAubio_CvecNormToArray (Py_cvec * self)
94{
95  npy_intp dims[] = { self->o->length, 1 };
96  return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm);
97}
98
99
100PyObject *
101PyAubio_CvecPhasToArray (Py_cvec * self)
102{
103  npy_intp dims[] = { self->o->length, 1 };
104  return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas);
105}
106
107PyObject *
108PyAubio_ArrayToCvecPhas (PyObject * self)
109{
110  return NULL;
111}
112
113PyObject *
114Py_cvec_get_norm (Py_cvec * self, void *closure)
115{
116  return PyAubio_CvecNormToArray(self);
117}
118
119PyObject *
120Py_cvec_get_phas (Py_cvec * self, void *closure)
121{
122  return PyAubio_CvecPhasToArray(self);
123}
124
125static int
126Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
127{
128  PyArrayObject * array;
129  if (input == NULL) {
130    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
131    goto fail;
132  }
133  if (PyArray_Check(input)) {
134
135    // we got an array, convert it to a cvec.norm
136    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
137      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
138      goto fail;
139    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
140      PyErr_SetString (PyExc_ValueError,
141          "input array has more than two dimensions");
142      goto fail;
143    }
144
145    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
146      PyErr_SetString (PyExc_ValueError, "input array should be float");
147      goto fail;
148    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
149      PyErr_SetString (PyExc_ValueError, "input array should be float32");
150      goto fail;
151    }
152    array = (PyArrayObject *)input;
153
154    // check input array dimensions
155    if (PyArray_NDIM (array) != 1) {
156      PyErr_Format (PyExc_ValueError,
157          "input array has %d dimensions, not 1",
158          PyArray_NDIM (array));
159      goto fail;
160    } else {
161      if (vec->o->length != PyArray_SIZE (array)) {
162          PyErr_Format (PyExc_ValueError,
163                  "input array has length %d, but cvec has length %d",
164                  (int)PyArray_SIZE (array), vec->o->length);
165          goto fail;
166      }
167    }
168
169    vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0);
170
171  } else {
172    PyErr_SetString (PyExc_ValueError, "can only accept array as input");
173    return 1;
174  }
175
176  Py_INCREF(array);
177  return 0;
178
179fail:
180  return 1;
181}
182
183static int
184Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
185{
186  PyArrayObject * array;
187  if (input == NULL) {
188    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
189    goto fail;
190  }
191  if (PyArray_Check(input)) {
192
193    // we got an array, convert it to a cvec.phas
194    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
195      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
196      goto fail;
197    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
198      PyErr_SetString (PyExc_ValueError,
199          "input array has more than two dimensions");
200      goto fail;
201    }
202
203    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
204      PyErr_SetString (PyExc_ValueError, "input array should be float");
205      goto fail;
206    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
207      PyErr_SetString (PyExc_ValueError, "input array should be float32");
208      goto fail;
209    }
210    array = (PyArrayObject *)input;
211
212    // check input array dimensions
213    if (PyArray_NDIM (array) != 1) {
214      PyErr_Format (PyExc_ValueError,
215          "input array has %d dimensions, not 1",
216          PyArray_NDIM (array));
217      goto fail;
218    } else {
219      if (vec->o->length != PyArray_SIZE (array)) {
220          PyErr_Format (PyExc_ValueError,
221                  "input array has length %d, but cvec has length %d",
222                  (int)PyArray_SIZE (array), vec->o->length);
223          goto fail;
224      }
225    }
226
227    vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0);
228
229  } else {
230    PyErr_SetString (PyExc_ValueError, "can only accept array as input");
231    return 1;
232  }
233
234  Py_INCREF(array);
235  return 0;
236
237fail:
238  return 1;
239}
240
241static PyMemberDef Py_cvec_members[] = {
242  // TODO remove READONLY flag and define getter/setter
243  {"length", T_INT, offsetof (Py_cvec, length), READONLY,
244      "length attribute"},
245  {NULL}                        /* Sentinel */
246};
247
248static PyMethodDef Py_cvec_methods[] = {
249  {NULL}
250};
251
252static PyGetSetDef Py_cvec_getseters[] = {
253  {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm, 
254      "Numpy vector of shape (length,) containing the magnitude",
255      NULL},
256  {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas, 
257      "Numpy vector of shape (length,) containing the phase",
258      NULL},
259  {NULL} /* sentinel */
260};
261
262PyTypeObject Py_cvecType = {
263  PyObject_HEAD_INIT (NULL)
264  0,                            /* ob_size           */
265  "aubio.cvec",                 /* tp_name           */
266  sizeof (Py_cvec),             /* tp_basicsize      */
267  0,                            /* tp_itemsize       */
268  (destructor) Py_cvec_del,     /* tp_dealloc        */
269  0,                            /* tp_print          */
270  0,                            /* tp_getattr        */
271  0,                            /* tp_setattr        */
272  0,                            /* tp_compare        */
273  (reprfunc) Py_cvec_repr,      /* tp_repr           */
274  0,                            /* tp_as_number      */
275  0, //&Py_cvec_tp_as_sequence,      /* tp_as_sequence    */
276  0,                            /* tp_as_mapping     */
277  0,                            /* tp_hash           */
278  0,                            /* tp_call           */
279  0,                            /* tp_str            */
280  0,                            /* tp_getattro       */
281  0,                            /* tp_setattro       */
282  0,                            /* tp_as_buffer      */
283  Py_TPFLAGS_DEFAULT,           /* tp_flags          */
284  Py_cvec_doc,                  /* tp_doc            */
285  0,                            /* tp_traverse       */
286  0,                            /* tp_clear          */
287  0,                            /* tp_richcompare    */
288  0,                            /* tp_weaklistoffset */
289  0,                            /* tp_iter           */
290  0,                            /* tp_iternext       */
291  Py_cvec_methods,              /* tp_methods        */
292  Py_cvec_members,              /* tp_members        */
293  Py_cvec_getseters,            /* tp_getset         */
294  0,                            /* tp_base           */
295  0,                            /* tp_dict           */
296  0,                            /* tp_descr_get      */
297  0,                            /* tp_descr_set      */
298  0,                            /* tp_dictoffset     */
299  (initproc) Py_cvec_init,      /* tp_init           */
300  0,                            /* tp_alloc          */
301  Py_cvec_new,                  /* tp_new            */
302};
Note: See TracBrowser for help on using the repository browser.