HWND hwndMain; //Main window handle
HDC whdc;
HPEN hpenB,hpenG,hpenOld;
MMRESULT num;
HWAVEIN hwi;
WAVEHDR *pwhi,whis[MAX_INQUEU];
unsigned char waveBufferRecord[MAX_INQUEU][BUFSIZE];
long currWavHdr=0;
DWORD sqrtPoints;
#if DEBUG
FILE *fp;
#endif
WAVEFORMATEX wfx;
bool flag;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
long k;
memset(&wfx,0,sizeof(WAVEFORMATEX));
wfx.wFormatTag=WAVE_FORMAT_PCM;
wfx.nChannels= 1;
wfx.wBitsPerSample= 8;
wfx.nSamplesPerSec= 22050L;
wfx.nBlockAlign= 1;
wfx.nAvgBytesPerSec= 22050L;
wfx.cbSize=0;
waveInOpen(&hwi,0,&wfx,(unsigned long)Handle,0,CALLBACK_WINDOW|WAVE_MAPPED);
for(k=0;k {
pwhi=&whis[k];
pwhi->dwFlags=0;
pwhi->dwLoops=0;
pwhi->dwBytesRecorded=0;
pwhi->dwBufferLength=BUFSIZE;
pwhi->lpData=waveBufferRecord[k];
}
pwhi=&whis[currWavHdr];
waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
#if DEBUG
fp = fopen("data.txt","w+");
#endif
if(flag == false)
{
waveInOpen(&hwi,0,&wfx,(unsigned long)Handle,0,CALLBACK_WINDOW|WAVE_MAPPED);
pwhi=&whis[currWavHdr];
waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
waveInStart(hwi);
}
waveInStart(hwi);
}
//---------------------------------------------------------------------------
void TForm1::DrawWave(WAVEHDR *whdr,HDC hdc)
{
static unsigned char prevBuf[BUFSIZE];
int i,SumCount = 0;
long sum = 0 ;
unsigned char Max = 0;
unsigned char Min = 255;
hpenOld = SelectObject(hdc, hpenB);
memcpy(prevBuf,waveBufferRecord[1-currWavHdr], BUFSIZE);
MoveToEx(hdc,0,prevBuf[0],NULL);
for(i=0;i {
LineTo(hdc,i,(unsigned int)prevBuf[i]);
#if DEBUG
fprintf(fp,"i%d=%d\n",i,(unsigned int)prevBuf[i]);
#endif
if (prevBuf[i]>Max)
Max = prevBuf[i];
if(prevBuf[i] Min = prevBuf[i];
sum+= prevBuf[i];
SumCount++;
Label1->Width = prevBuf[i];
}
Label1->Width = Max - Min;
SelectObject(hdc, hpenOld);
#if DEBUG
fprintf(fp,"sum=%d\n",sum/BUFSIZE + 200);
#endif
}
void _fastcall TForm1::CallBackPlay(TMessage *Message)
{
WAVEHDR *pwhi_tmp;
pwhi_tmp = pwhi;
whdc=GetDC(hwndMain);
waveInUnprepareHeader(hwi,pwhi,sizeof(WAVEHDR));
currWavHdr=1-currWavHdr;
pwhi=&whis[currWavHdr];
waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
TRect Clr;
Clr.Left = 0;
Clr.Top = 0;
Clr.Right = Screen->Monitors[0]->Width;
Clr.Bottom = Screen->Monitors[0]->Height/2;
Brush->Color = clWhite;
Canvas->Brush->Style = bsSolid;
Canvas->FillRect(Clr);
DrawWave(pwhi_tmp,whdc);
ReleaseDC(hwndMain,whdc);
}
void __fastcall TForm1::FormShow(TObject *Sender)
{
hwndMain = (HWND)Form1->Handle;
hpenG = CreatePen(PS_SOLID, 0, RGB(0, 255, 0));
hpenB = CreatePen(PS_SOLID, 0, RGB(0, 0, 0));
flag = true;
Label1->Left = 0;
Label1->Height = 30;
Label1->Top = Screen->Monitors[0]->Height - Label1->Height - 150;
Label1->Width = 120;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i;
waveInStop(hwi);
waveInUnprepareHeader(hwi,pwhi,sizeof(WAVEHDR));
DeleteObject(hpenB);
DeleteObject(hpenG);
DeleteObject(hpenOld);
waveInClose(hwi);
#if DEBUG
fclose(fp);
#endif
flag = false;
}