source: python/ext/py-sink.c @ 5652a8c

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 5652a8c was 5652a8c, checked in by Paul Brossier <piem@piem.org>, 4 years ago

ext/: no more hell, use plain c

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