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.";
73 Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
77 uint_t samplerate = 0;
80 static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
82 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
83 &uri, &samplerate, &hop_size, &channels)) {
87 self = (Py_source *) pytype->tp_alloc (pytype, 0);
99 if ((sint_t)samplerate > 0) {
100 self->samplerate = samplerate;
101 } else if ((sint_t)samplerate < 0) {
102 PyErr_SetString (PyExc_ValueError,
103 "can not use negative value for samplerate");
107 self->hop_size = Py_default_vector_length / 2;
108 if ((sint_t)hop_size > 0) {
109 self->hop_size = hop_size;
110 } else if ((sint_t)hop_size < 0) {
111 PyErr_SetString (PyExc_ValueError,
112 "can not use negative value for hop_size");
117 if ((sint_t)channels >= 0) {
118 self->channels = channels;
119 } else if ((sint_t)channels < 0) {
120 PyErr_SetString (PyExc_ValueError,
121 "can not use negative value for channels");
125 return (PyObject *) self;
129 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
131 self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
132 if (self->o == NULL) {
133 char_t errstr[30 + strlen(self->uri)];
134 sprintf(errstr, "error creating source with %s", self->uri);
135 PyErr_SetString (PyExc_StandardError, errstr);
138 self->samplerate = aubio_source_get_samplerate ( self->o );
139 if (self->channels == 0) {
140 self->channels = aubio_source_get_channels ( self->o );
148 /* function Py_source_do */
150 Py_source_do(Py_source * self, PyObject * args)
154 /* output vectors prototypes */
163 /* creating output read_to as a new_fvec of length self->hop_size */
164 read_to = new_fvec (self->hop_size);
168 /* compute _do function */
169 aubio_source_do (self->o, read_to, &read);
171 PyObject *outputs = PyList_New(0);
172 PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to));
173 //del_fvec (read_to);
174 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
178 /* function Py_source_do_multi */
180 Py_source_do_multi(Py_source * self, PyObject * args)
184 /* output vectors prototypes */
193 /* creating output read_to as a new_fvec of length self->hop_size */
194 read_to = new_fmat (self->channels, self->hop_size);
198 /* compute _do function */
199 aubio_source_do_multi (self->o, read_to, &read);
201 PyObject *outputs = PyList_New(0);
202 PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (read_to));
203 //del_fvec (read_to);
204 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
208 AUBIO_MEMBERS_START(source)
209 {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
210 "path at which the source was created"},
211 {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
212 "samplerate at which the source is viewed"},
213 {"channels", T_INT, offsetof (Py_source, channels), READONLY,
214 "number of channels found in the source"},
215 {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
216 "number of consecutive frames that will be read at each do or do_multi call"},
217 AUBIO_MEMBERS_STOP(source)
221 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
223 uint_t tmp = aubio_source_get_samplerate (self->o);
224 return (PyObject *)PyInt_FromLong (tmp);
228 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
230 uint_t tmp = aubio_source_get_channels (self->o);
231 return (PyObject *)PyInt_FromLong (tmp);
235 Pyaubio_source_close (Py_source *self, PyObject *unused)
237 aubio_source_close (self->o);
241 static PyMethodDef Py_source_methods[] = {
242 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
243 METH_NOARGS, Py_source_get_samplerate_doc},
244 {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
245 METH_NOARGS, Py_source_get_channels_doc},
246 {"do", (PyCFunction) Py_source_do,
247 METH_NOARGS, Py_source_do_doc},
248 {"do_multi", (PyCFunction) Py_source_do_multi,
249 METH_NOARGS, Py_source_do_multi_doc},
250 {"close", (PyCFunction) Pyaubio_source_close,
251 METH_NOARGS, Py_source_close_doc},
252 {NULL} /* sentinel */
255 AUBIO_TYPEOBJECT(source, "aubio.source")