From: ScottMcP [MVP] on
On Dec 22, 9:16 pm, "Larry" <dontmewit...(a)got.it> wrote:
>  btw this is some new code I have written...It seems to work...I'm wondering
> how I can put the recording into infinite...do I have to use threads??
>
> thanks,

For continuous recording you send a buffer to wavein while it is
already recording a buffer. Make sure it always has a buffer waiting
when it gets to the end of the previous buffer. For example, make 3
buffers and call waveInAddBuffer 3 times before you call waveInStart.
When the first buffer has been filled you receive the first WIM_DATA.
Process the data, then send that same buffer to wavein again by
calling waveInAddBuffer with it. Like juggling: It will keep filling
buffers and giving them to you, and you must keep giving buffers back
for reuse.
From: Larry on

"Larry" <dontmewithme(a)got.it> ha scritto nel messaggio
news:4b2df5c6$0$1106$4fafbaef(a)reader4.news.tin.it...
>

> Ok, I moving to CALLBACK_EVENT
>

I am just wondering why I cannot a regular message by the system. Instead, I
am getting a "3":

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)

{

WAVEHDR* pWaveHdr;

switch(uMsg)

{

case MM_WIM_DATA:

pWaveHdr = ((WAVEHDR*)dwParam1 );

switch(pWaveHdr->dwFlags)

{

case WHDR_BEGINLOOP:

std::cout << "WHDR_BEGINLOOP" << std::endl;

break;

case WHDR_DONE:

std::cout << "WHDR_DONE" << std::endl;

break;

case WHDR_ENDLOOP:

std::cout << "WHDR_ENDLOOP" << std::endl;

break;

case WHDR_INQUEUE:

std::cout << "WHDR_INQUEUE" << std::endl;

break;

case WHDR_PREPARED:

std::cout << "WHDR_PREPARED" << std::endl;

break;

case 3:

std::cout << "tre" << std::endl;

break;

}

std::cout << "MM_WIN_DATA" << std::endl;

std::cout << "dwFlags: " << pWaveHdr->dwFlags << std::endl;

std::cout << "dwBytesRecorded: " << pWaveHdr->dwBytesRecorded << std::endl;

std::cout << "dwuser: " << pWaveHdr->dwUser << std::endl;

std::cout << "lpData: " << sizeof(pWaveHdr->lpData) << std::endl;

break;

case MM_WIM_OPEN:

std::cout << "MM_WIN_OPEN" << std::endl;

break;

case MM_WIM_CLOSE:

std::cout << "MM_WIN_CLOSE" << std::endl;

break;

}

}

From: Larry on

"Larry" <dontmewithme(a)got.it> ha scritto nel messaggio
news:4b316f36$0$1102$4fafbaef(a)reader4.news.tin.it...
>
> "Larry" <dontmewithme(a)got.it> ha scritto nel messaggio
> news:4b2df5c6$0$1106$4fafbaef(a)reader4.news.tin.it...
>>
>
>> Ok, I moving to CALLBACK_EVENT
btw this is some new code I have written...It seems to work...I'm wondering
how I can put the recording into infinite...do I have to use threads??

thanks,

#include <windows.h>
#pragma comment (lib, "winmm.lib")
#include <mmsystem.h>
#include <iostream>
#include <stdlib.h> // Define "system" function
#include <string>
#define system_buf_len 32768

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2);
bool addbuffer(WAVEHDR *pWaveHdr);


int main()
{
// Definisco la struttura WAVEFORMATEX
WAVEFORMATEX waveFormat;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 1;
waveFormat.nSamplesPerSec = 44100;
waveFormat.nBlockAlign = (waveFormat.nChannels *
waveFormat.wBitsPerSample) / 8;
waveFormat.nAvgBytesPerSec = (waveFormat.nSamplesPerSec *
waveFormat.nBlockAlign);
waveFormat.cbSize = 0;

MMRESULT mmres; // ...
HWAVEIN phvi; // Handle for the input device
UINT uDevice = 0; // Device id "Gruppo Microfoni"

// waveInOpen
mmres = waveInOpen(&phvi,
uDevice,
(LPWAVEFORMATEX)&waveFormat,
(DWORD)waveInProc,
0,
CALLBACK_FUNCTION
);

// Prepare Buffer

int i=0;
int num_buff = 20;
WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);
for (i=0; i<num_buff; i++)
{
buffer[i].lpData = (LPSTR) malloc(system_buf_len);
buffer[i].dwBufferLength = system_buf_len;
buffer[i].dwBytesRecorded = 0;
buffer[i].dwUser = 0;
buffer[i].dwFlags = 0;
buffer[i].dwLoops = 0;

waveInPrepareHeader(phvi, &buffer[i], sizeof(WAVEHDR));
waveInAddBuffer(phvi, &buffer[i], sizeof(WAVEHDR));
}
//waveInStart;
waveInStart(phvi);

//waveInClose;
waveInClose(phvi);
system("pause");
return 0;
}

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1);
if (pWaveHdr && hwi)
{
if (pWaveHdr->dwFlags && WHDR_DONE == WHDR_DONE)
{
pWaveHdr->dwFlags = 0;
waveInUnprepareHeader(hwi, pWaveHdr, sizeof(WAVEHDR));
if (pWaveHdr->dwBytesRecorded > 0)
{
addbuffer(pWaveHdr);
}
delete[] pWaveHdr->lpData;
pWaveHdr->lpData = NULL;
}
}
break;

case MM_WIM_OPEN:
std::cout << "MM_WIN_OPEN" << std::endl;
break;
case MM_WIM_CLOSE:
std::cout << "MM_WIN_CLOSE" << std::endl;
break;
}
}

bool addbuffer(WAVEHDR *pWaveHdr)
{
std::cout << pWaveHdr->dwBytesRecorded << std::endl;
std::cout << pWaveHdr->lpData << std::endl;
return true;
}

From: Larry on

"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:aa9c2793-fbef-4fd8-94c6-25229c7c77ac(a)t12g2000vbk.googlegroups.com...

> For continuous recording you send a buffer to wavein while it is
> already recording a buffer. Make sure it always has a buffer waiting
> when it gets to the end of the previous buffer. For example, make 3
> buffers and call waveInAddBuffer 3 times before you call waveInStart.
> When the first buffer has been filled you receive the first WIM_DATA.
> Process the data, then send that same buffer to wavein again by
> calling waveInAddBuffer with it. Like juggling: It will keep filling
> buffers and giving them to you, and you must keep giving buffers back
> for reuse.

It sound as the only solutionj to go for! great! Yet, I do not seem to get
it working correctly...I tried adding the waveInAddBuffer in different
position inside the WIN_DATA scope:

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1);
if (pWaveHdr && hwi)
{
if (pWaveHdr->dwFlags && WHDR_DONE == WHDR_DONE)
{
pWaveHdr->dwFlags = 0;
waveInUnprepareHeader(hwi, pWaveHdr, sizeof(WAVEHDR));
if (pWaveHdr->dwBytesRecorded > 0)
{
addbuffer(pWaveHdr);
//waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
delete[] pWaveHdr->lpData;
pWaveHdr->lpData = NULL;
//waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
}
break;

case MM_WIM_OPEN:
std::cout << "MM_WIN_OPEN" << std::endl;
break;
case MM_WIM_CLOSE:
std::cout << "MM_WIN_CLOSE" << std::endl;
break;
}
}

From: Larry on

"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:90e0fd36-f591-411e-a020-80c5a0ea4180(a)v7g2000vbd.googlegroups.com...
On Dec 23, 6:58 am, "Larry" <dontmewit...(a)got.it> wrote:
> It sound as the only solutionj to go for! great! Yet, I do not seem to get
> it working correctly...I tried adding the waveInAddBuffer in different
> position inside the WIN_DATA scope:

> If you are going to reuse the buffer do not call waveInUnprepareHeader
> and do not delete the buffer!

Well, at first I did not understand why I should leave the buffer as is as
well as not calling the waveInUnprepare function! I thought I had to pass an
epty buffer to waveIn with empty flags (set to zero)

Now the code below is working great!

void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1);
if (pWaveHdr && hwi)
{
if (pWaveHdr->dwFlags && WHDR_DONE == WHDR_DONE)
{
addbuffer(pWaveHdr);
waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
}
break;

case MM_WIM_OPEN:
std::cout << "MM_WIN_OPEN" << std::endl;
break;
case MM_WIM_CLOSE:
std::cout << "MM_WIN_CLOSE" << std::endl;
break;
}
}

bool addbuffer(WAVEHDR *pWaveHdr)
{
std::cout << pWaveHdr->dwBytesRecorded << std::endl;
char * buff = pWaveHdr->lpData;
std::ofstream usc;
usc.open("sound.wave", std::ios::app|std::ios::binary);
usc.write(buff,pWaveHdr->dwBytesRecorded);
usc.close();

return true;
}

Although the .wave file is not playable by any wav player (maybe I have to
add some RIFF header at the beginning)

> As I pointed out earlier, the documentation says you are not allowed
> to call the wave functions from inside the waveInProc. That may be
> part of your problem. (Or it may not. I have heard that it works on
> some versions of Windows and fails on other versions.)

I am going to change to EVENT. Just wanted to check FUNCTION out first.

thanks