Ignore:
File:
1 edited

Legend:

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

    r0e70ef9 rfcb6e8c  
    8181  }
    8282
    83   self->uri = "none";
     83  self->uri = NULL;
    8484  if (uri != NULL) {
    85     self->uri = uri;
     85    self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1));
     86    strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1);
    8687  }
    8788
    8889  self->samplerate = Py_aubio_default_samplerate;
    89   if ((sint_t)samplerate > 0) {
     90  if (samplerate != 0) {
    9091    self->samplerate = samplerate;
    91   } else if ((sint_t)samplerate < 0) {
    92     PyErr_SetString (PyExc_ValueError,
    93         "can not use negative value for samplerate");
    94     return NULL;
    9592  }
    9693
    9794  self->channels = 1;
    98   if ((sint_t)channels > 0) {
     95  if (channels != 0) {
    9996    self->channels = channels;
    100   } else if ((sint_t)channels < 0) {
    101     PyErr_SetString (PyExc_ValueError,
    102         "can not use negative or null value for channels");
    103     return NULL;
    10497  }
    10598
     
    110103Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds)
    111104{
    112   if (self->channels == 1) {
    113     self->o = new_aubio_sink ( self->uri, self->samplerate );
    114   } else {
    115     self->o = new_aubio_sink ( self->uri, 0 );
    116     aubio_sink_preset_channels ( self->o, self->channels );
    117     aubio_sink_preset_samplerate ( self->o, self->samplerate );
    118   }
     105  self->o = new_aubio_sink ( self->uri, 0 );
    119106  if (self->o == NULL) {
    120     PyErr_SetString (PyExc_RuntimeError, "error creating sink with this uri");
     107    // error string was set in new_aubio_sink
    121108    return -1;
    122109  }
     110  if (aubio_sink_preset_channels(self->o, self->channels) != 0) {
     111    // error string was set in aubio_sink_preset_channels
     112    return -1;
     113  }
     114  if (aubio_sink_preset_samplerate(self->o, self->samplerate) != 0) {
     115    // error string was set in aubio_sink_preset_samplerate
     116    return -1;
     117  }
     118
    123119  self->samplerate = aubio_sink_get_samplerate ( self->o );
    124120  self->channels = aubio_sink_get_channels ( self->o );
     
    132128  del_aubio_sink(self->o);
    133129  free(self->mwrite_data.data);
     130  if (self->uri) {
     131    free(self->uri);
     132  }
    134133  Py_TYPE(self)->tp_free((PyObject *) self);
    135134}
     
    205204}
    206205
     206static char Pyaubio_sink_enter_doc[] = "";
     207static PyObject* Pyaubio_sink_enter(Py_sink *self, PyObject *unused) {
     208  Py_INCREF(self);
     209  return (PyObject*)self;
     210}
     211
     212static char Pyaubio_sink_exit_doc[] = "";
     213static PyObject* Pyaubio_sink_exit(Py_sink *self, PyObject *unused) {
     214  return Pyaubio_sink_close(self, unused);
     215}
     216
    207217static PyMethodDef Py_sink_methods[] = {
    208218  {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc},
    209219  {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc},
    210220  {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc},
     221  {"__enter__", (PyCFunction)Pyaubio_sink_enter, METH_NOARGS,
     222    Pyaubio_sink_enter_doc},
     223  {"__exit__",  (PyCFunction)Pyaubio_sink_exit, METH_VARARGS,
     224    Pyaubio_sink_exit_doc},
    211225  {NULL} /* sentinel */
    212226};
Note: See TracChangeset for help on using the changeset viewer.