1 #include "aubio-types.h"
18 static char Py_source_doc[] = ""
19 " __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
21 " Create a new source, opening the given path for reading.\n"
26 " Create a new source, using the original samplerate, with hop_size = 512:\n"
28 " >>> source('/tmp/t.wav')\n"
30 " Create a new source, resampling the original to 8000Hz:\n"
32 " >>> source('/tmp/t.wav', samplerate = 8000)\n"
34 " Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
36 " >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
38 " Create a new source, using its original samplerate:\n"
40 " >>> source('/tmp/t.wav', samplerate = 0)\n"
43 " vec, read = x() <==> vec, read = x.do()\n"
45 " Read vector from source.\n"
52 static char Py_source_get_samplerate_doc[] = ""
53 "x.get_samplerate() -> source samplerate\n"
55 "Get samplerate of source.";
57 static char Py_source_get_channels_doc[] = ""
58 "x.get_channels() -> number of channels\n"
60 "Get number of channels in source.";
62 static char Py_source_do_doc[] = ""
63 "vec, read = x.do() <==> vec, read = x()\n"
65 "Read monophonic vector from source.";
67 static char Py_source_do_multi_doc[] = ""
68 "mat, read = x.do_multi()\n"
70 "Read polyphonic vector from source.";
72 static char Py_source_close_doc[] = ""
75 "Close this source now.";
77 static char Py_source_seek_doc[] = ""
80 "Seek to resampled frame position.";
83 Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
87 uint_t samplerate = 0;
90 static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
92 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
93 &uri, &samplerate, &hop_size, &channels)) {
97 self = (Py_source *) pytype->tp_alloc (pytype, 0);
105 self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
106 strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
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");
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");
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");
136 return (PyObject *) self;
140 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
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
148 self->samplerate = aubio_source_get_samplerate ( self->o );
149 if (self->channels == 0) {
150 self->channels = aubio_source_get_channels ( self->o );
152 self->duration = aubio_source_get_duration ( self->o );
154 self->read_to = new_py_fvec(self->hop_size);
155 self->mread_to = new_py_fmat(self->channels, self->hop_size);
161 Py_source_del (Py_source *self, PyObject *unused)
164 del_aubio_source(self->o);
165 free(self->c_mread_to.data);
167 Py_XDECREF(self->read_to);
168 Py_XDECREF(self->mread_to);
169 Py_TYPE(self)->tp_free((PyObject *) self);
173 /* function Py_source_do */
175 Py_source_do(Py_source * self, PyObject * args)
181 Py_INCREF(self->read_to);
182 if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) {
185 /* compute _do function */
186 aubio_source_do (self->o, &(self->c_read_to), &read);
188 outputs = PyTuple_New(2);
189 PyTuple_SetItem( outputs, 0, self->read_to );
190 PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
194 /* function Py_source_do_multi */
196 Py_source_do_multi(Py_source * self, PyObject * args)
202 Py_INCREF(self->mread_to);
203 if (!PyAubio_ArrayToCFmat(self->mread_to, &(self->c_mread_to))) {
206 /* compute _do function */
207 aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
209 outputs = PyTuple_New(2);
210 PyTuple_SetItem( outputs, 0, self->mread_to);
211 PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
215 static PyMemberDef Py_source_members[] = {
216 {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
217 "path at which the source was created"},
218 {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
219 "samplerate at which the source is viewed"},
220 {"channels", T_INT, offsetof (Py_source, channels), READONLY,
221 "number of channels found in the source"},
222 {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
223 "number of consecutive frames that will be read at each do or do_multi call"},
224 {"duration", T_INT, offsetof (Py_source, duration), READONLY,
225 "total number of frames in the source (estimated)"},
230 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
232 uint_t tmp = aubio_source_get_samplerate (self->o);
233 return (PyObject *)PyLong_FromLong (tmp);
237 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
239 uint_t tmp = aubio_source_get_channels (self->o);
240 return (PyObject *)PyLong_FromLong (tmp);
244 Pyaubio_source_close (Py_source *self, PyObject *unused)
246 if (aubio_source_close(self->o) != 0) return NULL;
251 Pyaubio_source_seek (Py_source *self, PyObject *args)
256 if (!PyArg_ParseTuple (args, "I", &position)) {
261 PyErr_Format(PyExc_ValueError,
262 "error when seeking in source: can not seek to negative value %d",
267 err = aubio_source_seek(self->o, position);
269 PyErr_SetString (PyExc_ValueError,
270 "error when seeking in source");
276 static char Pyaubio_source_enter_doc[] = "";
277 static PyObject* Pyaubio_source_enter(Py_source *self, PyObject *unused) {
279 return (PyObject*)self;
282 static char Pyaubio_source_exit_doc[] = "";
283 static PyObject* Pyaubio_source_exit(Py_source *self, PyObject *unused) {
284 return Pyaubio_source_close(self, unused);
287 static PyObject* Pyaubio_source_iter(PyObject *self) {
289 return (PyObject*)self;
292 static PyObject* Pyaubio_source_iter_next(Py_source *self) {
293 PyObject *done, *size;
294 if (self->channels == 1) {
295 done = Py_source_do(self, NULL);
297 done = Py_source_do_multi(self, NULL);
299 if (!PyTuple_Check(done)) {
300 PyErr_Format(PyExc_ValueError,
301 "error when reading source: not opened?");
304 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);
309 } else if (PyLong_AsLong(size) > 0) {
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);
319 newdims.ptr[1] = PyLong_AsLong(size);
321 PyArray_Resize(shortread, &newdims, 1, NPY_ANYORDER);
322 return (PyObject*)shortread;
324 PyErr_SetNone(PyExc_StopIteration);
328 PyErr_SetNone(PyExc_StopIteration);
333 static 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 */
353 PyTypeObject Py_sourceType = {
354 PyVarObject_HEAD_INIT (NULL, 0)
358 (destructor) Py_source_del,
368 (ternaryfunc)Py_source_do,
380 (unaryfunc)Pyaubio_source_iter_next,
389 (initproc) Py_source_init,