python/ext/py-source.c: fix error string, only delete if created
[aubio.git] / python / ext / py-source.c
1 #include "aubio-types.h"
2
3 typedef struct
4 {
5   PyObject_HEAD
6   aubio_source_t * o;
7   char_t* uri;
8   uint_t samplerate;
9   uint_t channels;
10   uint_t hop_size;
11   uint_t duration;
12   PyObject *read_to;
13   fvec_t c_read_to;
14   PyObject *mread_to;
15   fmat_t c_mread_to;
16 } Py_source;
17
18 static char Py_source_doc[] = ""
19 "   __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
20 "\n"
21 "       Create a new source, opening the given path for reading.\n"
22 "\n"
23 "       Examples\n"
24 "       --------\n"
25 "\n"
26 "       Create a new source, using the original samplerate, with hop_size = 512:\n"
27 "\n"
28 "       >>> source('/tmp/t.wav')\n"
29 "\n"
30 "       Create a new source, resampling the original to 8000Hz:\n"
31 "\n"
32 "       >>> source('/tmp/t.wav', samplerate = 8000)\n"
33 "\n"
34 "       Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
35 "\n"
36 "       >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
37 "\n"
38 "       Create a new source, using its original samplerate:\n"
39 "\n"
40 "       >>> source('/tmp/t.wav', samplerate = 0)\n"
41 "\n"
42 "   __call__()\n"
43 "       vec, read = x() <==> vec, read = x.do()\n"
44 "\n"
45 "       Read vector from source.\n"
46 "\n"
47 "       See also\n"
48 "       --------\n"
49 "       aubio.source.do\n"
50 "\n";
51
52 static char Py_source_get_samplerate_doc[] = ""
53 "x.get_samplerate() -> source samplerate\n"
54 "\n"
55 "Get samplerate of source.";
56
57 static char Py_source_get_channels_doc[] = ""
58 "x.get_channels() -> number of channels\n"
59 "\n"
60 "Get number of channels in source.";
61
62 static char Py_source_do_doc[] = ""
63 "vec, read = x.do() <==> vec, read = x()\n"
64 "\n"
65 "Read monophonic vector from source.";
66
67 static char Py_source_do_multi_doc[] = ""
68 "mat, read = x.do_multi()\n"
69 "\n"
70 "Read polyphonic vector from source.";
71
72 static char Py_source_close_doc[] = ""
73 "x.close()\n"
74 "\n"
75 "Close this source now.";
76
77 static char Py_source_seek_doc[] = ""
78 "x.seek(position)\n"
79 "\n"
80 "Seek to resampled frame position.";
81
82 static PyObject *
83 Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
84 {
85   Py_source *self;
86   char_t* uri = NULL;
87   uint_t samplerate = 0;
88   uint_t hop_size = 0;
89   uint_t channels = 0;
90   static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
91
92   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
93           &uri, &samplerate, &hop_size, &channels)) {
94     return NULL;
95   }
96
97   self = (Py_source *) pytype->tp_alloc (pytype, 0);
98
99   if (self == NULL) {
100     return NULL;
101   }
102
103   self->uri = "none";
104   if (uri != NULL) {
105     self->uri = uri;
106   }
107
108   self->samplerate = 0;
109   if ((sint_t)samplerate > 0) {
110     self->samplerate = samplerate;
111   } else if ((sint_t)samplerate < 0) {
112     PyErr_SetString (PyExc_ValueError,
113         "can not use negative value for samplerate");
114     return NULL;
115   }
116
117   self->hop_size = Py_default_vector_length / 2;
118   if ((sint_t)hop_size > 0) {
119     self->hop_size = hop_size;
120   } else if ((sint_t)hop_size < 0) {
121     PyErr_SetString (PyExc_ValueError,
122         "can not use negative value for hop_size");
123     return NULL;
124   }
125
126   self->channels = 1;
127   if ((sint_t)channels >= 0) {
128     self->channels = channels;
129   } else if ((sint_t)channels < 0) {
130     PyErr_SetString (PyExc_ValueError,
131         "can not use negative value for channels");
132     return NULL;
133   }
134
135   return (PyObject *) self;
136 }
137
138 static int
139 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
140 {
141   self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
142   if (self->o == NULL) {
143     PyErr_Format (PyExc_RuntimeError, "error creating source with \"%s\"",
144         self->uri);
145     return -1;
146   }
147   self->samplerate = aubio_source_get_samplerate ( self->o );
148   if (self->channels == 0) {
149     self->channels = aubio_source_get_channels ( self->o );
150   }
151   self->duration = aubio_source_get_duration ( self->o );
152
153   self->read_to = new_py_fvec(self->hop_size);
154   self->mread_to = new_py_fmat(self->channels, self->hop_size);
155
156   return 0;
157 }
158
159 static void
160 Py_source_del (Py_source *self, PyObject *unused)
161 {
162   if (self->o) {
163     del_aubio_source(self->o);
164     free(self->mread_to.data);
165   }
166   Py_XDECREF(self->read_to);
167   Py_XDECREF(self->mread_to);
168   Py_TYPE(self)->tp_free((PyObject *) self);
169 }
170
171
172 /* function Py_source_do */
173 static PyObject *
174 Py_source_do(Py_source * self, PyObject * args)
175 {
176   uint_t read;
177   read = 0;
178
179   Py_INCREF(self->read_to);
180   if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) {
181     return NULL;
182   }
183   /* compute _do function */
184   aubio_source_do (self->o, &(self->c_read_to), &read);
185
186   PyObject *outputs = PyTuple_New(2);
187   PyTuple_SetItem( outputs, 0, self->read_to );
188   PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
189   return outputs;
190 }
191
192 /* function Py_source_do_multi */
193 static PyObject *
194 Py_source_do_multi(Py_source * self, PyObject * args)
195 {
196   uint_t read;
197   read = 0;
198
199   Py_INCREF(self->mread_to);
200   if (!PyAubio_ArrayToCFmat(self->mread_to,  &(self->c_mread_to))) {
201     return NULL;
202   }
203   /* compute _do function */
204   aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
205
206   PyObject *outputs = PyTuple_New(2);
207   PyTuple_SetItem( outputs, 0, self->mread_to);
208   PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
209   return outputs;
210 }
211
212 static PyMemberDef Py_source_members[] = {
213   {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
214     "path at which the source was created"},
215   {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
216     "samplerate at which the source is viewed"},
217   {"channels", T_INT, offsetof (Py_source, channels), READONLY,
218     "number of channels found in the source"},
219   {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
220     "number of consecutive frames that will be read at each do or do_multi call"},
221   {"duration", T_INT, offsetof (Py_source, duration), READONLY,
222     "total number of frames in the source (estimated)"},
223   { NULL } // sentinel
224 };
225
226 static PyObject *
227 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
228 {
229   uint_t tmp = aubio_source_get_samplerate (self->o);
230   return (PyObject *)PyLong_FromLong (tmp);
231 }
232
233 static PyObject *
234 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
235 {
236   uint_t tmp = aubio_source_get_channels (self->o);
237   return (PyObject *)PyLong_FromLong (tmp);
238 }
239
240 static PyObject *
241 Pyaubio_source_close (Py_source *self, PyObject *unused)
242 {
243   aubio_source_close (self->o);
244   Py_RETURN_NONE;
245 }
246
247 static PyObject *
248 Pyaubio_source_seek (Py_source *self, PyObject *args)
249 {
250   uint_t err = 0;
251
252   uint_t position;
253   if (!PyArg_ParseTuple (args, "I", &position)) {
254     return NULL;
255   }
256
257   err = aubio_source_seek(self->o, position);
258   if (err != 0) {
259     PyErr_SetString (PyExc_ValueError,
260         "error when seeking in source");
261     return NULL;
262   }
263   Py_RETURN_NONE;
264 }
265
266 static PyMethodDef Py_source_methods[] = {
267   {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
268     METH_NOARGS, Py_source_get_samplerate_doc},
269   {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
270     METH_NOARGS, Py_source_get_channels_doc},
271   {"do", (PyCFunction) Py_source_do,
272     METH_NOARGS, Py_source_do_doc},
273   {"do_multi", (PyCFunction) Py_source_do_multi,
274     METH_NOARGS, Py_source_do_multi_doc},
275   {"close", (PyCFunction) Pyaubio_source_close,
276     METH_NOARGS, Py_source_close_doc},
277   {"seek", (PyCFunction) Pyaubio_source_seek,
278     METH_VARARGS, Py_source_seek_doc},
279   {NULL} /* sentinel */
280 };
281
282 PyTypeObject Py_sourceType = {
283   PyVarObject_HEAD_INIT (NULL, 0)
284   "aubio.source",
285   sizeof (Py_source),
286   0,
287   (destructor) Py_source_del,
288   0,
289   0,
290   0,
291   0,
292   0,
293   0,
294   0,
295   0,
296   0,
297   (ternaryfunc)Py_source_do,
298   0,
299   0,
300   0,
301   0,
302   Py_TPFLAGS_DEFAULT,
303   Py_source_doc,
304   0,
305   0,
306   0,
307   0,
308   0,
309   0,
310   Py_source_methods,
311   Py_source_members,
312   0,
313   0,
314   0,
315   0,
316   0,
317   0,
318   (initproc) Py_source_init,
319   0,
320   Py_source_new,
321 };