[5652a8c] | 1 | #include "aubio-types.h" |
---|
[d27634d] | 2 | |
---|
| 3 | typedef struct |
---|
| 4 | { |
---|
| 5 | PyObject_HEAD |
---|
| 6 | aubio_source_t * o; |
---|
| 7 | char_t* uri; |
---|
| 8 | uint_t samplerate; |
---|
[1164bcdf] | 9 | uint_t channels; |
---|
[d27634d] | 10 | uint_t hop_size; |
---|
[cfa46b9] | 11 | uint_t duration; |
---|
[1ee5e21] | 12 | PyObject *read_to; |
---|
| 13 | fvec_t c_read_to; |
---|
| 14 | PyObject *mread_to; |
---|
| 15 | fmat_t c_mread_to; |
---|
[d27634d] | 16 | } Py_source; |
---|
| 17 | |
---|
[a79ec76] | 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."; |
---|
[d27634d] | 76 | |
---|
[11b49d7] | 77 | static char Py_source_seek_doc[] = "" |
---|
| 78 | "x.seek(position)\n" |
---|
| 79 | "\n" |
---|
| 80 | "Seek to resampled frame position."; |
---|
| 81 | |
---|
[d27634d] | 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; |
---|
[24931d5] | 89 | uint_t channels = 0; |
---|
| 90 | static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL }; |
---|
[d27634d] | 91 | |
---|
[24931d5] | 92 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist, |
---|
| 93 | &uri, &samplerate, &hop_size, &channels)) { |
---|
[d27634d] | 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | self = (Py_source *) pytype->tp_alloc (pytype, 0); |
---|
| 98 | |
---|
| 99 | if (self == NULL) { |
---|
| 100 | return NULL; |
---|
| 101 | } |
---|
| 102 | |
---|
[b8cedb6] | 103 | self->uri = NULL; |
---|
[d27634d] | 104 | if (uri != NULL) { |
---|
[b8cedb6] | 105 | self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1)); |
---|
| 106 | strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1); |
---|
[d27634d] | 107 | } |
---|
| 108 | |
---|
[2f9af5d] | 109 | self->samplerate = 0; |
---|
[18e22f9] | 110 | if ((sint_t)samplerate > 0) { |
---|
[d27634d] | 111 | self->samplerate = samplerate; |
---|
[18e22f9] | 112 | } else if ((sint_t)samplerate < 0) { |
---|
| 113 | PyErr_SetString (PyExc_ValueError, |
---|
| 114 | "can not use negative value for samplerate"); |
---|
| 115 | return NULL; |
---|
[d27634d] | 116 | } |
---|
| 117 | |
---|
| 118 | self->hop_size = Py_default_vector_length / 2; |
---|
[18e22f9] | 119 | if ((sint_t)hop_size > 0) { |
---|
[d27634d] | 120 | self->hop_size = hop_size; |
---|
[18e22f9] | 121 | } else if ((sint_t)hop_size < 0) { |
---|
| 122 | PyErr_SetString (PyExc_ValueError, |
---|
| 123 | "can not use negative value for hop_size"); |
---|
| 124 | return NULL; |
---|
[d27634d] | 125 | } |
---|
| 126 | |
---|
[24931d5] | 127 | self->channels = 1; |
---|
| 128 | if ((sint_t)channels >= 0) { |
---|
| 129 | self->channels = channels; |
---|
| 130 | } else if ((sint_t)channels < 0) { |
---|
| 131 | PyErr_SetString (PyExc_ValueError, |
---|
| 132 | "can not use negative value for channels"); |
---|
| 133 | return NULL; |
---|
| 134 | } |
---|
| 135 | |
---|
[d27634d] | 136 | return (PyObject *) self; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | static int |
---|
| 140 | Py_source_init (Py_source * self, PyObject * args, PyObject * kwds) |
---|
| 141 | { |
---|
| 142 | self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size ); |
---|
| 143 | if (self->o == NULL) { |
---|
[bd8a92d] | 144 | // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called |
---|
| 145 | // AUBIO_ERR when failing |
---|
[d27634d] | 146 | return -1; |
---|
| 147 | } |
---|
| 148 | self->samplerate = aubio_source_get_samplerate ( self->o ); |
---|
[24931d5] | 149 | if (self->channels == 0) { |
---|
| 150 | self->channels = aubio_source_get_channels ( self->o ); |
---|
| 151 | } |
---|
[cfa46b9] | 152 | self->duration = aubio_source_get_duration ( self->o ); |
---|
[d27634d] | 153 | |
---|
[1ee5e21] | 154 | self->read_to = new_py_fvec(self->hop_size); |
---|
| 155 | self->mread_to = new_py_fmat(self->channels, self->hop_size); |
---|
[a28dab6] | 156 | |
---|
[d27634d] | 157 | return 0; |
---|
| 158 | } |
---|
| 159 | |
---|
[a28dab6] | 160 | static void |
---|
| 161 | Py_source_del (Py_source *self, PyObject *unused) |
---|
| 162 | { |
---|
[cd2791f] | 163 | if (self->o) { |
---|
| 164 | del_aubio_source(self->o); |
---|
[c18bbef] | 165 | free(self->c_mread_to.data); |
---|
[cd2791f] | 166 | } |
---|
[1ee5e21] | 167 | Py_XDECREF(self->read_to); |
---|
| 168 | Py_XDECREF(self->mread_to); |
---|
[5c1200a] | 169 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[a28dab6] | 170 | } |
---|
| 171 | |
---|
[d27634d] | 172 | |
---|
| 173 | /* function Py_source_do */ |
---|
[1164bcdf] | 174 | static PyObject * |
---|
[d27634d] | 175 | Py_source_do(Py_source * self, PyObject * args) |
---|
| 176 | { |
---|
[a138975] | 177 | PyObject *outputs; |
---|
[d27634d] | 178 | uint_t read; |
---|
| 179 | read = 0; |
---|
| 180 | |
---|
[1ee5e21] | 181 | Py_INCREF(self->read_to); |
---|
| 182 | if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) { |
---|
| 183 | return NULL; |
---|
| 184 | } |
---|
[d27634d] | 185 | /* compute _do function */ |
---|
[1ee5e21] | 186 | aubio_source_do (self->o, &(self->c_read_to), &read); |
---|
[d27634d] | 187 | |
---|
[a138975] | 188 | outputs = PyTuple_New(2); |
---|
[1ee5e21] | 189 | PyTuple_SetItem( outputs, 0, self->read_to ); |
---|
[26eb6d0] | 190 | PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read)); |
---|
[d27634d] | 191 | return outputs; |
---|
| 192 | } |
---|
| 193 | |
---|
[1164bcdf] | 194 | /* function Py_source_do_multi */ |
---|
| 195 | static PyObject * |
---|
| 196 | Py_source_do_multi(Py_source * self, PyObject * args) |
---|
| 197 | { |
---|
[a138975] | 198 | PyObject *outputs; |
---|
[1164bcdf] | 199 | uint_t read; |
---|
| 200 | read = 0; |
---|
| 201 | |
---|
[1ee5e21] | 202 | Py_INCREF(self->mread_to); |
---|
| 203 | if (!PyAubio_ArrayToCFmat(self->mread_to, &(self->c_mread_to))) { |
---|
| 204 | return NULL; |
---|
| 205 | } |
---|
[1164bcdf] | 206 | /* compute _do function */ |
---|
[1ee5e21] | 207 | aubio_source_do_multi (self->o, &(self->c_mread_to), &read); |
---|
[1164bcdf] | 208 | |
---|
[a138975] | 209 | outputs = PyTuple_New(2); |
---|
[1ee5e21] | 210 | PyTuple_SetItem( outputs, 0, self->mread_to); |
---|
[26eb6d0] | 211 | PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read)); |
---|
[1164bcdf] | 212 | return outputs; |
---|
| 213 | } |
---|
| 214 | |
---|
[5652a8c] | 215 | static PyMemberDef Py_source_members[] = { |
---|
[a79ec76] | 216 | {"uri", T_STRING, offsetof (Py_source, uri), READONLY, |
---|
| 217 | "path at which the source was created"}, |
---|
| 218 | {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY, |
---|
| 219 | "samplerate at which the source is viewed"}, |
---|
| 220 | {"channels", T_INT, offsetof (Py_source, channels), READONLY, |
---|
| 221 | "number of channels found in the source"}, |
---|
| 222 | {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY, |
---|
| 223 | "number of consecutive frames that will be read at each do or do_multi call"}, |
---|
[cfa46b9] | 224 | {"duration", T_INT, offsetof (Py_source, duration), READONLY, |
---|
| 225 | "total number of frames in the source (estimated)"}, |
---|
[5652a8c] | 226 | { NULL } // sentinel |
---|
| 227 | }; |
---|
[d27634d] | 228 | |
---|
| 229 | static PyObject * |
---|
| 230 | Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused) |
---|
| 231 | { |
---|
| 232 | uint_t tmp = aubio_source_get_samplerate (self->o); |
---|
[5c1200a] | 233 | return (PyObject *)PyLong_FromLong (tmp); |
---|
[d27634d] | 234 | } |
---|
| 235 | |
---|
| 236 | static PyObject * |
---|
| 237 | Pyaubio_source_get_channels (Py_source *self, PyObject *unused) |
---|
| 238 | { |
---|
| 239 | uint_t tmp = aubio_source_get_channels (self->o); |
---|
[5c1200a] | 240 | return (PyObject *)PyLong_FromLong (tmp); |
---|
[d27634d] | 241 | } |
---|
| 242 | |
---|
[7b56229] | 243 | static PyObject * |
---|
| 244 | Pyaubio_source_close (Py_source *self, PyObject *unused) |
---|
| 245 | { |
---|
[23be736] | 246 | if (aubio_source_close(self->o) != 0) return NULL; |
---|
[7b56229] | 247 | Py_RETURN_NONE; |
---|
| 248 | } |
---|
| 249 | |
---|
[11b49d7] | 250 | static PyObject * |
---|
| 251 | Pyaubio_source_seek (Py_source *self, PyObject *args) |
---|
| 252 | { |
---|
| 253 | uint_t err = 0; |
---|
| 254 | |
---|
[a2ab20a] | 255 | int position; |
---|
[11b49d7] | 256 | if (!PyArg_ParseTuple (args, "I", &position)) { |
---|
| 257 | return NULL; |
---|
| 258 | } |
---|
| 259 | |
---|
[a2ab20a] | 260 | if (position < 0) { |
---|
| 261 | PyErr_Format(PyExc_ValueError, |
---|
| 262 | "error when seeking in source: can not seek to negative value %d", |
---|
| 263 | position); |
---|
| 264 | return NULL; |
---|
| 265 | } |
---|
| 266 | |
---|
[11b49d7] | 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 | |
---|
[f1f2e7e] | 276 | static char Pyaubio_source_enter_doc[] = ""; |
---|
| 277 | static PyObject* Pyaubio_source_enter(Py_source *self, PyObject *unused) { |
---|
| 278 | Py_INCREF(self); |
---|
| 279 | return (PyObject*)self; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | static char Pyaubio_source_exit_doc[] = ""; |
---|
| 283 | static PyObject* Pyaubio_source_exit(Py_source *self, PyObject *unused) { |
---|
| 284 | return Pyaubio_source_close(self, unused); |
---|
| 285 | } |
---|
| 286 | |
---|
[6dda1c0] | 287 | static PyObject* Pyaubio_source_iter(PyObject *self) { |
---|
| 288 | Py_INCREF(self); |
---|
| 289 | return (PyObject*)self; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | static PyObject* Pyaubio_source_iter_next(Py_source *self) { |
---|
[a6222fc] | 293 | PyObject *done, *size; |
---|
[6dda1c0] | 294 | if (self->channels == 1) { |
---|
| 295 | done = Py_source_do(self, NULL); |
---|
| 296 | } else { |
---|
| 297 | done = Py_source_do_multi(self, NULL); |
---|
| 298 | } |
---|
| 299 | if (!PyTuple_Check(done)) { |
---|
| 300 | PyErr_Format(PyExc_ValueError, |
---|
| 301 | "error when reading source: not opened?"); |
---|
| 302 | return NULL; |
---|
| 303 | } |
---|
[a6222fc] | 304 | size = PyTuple_GetItem(done, 1); |
---|
[6dda1c0] | 305 | if (size != NULL && PyLong_Check(size)) { |
---|
| 306 | if (PyLong_AsLong(size) == (long)self->hop_size) { |
---|
| 307 | PyObject *vec = PyTuple_GetItem(done, 0); |
---|
| 308 | return vec; |
---|
| 309 | } else if (PyLong_AsLong(size) > 0) { |
---|
| 310 | // short read |
---|
| 311 | PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0); |
---|
| 312 | PyArray_Dims newdims; |
---|
| 313 | newdims.len = PyArray_NDIM(shortread); |
---|
| 314 | newdims.ptr = PyArray_DIMS(shortread); |
---|
| 315 | // mono or multiple channels? |
---|
| 316 | if (newdims.len == 1) { |
---|
| 317 | newdims.ptr[0] = PyLong_AsLong(size); |
---|
| 318 | } else { |
---|
| 319 | newdims.ptr[1] = PyLong_AsLong(size); |
---|
| 320 | } |
---|
| 321 | PyArray_Resize(shortread, &newdims, 1, NPY_ANYORDER); |
---|
| 322 | return (PyObject*)shortread; |
---|
| 323 | } else { |
---|
| 324 | PyErr_SetNone(PyExc_StopIteration); |
---|
| 325 | return NULL; |
---|
| 326 | } |
---|
| 327 | } else { |
---|
| 328 | PyErr_SetNone(PyExc_StopIteration); |
---|
| 329 | return NULL; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
[d27634d] | 333 | static PyMethodDef Py_source_methods[] = { |
---|
| 334 | {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate, |
---|
[a79ec76] | 335 | METH_NOARGS, Py_source_get_samplerate_doc}, |
---|
[d27634d] | 336 | {"get_channels", (PyCFunction) Pyaubio_source_get_channels, |
---|
[a79ec76] | 337 | METH_NOARGS, Py_source_get_channels_doc}, |
---|
| 338 | {"do", (PyCFunction) Py_source_do, |
---|
| 339 | METH_NOARGS, Py_source_do_doc}, |
---|
[1164bcdf] | 340 | {"do_multi", (PyCFunction) Py_source_do_multi, |
---|
[a79ec76] | 341 | METH_NOARGS, Py_source_do_multi_doc}, |
---|
[7b56229] | 342 | {"close", (PyCFunction) Pyaubio_source_close, |
---|
[a79ec76] | 343 | METH_NOARGS, Py_source_close_doc}, |
---|
[11b49d7] | 344 | {"seek", (PyCFunction) Pyaubio_source_seek, |
---|
| 345 | METH_VARARGS, Py_source_seek_doc}, |
---|
[f1f2e7e] | 346 | {"__enter__", (PyCFunction)Pyaubio_source_enter, METH_NOARGS, |
---|
| 347 | Pyaubio_source_enter_doc}, |
---|
| 348 | {"__exit__", (PyCFunction)Pyaubio_source_exit, METH_VARARGS, |
---|
| 349 | Pyaubio_source_exit_doc}, |
---|
[d27634d] | 350 | {NULL} /* sentinel */ |
---|
| 351 | }; |
---|
| 352 | |
---|
[5652a8c] | 353 | PyTypeObject Py_sourceType = { |
---|
| 354 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 355 | "aubio.source", |
---|
| 356 | sizeof (Py_source), |
---|
| 357 | 0, |
---|
| 358 | (destructor) Py_source_del, |
---|
| 359 | 0, |
---|
| 360 | 0, |
---|
| 361 | 0, |
---|
| 362 | 0, |
---|
| 363 | 0, |
---|
| 364 | 0, |
---|
| 365 | 0, |
---|
| 366 | 0, |
---|
| 367 | 0, |
---|
| 368 | (ternaryfunc)Py_source_do, |
---|
| 369 | 0, |
---|
| 370 | 0, |
---|
| 371 | 0, |
---|
| 372 | 0, |
---|
| 373 | Py_TPFLAGS_DEFAULT, |
---|
| 374 | Py_source_doc, |
---|
| 375 | 0, |
---|
| 376 | 0, |
---|
| 377 | 0, |
---|
| 378 | 0, |
---|
[6dda1c0] | 379 | Pyaubio_source_iter, |
---|
| 380 | (unaryfunc)Pyaubio_source_iter_next, |
---|
[5652a8c] | 381 | Py_source_methods, |
---|
| 382 | Py_source_members, |
---|
| 383 | 0, |
---|
| 384 | 0, |
---|
| 385 | 0, |
---|
| 386 | 0, |
---|
| 387 | 0, |
---|
| 388 | 0, |
---|
| 389 | (initproc) Py_source_init, |
---|
| 390 | 0, |
---|
| 391 | Py_source_new, |
---|
[0e70ef9] | 392 | 0, |
---|
| 393 | 0, |
---|
| 394 | 0, |
---|
| 395 | 0, |
---|
| 396 | 0, |
---|
| 397 | 0, |
---|
| 398 | 0, |
---|
| 399 | 0, |
---|
| 400 | 0, |
---|
[5652a8c] | 401 | }; |
---|