source: python/ext/py-source.c @ fc359e0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since fc359e0 was 8b7cdba, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/ext/py-source.c: use PyArray_NewShape, not _Resize

  • Property mode set to 100644
File size: 10.0 KB
Line 
1#include "aubio-types.h"
2
3typedef struct
4{
5  PyObject_HEAD
6  aubio_source_t * o;
7  char_t* uri;
8  uint_t samplerate;
9  uint_t channels;
10  uint_t hop_size;
11  uint_t duration;
12  PyObject *read_to;
13  fvec_t c_read_to;
14  PyObject *mread_to;
15  fmat_t c_mread_to;
16} Py_source;
17
18static char Py_source_doc[] = ""
19"   __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
20"\n"
21"       Create a new source, opening the given path for reading.\n"
22"\n"
23"       Examples\n"
24"       --------\n"
25"\n"
26"       Create a new source, using the original samplerate, with hop_size = 512:\n"
27"\n"
28"       >>> source('/tmp/t.wav')\n"
29"\n"
30"       Create a new source, resampling the original to 8000Hz:\n"
31"\n"
32"       >>> source('/tmp/t.wav', samplerate = 8000)\n"
33"\n"
34"       Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
35"\n"
36"       >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
37"\n"
38"       Create a new source, using its original samplerate:\n"
39"\n"
40"       >>> source('/tmp/t.wav', samplerate = 0)\n"
41"\n"
42"   __call__()\n"
43"       vec, read = x() <==> vec, read = x.do()\n"
44"\n"
45"       Read vector from source.\n"
46"\n"
47"       See also\n"
48"       --------\n"
49"       aubio.source.do\n"
50"\n";
51
52static char Py_source_get_samplerate_doc[] = ""
53"x.get_samplerate() -> source samplerate\n"
54"\n"
55"Get samplerate of source.";
56
57static char Py_source_get_channels_doc[] = ""
58"x.get_channels() -> number of channels\n"
59"\n"
60"Get number of channels in source.";
61
62static char Py_source_do_doc[] = ""
63"vec, read = x.do() <==> vec, read = x()\n"
64"\n"
65"Read monophonic vector from source.";
66
67static char Py_source_do_multi_doc[] = ""
68"mat, read = x.do_multi()\n"
69"\n"
70"Read polyphonic vector from source.";
71
72static char Py_source_close_doc[] = ""
73"x.close()\n"
74"\n"
75"Close this source now.";
76
77static char Py_source_seek_doc[] = ""
78"x.seek(position)\n"
79"\n"
80"Seek to resampled frame position.";
81
82static PyObject *
83Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
84{
85  Py_source *self;
86  char_t* uri = NULL;
87  uint_t samplerate = 0;
88  uint_t hop_size = 0;
89  uint_t channels = 0;
90  static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
91
92  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
93          &uri, &samplerate, &hop_size, &channels)) {
94    return NULL;
95  }
96
97  self = (Py_source *) pytype->tp_alloc (pytype, 0);
98
99  if (self == NULL) {
100    return NULL;
101  }
102
103  self->uri = NULL;
104  if (uri != NULL) {
105    self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
106    strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
107  }
108
109  self->samplerate = 0;
110  if ((sint_t)samplerate > 0) {
111    self->samplerate = samplerate;
112  } else if ((sint_t)samplerate < 0) {
113    PyErr_SetString (PyExc_ValueError,
114        "can not use negative value for samplerate");
115    return NULL;
116  }
117
118  self->hop_size = Py_default_vector_length / 2;
119  if ((sint_t)hop_size > 0) {
120    self->hop_size = hop_size;
121  } else if ((sint_t)hop_size < 0) {
122    PyErr_SetString (PyExc_ValueError,
123        "can not use negative value for hop_size");
124    return NULL;
125  }
126
127  self->channels = 1;
128  if ((sint_t)channels >= 0) {
129    self->channels = channels;
130  } else if ((sint_t)channels < 0) {
131    PyErr_SetString (PyExc_ValueError,
132        "can not use negative value for channels");
133    return NULL;
134  }
135
136  return (PyObject *) self;
137}
138
139static int
140Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
141{
142  self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
143  if (self->o == NULL) {
144    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
145    // AUBIO_ERR when failing
146    return -1;
147  }
148  self->samplerate = aubio_source_get_samplerate ( self->o );
149  if (self->channels == 0) {
150    self->channels = aubio_source_get_channels ( self->o );
151  }
152  self->duration = aubio_source_get_duration ( self->o );
153
154  self->read_to = new_py_fvec(self->hop_size);
155  self->mread_to = new_py_fmat(self->channels, self->hop_size);
156
157  return 0;
158}
159
160static void
161Py_source_del (Py_source *self, PyObject *unused)
162{
163  if (self->o) {
164    del_aubio_source(self->o);
165    free(self->c_mread_to.data);
166  }
167  if (self->uri) {
168    free(self->uri);
169  }
170  Py_XDECREF(self->read_to);
171  Py_XDECREF(self->mread_to);
172  Py_TYPE(self)->tp_free((PyObject *) self);
173}
174
175
176/* function Py_source_do */
177static PyObject *
178Py_source_do(Py_source * self, PyObject * args)
179{
180  PyObject *outputs;
181  uint_t read;
182  read = 0;
183
184  Py_INCREF(self->read_to);
185  if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) {
186    return NULL;
187  }
188  /* compute _do function */
189  aubio_source_do (self->o, &(self->c_read_to), &read);
190
191  outputs = PyTuple_New(2);
192  PyTuple_SetItem( outputs, 0, self->read_to );
193  PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
194  return outputs;
195}
196
197/* function Py_source_do_multi */
198static PyObject *
199Py_source_do_multi(Py_source * self, PyObject * args)
200{
201  PyObject *outputs;
202  uint_t read;
203  read = 0;
204
205  Py_INCREF(self->mread_to);
206  if (!PyAubio_ArrayToCFmat(self->mread_to,  &(self->c_mread_to))) {
207    return NULL;
208  }
209  /* compute _do function */
210  aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
211
212  outputs = PyTuple_New(2);
213  PyTuple_SetItem( outputs, 0, self->mread_to);
214  PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
215  return outputs;
216}
217
218static PyMemberDef Py_source_members[] = {
219  {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
220    "path at which the source was created"},
221  {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
222    "samplerate at which the source is viewed"},
223  {"channels", T_INT, offsetof (Py_source, channels), READONLY,
224    "number of channels found in the source"},
225  {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
226    "number of consecutive frames that will be read at each do or do_multi call"},
227  {"duration", T_INT, offsetof (Py_source, duration), READONLY,
228    "total number of frames in the source (estimated)"},
229  { NULL } // sentinel
230};
231
232static PyObject *
233Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
234{
235  uint_t tmp = aubio_source_get_samplerate (self->o);
236  return (PyObject *)PyLong_FromLong (tmp);
237}
238
239static PyObject *
240Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
241{
242  uint_t tmp = aubio_source_get_channels (self->o);
243  return (PyObject *)PyLong_FromLong (tmp);
244}
245
246static PyObject *
247Pyaubio_source_close (Py_source *self, PyObject *unused)
248{
249  if (aubio_source_close(self->o) != 0) return NULL;
250  Py_RETURN_NONE;
251}
252
253static PyObject *
254Pyaubio_source_seek (Py_source *self, PyObject *args)
255{
256  uint_t err = 0;
257
258  int position;
259  if (!PyArg_ParseTuple (args, "I", &position)) {
260    return NULL;
261  }
262
263  if (position < 0) {
264    PyErr_Format(PyExc_ValueError,
265        "error when seeking in source: can not seek to negative value %d",
266        position);
267    return NULL;
268  }
269
270  err = aubio_source_seek(self->o, position);
271  if (err != 0) {
272    PyErr_SetString (PyExc_ValueError,
273        "error when seeking in source");
274    return NULL;
275  }
276  Py_RETURN_NONE;
277}
278
279static char Pyaubio_source_enter_doc[] = "";
280static PyObject* Pyaubio_source_enter(Py_source *self, PyObject *unused) {
281  Py_INCREF(self);
282  return (PyObject*)self;
283}
284
285static char Pyaubio_source_exit_doc[] = "";
286static PyObject* Pyaubio_source_exit(Py_source *self, PyObject *unused) {
287  return Pyaubio_source_close(self, unused);
288}
289
290static PyObject* Pyaubio_source_iter(PyObject *self) {
291  Py_INCREF(self);
292  return (PyObject*)self;
293}
294
295static PyObject* Pyaubio_source_iter_next(Py_source *self) {
296  PyObject *done, *size;
297  if (self->channels == 1) {
298    done = Py_source_do(self, NULL);
299  } else {
300    done = Py_source_do_multi(self, NULL);
301  }
302  if (!PyTuple_Check(done)) {
303    PyErr_Format(PyExc_ValueError,
304        "error when reading source: not opened?");
305    return NULL;
306  }
307  size = PyTuple_GetItem(done, 1);
308  if (size != NULL && PyLong_Check(size)) {
309    if (PyLong_AsLong(size) == (long)self->hop_size) {
310      PyObject *vec = PyTuple_GetItem(done, 0);
311      return vec;
312    } else if (PyLong_AsLong(size) > 0) {
313      // short read, return a shorter array
314      PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0);
315      PyArray_Dims newdims;
316      PyObject *reshaped;
317      newdims.len = PyArray_NDIM(shortread);
318      newdims.ptr = PyArray_DIMS(shortread);
319      // mono or multiple channels?
320      if (newdims.len == 1) {
321        newdims.ptr[0] = PyLong_AsLong(size);
322      } else {
323        newdims.ptr[1] = PyLong_AsLong(size);
324      }
325      reshaped = PyArray_Newshape(shortread, &newdims, NPY_CORDER);
326      Py_DECREF(shortread);
327      return reshaped;
328    } else {
329      PyErr_SetNone(PyExc_StopIteration);
330      return NULL;
331    }
332  } else {
333    PyErr_SetNone(PyExc_StopIteration);
334    return NULL;
335  }
336}
337
338static PyMethodDef Py_source_methods[] = {
339  {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
340    METH_NOARGS, Py_source_get_samplerate_doc},
341  {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
342    METH_NOARGS, Py_source_get_channels_doc},
343  {"do", (PyCFunction) Py_source_do,
344    METH_NOARGS, Py_source_do_doc},
345  {"do_multi", (PyCFunction) Py_source_do_multi,
346    METH_NOARGS, Py_source_do_multi_doc},
347  {"close", (PyCFunction) Pyaubio_source_close,
348    METH_NOARGS, Py_source_close_doc},
349  {"seek", (PyCFunction) Pyaubio_source_seek,
350    METH_VARARGS, Py_source_seek_doc},
351  {"__enter__", (PyCFunction)Pyaubio_source_enter, METH_NOARGS,
352    Pyaubio_source_enter_doc},
353  {"__exit__",  (PyCFunction)Pyaubio_source_exit, METH_VARARGS,
354    Pyaubio_source_exit_doc},
355  {NULL} /* sentinel */
356};
357
358PyTypeObject Py_sourceType = {
359  PyVarObject_HEAD_INIT (NULL, 0)
360  "aubio.source",
361  sizeof (Py_source),
362  0,
363  (destructor) Py_source_del,
364  0,
365  0,
366  0,
367  0,
368  0,
369  0,
370  0,
371  0,
372  0,
373  (ternaryfunc)Py_source_do,
374  0,
375  0,
376  0,
377  0,
378  Py_TPFLAGS_DEFAULT,
379  Py_source_doc,
380  0,
381  0,
382  0,
383  0,
384  Pyaubio_source_iter,
385  (unaryfunc)Pyaubio_source_iter_next,
386  Py_source_methods,
387  Py_source_members,
388  0,
389  0,
390  0,
391  0,
392  0,
393  0,
394  (initproc) Py_source_init,
395  0,
396  Py_source_new,
397  0,
398  0,
399  0,
400  0,
401  0,
402  0,
403  0,
404  0,
405  0,
406};
Note: See TracBrowser for help on using the repository browser.