1 #include "aubiowraphell.h"
15 static char Py_source_doc[] = ""
16 " __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
18 " Create a new source, opening the given path for reading.\n"
23 " Create a new source, using the original samplerate, with hop_size = 512:\n"
25 " >>> source('/tmp/t.wav')\n"
27 " Create a new source, resampling the original to 8000Hz:\n"
29 " >>> source('/tmp/t.wav', samplerate = 8000)\n"
31 " Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
33 " >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
35 " Create a new source, using its original samplerate:\n"
37 " >>> source('/tmp/t.wav', samplerate = 0)\n"
40 " vec, read = x() <==> vec, read = x.do()\n"
42 " Read vector from source.\n"
49 static char Py_source_get_samplerate_doc[] = ""
50 "x.get_samplerate() -> source samplerate\n"
52 "Get samplerate of source.";
54 static char Py_source_get_channels_doc[] = ""
55 "x.get_channels() -> number of channels\n"
57 "Get number of channels in source.";
59 static char Py_source_do_doc[] = ""
60 "vec, read = x.do() <==> vec, read = x()\n"
62 "Read monophonic vector from source.";
64 static char Py_source_do_multi_doc[] = ""
65 "mat, read = x.do_multi()\n"
67 "Read polyphonic vector from source.";
69 static char Py_source_close_doc[] = ""
72 "Close this source now.";
74 static char Py_source_seek_doc[] = ""
77 "Seek to resampled frame position.";
80 Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
84 uint_t samplerate = 0;
87 static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
89 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
90 &uri, &samplerate, &hop_size, &channels)) {
94 self = (Py_source *) pytype->tp_alloc (pytype, 0);
105 self->samplerate = 0;
106 if ((sint_t)samplerate > 0) {
107 self->samplerate = samplerate;
108 } else if ((sint_t)samplerate < 0) {
109 PyErr_SetString (PyExc_ValueError,
110 "can not use negative value for samplerate");
114 self->hop_size = Py_default_vector_length / 2;
115 if ((sint_t)hop_size > 0) {
116 self->hop_size = hop_size;
117 } else if ((sint_t)hop_size < 0) {
118 PyErr_SetString (PyExc_ValueError,
119 "can not use negative value for hop_size");
124 if ((sint_t)channels >= 0) {
125 self->channels = channels;
126 } else if ((sint_t)channels < 0) {
127 PyErr_SetString (PyExc_ValueError,
128 "can not use negative value for channels");
132 return (PyObject *) self;
136 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
138 self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
139 if (self->o == NULL) {
140 char_t errstr[30 + strlen(self->uri)];
141 sprintf(errstr, "error creating source with %s", self->uri);
142 PyErr_SetString (PyExc_RuntimeError, errstr);
145 self->samplerate = aubio_source_get_samplerate ( self->o );
146 if (self->channels == 0) {
147 self->channels = aubio_source_get_channels ( self->o );
150 self->read_to = new_fvec(self->hop_size);
151 self->mread_to = new_fmat (self->channels, self->hop_size);
157 Py_source_del (Py_source *self, PyObject *unused)
159 del_aubio_source(self->o);
160 del_fvec(self->read_to);
161 del_fmat(self->mread_to);
162 self->ob_type->tp_free((PyObject *) self);
166 /* function Py_source_do */
168 Py_source_do(Py_source * self, PyObject * args)
172 /* output vectors prototypes */
180 /* creating output read_to as a new_fvec of length self->hop_size */
184 /* compute _do function */
185 aubio_source_do (self->o, self->read_to, &read);
187 PyObject *outputs = PyList_New(0);
188 PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (self->read_to));
189 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
193 /* function Py_source_do_multi */
195 Py_source_do_multi(Py_source * self, PyObject * args)
199 /* output vectors prototypes */
207 /* creating output mread_to as a new_fvec of length self->hop_size */
211 /* compute _do function */
212 aubio_source_do_multi (self->o, self->mread_to, &read);
214 PyObject *outputs = PyList_New(0);
215 PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (self->mread_to));
216 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
220 AUBIO_MEMBERS_START(source)
221 {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
222 "path at which the source was created"},
223 {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
224 "samplerate at which the source is viewed"},
225 {"channels", T_INT, offsetof (Py_source, channels), READONLY,
226 "number of channels found in the source"},
227 {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
228 "number of consecutive frames that will be read at each do or do_multi call"},
229 AUBIO_MEMBERS_STOP(source)
233 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
235 uint_t tmp = aubio_source_get_samplerate (self->o);
236 return (PyObject *)PyInt_FromLong (tmp);
240 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
242 uint_t tmp = aubio_source_get_channels (self->o);
243 return (PyObject *)PyInt_FromLong (tmp);
247 Pyaubio_source_close (Py_source *self, PyObject *unused)
249 aubio_source_close (self->o);
254 Pyaubio_source_seek (Py_source *self, PyObject *args)
259 if (!PyArg_ParseTuple (args, "I", &position)) {
263 err = aubio_source_seek(self->o, position);
265 PyErr_SetString (PyExc_ValueError,
266 "error when seeking in source");
272 static PyMethodDef Py_source_methods[] = {
273 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
274 METH_NOARGS, Py_source_get_samplerate_doc},
275 {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
276 METH_NOARGS, Py_source_get_channels_doc},
277 {"do", (PyCFunction) Py_source_do,
278 METH_NOARGS, Py_source_do_doc},
279 {"do_multi", (PyCFunction) Py_source_do_multi,
280 METH_NOARGS, Py_source_do_multi_doc},
281 {"close", (PyCFunction) Pyaubio_source_close,
282 METH_NOARGS, Py_source_close_doc},
283 {"seek", (PyCFunction) Pyaubio_source_seek,
284 METH_VARARGS, Py_source_seek_doc},
285 {NULL} /* sentinel */
288 AUBIO_TYPEOBJECT(source, "aubio.source")