forked from auracaster/pyalsaaudio
Fix #51: Only return valid part of the buffer in the read function; avoid unnecesssary work by only changing size when needed
This commit is contained in:
10
alsaaudio.c
10
alsaaudio.c
@@ -1051,7 +1051,7 @@ alsapcm_read(alsapcm_t *self, PyObject *args)
|
|||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
int size = self->framesize * self->periodsize;
|
int size = self->framesize * self->periodsize;
|
||||||
int sizeout;
|
int sizeout = 0;
|
||||||
PyObject *buffer_obj, *tuple_obj, *res_obj;
|
PyObject *buffer_obj, *tuple_obj, *res_obj;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
|
|
||||||
@@ -1106,12 +1106,11 @@ alsapcm_read(alsapcm_t *self, PyObject *args)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res <= 0) {
|
if (res > 0 ) {
|
||||||
sizeout = 0;
|
|
||||||
} else {
|
|
||||||
sizeout = res * self->framesize;
|
sizeout = res * self->framesize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (size != sizeout) {
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
/* If the following fails, it will free the object */
|
/* If the following fails, it will free the object */
|
||||||
if (_PyString_Resize(&buffer_obj, sizeout))
|
if (_PyString_Resize(&buffer_obj, sizeout))
|
||||||
@@ -1121,7 +1120,8 @@ alsapcm_read(alsapcm_t *self, PyObject *args)
|
|||||||
if (_PyBytes_Resize(&buffer_obj, sizeout))
|
if (_PyBytes_Resize(&buffer_obj, sizeout))
|
||||||
return NULL;
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
res_obj = PyLong_FromLong(res);
|
res_obj = PyLong_FromLong(res);
|
||||||
if (!res_obj) {
|
if (!res_obj) {
|
||||||
Py_DECREF(buffer_obj);
|
Py_DECREF(buffer_obj);
|
||||||
|
|||||||
Reference in New Issue
Block a user