source: python/ext/py-sink.c @ fc359e0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since fc359e0 was fcb6e8c, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/ext/py-sink.c: add 'with' interface (PEP 343)

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[5652a8c]1#include "aubio-types.h"
[f1100a4]2
3typedef struct
4{
5  PyObject_HEAD
6  aubio_sink_t * o;
7  char_t* uri;
8  uint_t samplerate;
[1a121ca]9  uint_t channels;
[569b363]10  fvec_t write_data;
11  fmat_t mwrite_data;
[f1100a4]12} Py_sink;
13
[a79ec76]14static char Py_sink_doc[] = ""
15"  __new__(path, samplerate = 44100, channels = 1)\n"
16"\n"
17"      Create a new sink, opening the given path for writing.\n"
18"\n"
19"      Examples\n"
20"      --------\n"
21"\n"
22"      Create a new sink at 44100Hz, mono:\n"
23"\n"
24"      >>> sink('/tmp/t.wav')\n"
25"\n"
26"      Create a new sink at 8000Hz, mono:\n"
27"\n"
28"      >>> sink('/tmp/t.wav', samplerate = 8000)\n"
29"\n"
30"      Create a new sink at 32000Hz, stereo:\n"
31"\n"
32"      >>> sink('/tmp/t.wav', samplerate = 32000, channels = 2)\n"
33"\n"
34"      Create a new sink at 32000Hz, 5 channels:\n"
35"\n"
36"      >>> sink('/tmp/t.wav', channels = 5, samplerate = 32000)\n"
37"\n"
38"  __call__(vec, write)\n"
39"      x(vec,write) <==> x.do(vec, write)\n"
40"\n"
41"      Write vector to sink.\n"
42"\n"
43"      See also\n"
44"      --------\n"
45"      aubio.sink.do\n"
46"\n";
47
48static char Py_sink_do_doc[] = ""
49"x.do(vec, write) <==> x(vec, write)\n"
50"\n"
51"write monophonic vector to sink";
52
53static char Py_sink_do_multi_doc[] = ""
54"x.do_multi(mat, write)\n"
55"\n"
56"write polyphonic vector to sink";
57
58static char Py_sink_close_doc[] = ""
59"x.close()\n"
60"\n"
61"close this sink now";
[f1100a4]62
63static PyObject *
64Py_sink_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
65{
66  Py_sink *self;
67  char_t* uri = NULL;
68  uint_t samplerate = 0;
[1a121ca]69  uint_t channels = 0;
70  static char *kwlist[] = { "uri", "samplerate", "channels", NULL };
[f1100a4]71
[1a121ca]72  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
73          &uri, &samplerate, &channels)) {
[f1100a4]74    return NULL;
75  }
76
77  self = (Py_sink *) pytype->tp_alloc (pytype, 0);
78
79  if (self == NULL) {
80    return NULL;
81  }
82
[b8cedb6]83  self->uri = NULL;
[f1100a4]84  if (uri != NULL) {
[b8cedb6]85    self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
86    strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
[f1100a4]87  }
88
89  self->samplerate = Py_aubio_default_samplerate;
[b81a642]90  if (samplerate != 0) {
[f1100a4]91    self->samplerate = samplerate;
92  }
93
[1a121ca]94  self->channels = 1;
[b81a642]95  if (channels != 0) {
[1a121ca]96    self->channels = channels;
97  }
98
[f1100a4]99  return (PyObject *) self;
100}
101
[1a121ca]102static int
103Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds)
104{
[b81a642]105  self->o = new_aubio_sink ( self->uri, 0 );
[1a121ca]106  if (self->o == NULL) {
[b81a642]107    // error string was set in new_aubio_sink
108    return -1;
109  }
110  if (aubio_sink_preset_channels(self->o, self->channels) != 0) {
111    // error string was set in aubio_sink_preset_channels
112    return -1;
113  }
114  if (aubio_sink_preset_samplerate(self->o, self->samplerate) != 0) {
115    // error string was set in aubio_sink_preset_samplerate
[1a121ca]116    return -1;
117  }
[b81a642]118
[1a121ca]119  self->samplerate = aubio_sink_get_samplerate ( self->o );
120  self->channels = aubio_sink_get_channels ( self->o );
121
122  return 0;
123}
[f1100a4]124
[5652a8c]125static void
126Py_sink_del (Py_sink *self, PyObject *unused)
127{
128  del_aubio_sink(self->o);
[569b363]129  free(self->mwrite_data.data);
[4f89154]130  if (self->uri) {
131    free(self->uri);
132  }
[5652a8c]133  Py_TYPE(self)->tp_free((PyObject *) self);
134}
[f1100a4]135
136/* function Py_sink_do */
[1a121ca]137static PyObject *
[f1100a4]138Py_sink_do(Py_sink * self, PyObject * args)
139{
140  /* input vectors python prototypes */
141  PyObject * write_data_obj;
142
143  /* input vectors prototypes */
144  uint_t write;
145
146
147  if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
148    return NULL;
149  }
150
151  /* input vectors parsing */
[569b363]152  if (!PyAubio_ArrayToCFvec(write_data_obj, &(self->write_data))) {
[f1100a4]153    return NULL;
154  }
155
[1a121ca]156
[f1100a4]157  /* compute _do function */
[569b363]158  aubio_sink_do (self->o, &(self->write_data), write);
[f1100a4]159
160  Py_RETURN_NONE;
161}
162
[a79ec76]163/* function Py_sink_do_multi */
[1a121ca]164static PyObject *
165Py_sink_do_multi(Py_sink * self, PyObject * args)
166{
167  /* input vectors python prototypes */
168  PyObject * write_data_obj;
169
170  /* input vectors prototypes */
171  uint_t write;
172
173
174  if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
175    return NULL;
176  }
177
178
179  /* input vectors parsing */
[569b363]180  if (!PyAubio_ArrayToCFmat(write_data_obj, &(self->mwrite_data))) {
[1a121ca]181    return NULL;
182  }
183
184  /* compute _do function */
[569b363]185  aubio_sink_do_multi (self->o, &(self->mwrite_data), write);
[1a121ca]186  Py_RETURN_NONE;
187}
188
[5652a8c]189static PyMemberDef Py_sink_members[] = {
[a79ec76]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"},
[5652a8c]196  { NULL } // sentinel
197};
[f1100a4]198
[7b56229]199static PyObject *
200Pyaubio_sink_close (Py_sink *self, PyObject *unused)
201{
[3cc3fd8]202  aubio_sink_close (self->o);
[7b56229]203  Py_RETURN_NONE;
204}
[f1100a4]205
[fcb6e8c]206static char Pyaubio_sink_enter_doc[] = "";
207static PyObject* Pyaubio_sink_enter(Py_sink *self, PyObject *unused) {
208  Py_INCREF(self);
209  return (PyObject*)self;
210}
211
212static char Pyaubio_sink_exit_doc[] = "";
213static PyObject* Pyaubio_sink_exit(Py_sink *self, PyObject *unused) {
214  return Pyaubio_sink_close(self, unused);
215}
216
[f1100a4]217static PyMethodDef Py_sink_methods[] = {
[a79ec76]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},
[fcb6e8c]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},
[f1100a4]225  {NULL} /* sentinel */
226};
227
[5652a8c]228PyTypeObject Py_sinkType = {
229  PyVarObject_HEAD_INIT (NULL, 0)
230  "aubio.sink",
231  sizeof (Py_sink),
232  0,
233  (destructor) Py_sink_del,
234  0,
235  0,
236  0,
237  0,
238  0,
239  0,
240  0,
241  0,
242  0,
243  (ternaryfunc)Py_sink_do,
244  0,
245  0,
246  0,
247  0,
248  Py_TPFLAGS_DEFAULT,
249  Py_sink_doc,
250  0,
251  0,
252  0,
253  0,
254  0,
255  0,
256  Py_sink_methods,
257  Py_sink_members,
258  0,
259  0,
260  0,
261  0,
262  0,
263  0,
264  (initproc) Py_sink_init,
265  0,
266  Py_sink_new,
[0e70ef9]267  0,
268  0,
269  0,
270  0,
271  0,
272  0,
273  0,
274  0,
275  0,
[5652a8c]276};
Note: See TracBrowser for help on using the repository browser.