1 #include "aubio-types.h"
14 static char Py_sink_doc[] = ""
15 "sink(path, samplerate=44100, channels=1)\n"
17 "Open `path` to write a WAV file.\n"
22 " Pathname of the file to be opened for writing.\n"
24 " Sampling rate of the file, in Hz.\n"
26 " Number of channels to create the file with.\n"
31 "Create a new sink at 44100Hz, mono:\n"
33 ">>> snk = aubio.sink('out.wav')\n"
35 "Create a new sink at 32000Hz, stereo, write 100 samples into it:\n"
37 ">>> snk = aubio.sink('out.wav', samplerate=16000, channels=3)\n"
38 ">>> snk(aubio.fvec(100), 100)\n"
40 "Open a new sink at 48000Hz, stereo, write `1234` samples into it:\n"
42 ">>> with aubio.sink('out.wav', samplerate=48000, channels=2) as src:\n"
43 "... snk(aubio.fvec(1024), 1024)\n"
44 "... snk(aubio.fvec(210), 210)\n"
49 "source: read audio samples from a file.\n"
52 static char Py_sink_do_doc[] = ""
55 "Write a single channel vector to sink.\n"
60 " input vector `(n,)` where `n >= 0`.\n"
62 " Number of samples to write.\n"
65 static char Py_sink_do_multi_doc[] = ""
66 "do_multi(mat, write)\n"
68 "Write a matrix containing vectors from multiple channels to sink.\n"
72 "mat : numpy.ndarray\n"
73 " input matrix of shape `(channels, n)`, where `n >= 0`.\n"
75 " Number of frames to write.\n"
78 static char Py_sink_close_doc[] = ""
81 "Close this sink now.\n"
83 "By default, the sink will be closed before being deleted.\n"
84 "Explicitely closing a sink can be useful to control the number\n"
85 "of files simultaneously opened.\n"
89 Py_sink_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
93 uint_t samplerate = 0;
95 static char *kwlist[] = { "uri", "samplerate", "channels", NULL };
97 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
98 &uri, &samplerate, &channels)) {
102 self = (Py_sink *) pytype->tp_alloc (pytype, 0);
110 self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
111 strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
114 self->samplerate = Py_aubio_default_samplerate;
115 if (samplerate != 0) {
116 self->samplerate = samplerate;
121 self->channels = channels;
124 return (PyObject *) self;
128 Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds)
130 self->o = new_aubio_sink ( self->uri, 0 );
131 if (self->o == NULL) {
132 // error string was set in new_aubio_sink
135 if (aubio_sink_preset_channels(self->o, self->channels) != 0) {
136 // error string was set in aubio_sink_preset_channels
139 if (aubio_sink_preset_samplerate(self->o, self->samplerate) != 0) {
140 // error string was set in aubio_sink_preset_samplerate
144 self->samplerate = aubio_sink_get_samplerate ( self->o );
145 self->channels = aubio_sink_get_channels ( self->o );
151 Py_sink_del (Py_sink *self, PyObject *unused)
153 del_aubio_sink(self->o);
154 free(self->mwrite_data.data);
158 Py_TYPE(self)->tp_free((PyObject *) self);
161 /* function Py_sink_do */
163 Py_sink_do(Py_sink * self, PyObject * args)
165 /* input vectors python prototypes */
166 PyObject * write_data_obj;
168 /* input vectors prototypes */
172 if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
176 /* input vectors parsing */
177 if (!PyAubio_ArrayToCFvec(write_data_obj, &(self->write_data))) {
182 /* compute _do function */
183 aubio_sink_do (self->o, &(self->write_data), write);
188 /* function Py_sink_do_multi */
190 Py_sink_do_multi(Py_sink * self, PyObject * args)
192 /* input vectors python prototypes */
193 PyObject * write_data_obj;
195 /* input vectors prototypes */
199 if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
204 /* input vectors parsing */
205 if (!PyAubio_ArrayToCFmat(write_data_obj, &(self->mwrite_data))) {
209 /* compute _do function */
210 aubio_sink_do_multi (self->o, &(self->mwrite_data), write);
214 static PyMemberDef Py_sink_members[] = {
215 {"uri", T_STRING, offsetof (Py_sink, uri), READONLY,
216 "str (read-only): Path at which the sink was created."},
217 {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY,
218 "int (read-only): Samplerate at which the sink was created."},
219 {"channels", T_INT, offsetof (Py_sink, channels), READONLY,
220 "int (read-only): Number of channels with which the sink was created."},
225 Pyaubio_sink_close (Py_sink *self, PyObject *unused)
227 aubio_sink_close (self->o);
231 static char Pyaubio_sink_enter_doc[] = "";
232 static PyObject* Pyaubio_sink_enter(Py_sink *self, PyObject *unused) {
234 return (PyObject*)self;
237 static char Pyaubio_sink_exit_doc[] = "";
238 static PyObject* Pyaubio_sink_exit(Py_sink *self, PyObject *unused) {
239 return Pyaubio_sink_close(self, unused);
242 static PyMethodDef Py_sink_methods[] = {
243 {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc},
244 {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc},
245 {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc},
246 {"__enter__", (PyCFunction)Pyaubio_sink_enter, METH_NOARGS,
247 Pyaubio_sink_enter_doc},
248 {"__exit__", (PyCFunction)Pyaubio_sink_exit, METH_VARARGS,
249 Pyaubio_sink_exit_doc},
250 {NULL} /* sentinel */
253 PyTypeObject Py_sinkType = {
254 PyVarObject_HEAD_INIT (NULL, 0)
258 (destructor) Py_sink_del,
268 (ternaryfunc)Py_sink_do,
289 (initproc) Py_sink_init,