Changeset 6dda1c0


Ignore:
Timestamp:
Feb 27, 2017, 1:14:49 AM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, sampler
Children:
23be736
Parents:
f1f2e7e
Message:

python/ext/py-source.c: add iterator interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/ext/py-source.c

    rf1f2e7e r6dda1c0  
    283283  Pyaubio_source_close(self, unused);
    284284  return Pyaubio_source_close(self, unused);
     285}
     286
     287static PyObject* Pyaubio_source_iter(PyObject *self) {
     288  Py_INCREF(self);
     289  return (PyObject*)self;
     290}
     291
     292static PyObject* Pyaubio_source_iter_next(Py_source *self) {
     293  PyObject *done;
     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  }
     304  PyObject *size = PyTuple_GetItem(done, 1);
     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  }
    285331}
    286332
     
    331377  0,
    332378  0,
    333   0,
    334   0,
     379  Pyaubio_source_iter,
     380  (unaryfunc)Pyaubio_source_iter_next,
    335381  Py_source_methods,
    336382  Py_source_members,
Note: See TracChangeset for help on using the changeset viewer.