2 buffers to record audio

Hello to everybody:

I am writing a c++ command-line program that picks up the samples of the audio card using the API. In order to this, I use the "waveIn" functions: waveInOpen (), waveInPrepareHeader (), waveInAddBuffer () etc., and everything works ok.

The problem arises when I want to use 2 buffers, in order to create a circular (infinite) way of picking up data. I visualize the lpData of the WAVEHDR structure, and I find that the two buffers work fine the first time, but not the others, because there is a big drop of +/- 30 samples in the final signal.

I have some doubts about the way I am using the estructures:

- When buf2 is capturing data, how can I put the buf1 again in the cue? Is it necessary to waveInUnprepareHeader() and waveInAddBuffer(), or is enough with waveInAddBuffer()?

- Do I have to change the values of the WAVEHDR structure of the buffers once they are finished, before adding them to the cue again?

- Is it necessary to use CALLBACK_FUNCTION when I open the audio card (I do not, I use CALLBACK_NULL), or is it enough, in order to know when a buffer is full, with the dwFlags:

while((first_buffer.dwFlags & WHDR_DONE) != TRUE)

- Is it always sufficient with 2 buffers? Or does it depend on the length of them?

Perhaps these are too many questions for a message; if anyone knows where I can find the code, I would be very grateful for this.

Best regards,

Xagutxu


i've never done any work recording audio, but I did a little searching and have a few ideas.

1) more detail on how you are implementing your method may be helpful. although, at one point in my search, i did find a loop similar to yours, however, it was using 'waveInUnprepareHeader()' and checking to see if it was equal to a constant that represented that the buffer was still recording... perhaps this is of use?

2) from what i could find, it appears you can just put buffers back in the queue by using 'waveInAddBuffer()'.

3) also from what i could find, the number of buffers does seem to depend on how you are using them, and how long they are. since you are using c++, i have a suggestion:
why not create an STL queue in which you store pointers to buffers you allocate. as each buffer gets filled, add it's pointer to the queue, at which point you pass a newly allocated buffer to the driver to fill, and can take your time to process and deallocate buffers that have been added to your queue... that way, you are essentially creating a dynamic buffer system. (of course you will probably want to keep track of and limit the number of buffers you use to limit resource usage)

4) it would also appear that setting up a callback function is the generally accepted "correct" way of dealing with multiple buffers, but i could not really find more in depth information.

5) if you haven't already, MSDN is a great reference for each of the individual functions, and sometimes you can find relevant articles on how to assemble sequences of the functions to accomplish a task.
Last edited on
Topic archived. No new replies allowed.