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