WaveForm API / dealing with many buffers
Hi,
I am still stuck at programming with the waveForm API on windows.
Actually, I managed to write some code to record the audio from the mic real
time. Yet, I'd like to use more then one buffer to do it in order to avoid
choppy recording.
In a nutshell, I should pass the waveInPrepareHeader and waveInAddBuffer
more then one single buffer, something like this:
[code]
// Define WAVEHDR Structure:
WAVEHDR *buff = new WAVEHDR[num_buffers];
for (int i = 0; i<num_buffers; i++)
{
buff[i].lpData = (LPSTR) malloc(system_buf_len);
buff[i].dwBufferLength = system_buf_len;
buff[i].dwBytesRecorded = 0;
buff[i].dwUser = 0;
buff[i].dwFlags = 0;
buff[i].dwLoops = 0;
if(waveInPrepareHeader(hwi, &buff[i], sizeof(WAVEHDR)) !=
MMSYSERR_NOERROR)
printf("waveInPrepareHedare: ERROR!\n"); // return -1;
if(waveInAddBuffer(hwi, &buff[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
printf("waveInAddBuffer: ERROR!\n"); // return -1;
}
if(waveInStart(hwi) != MMSYSERR_NOERROR)
printf("waveInStart: ERROR!\n"); // return -1;
[/code]
The system sends me messages about the filling of a buffer by using a
CALLBACK_EVENT, so I have to wait for it with WaitForSingleObject()
function.
Now, I am stuck at guessing how to capture the single filled buffer from the
array of buffers...I tried something like this:
[code]
// Loop...
while(1)
{
if(stop_thread_flag)
break;
// CALLBACK EVENT
WaitForSingleObject(hevent, INFINITE);
for (int k = 0; k<num_buffers; k++)
{
if(buff[k].dwFlags & WHDR_DONE)
{
save_buffer(&buff[k]);
waveInAddBuffer(hwi, &buff[k], sizeof(WAVEHDR));
}
}
}
for (int u = 0; u<num_buffers; u++)
{
waveInUnprepareHeader(hwi, &buff[u], sizeof(WAVEHDR));
}
waveInClose(hwi);
return 0;
[/code]
Although it seems to be working, I am not sure wheter it is the right way to
deal with that. Do you think I shoud add a "last" after I check for the
flags to be WHDR_DONE? How can I make sure I am not getting the same single
buffer twice or more?
The whole code is avaible here:
http://theartofweb.net/cpp/waveform_recorder_05.txt
thanks