Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero fill output buffer in C callback #22

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions pysoundio/_soundiox.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ pysoundio__set_read_callback(PyObject *self, PyObject *args)
return NULL;
}
Py_XINCREF(temp);
// Py_XDECREF(rc.read_callback);
Py_XDECREF(rc.read_callback);
rc.read_callback = temp;
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -1008,6 +1008,7 @@ write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int fram
{
struct RecordContext *rc = outstream->userdata;
struct SoundIoChannelArea *areas;
int frame_count;
int err;

if (!rc->output_buffer)
Expand All @@ -1020,8 +1021,34 @@ write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int fram
int read_count = min_int(frame_count_max, fill_count);
int frames_left = read_count;

if (frame_count_min > fill_count) {
frames_left = frame_count_min;
while (frames_left > 0) {
frame_count = frames_left;
if (frame_count <= 0)
return;
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
PyErr_SetString(PySoundIoError, soundio_strerror(err));
return;
}
if (frame_count <= 0)
return;
for (int frame = 0; frame < frame_count; frame += 1) {
for (int ch = 0; ch < outstream->layout.channel_count; ch += 1) {
memset(areas[ch].ptr, 0, outstream->bytes_per_sample);
areas[ch].ptr += areas[ch].step;
}
}
if ((err = soundio_outstream_end_write(outstream))) {
PyErr_SetString(PySoundIoError, soundio_strerror(err));
return;
}
frames_left -= frame_count;
}
}

while (frames_left > 0) {
int frame_count = frames_left;
frame_count = frames_left;
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
PyErr_SetString(PySoundIoError, soundio_strerror(err));
return;
Expand Down
6 changes: 0 additions & 6 deletions pysoundio/pysoundio.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,5 @@ def write_callback(data, length):
pystream.contents.sample_rate * pystream.contents.bytes_per_frame)
self._create_output_ring_buffer(capacity)
self._clear_output_buffer()

length = self.block_size if self.block_size else 4096
data = bytearray(b'\x00' * length * self.output_bytes_per_frame)
soundio.ring_buffer_write_ptr(self.output_buffer, data, len(data))
soundio.ring_buffer_advance_write_ptr(self.output_buffer, len(data))

self._start_output_stream()
self.flush()