source: python/ext/py-source.c @ 23be736

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

python/ext/py-source.c: raise on closing file failed

  • Property mode set to 100644
File size: 9.8 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 = "none";
104  if (uri != NULL) {
105    self->uri = uri;
106  }
107
108  self->samplerate = 0;
109  if ((sint_t)samplerate > 0) {
110    self->samplerate = samplerate;
111  } else if ((sint_t)samplerate < 0) {
112    PyErr_SetString (PyExc_ValueError,
113        "can not use negative value for samplerate");
114    return NULL;
115  }
116
117  self->hop_size = Py_default_vector_length / 2;
118  if ((sint_t)hop_size > 0) {
119    self->hop_size = hop_size;
120  } else if ((sint_t)hop_size < 0) {
121    PyErr_SetString (PyExc_ValueError,
122        "can not use negative value for hop_size");
123    return NULL;
124  }
125
126  self->channels = 1;
127  if ((sint_t)channels >= 0) {
128    self->channels = channels;
129  } else if ((sint_t)channels < 0) {
130    PyErr_SetString (PyExc_ValueError,
131        "can not use negative value for channels");
132    return NULL;
133  }
134
135  return (PyObject *) self;
136}
137
138static int
139Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
140{
141  self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
142  if (self->o == NULL) {
143    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
144    // AUBIO_ERR when failing
145    return -1;
146  }
147  self->samplerate = aubio_source_get_samplerate ( self->o );
148  if (self->channels == 0) {
149    self->channels = aubio_source_get_channels ( self->o );
150  }
151  self->duration = aubio_source_get_duration ( self->o );
152
153  self->read_to = new_py_fvec(self->hop_size);
154  self->mread_to = new_py_fmat(self->channels, self->hop_size);
155
156  return 0;
157}
158
159static void
160Py_source_del (Py_source *self, PyObject *unused)
161{
162  if (self->o) {
163    del_aubio_source(self->o);
164    free(self->c_mread_to.data);
165  }
166  Py_XDECREF(self->read_to);
167  Py_XDECREF(self->mread_to);
168  Py_TYPE(self)->tp_free((PyObject *) self);
169}
170
171
172/* function Py_source_do */
173static PyObject *
174Py_source_do(Py_source * self, PyObject * args)
175{
176  PyObject *outputs;
177  uint_t read;
178  read = 0;
179
180  Py_INCREF(self->read_to);
181  if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) {
182    return NULL;
183  }
184  /* compute _do function */
185  aubio_source_do (self->o, &(self->c_read_to), &read);
186
187  outputs = PyTuple_New(2);
188  PyTuple_SetItem( outputs, 0, self->read_to );
189  PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
190  return outputs;
191}
192
193/* function Py_source_do_multi */
194static PyObject *
195Py_source_do_multi(Py_source * self, PyObject * args)
196{
197  PyObject *outputs;
198  uint_t read;
199  read = 0;
200
201  Py_INCREF(self->mread_to);
202  if (!PyAubio_ArrayToCFmat(self->mread_to,  &(self->c_mread_to))) {
203    return NULL;
204  }
205  /* compute _do function */
206  aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
207
208  outputs = PyTuple_New(2);
209  PyTuple_SetItem( outputs, 0, self->mread_to);
210  PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
211  return outputs;
212}
213
214static PyMemberDef Py_source_members[] = {
215  {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
216    "path at which the source was created"},
217  {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
218    "samplerate at which the source is viewed"},
219  {"channels", T_INT, offsetof (Py_source, channels), READONLY,
220    "number of channels found in the source"},
221  {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
222    "number of consecutive frames that will be read at each do or do_multi call"},
223  {"duration", T_INT, offsetof (Py_source, duration), READONLY,
224    "total number of frames in the source (estimated)"},
225  { NULL } // sentinel
226};
227
228static PyObject *
229Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
230{
231  uint_t tmp = aubio_source_get_samplerate (self->o);
232  return (PyObject *)PyLong_FromLong (tmp);
233}
234
235static PyObject *
236Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
237{
238  uint_t tmp = aubio_source_get_channels (self->o);
239  return (PyObject *)PyLong_FromLong (tmp);
240}
241
242static PyObject *
243Pyaubio_source_close (Py_source *self, PyObject *unused)
244{
245  if (aubio_source_close(self->o) != 0) return NULL;
246  Py_RETURN_NONE;
247}
248
249static PyObject *
250Pyaubio_source_seek (Py_source *self, PyObject *args)
251{
252  uint_t err = 0;
253
254  int position;
255  if (!PyArg_ParseTuple (args, "I", &position)) {
256    return NULL;
257  }
258
259  if (position < 0) {
260    PyErr_Format(PyExc_ValueError,
261        "error when seeking in source: can not seek to negative value %d",
262        position);
263    return NULL;
264  }
265
266  err = aubio_source_seek(self->o, position);
267  if (err != 0) {
268    PyErr_SetString (PyExc_ValueError,
269        "error when seeking in source");
270    return NULL;
271  }
272  Py_RETURN_NONE;
273}
274
275static char Pyaubio_source_enter_doc[] = "";
276static PyObject* Pyaubio_source_enter(Py_source *self, PyObject *unused) {
277  Py_INCREF(self);
278  return (PyObject*)self;
279}
280
281static char Pyaubio_source_exit_doc[] = "";
282static PyObject* Pyaubio_source_exit(Py_source *self, PyObject *unused) {
283  Pyaubio_source_close(self, unused);
284  return Pyaubio_source_close(self, unused);
285}
286
287static PyObject* Pyaubio_source_iter(PyObject *self) {
288  Py_INCREF(self);
289  return (PyObject*)self;
290}
291
292static PyObject* Pyaubio_source_iter_next(Py_source *self) {
293  PyObject *done;
294  if (self->channels == 1) {
295    done = Py_source_do(self, NULL);
296  } else {
297    done = Py_source_do_multi(self, NULL);
298  }
299  if (!PyTuple_Check(done)) {
300    PyErr_Format(PyExc_ValueError,
301        "error when reading source: not opened?");
302    return NULL;
303  }
304  PyObject *size = PyTuple_GetItem(done, 1);
305  if (size != NULL && PyLong_Check(size)) {
306    if (PyLong_AsLong(size) == (long)self->hop_size) {
307      PyObject *vec = PyTuple_GetItem(done, 0);
308      return vec;
309    } else if (PyLong_AsLong(size) > 0) {
310      // short read
311      PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0);
312      PyArray_Dims newdims;
313      newdims.len = PyArray_NDIM(shortread);
314      newdims.ptr = PyArray_DIMS(shortread);
315      // mono or multiple channels?
316      if (newdims.len == 1) {
317        newdims.ptr[0] = PyLong_AsLong(size);
318      } else {
319        newdims.ptr[1] = PyLong_AsLong(size);
320      }
321      PyArray_Resize(shortread, &newdims, 1, NPY_ANYORDER);
322      return (PyObject*)shortread;
323    } else {
324      PyErr_SetNone(PyExc_StopIteration);
325      return NULL;
326    }
327  } else {
328    PyErr_SetNone(PyExc_StopIteration);
329    return NULL;
330  }
331}
332
333static PyMethodDef Py_source_methods[] = {
334  {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
335    METH_NOARGS, Py_source_get_samplerate_doc},
336  {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
337    METH_NOARGS, Py_source_get_channels_doc},
338  {"do", (PyCFunction) Py_source_do,
339    METH_NOARGS, Py_source_do_doc},
340  {"do_multi", (PyCFunction) Py_source_do_multi,
341    METH_NOARGS, Py_source_do_multi_doc},
342  {"close", (PyCFunction) Pyaubio_source_close,
343    METH_NOARGS, Py_source_close_doc},
344  {"seek", (PyCFunction) Pyaubio_source_seek,
345    METH_VARARGS, Py_source_seek_doc},
346  {"__enter__", (PyCFunction)Pyaubio_source_enter, METH_NOARGS,
347    Pyaubio_source_enter_doc},
348  {"__exit__",  (PyCFunction)Pyaubio_source_exit, METH_VARARGS,
349    Pyaubio_source_exit_doc},
350  {NULL} /* sentinel */
351};
352
353PyTypeObject Py_sourceType = {
354  PyVarObject_HEAD_INIT (NULL, 0)
355  "aubio.source",
356  sizeof (Py_source),
357  0,
358  (destructor) Py_source_del,
359  0,
360  0,
361  0,
362  0,
363  0,
364  0,
365  0,
366  0,
367  0,
368  (ternaryfunc)Py_source_do,
369  0,
370  0,
371  0,
372  0,
373  Py_TPFLAGS_DEFAULT,
374  Py_source_doc,
375  0,
376  0,
377  0,
378  0,
379  Pyaubio_source_iter,
380  (unaryfunc)Pyaubio_source_iter_next,
381  Py_source_methods,
382  Py_source_members,
383  0,
384  0,
385  0,
386  0,
387  0,
388  0,
389  (initproc) Py_source_init,
390  0,
391  Py_source_new,
392  0,
393  0,
394  0,
395  0,
396  0,
397  0,
398  0,
399  0,
400  0,
401};
Note: See TracBrowser for help on using the repository browser.