From e3973967d2fa330df578164ed2e9020e59d4ee4f Mon Sep 17 00:00:00 2001 From: Nils Daumann Date: Wed, 24 May 2017 22:57:35 +0200 Subject: [PATCH] Fixed WASAPI input stream releasing a different buffer size than it got. This caused a SoundIOErrorStreaming error and all following reads would fail. It took a while in the microphone sample, but after ~30 seconds it would always fail for me. --- src/wasapi.c | 19 ++++++++++++++----- src/wasapi.h | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/wasapi.c b/src/wasapi.c index 9637f64..5556a6b 100644 --- a/src/wasapi.c +++ b/src/wasapi.c @@ -2084,7 +2084,9 @@ static int instream_begin_read_wasapi(struct SoundIoPrivate *si, struct SoundIoI { return SoundIoErrorStreaming; } - isw->read_buf_frames_left = frames_to_read; + isw->opened_buf_frames = frames_to_read; + isw->read_buf_frames_left = frames_to_read; + if (flags & AUDCLNT_BUFFERFLAGS_SILENT) isw->read_buf = NULL; } @@ -2096,6 +2098,8 @@ static int instream_begin_read_wasapi(struct SoundIoPrivate *si, struct SoundIoI for (int ch = 0; ch < instream->layout.channel_count; ch += 1) { isw->areas[ch].ptr = isw->read_buf + ch * instream->bytes_per_sample; isw->areas[ch].step = instream->bytes_per_frame; + + isw->areas[ch].ptr += instream->bytes_per_frame * (isw->opened_buf_frames - isw->read_buf_frames_left); } *out_areas = isw->areas; @@ -2109,10 +2113,15 @@ static int instream_begin_read_wasapi(struct SoundIoPrivate *si, struct SoundIoI static int instream_end_read_wasapi(struct SoundIoPrivate *si, struct SoundIoInStreamPrivate *is) { struct SoundIoInStreamWasapi *isw = &is->backend_data.wasapi; HRESULT hr; - if (FAILED(hr = IAudioCaptureClient_ReleaseBuffer(isw->audio_capture_client, isw->read_frame_count))) { - return SoundIoErrorStreaming; - } - isw->read_buf_frames_left -= isw->read_frame_count; + + isw->read_buf_frames_left -= isw->read_frame_count; + + if (isw->read_buf_frames_left <= 0) { + if (FAILED(hr = IAudioCaptureClient_ReleaseBuffer(isw->audio_capture_client, isw->opened_buf_frames))) { + return SoundIoErrorStreaming; + } + } + return 0; } diff --git a/src/wasapi.h b/src/wasapi.h index 5860f86..e27b3f8 100644 --- a/src/wasapi.h +++ b/src/wasapi.h @@ -100,6 +100,7 @@ struct SoundIoInStreamWasapi { bool started; char *read_buf; int read_buf_frames_left; + int opened_buf_frames; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; };