1

The sample code of WASAPI capture on MSDN, loops till the GetNextPacketSize return 0. I just want to understand when will this happen:

  1. Will it happen if there is silence registered on the microphone? (In this case will it loop infinitely if i keep making noise on microphone?)
  2. It depends on some audio capture fundamental concept which I am missing (I am quite new to audio APIs :)).

1 Answer 1

2

The API helps in determining the size of the data buffer to be captured so that API client does not need to guess or allocate a buffer with excess etc. The API will return zero when there is no data to capture yet (not a single frame). This can happen in ongoing audio capture session when/if you call the API too early, and the caller is basically expected to try once again later since new data can still be generated.

In some conditions zero return might indicate an end of the stream. Specifically, if you capture from loopback device and there are no active playback sessions that can generate data for loopback delivery, capture API might keep delivering no data until new playback session emerges.

The sample code loop checks for zero packet size in conjunction with Sleep call. This way the loop expects that at least some data is generated during the sleep time and under normal conditions of continuous generation of audio data there is no zero length returned every first call within the outer loop. The inner loop attempts to read as many non-empty buffers as possible until zero indicates that all data, which was ready for delivery, was already returned to the client.

Outer loop keeps running until sink passes end-of-capture event through bDone variable. There is a catch here that somehow inner loop might be rolling without breaking into outer loop - according to the sample code - and capture is not correctly stopped. The sample assumes that sink processes data fast enough so that inner loop could process all currently available data and break out to reach Sleep call. That is, the WASAPI calls are all non-blocking and in assumption that these loops runs pretty fast the idea is that audio data is processed faster than it is captured, and the loop spends most of the thread time being in the Sleep call. Perhaps not the best sample code for beginners. You can improve this by checking bDone in the inner loop as well, to make it more reliable.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Roman for your explanation. Now its clear to me. But there is still one more confusion for 'recording stop' scenario, in the sample code. There are 2 loops (outer while loop which checks to stop or not & inner while loop which checks for packet size). If the pMySink returns 'bDone' as true and the packets are still coming, then the recording will still go on even if the pMySink signalled to stop. On the other hand if we check for bDone in inner loop also then there is possibility of lost packets if there was data still to be read in the stream.
I appended above. bDone is assumed to be "end of capture" signal meaning that sink does not need new data. Apparently there can be new data available in general, esp. if the source is live. The sample does not have in mind that you might need to continue capture afterwards from the point which you indicated with bDone.
Thanks, now I understand it. Actually I am making an audio capture application and user can press record & stop buttons to start & stop recording respt. Recording (WASAPI capture) is done in separate non-ui thread & UI will signal for record stop, so my intention was to not capture extra but also not to have any cutoffs. Your explanation clears the confusions I had and now I will fine tune my implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.