B
BlessYouBlessMe
Unregistered / Unconfirmed
GUEST, unregistred user!
以下程序是检测音量,为什么有时候在停止时waveInReset会不返回?
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Vol.h"
#include <Mmsystem.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
const int Buffer_Size=1024;// 44100;//11.025Khz*16*2/8=44100字节;
WAVEFORMATEX waveformat;
HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
BYTE WaveInBuffer[Buffer_Size*2];
MMRESULT res;
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2);
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
BYTE GetAverage()
{
int sum=0;
char byte;
for(int i=0;i<Buffer_Size*2;i++)
{
byte=WaveInBuffer-128;
sum+=abs(byte);
}
byte=sum/(Buffer_Size*2);
return byte;
}
//---------------------------------------------------------------------------
bool TForm1::StartCheck(void)
{
bool sucess;
Stop= false;
waveformat.wFormatTag=WAVE_FORMAT_PCM;
waveformat.nChannels=2;
waveformat.nSamplesPerSec=22050;
waveformat.nAvgBytesPerSec=22050*2;//nSamplesPerSec*nChannels*wBitPerSample/8
waveformat.nBlockAlign=4;
//nChannels*wBitsPerSample/8
waveformat.wBitsPerSample=8;//指定录音格式
waveformat.cbSize=0;
if ( waveInGetNumDevs()==0 ) {ShowMessage("无音频采集设备"return false;}
res=waveInOpen(&hWaveIn,WAVE_MAPPER, &waveformat, (DWORD)waveInProc,0L,CALLBACK_FUNCTION);
if ( res != MMSYSERR_NOERROR ){ ShowMessage("无法打开音频设备"return false;}
WaveInHdr.lpData = (char *)&WaveInBuffer[0];
memset(WaveInHdr.lpData, 0, Buffer_Size*2);
WaveInHdr.dwBufferLength = Buffer_Size*2;
WaveInHdr.dwBytesRecorded = 0;
WaveInHdr.dwUser = 0;
WaveInHdr.dwFlags= 0;
WaveInHdr.dwLoops= 0;
WaveInHdr.lpNext = 0;
WaveInHdr.reserved=0;
waveInPrepareHeader( hWaveIn, &WaveInHdr, sizeof(WAVEHDR) );
//准备内存块录音
waveInAddBuffer( hWaveIn, &WaveInHdr, sizeof(WAVEHDR) );
res = waveInStart(hWaveIn);
if ( res != MMSYSERR_NOERROR){ShowMessage("不能打开音频监测设备!"return false;}
return true;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StartCheck();
}
//---------------------------------------------------------------------------
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
{
BYTE vol;
if(uMsg!=MM_WIM_DATA ||Form1->Stop==true) //等待缓冲区处理完毕,才可以调用waveInReset.
{
return;
}
waveInUnprepareHeader(hwi,&WaveInHdr,sizeof(WAVEHDR));
vol=GetAverage();
Form1->ProgressBar1->Position=vol;
WaveInHdr.lpData=(char *)&WaveInBuffer[0];
//指向锁定的数据缓冲区的指针
WaveInHdr.dwBufferLength=Buffer_Size*2;
//数据缓冲区的大小
WaveInHdr.dwFlags=0;
waveInPrepareHeader(hwi,&WaveInHdr,sizeof(WAVEHDR));
waveInAddBuffer(hwi,&WaveInHdr,sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void TForm1::StopCheck(void)
{
Stop=true;
Sleep(50);
waveInReset(hWaveIn);
//为什么会死掉
waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
if(CheckBox1->Checked)
{
StopCheck();
}
else
{
StartCheck();
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Vol.h"
#include <Mmsystem.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
const int Buffer_Size=1024;// 44100;//11.025Khz*16*2/8=44100字节;
WAVEFORMATEX waveformat;
HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
BYTE WaveInBuffer[Buffer_Size*2];
MMRESULT res;
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2);
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
BYTE GetAverage()
{
int sum=0;
char byte;
for(int i=0;i<Buffer_Size*2;i++)
{
byte=WaveInBuffer-128;
sum+=abs(byte);
}
byte=sum/(Buffer_Size*2);
return byte;
}
//---------------------------------------------------------------------------
bool TForm1::StartCheck(void)
{
bool sucess;
Stop= false;
waveformat.wFormatTag=WAVE_FORMAT_PCM;
waveformat.nChannels=2;
waveformat.nSamplesPerSec=22050;
waveformat.nAvgBytesPerSec=22050*2;//nSamplesPerSec*nChannels*wBitPerSample/8
waveformat.nBlockAlign=4;
//nChannels*wBitsPerSample/8
waveformat.wBitsPerSample=8;//指定录音格式
waveformat.cbSize=0;
if ( waveInGetNumDevs()==0 ) {ShowMessage("无音频采集设备"return false;}
res=waveInOpen(&hWaveIn,WAVE_MAPPER, &waveformat, (DWORD)waveInProc,0L,CALLBACK_FUNCTION);
if ( res != MMSYSERR_NOERROR ){ ShowMessage("无法打开音频设备"return false;}
WaveInHdr.lpData = (char *)&WaveInBuffer[0];
memset(WaveInHdr.lpData, 0, Buffer_Size*2);
WaveInHdr.dwBufferLength = Buffer_Size*2;
WaveInHdr.dwBytesRecorded = 0;
WaveInHdr.dwUser = 0;
WaveInHdr.dwFlags= 0;
WaveInHdr.dwLoops= 0;
WaveInHdr.lpNext = 0;
WaveInHdr.reserved=0;
waveInPrepareHeader( hWaveIn, &WaveInHdr, sizeof(WAVEHDR) );
//准备内存块录音
waveInAddBuffer( hWaveIn, &WaveInHdr, sizeof(WAVEHDR) );
res = waveInStart(hWaveIn);
if ( res != MMSYSERR_NOERROR){ShowMessage("不能打开音频监测设备!"return false;}
return true;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StartCheck();
}
//---------------------------------------------------------------------------
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
{
BYTE vol;
if(uMsg!=MM_WIM_DATA ||Form1->Stop==true) //等待缓冲区处理完毕,才可以调用waveInReset.
{
return;
}
waveInUnprepareHeader(hwi,&WaveInHdr,sizeof(WAVEHDR));
vol=GetAverage();
Form1->ProgressBar1->Position=vol;
WaveInHdr.lpData=(char *)&WaveInBuffer[0];
//指向锁定的数据缓冲区的指针
WaveInHdr.dwBufferLength=Buffer_Size*2;
//数据缓冲区的大小
WaveInHdr.dwFlags=0;
waveInPrepareHeader(hwi,&WaveInHdr,sizeof(WAVEHDR));
waveInAddBuffer(hwi,&WaveInHdr,sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void TForm1::StopCheck(void)
{
Stop=true;
Sleep(50);
waveInReset(hWaveIn);
//为什么会死掉
waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
if(CheckBox1->Checked)
{
StopCheck();
}
else
{
StartCheck();
}
}
//---------------------------------------------------------------------------