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;
79 static char *kwlist[] = { "uri", "samplerate", "hop_size", NULL };
81 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
82 &uri, &samplerate, &hop_size)) {
86 self = (Py_source *) pytype->tp_alloc (pytype, 0);
98 if ((sint_t)samplerate > 0) {
99 self->samplerate = samplerate;
100 } else if ((sint_t)samplerate < 0) {
101 PyErr_SetString (PyExc_ValueError,
102 "can not use negative value for samplerate");
106 self->hop_size = Py_default_vector_length / 2;
107 if ((sint_t)hop_size > 0) {
108 self->hop_size = hop_size;
109 } else if ((sint_t)hop_size < 0) {
110 PyErr_SetString (PyExc_ValueError,
111 "can not use negative value for hop_size");
115 return (PyObject *) self;
119 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
121 self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
122 if (self->o == NULL) {
123 PyErr_SetString (PyExc_StandardError, "error creating object");
126 self->samplerate = aubio_source_get_samplerate ( self->o );
127 self->channels = aubio_source_get_channels ( self->o );
134 /* function Py_source_do */
136 Py_source_do(Py_source * self, PyObject * args)
140 /* output vectors prototypes */
149 /* creating output read_to as a new_fvec of length self->hop_size */
150 read_to = new_fvec (self->hop_size);
154 /* compute _do function */
155 aubio_source_do (self->o, read_to, &read);
157 PyObject *outputs = PyList_New(0);
158 PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to));
159 //del_fvec (read_to);
160 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
164 /* function Py_source_do_multi */
166 Py_source_do_multi(Py_source * self, PyObject * args)
170 /* output vectors prototypes */
179 /* creating output read_to as a new_fvec of length self->hop_size */
180 read_to = new_fmat (self->channels, self->hop_size);
184 /* compute _do function */
185 aubio_source_do_multi (self->o, read_to, &read);
187 PyObject *outputs = PyList_New(0);
188 PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (read_to));
189 //del_fvec (read_to);
190 PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
194 AUBIO_MEMBERS_START(source)
195 {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
196 "path at which the source was created"},
197 {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
198 "samplerate at which the source is viewed"},
199 {"channels", T_INT, offsetof (Py_source, channels), READONLY,
200 "number of channels found in the source"},
201 {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
202 "number of consecutive frames that will be read at each do or do_multi call"},
203 AUBIO_MEMBERS_STOP(source)
207 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
209 uint_t tmp = aubio_source_get_samplerate (self->o);
210 return (PyObject *)PyInt_FromLong (tmp);
214 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
216 uint_t tmp = aubio_source_get_channels (self->o);
217 return (PyObject *)PyInt_FromLong (tmp);
221 Pyaubio_source_close (Py_source *self, PyObject *unused)
223 aubio_source_close (self->o);
227 static PyMethodDef Py_source_methods[] = {
228 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
229 METH_NOARGS, Py_source_get_samplerate_doc},
230 {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
231 METH_NOARGS, Py_source_get_channels_doc},
232 {"do", (PyCFunction) Py_source_do,
233 METH_NOARGS, Py_source_do_doc},
234 {"do_multi", (PyCFunction) Py_source_do_multi,
235 METH_NOARGS, Py_source_do_multi_doc},
236 {"close", (PyCFunction) Pyaubio_source_close,
237 METH_NOARGS, Py_source_close_doc},
238 {NULL} /* sentinel */
241 AUBIO_TYPEOBJECT(source, "aubio.source")