From ae7fc0177ba39ce687836006f3f7da3399385458 Mon Sep 17 00:00:00 2001 From: Ruslan Prokopchuk Date: Mon, 27 Mar 2017 10:39:38 +0300 Subject: [PATCH] fix microphone example fill with zeros case Usually when microphone example is run you never hit buffer underflow and "fill with zeros" branch is never executed. But when it's executed it fails with "invalid value" because of checks performed on frame_count. This platform-independent check https://github.com/andrewrk/libsoundio/blob/8094dc52498dc97dcadd60c35df737c86d6c6614/src/soundio.c#L447 sometimes passed because unitialized local frame_count have arbitrary value, but this CoreAudio one https://github.com/andrewrk/libsoundio/blob/8094dc52498dc97dcadd60c35df737c86d6c6614/src/coreaudio.c#L1078 is not passed, and "fill with zeros" scenario always fails on MacOS. --- example/sio_microphone.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/example/sio_microphone.c b/example/sio_microphone.c index 0e3d3d4..0fa8cca 100644 --- a/example/sio_microphone.c +++ b/example/sio_microphone.c @@ -113,6 +113,7 @@ static void read_callback(struct SoundIoInStream *instream, int frame_count_min, static void write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int frame_count_max) { struct SoundIoChannelArea *areas; + int frames_left; int frame_count; int err; @@ -122,7 +123,11 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m if (frame_count_min > fill_count) { // Ring buffer does not have enough data, fill with zeroes. + frames_left = frame_count_min; for (;;) { + frame_count = frames_left; + if (frame_count <= 0) + return; if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) panic("begin write error: %s", soundio_strerror(err)); if (frame_count <= 0) @@ -135,11 +140,12 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m } if ((err = soundio_outstream_end_write(outstream))) panic("end write error: %s", soundio_strerror(err)); + frames_left -= frame_count; } } int read_count = min_int(frame_count_max, fill_count); - int frames_left = read_count; + frames_left = read_count; while (frames_left > 0) { int frame_count = frames_left;