Changeset 41b985f for python/ext/py-source.c
- Timestamp:
- Mar 12, 2017, 11:26:24 AM (7 years ago)
- Branches:
- sampler
- Children:
- bde49c4a
- Parents:
- 71f2e5f (diff), 67b6618 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/ext/py-source.c
r71f2e5f r41b985f 101 101 } 102 102 103 self->uri = "none";103 self->uri = NULL; 104 104 if (uri != NULL) { 105 self->uri = uri; 105 self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1)); 106 strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1); 106 107 } 107 108 … … 163 164 del_aubio_source(self->o); 164 165 free(self->c_mread_to.data); 166 } 167 if (self->uri) { 168 free(self->uri); 165 169 } 166 170 Py_XDECREF(self->read_to); … … 243 247 Pyaubio_source_close (Py_source *self, PyObject *unused) 244 248 { 245 aubio_source_close (self->o);249 if (aubio_source_close(self->o) != 0) return NULL; 246 250 Py_RETURN_NONE; 247 251 } … … 271 275 } 272 276 Py_RETURN_NONE; 277 } 278 279 static char Pyaubio_source_enter_doc[] = ""; 280 static PyObject* Pyaubio_source_enter(Py_source *self, PyObject *unused) { 281 Py_INCREF(self); 282 return (PyObject*)self; 283 } 284 285 static char Pyaubio_source_exit_doc[] = ""; 286 static PyObject* Pyaubio_source_exit(Py_source *self, PyObject *unused) { 287 return Pyaubio_source_close(self, unused); 288 } 289 290 static PyObject* Pyaubio_source_iter(PyObject *self) { 291 Py_INCREF(self); 292 return (PyObject*)self; 293 } 294 295 static PyObject* Pyaubio_source_iter_next(Py_source *self) { 296 PyObject *done, *size; 297 if (self->channels == 1) { 298 done = Py_source_do(self, NULL); 299 } else { 300 done = Py_source_do_multi(self, NULL); 301 } 302 if (!PyTuple_Check(done)) { 303 PyErr_Format(PyExc_ValueError, 304 "error when reading source: not opened?"); 305 return NULL; 306 } 307 size = PyTuple_GetItem(done, 1); 308 if (size != NULL && PyLong_Check(size)) { 309 if (PyLong_AsLong(size) == (long)self->hop_size) { 310 PyObject *vec = PyTuple_GetItem(done, 0); 311 return vec; 312 } else if (PyLong_AsLong(size) > 0) { 313 // short read, return a shorter array 314 PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0); 315 PyArray_Dims newdims; 316 PyObject *reshaped; 317 newdims.len = PyArray_NDIM(shortread); 318 newdims.ptr = PyArray_DIMS(shortread); 319 // mono or multiple channels? 320 if (newdims.len == 1) { 321 newdims.ptr[0] = PyLong_AsLong(size); 322 } else { 323 newdims.ptr[1] = PyLong_AsLong(size); 324 } 325 reshaped = PyArray_Newshape(shortread, &newdims, NPY_CORDER); 326 Py_DECREF(shortread); 327 return reshaped; 328 } else { 329 PyErr_SetNone(PyExc_StopIteration); 330 return NULL; 331 } 332 } else { 333 PyErr_SetNone(PyExc_StopIteration); 334 return NULL; 335 } 273 336 } 274 337 … … 286 349 {"seek", (PyCFunction) Pyaubio_source_seek, 287 350 METH_VARARGS, Py_source_seek_doc}, 351 {"__enter__", (PyCFunction)Pyaubio_source_enter, METH_NOARGS, 352 Pyaubio_source_enter_doc}, 353 {"__exit__", (PyCFunction)Pyaubio_source_exit, METH_VARARGS, 354 Pyaubio_source_exit_doc}, 288 355 {NULL} /* sentinel */ 289 356 }; … … 315 382 0, 316 383 0, 317 0,318 0,384 Pyaubio_source_iter, 385 (unaryfunc)Pyaubio_source_iter_next, 319 386 Py_source_methods, 320 387 Py_source_members,
Note: See TracChangeset
for help on using the changeset viewer.