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

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since ac7e49b was ac7e49b, checked in by Nils Philippsen <nils@tiptoe.de>, 4 years ago

Python 3: raise RuntimeErrors?, not generic exceptions

  • Property mode set to 100644
File size: 4.9 KB
Line 
1#include "aubiowraphell.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
127AUBIO_DEL(sink)
128
129/* function Py_sink_do */
130static PyObject *
131Py_sink_do(Py_sink * self, PyObject * args)
132{
133  /* input vectors python prototypes */
134  PyObject * write_data_obj;
135
136  /* input vectors prototypes */
137  fvec_t* write_data;
138  uint_t write;
139
140
141  if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
142    return NULL;
143  }
144
145
146  /* input vectors parsing */
147  write_data = PyAubio_ArrayToCFvec (write_data_obj);
148
149  if (write_data == NULL) {
150    return NULL;
151  }
152
153
154
155
156
157  /* compute _do function */
158  aubio_sink_do (self->o, write_data, write);
159
160  Py_RETURN_NONE;
161}
162
163/* function Py_sink_do_multi */
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  fmat_t * write_data;
172  uint_t write;
173
174
175  if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) {
176    return NULL;
177  }
178
179
180  /* input vectors parsing */
181  write_data = PyAubio_ArrayToCFmat (write_data_obj);
182
183  if (write_data == NULL) {
184    return NULL;
185  }
186
187
188
189
190
191  /* compute _do function */
192  aubio_sink_do_multi (self->o, write_data, write);
193  Py_RETURN_NONE;
194}
195
196AUBIO_MEMBERS_START(sink)
197  {"uri", T_STRING, offsetof (Py_sink, uri), READONLY,
198    "path at which the sink was created"},
199  {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY,
200    "samplerate at which the sink was created"},
201  {"channels", T_INT, offsetof (Py_sink, channels), READONLY,
202    "number of channels with which the sink was created"},
203AUBIO_MEMBERS_STOP(sink)
204
205static PyObject *
206Pyaubio_sink_close (Py_sink *self, PyObject *unused)
207{
208  aubio_sink_close (self->o);
209  Py_RETURN_NONE;
210}
211
212static PyMethodDef Py_sink_methods[] = {
213  {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc},
214  {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc},
215  {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc},
216  {NULL} /* sentinel */
217};
218
219AUBIO_TYPEOBJECT(sink, "aubio.sink")
Note: See TracBrowser for help on using the repository browser.