1 #include "aubiowraphell.h"
13 static char Py_source_doc[] = ""
14 " __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
16 " Create a new source, opening the given path for reading.\n"
21 " Create a new source, using the original samplerate, with hop_size = 512:\n"
23 " >>> source('/tmp/t.wav')\n"
25 " Create a new source, resampling the original to 8000Hz:\n"
27 " >>> source('/tmp/t.wav', samplerate = 8000)\n"
29 " Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
31 " >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
33 " Create a new source, using its original samplerate:\n"
35 " >>> source('/tmp/t.wav', samplerate = 0)\n"
38 " vec, read = x() <==> vec, read = x.do()\n"
40 " Read vector from source.\n"
47 static char Py_source_get_samplerate_doc[] = ""
48 "x.get_samplerate() -> source samplerate\n"
50 "Get samplerate of source.";
52 static char Py_source_get_channels_doc[] = ""
53 "x.get_channels() -> number of channels\n"
55 "Get number of channels in source.";
57 static char Py_source_do_doc[] = ""
58 "vec, read = x.do() <==> vec, read = x()\n"
60 "Read monophonic vector from source.";
62 static char Py_source_do_multi_doc[] = ""
63 "mat, read = x.do_multi()\n"
65 "Read polyphonic vector from source.";
67 static char Py_source_close_doc[] = ""
70 "Close this source now.";
72 static char Py_source_seek_doc[] = ""
75 "Seek to resampled frame position.";
78 Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
82 uint_t samplerate = 0;
85 static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
87 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
88 &uri, &samplerate, &hop_size, &channels)) {
92 self = (Py_source *) pytype->tp_alloc (pytype, 0);
103 self->samplerate = 0;
104 if ((sint_t)samplerate > 0) {
105 self->samplerate = samplerate;
106 } else if ((sint_t)samplerate < 0) {
107 PyErr_SetString (PyExc_ValueError,
108 "can not use negative value for samplerate");
112 self->hop_size = Py_default_vector_length / 2;
113 if ((sint_t)hop_size > 0) {
114 self->hop_size = hop_size;
115 } else if ((sint_t)hop_size < 0) {
116 PyErr_SetString (PyExc_ValueError,
117 "can not use negative value for hop_size");
122 if ((sint_t)channels >= 0) {
123 self->channels = channels;
124 } else if ((sint_t)channels < 0) {
125 PyErr_SetString (PyExc_ValueError,
126 "can not use negative value for channels");
130 return (PyObject *) self;
134 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
136 self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
137 if (self->o == NULL) {
138 char_t errstr[30 + strlen(self->uri)];
139 sprintf(errstr, "error creating source with %s", self->uri);
140 PyErr_SetString (PyExc_StandardError, errstr);
143 self->samplerate = aubio_source_get_samplerate ( self->o );
144 if (self->channels == 0) {
145 self->channels = aubio_source_get_channels ( self->o );
153 /* function Py_source_do */
155 Py_source_do(Py_source * self, PyObject * args)
159 /* output vectors prototypes */
168 /* creating output read_to as a new_fvec of length self->hop_size */
169 read_to = new_fvec (self->hop_size);
173 /* compute _do function */
174 aubio_source_do (self->o, read_to, &read);
176 PyObject *outputs = PyList_New(0);
177 PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to));
178 //del_fvec (read_to);
179 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
183 /* function Py_source_do_multi */
185 Py_source_do_multi(Py_source * self, PyObject * args)
189 /* output vectors prototypes */
198 /* creating output read_to as a new_fvec of length self->hop_size */
199 read_to = new_fmat (self->channels, self->hop_size);
203 /* compute _do function */
204 aubio_source_do_multi (self->o, read_to, &read);
206 PyObject *outputs = PyList_New(0);
207 PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (read_to));
208 //del_fvec (read_to);
209 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
213 AUBIO_MEMBERS_START(source)
214 {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
215 "path at which the source was created"},
216 {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
217 "samplerate at which the source is viewed"},
218 {"channels", T_INT, offsetof (Py_source, channels), READONLY,
219 "number of channels found in the source"},
220 {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
221 "number of consecutive frames that will be read at each do or do_multi call"},
222 AUBIO_MEMBERS_STOP(source)
226 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
228 uint_t tmp = aubio_source_get_samplerate (self->o);
229 return (PyObject *)PyInt_FromLong (tmp);
233 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
235 uint_t tmp = aubio_source_get_channels (self->o);
236 return (PyObject *)PyInt_FromLong (tmp);
240 Pyaubio_source_close (Py_source *self, PyObject *unused)
242 aubio_source_close (self->o);
247 Pyaubio_source_seek (Py_source *self, PyObject *args)
252 if (!PyArg_ParseTuple (args, "I", &position)) {
256 err = aubio_source_seek(self->o, position);
258 PyErr_SetString (PyExc_ValueError,
259 "error when seeking in source");
265 static PyMethodDef Py_source_methods[] = {
266 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
267 METH_NOARGS, Py_source_get_samplerate_doc},
268 {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
269 METH_NOARGS, Py_source_get_channels_doc},
270 {"do", (PyCFunction) Py_source_do,
271 METH_NOARGS, Py_source_do_doc},
272 {"do_multi", (PyCFunction) Py_source_do_multi,
273 METH_NOARGS, Py_source_do_multi_doc},
274 {"close", (PyCFunction) Pyaubio_source_close,
275 METH_NOARGS, Py_source_close_doc},
276 {"seek", (PyCFunction) Pyaubio_source_seek,
277 METH_VARARGS, Py_source_seek_doc},
278 {NULL} /* sentinel */
281 AUBIO_TYPEOBJECT(source, "aubio.source")