source: python/ext/py-sink.c @ 271434f

feature/autosinkfeature/cnnfeature/crepefeature/crepe_orgfeature/timestretchfix/ffmpeg5
Last change on this file since 271434f was 271434f, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[py] fix typo in sink docstrings

  • Property mode set to 100644
File size: 6.5 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[] = ""
[d281698]15"sink(path, samplerate=44100, channels=1)\n"
[a79ec76]16"\n"
[a5c3f2a]17"Write audio samples to file.\n"
[a79ec76]18"\n"
[d281698]19"Parameters\n"
20"----------\n"
21"path : str\n"
22"   Pathname of the file to be opened for writing.\n"
23"samplerate : int\n"
24"   Sampling rate of the file, in Hz.\n"
25"channels : int\n"
26"   Number of channels to create the file with.\n"
[a79ec76]27"\n"
[d281698]28"Examples\n"
29"--------\n"
[a79ec76]30"\n"
[d281698]31"Create a new sink at 44100Hz, mono:\n"
[a79ec76]32"\n"
[d281698]33">>> snk = aubio.sink('out.wav')\n"
[a79ec76]34"\n"
[d281698]35"Create a new sink at 32000Hz, stereo, write 100 samples into it:\n"
[a79ec76]36"\n"
[d281698]37">>> snk = aubio.sink('out.wav', samplerate=16000, channels=3)\n"
38">>> snk(aubio.fvec(100), 100)\n"
[a79ec76]39"\n"
[d281698]40"Open a new sink at 48000Hz, stereo, write `1234` samples into it:\n"
[a79ec76]41"\n"
[d281698]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"
45"...\n"
[a79ec76]46"\n"
[d281698]47"See also\n"
48"--------\n"
49"source: read audio samples from a file.\n"
[a79ec76]50"\n";
51
52static char Py_sink_do_doc[] = ""
[d281698]53"do(vec, write)\n"
[a79ec76]54"\n"
[d281698]55"Write a single channel vector to sink.\n"
56"\n"
57"Parameters\n"
58"----------\n"
59"vec : fvec\n"
60"   input vector `(n,)` where `n >= 0`.\n"
61"write : int\n"
62"   Number of samples to write.\n"
63"";
[a79ec76]64
65static char Py_sink_do_multi_doc[] = ""
[d281698]66"do_multi(mat, write)\n"
67"\n"
68"Write a matrix containing vectors from multiple channels to sink.\n"
[a79ec76]69"\n"
[d281698]70"Parameters\n"
71"----------\n"
72"mat : numpy.ndarray\n"
73"   input matrix of shape `(channels, n)`, where `n >= 0`.\n"
74"write : int\n"
75"   Number of frames to write.\n"
76"";
[a79ec76]77
78static char Py_sink_close_doc[] = ""
[d281698]79"close()\n"
80"\n"
81"Close this sink now.\n"
[a79ec76]82"\n"
[0629356]83"By default, the sink will be closed before being deleted.\n"
[271434f]84"Explicitly closing a sink can be useful to control the number\n"
[0629356]85"of files simultaneously opened.\n"
[d281698]86"";
[f1100a4]87
88static PyObject *
89Py_sink_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
90{
91  Py_sink *self;
92  char_t* uri = NULL;
93  uint_t samplerate = 0;
[1a121ca]94  uint_t channels = 0;
95  static char *kwlist[] = { "uri", "samplerate", "channels", NULL };
[f1100a4]96
[1a121ca]97  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
98          &uri, &samplerate, &channels)) {
[f1100a4]99    return NULL;
100  }
101
102  self = (Py_sink *) pytype->tp_alloc (pytype, 0);
103
104  if (self == NULL) {
105    return NULL;
106  }
107
[b8cedb6]108  self->uri = NULL;
[f1100a4]109  if (uri != NULL) {
[b8cedb6]110    self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
111    strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
[f1100a4]112  }
113
114  self->samplerate = Py_aubio_default_samplerate;
[b81a642]115  if (samplerate != 0) {
[f1100a4]116    self->samplerate = samplerate;
117  }
118
[1a121ca]119  self->channels = 1;
[b81a642]120  if (channels != 0) {
[1a121ca]121    self->channels = channels;
122  }
123
[f1100a4]124  return (PyObject *) self;
125}
126
[1a121ca]127static int
128Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds)
129{
[b81a642]130  self->o = new_aubio_sink ( self->uri, 0 );
[1a121ca]131  if (self->o == NULL) {
[b81a642]132    // error string was set in new_aubio_sink
133    return -1;
134  }
135  if (aubio_sink_preset_channels(self->o, self->channels) != 0) {
136    // error string was set in aubio_sink_preset_channels
137    return -1;
138  }
139  if (aubio_sink_preset_samplerate(self->o, self->samplerate) != 0) {
140    // error string was set in aubio_sink_preset_samplerate
[1a121ca]141    return -1;
142  }
[b81a642]143
[1a121ca]144  self->samplerate = aubio_sink_get_samplerate ( self->o );
145  self->channels = aubio_sink_get_channels ( self->o );
146
147  return 0;
148}
[f1100a4]149
[5652a8c]150static void
151Py_sink_del (Py_sink *self, PyObject *unused)
152{
[98d7aaa]153  if (self->o) {
154    del_aubio_sink(self->o);
155    free(self->mwrite_data.data);
156  }
[4f89154]157  if (self->uri) {
158    free(self->uri);
159  }
[5652a8c]160  Py_TYPE(self)->tp_free((PyObject *) self);
161}
[f1100a4]162
163/* function Py_sink_do */
[1a121ca]164static PyObject *
[f1100a4]165Py_sink_do(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  /* input vectors parsing */
[569b363]179  if (!PyAubio_ArrayToCFvec(write_data_obj, &(self->write_data))) {
[f1100a4]180    return NULL;
181  }
182
[1a121ca]183
[f1100a4]184  /* compute _do function */
[569b363]185  aubio_sink_do (self->o, &(self->write_data), write);
[f1100a4]186
187  Py_RETURN_NONE;
188}
189
[a79ec76]190/* function Py_sink_do_multi */
[1a121ca]191static PyObject *
192Py_sink_do_multi(Py_sink * self, PyObject * args)
193{
194  /* input vectors python prototypes */
195  PyObject * write_data_obj;
196
197  /* input vectors prototypes */
198  uint_t write;
199
200
201  if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
202    return NULL;
203  }
204
205
206  /* input vectors parsing */
[569b363]207  if (!PyAubio_ArrayToCFmat(write_data_obj, &(self->mwrite_data))) {
[1a121ca]208    return NULL;
209  }
210
211  /* compute _do function */
[569b363]212  aubio_sink_do_multi (self->o, &(self->mwrite_data), write);
[1a121ca]213  Py_RETURN_NONE;
214}
215
[5652a8c]216static PyMemberDef Py_sink_members[] = {
[a79ec76]217  {"uri", T_STRING, offsetof (Py_sink, uri), READONLY,
[d281698]218    "str (read-only): Path at which the sink was created."},
[a79ec76]219  {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY,
[d281698]220    "int (read-only): Samplerate at which the sink was created."},
[a79ec76]221  {"channels", T_INT, offsetof (Py_sink, channels), READONLY,
[d281698]222    "int (read-only): Number of channels with which the sink was created."},
[5652a8c]223  { NULL } // sentinel
224};
[f1100a4]225
[7b56229]226static PyObject *
227Pyaubio_sink_close (Py_sink *self, PyObject *unused)
228{
[3cc3fd8]229  aubio_sink_close (self->o);
[7b56229]230  Py_RETURN_NONE;
231}
[f1100a4]232
[fcb6e8c]233static char Pyaubio_sink_enter_doc[] = "";
234static PyObject* Pyaubio_sink_enter(Py_sink *self, PyObject *unused) {
235  Py_INCREF(self);
236  return (PyObject*)self;
237}
238
239static char Pyaubio_sink_exit_doc[] = "";
240static PyObject* Pyaubio_sink_exit(Py_sink *self, PyObject *unused) {
241  return Pyaubio_sink_close(self, unused);
242}
243
[f1100a4]244static PyMethodDef Py_sink_methods[] = {
[a79ec76]245  {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc},
246  {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc},
247  {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc},
[fcb6e8c]248  {"__enter__", (PyCFunction)Pyaubio_sink_enter, METH_NOARGS,
249    Pyaubio_sink_enter_doc},
250  {"__exit__",  (PyCFunction)Pyaubio_sink_exit, METH_VARARGS,
251    Pyaubio_sink_exit_doc},
[f1100a4]252  {NULL} /* sentinel */
253};
254
[5652a8c]255PyTypeObject Py_sinkType = {
256  PyVarObject_HEAD_INIT (NULL, 0)
257  "aubio.sink",
258  sizeof (Py_sink),
259  0,
260  (destructor) Py_sink_del,
261  0,
262  0,
263  0,
264  0,
265  0,
266  0,
267  0,
268  0,
269  0,
270  (ternaryfunc)Py_sink_do,
271  0,
272  0,
273  0,
274  0,
275  Py_TPFLAGS_DEFAULT,
276  Py_sink_doc,
277  0,
278  0,
279  0,
280  0,
281  0,
282  0,
283  Py_sink_methods,
284  Py_sink_members,
285  0,
286  0,
287  0,
288  0,
289  0,
290  0,
291  (initproc) Py_sink_init,
292  0,
293  Py_sink_new,
[0e70ef9]294  0,
295  0,
296  0,
297  0,
298  0,
299  0,
300  0,
301  0,
302  0,
[5652a8c]303};
Note: See TracBrowser for help on using the repository browser.