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);
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");
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");
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");
135 return (PyObject *) self;
139 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
141 self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
142 if (self->o == NULL) {
143 PyErr_Format (PyExc_RuntimeError, "error creating source with \"%s\"",
147 self->samplerate = aubio_source_get_samplerate ( self->o );
148 if (self->channels == 0) {
149 self->channels = aubio_source_get_channels ( self->o );
151 self->duration = aubio_source_get_duration ( self->o );
153 self->read_to = new_py_fvec(self->hop_size);
154 self->mread_to = new_py_fmat(self->channels, self->hop_size);
160 Py_source_del (Py_source *self, PyObject *unused)
163 del_aubio_source(self->o);
164 free(self->c_mread_to.data);
166 Py_XDECREF(self->read_to);
167 Py_XDECREF(self->mread_to);
168 Py_TYPE(self)->tp_free((PyObject *) self);
172 /* function Py_source_do */
174 Py_source_do(Py_source * self, PyObject * args)
180 Py_INCREF(self->read_to);
181 if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) {
184 /* compute _do function */
185 aubio_source_do (self->o, &(self->c_read_to), &read);
187 outputs = PyTuple_New(2);
188 PyTuple_SetItem( outputs, 0, self->read_to );
189 PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
193 /* function Py_source_do_multi */
195 Py_source_do_multi(Py_source * self, PyObject * args)
201 Py_INCREF(self->mread_to);
202 if (!PyAubio_ArrayToCFmat(self->mread_to, &(self->c_mread_to))) {
205 /* compute _do function */
206 aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
208 outputs = PyTuple_New(2);
209 PyTuple_SetItem( outputs, 0, self->mread_to);
210 PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
214 static 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)"},
229 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
231 uint_t tmp = aubio_source_get_samplerate (self->o);
232 return (PyObject *)PyLong_FromLong (tmp);
236 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
238 uint_t tmp = aubio_source_get_channels (self->o);
239 return (PyObject *)PyLong_FromLong (tmp);
243 Pyaubio_source_close (Py_source *self, PyObject *unused)
245 aubio_source_close (self->o);
250 Pyaubio_source_seek (Py_source *self, PyObject *args)
255 if (!PyArg_ParseTuple (args, "I", &position)) {
259 err = aubio_source_seek(self->o, position);
261 PyErr_SetString (PyExc_ValueError,
262 "error when seeking in source");
268 static PyMethodDef Py_source_methods[] = {
269 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
270 METH_NOARGS, Py_source_get_samplerate_doc},
271 {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
272 METH_NOARGS, Py_source_get_channels_doc},
273 {"do", (PyCFunction) Py_source_do,
274 METH_NOARGS, Py_source_do_doc},
275 {"do_multi", (PyCFunction) Py_source_do_multi,
276 METH_NOARGS, Py_source_do_multi_doc},
277 {"close", (PyCFunction) Pyaubio_source_close,
278 METH_NOARGS, Py_source_close_doc},
279 {"seek", (PyCFunction) Pyaubio_source_seek,
280 METH_VARARGS, Py_source_seek_doc},
281 {NULL} /* sentinel */
284 PyTypeObject Py_sourceType = {
285 PyVarObject_HEAD_INIT (NULL, 0)
289 (destructor) Py_source_del,
299 (ternaryfunc)Py_source_do,
320 (initproc) Py_source_init,