1 #include "aubio-types.h"
14 static char Py_sink_doc[] = ""
15 " __new__(path, samplerate = 44100, channels = 1)\n"
17 " Create a new sink, opening the given path for writing.\n"
22 " Create a new sink at 44100Hz, mono:\n"
24 " >>> sink('/tmp/t.wav')\n"
26 " Create a new sink at 8000Hz, mono:\n"
28 " >>> sink('/tmp/t.wav', samplerate = 8000)\n"
30 " Create a new sink at 32000Hz, stereo:\n"
32 " >>> sink('/tmp/t.wav', samplerate = 32000, channels = 2)\n"
34 " Create a new sink at 32000Hz, 5 channels:\n"
36 " >>> sink('/tmp/t.wav', channels = 5, samplerate = 32000)\n"
38 " __call__(vec, write)\n"
39 " x(vec,write) <==> x.do(vec, write)\n"
41 " Write vector to sink.\n"
48 static char Py_sink_do_doc[] = ""
49 "x.do(vec, write) <==> x(vec, write)\n"
51 "write monophonic vector to sink";
53 static char Py_sink_do_multi_doc[] = ""
54 "x.do_multi(mat, write)\n"
56 "write polyphonic vector to sink";
58 static char Py_sink_close_doc[] = ""
61 "close this sink now";
64 Py_sink_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
68 uint_t samplerate = 0;
70 static char *kwlist[] = { "uri", "samplerate", "channels", NULL };
72 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
73 &uri, &samplerate, &channels)) {
77 self = (Py_sink *) pytype->tp_alloc (pytype, 0);
85 self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
86 strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
89 self->samplerate = Py_aubio_default_samplerate;
90 if (samplerate != 0) {
91 self->samplerate = samplerate;
96 self->channels = channels;
99 return (PyObject *) self;
103 Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds)
105 self->o = new_aubio_sink ( self->uri, 0 );
106 if (self->o == NULL) {
107 // error string was set in new_aubio_sink
110 if (aubio_sink_preset_channels(self->o, self->channels) != 0) {
111 // error string was set in aubio_sink_preset_channels
114 if (aubio_sink_preset_samplerate(self->o, self->samplerate) != 0) {
115 // error string was set in aubio_sink_preset_samplerate
119 self->samplerate = aubio_sink_get_samplerate ( self->o );
120 self->channels = aubio_sink_get_channels ( self->o );
126 Py_sink_del (Py_sink *self, PyObject *unused)
128 del_aubio_sink(self->o);
129 free(self->mwrite_data.data);
133 Py_TYPE(self)->tp_free((PyObject *) self);
136 /* function Py_sink_do */
138 Py_sink_do(Py_sink * self, PyObject * args)
140 /* input vectors python prototypes */
141 PyObject * write_data_obj;
143 /* input vectors prototypes */
147 if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
151 /* input vectors parsing */
152 if (!PyAubio_ArrayToCFvec(write_data_obj, &(self->write_data))) {
157 /* compute _do function */
158 aubio_sink_do (self->o, &(self->write_data), write);
163 /* function Py_sink_do_multi */
165 Py_sink_do_multi(Py_sink * self, PyObject * args)
167 /* input vectors python prototypes */
168 PyObject * write_data_obj;
170 /* input vectors prototypes */
174 if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
179 /* input vectors parsing */
180 if (!PyAubio_ArrayToCFmat(write_data_obj, &(self->mwrite_data))) {
184 /* compute _do function */
185 aubio_sink_do_multi (self->o, &(self->mwrite_data), write);
189 static PyMemberDef Py_sink_members[] = {
190 {"uri", T_STRING, offsetof (Py_sink, uri), READONLY,
191 "path at which the sink was created"},
192 {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY,
193 "samplerate at which the sink was created"},
194 {"channels", T_INT, offsetof (Py_sink, channels), READONLY,
195 "number of channels with which the sink was created"},
200 Pyaubio_sink_close (Py_sink *self, PyObject *unused)
202 aubio_sink_close (self->o);
206 static char Pyaubio_sink_enter_doc[] = "";
207 static PyObject* Pyaubio_sink_enter(Py_sink *self, PyObject *unused) {
209 return (PyObject*)self;
212 static char Pyaubio_sink_exit_doc[] = "";
213 static PyObject* Pyaubio_sink_exit(Py_sink *self, PyObject *unused) {
214 return Pyaubio_sink_close(self, unused);
217 static PyMethodDef Py_sink_methods[] = {
218 {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc},
219 {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc},
220 {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc},
221 {"__enter__", (PyCFunction)Pyaubio_sink_enter, METH_NOARGS,
222 Pyaubio_sink_enter_doc},
223 {"__exit__", (PyCFunction)Pyaubio_sink_exit, METH_VARARGS,
224 Pyaubio_sink_exit_doc},
225 {NULL} /* sentinel */
228 PyTypeObject Py_sinkType = {
229 PyVarObject_HEAD_INIT (NULL, 0)
233 (destructor) Py_sink_del,
243 (ternaryfunc)Py_sink_do,
264 (initproc) Py_sink_init,