source: interfaces/python/py-fvec.c @ 352fd5f

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

python/py-fvec.c, python/aubio-types.g: define AUBIO_FLOAT, move default length and channels here, rename fvec to aubio.fvec

  • Property mode set to 100644
File size: 7.3 KB
Line 
1#include "aubio-types.h"
2
3/* fvec type definition
4
5class fvec():
6    def __init__(self, length = 1024, channels = 1):
7        self.length = length
8        self.channels = channels
9        self.data = array(length, channels)
10
11*/
12
13static char Py_fvec_doc[] = "fvec object";
14
15static PyObject *
16Py_fvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
17{
18  int length= 0, channels = 0;
19  Py_fvec *self;
20  static char *kwlist[] = { "length", "channels", NULL };
21
22  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
23          &length, &channels)) {
24    return NULL;
25  }
26
27
28  self = (Py_fvec *) type->tp_alloc (type, 0);
29
30  self->length = Py_fvec_default_length;
31  self->channels = Py_fvec_default_channels;
32
33  if (self == NULL) {
34    return NULL;
35  }
36
37  if (length > 0) {
38    self->length = length;
39  } else if (length < 0) {
40    PyErr_SetString (PyExc_ValueError,
41        "can not use negative number of elements");
42    return NULL;
43  }
44
45  if (channels > 0) {
46    self->channels = channels;
47  } else if (channels < 0) {
48    PyErr_SetString (PyExc_ValueError,
49        "can not use negative number of channels");
50    return NULL;
51  }
52
53
54  return (PyObject *) self;
55}
56
57static int
58Py_fvec_init (Py_fvec * self, PyObject * args, PyObject * kwds)
59{
60  self->o = new_fvec (self->length, self->channels);
61  if (self->o == NULL) {
62    return -1;
63  }
64
65  return 0;
66}
67
68static void
69Py_fvec_del (Py_fvec * self)
70{
71  del_fvec (self->o);
72  self->ob_type->tp_free ((PyObject *) self);
73}
74
75static PyObject *
76Py_fvec_repr (Py_fvec * self, PyObject * unused)
77{
78  PyObject *format = NULL;
79  PyObject *args = NULL;
80  PyObject *result = NULL;
81
82  format = PyString_FromString ("aubio fvec of %d elements with %d channels");
83  if (format == NULL) {
84    goto fail;
85  }
86
87  args = Py_BuildValue ("II", self->length, self->channels);
88  if (args == NULL) {
89    goto fail;
90  }
91
92  result = PyString_Format (format, args);
93
94fail:
95  Py_XDECREF (format);
96  Py_XDECREF (args);
97
98  return result;
99}
100
101static PyObject *
102Py_fvec_print (Py_fvec * self, PyObject * unused)
103{
104  fvec_print (self->o);
105  return Py_None;
106}
107
108PyObject *
109PyAubio_FvecToArray (Py_fvec * self)
110{
111  PyObject *array = NULL;
112  if (self->channels == 1) {
113    npy_intp dims[] = { self->length, 1 };
114    array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[0]);
115  } else {
116    uint_t i;
117    npy_intp dims[] = { self->length, 1 };
118    PyObject *concat = PyList_New (0), *tmp = NULL;
119    for (i = 0; i < self->channels; i++) {
120      tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[i]);
121      PyList_Append (concat, tmp);
122      Py_DECREF (tmp);
123    }
124    array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
125    Py_DECREF (concat);
126  }
127  return array;
128}
129
130static Py_ssize_t
131Py_fvec_getchannels (Py_fvec * self)
132{
133  return self->channels;
134}
135
136static PyObject *
137Py_fvec_getitem (Py_fvec * self, Py_ssize_t index)
138{
139  PyObject *array;
140
141  if (index < 0 || index >= self->channels) {
142    PyErr_SetString (PyExc_IndexError, "no such channel");
143    return NULL;
144  }
145
146  npy_intp dims[] = { self->length, 1 };
147  array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[index]);
148  return array;
149}
150
151static int
152Py_fvec_setitem (Py_fvec * self, Py_ssize_t index, PyObject * o)
153{
154  PyObject *array;
155
156  if (index < 0 || index >= self->channels) {
157    PyErr_SetString (PyExc_IndexError, "no such channel");
158    return -1;
159  }
160
161  array = PyArray_FROM_OT (o, NPY_FLOAT);
162  if (array == NULL) {
163    PyErr_SetString (PyExc_ValueError, "should be an array of float");
164    goto fail;
165  }
166
167  if (PyArray_NDIM (array) != 1) {
168    PyErr_SetString (PyExc_ValueError, "should be a one-dimensional array");
169    goto fail;
170  }
171
172  if (PyArray_SIZE (array) != self->length) {
173    PyErr_SetString (PyExc_ValueError,
174        "should be an array of same length as target fvec");
175    goto fail;
176  }
177
178  self->o->data[index] = (smpl_t *) PyArray_GETPTR1 (array, 0);
179
180  return 0;
181
182fail:
183  return -1;
184}
185
186static PyMemberDef Py_fvec_members[] = {
187  // TODO remove READONLY flag and define getter/setter
188  {"length", T_INT, offsetof (Py_fvec, length), READONLY,
189      "length attribute"},
190  {"channels", T_INT, offsetof (Py_fvec, channels), READONLY,
191      "channels attribute"},
192  {NULL}                        /* Sentinel */
193};
194
195static PyMethodDef Py_fvec_methods[] = {
196  {"dump", (PyCFunction) Py_fvec_print, METH_NOARGS,
197      "Dumps the contents of the vector to stdout."},
198  {"__array__", (PyCFunction) PyAubio_FvecToArray, METH_NOARGS,
199      "Returns the first channel as a numpy array."},
200  {NULL}
201};
202
203static PySequenceMethods Py_fvec_tp_as_sequence = {
204  (lenfunc) Py_fvec_getchannels,        /* sq_length         */
205  0,                                    /* sq_concat         */
206  0,                                    /* sq_repeat         */
207  (ssizeargfunc) Py_fvec_getitem,       /* sq_item           */
208  0,                                    /* sq_slice          */
209  (ssizeobjargproc) Py_fvec_setitem,    /* sq_ass_item       */
210  0,                                    /* sq_ass_slice      */
211  0,                                    /* sq_contains       */
212  0,                                    /* sq_inplace_concat */
213  0,                                    /* sq_inplace_repeat */
214};
215
216
217PyTypeObject Py_fvecType = {
218  PyObject_HEAD_INIT (NULL)
219  0,                            /* ob_size           */
220  "aubio.fvec",                 /* tp_name           */
221  sizeof (Py_fvec),             /* tp_basicsize      */
222  0,                            /* tp_itemsize       */
223  (destructor) Py_fvec_del,     /* tp_dealloc        */
224  0,                            /* tp_print          */
225  0,                            /* tp_getattr        */
226  0,                            /* tp_setattr        */
227  0,                            /* tp_compare        */
228  (reprfunc) Py_fvec_repr,      /* tp_repr           */
229  0,                            /* tp_as_number      */
230  &Py_fvec_tp_as_sequence,      /* tp_as_sequence    */
231  0,                            /* tp_as_mapping     */
232  0,                            /* tp_hash           */
233  0,                            /* tp_call           */
234  0,                            /* tp_str            */
235  0,                            /* tp_getattro       */
236  0,                            /* tp_setattro       */
237  0,                            /* tp_as_buffer      */
238  Py_TPFLAGS_DEFAULT,           /* tp_flags          */
239  Py_fvec_doc,                  /* tp_doc            */
240  0,                            /* tp_traverse       */
241  0,                            /* tp_clear          */
242  0,                            /* tp_richcompare    */
243  0,                            /* tp_weaklistoffset */
244  0,                            /* tp_iter           */
245  0,                            /* tp_iternext       */
246  Py_fvec_methods,              /* tp_methods        */
247  Py_fvec_members,              /* tp_members        */
248  0,                            /* tp_getset         */
249  0,                            /* tp_base           */
250  0,                            /* tp_dict           */
251  0,                            /* tp_descr_get      */
252  0,                            /* tp_descr_set      */
253  0,                            /* tp_dictoffset     */
254  (initproc) Py_fvec_init,      /* tp_init           */
255  0,                            /* tp_alloc          */
256  Py_fvec_new,                  /* tp_new            */
257};
Note: See TracBrowser for help on using the repository browser.