From: Larry on
> If you use CALLBACK_EVENT then create an event with CreateEvent, pass
> the event handle in dwCallback, and wait for the recording to finish
> by calling WaitForSingleObject with the event handle.
> WaitForSingleObject will suspend your code until the buffer has been
> filled, then it will return.

Ok, I can I catch the data? I mean, if I create an event and wait for its
response with "WaitForSingleObject" I no longer have the parameters I used
to with the regular CALLBACK.

Let me explain with the following code:


**code
HANDLE hEvent = CreateEvent(NULL,TRUE,TRUE,NULL);
HWAVEIN hwi;

waveInOpen(&hwi,0,(LPWAVEFORMATEX)&pwf,(DWORD)hEvent,0,CALLBACK_EVENT );
waveInPrepareHeader(...);
waveInAddBuffer(...);
waveInStart(hwi);

ResetEvent(hEvent); // Should I?

while(1)
{
WaitForSingleObject(event, INFINITE);
...process buffer...how can?...

waveInAddBuffer(...);
}
**code

From where should I get the lpData end other flags ??

thanks

From: ScottMcP [MVP] on
On Dec 25, 8:48 pm, "Larry" <dontmewit...(a)got.it> wrote:
> > If you use CALLBACK_EVENT then create an event with CreateEvent, pass
> > the event handle in dwCallback, and wait for the recording to finish
> > by calling WaitForSingleObject with the event handle.
> > WaitForSingleObject will suspend your code until the buffer has been
> > filled, then it will return.
>
> Ok, I can I catch the data? I mean, if I create an event and wait for its
> response with "WaitForSingleObject" I no longer have the parameters I used
> to with the regular CALLBACK.
>
> Let me explain with the following code:
>
> **code
> HANDLE hEvent = CreateEvent(NULL,TRUE,TRUE,NULL);
> HWAVEIN hwi;
>
> waveInOpen(&hwi,0,(LPWAVEFORMATEX)&pwf,(DWORD)hEvent,0,CALLBACK_EVENT );
> waveInPrepareHeader(...);
> waveInAddBuffer(...);
> waveInStart(hwi);
>
> ResetEvent(hEvent); // Should I?
>
> while(1)
> {
>  WaitForSingleObject(event, INFINITE);
>  ...process buffer...how can?...
>
>  waveInAddBuffer(...);}
>
> **code
>
> From where should I get the lpData end other flags ??
>
> thanks

You allocated all the WAVEINHDR structs and the lpData. Don't lose
track of them. After all, you're supposed to delete them later, when
you're finished.

The dwUser field in WAVEINHDR can be used by you. For example, put a
0 in there for buffer 0, a 1 in there for buffer 1, etc.

When WaitForSingleObject returns you are going to have to assume which
buffer finished. Of course, if you have a nextbuffernumber counter
that starts at zero you can use that. Make it an index into wherever
your array of WAVEINHDR is.

From: Larry on
"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:4c1e1254-2c4f-4fd5-88cd-5a66d5e32d98(a)n13g2000vbe.googlegroups.com...


> You allocated all the WAVEINHDR structs and the lpData. Don't lose
> track of them. After all, you're supposed to delete them later, when
> you're finished.

> The dwUser field in WAVEINHDR can be used by you. For example, put a
> 0 in there for buffer 0, a 1 in there for buffer 1, etc.

> When WaitForSingleObject returns you are going to have to assume which
> buffer finished. Of course, if you have a nextbuffernumber counter
> that starts at zero you can use that. Make it an index into wherever
> your array of WAVEINHDR is.

Ok. Suppose I am just using ONE single buffer. Hopefully it'll help me
understand better!

The recording function is called with CreateThread() and the following is
its code:

DWORD WINAPI start_recorder(const LPVOID lpParam)
{
// Define WAVEFORMATEX
WAVEFORMATEX wf;
(...)

// I pass to the waveInOpen() function the "hevent" HANDLE to an event a
created with,
// CreateEvent(NULL, TRUE, TRUE, NULL); in the main(); Its HANDLE (hevent)
is global.
waveInOpen(&hwi,0,(LPWAVEFORMATEX)&wf,(DWORD)hevent,0,CALLBACK_EVENT);

// Define WAVEHDR Structure:
WAVEHDR *buff = new (WAVEHDR);
buff->lpData = (LPSTR) malloc(system_buf_len);
buff->dwBufferLength = system_buf_len;
buff->dwBytesRecorded = 0;

//Prepare, add and Start
waveInPrepareHeader(hwi, buff, sizeof(WAVEHDR));
waveInAddBuffer(hwi, buff, sizeof(WAVEHDR));
waveInStart(hwi);

//ResetEvent()
ResetEvent(hevent);

//Loop...
while(1)
{
WaitForSingleObject(hevent, INFINITE);

if (buff->dwFlags)
{
printf("%d", buff->dwBytesRecorded); printf("\n");
}
waveInAddBuffer(hwi,buff, sizeof(WAVEHDR));

if(stop_thread_flag)
break;
}
}

waveInClose(hwi);
return 0;
} // end function

is this correct approach?? NB. in buff->dwBytesRecorded I am getting
differents values each time...I thought I'd get 32768 always...

Anyway, you can have a view at the whole code here:
http://theartofweb.net/cpp/recorder.txt

thanks

From: ScottMcP [MVP] on
On Dec 26, 1:16 pm, "Larry" <dontmewit...(a)got.it> wrote:
> "ScottMcP [MVP]" <scott...(a)mvps.org> ha scritto nel messaggionews:4c1e1254-2c4f-4fd5-88cd-5a66d5e32d98(a)n13g2000vbe.googlegroups.com...
>
> > You allocated all the WAVEINHDR structs and the lpData.  Don't lose
> > track of them.  After all, you're supposed to delete them later, when
> > you're finished.
> > The dwUser field in WAVEINHDR can be used by you.  For example, put a
> > 0 in there for buffer 0, a 1 in there for buffer 1, etc.
> > When WaitForSingleObject returns you are going to have to assume which
> > buffer finished.  Of course, if you have a nextbuffernumber counter
> > that starts at zero you can use that.  Make it an index into wherever
> > your array of WAVEINHDR is.
>
> Ok. Suppose I am just using ONE single buffer. Hopefully it'll help me
> understand better!
>
> The recording function is called with CreateThread() and the following is
> its code:
>
> DWORD WINAPI start_recorder(const LPVOID lpParam)
> {
>  // Define WAVEFORMATEX
> WAVEFORMATEX wf;
> (...)
>
> // I pass to the waveInOpen() function the "hevent" HANDLE to an event a
> created with,
> // CreateEvent(NULL, TRUE, TRUE, NULL); in the main(); Its HANDLE (hevent)
> is global.
> waveInOpen(&hwi,0,(LPWAVEFORMATEX)&wf,(DWORD)hevent,0,CALLBACK_EVENT);
>
>  // Define WAVEHDR Structure:
>  WAVEHDR *buff         = new (WAVEHDR);
>  buff->lpData          = (LPSTR) malloc(system_buf_len);
>  buff->dwBufferLength  = system_buf_len;
>  buff->dwBytesRecorded = 0;
>
> //Prepare, add and Start
> waveInPrepareHeader(hwi, buff, sizeof(WAVEHDR));
> waveInAddBuffer(hwi, buff, sizeof(WAVEHDR));
> waveInStart(hwi);
>
> //ResetEvent()
> ResetEvent(hevent);
>
> //Loop...
> while(1)
>  {
>   WaitForSingleObject(hevent, INFINITE);
>
>   if (buff->dwFlags)
>   {
>    printf("%d", buff->dwBytesRecorded); printf("\n");
>   }
>   waveInAddBuffer(hwi,buff, sizeof(WAVEHDR));
>
>   if(stop_thread_flag)
>    break;
>  }
>
> }
>
> waveInClose(hwi);
> return 0;
>
> } // end function
>
> is this correct approach?? NB. in buff->dwBytesRecorded I am getting
> differents values each time...I thought I'd get 32768 always...
>
> Anyway, you can have a view at the whole code here:http://theartofweb.net/cpp/recorder.txt
>
> thanks

I see only a couple of small problems. You don't show any
initialization of buff->dwFlags but your code tests it for some
reason. And you have set the event to manual reset, so it needs a
ResetEvent call right after WaitForSingleObject returns. (Or use
CreateEvent(NULL, 0, 0, NULL) so you don't need to ever reset it.)
From: Larry on
"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:b064d2c6-57ab-4fe1-8356-d8d4e3a66f15(a)r12g2000vbm.googlegroups.com...

> I see only a couple of small problems. You don't show any
> initialization of buff->dwFlags but your code tests it for some
> reason. And you have set the event to manual reset, so it needs a
> ResetEvent call right after WaitForSingleObject returns. (Or use
> CreateEvent(NULL, 0, 0, NULL) so you don't need to ever reset it.)

I'm sorry that's because I was writing fast to the group!

Actually, I did initializate buff->dwFlags to "0".

Anyway, now that I am using CreateEvent(NULL,0,0,NULL) it is working!

// Loop...
while(1)
{
WaitForSingleObject(hevent, INFINITE);
save_buffer(buff);
waveInAddBuffer(hwi, buff, sizeof(WAVEHDR));

if(stop_thread_flag)
break;
}

waveInClose(hwi);
return 0;
}

VOID save_buffer(WAVEHDR *pWaveHdr)
{
printf("%d", pWaveHdr->dwBytesRecorded); printf("\n");
}

Yet, this is using just one single buffer :-(