www.pudn.com > IP_phone.rar > WaveOut.cpp
/**********************************************************/
/*类名:CWaveIn */
/*简述:封装的音频播放类 */
/**********************************************************/
// WaveOut.cpp: implementation of the CWaveOut class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "../NC_Client.h"
#include "WaveOut.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#pragma comment(lib,"Winmm")
DWORD CWaveOut::s_dwInstance = 0;
DWORD WINAPI CWaveOut::AudioOutThreadProc(LPVOID lpParameter);
CWaveOut::CWaveOut()
{
m_wChannel=1;
m_dwSample=WISA_POOR;
m_wBit=16;
m_hOut=0;
m_hAudioOut=0;
m_dwAudioOutId=0;
m_iBufferNum=0;
m_bThreadStart=false;
m_bDevOpen=false;
s_dwInstance++;
}
CWaveOut::~CWaveOut()
{
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::Ini()
{
//启动监听线程
if (!StartThread())
{
return false;
};
//打开输出设备
if (!OpenDev())
{
StopThread ();
};
return true;
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::Release()
{
CloseDev();
StopThread ();
return true;
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::StartThread()
{
if (m_bThreadStart)
return false;
m_hAudioOut=CreateThread(0,0,AudioOutThreadProc,this,0,&m_dwAudioOutId);
if(!m_hAudioOut)
{
AfxMessageBox("监听线程启动失败!");
return false;
}
m_bThreadStart=true;
return true;
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::StopThread()
{
if (!m_bThreadStart)
return false;
if(m_hAudioOut)
{
int t=50;
DWORD ExitCode;
BOOL bEnd=false;
PostThreadMessage(m_dwAudioOutId,WM_QUIT,0,0);
while(t)
{
GetExitCodeThread(m_hAudioOut,&ExitCode);
if(ExitCode!= STILL_ACTIVE)
{
bEnd=true;
break;
}
else
Sleep(10);
t--;
}
if(!bEnd)
{
TerminateThread(m_hAudioOut,0);
}
m_hAudioOut=0;
}
m_bThreadStart=false;
return true;
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::OpenDev()
{
if (m_bDevOpen)
return false;
//定义音频格式
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = m_wChannel;
wfx.nSamplesPerSec = m_dwSample;
wfx.nAvgBytesPerSec = m_wChannel * m_dwSample * m_wBit / 8;
wfx.nBlockAlign = m_wBit * m_wChannel / 8;
wfx.wBitsPerSample = m_wBit;
wfx.cbSize = 0;
m_mmr=waveOutOpen (0,WAVE_MAPPER,&wfx,0,0,WAVE_FORMAT_QUERY);
if(m_mmr)
{
AfxMessageBox("waveOutOpen error!");
return false;
}
m_mmr=waveOutOpen(&m_hOut,WAVE_MAPPER,&wfx,m_dwAudioOutId,s_dwInstance,CALLBACK_THREAD);
if(m_mmr)
{
AfxMessageBox("waveOutOpen error!");
return false;
}
m_bDevOpen=true;
m_iBufferNum=0;
return true;
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::CloseDev()
{
if (!m_bDevOpen)
return false;
if(!m_hOut)
{
AfxMessageBox("Device hasn't opened!");
return false;
}
m_mmr=waveOutClose(m_hOut);
if(m_mmr)
{
AfxMessageBox("waveInClose error!");
return false;
}
m_hOut=0;
m_bDevOpen = false;
return true;
}
//////////////////////////////////////////////////////////////////////
MMRESULT CWaveOut::GetLastMMError()
{
return m_mmr;
}
/////////////////////////////////////////////////////////////////////////
CString CWaveOut::GetLastErrorString()
{
char buffer[256];
memset(buffer,0,256);
waveInGetErrorText(m_mmr,buffer,256);
return buffer;
}
/////////////////////////////////////////////////////////////////////////
void CWaveOut::SetBit(WORD wBit)
{
m_wBit = (wBit == 8) ? 8:16;
}
/////////////////////////////////////////////////////////////////////////
void CWaveOut::SetSample(DWORD dwSample)
{
m_dwSample = dwSample;
}
/////////////////////////////////////////////////////////////////////////
void CWaveOut::SetChannel(WORD wChannel)
{
m_wChannel = (m_wChannel == wChannel) ? 2:1;
}
/////////////////////////////////////////////////////////////////////////
WORD CWaveOut::GetBit()
{
return m_wBit;
}
/////////////////////////////////////////////////////////////////////////
DWORD CWaveOut::GetSample()
{
return m_dwSample;
}
/////////////////////////////////////////////////////////////////////////
WORD CWaveOut::GetChannel()
{
return m_wChannel;
}
/////////////////////////////////////////////////////////////////////////
DWORD CWaveOut::GetInstance()
{
return s_dwInstance;
}
/////////////////////////////////////////////////////////////////////////
int CWaveOut::GetBufferNum()
{
int iTemp;
m_csLock.Lock ();
iTemp = m_iBufferNum;
m_csLock.Unlock ();
return iTemp;
}
//////////////////////////////////////////////////////////////////////
void CWaveOut::BufferSub()
{
m_csLock.Lock ();
m_iBufferNum --;
m_csLock.Unlock ();
}
//////////////////////////////////////////////////////////////////////
void CWaveOut::BufferAdd()
{
m_csLock.Lock ();
m_iBufferNum ++;
m_csLock.Unlock ();
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::SetFormatByFile(CString file)
{
#pragma pack(push, 1)
struct FileHeader
{
char cFlagFiff[4];
unsigned __int32 iFileLen;
char cFlagWave[4];
char cFlagFmt[4];
char cResove[4];
unsigned __int16 cFormat;
unsigned __int16 cChannel;
unsigned __int32 cSample;
unsigned __int32 cBytePerSec;
unsigned __int16 cByteprocess;
unsigned __int16 cBit;
char cFlagDat[4];
unsigned __int32 iLenData;
};
#pragma pack(pop)
CFile fi;
if (!fi.Open(file,CFile::modeRead,NULL))
{
return false;
};
struct FileHeader head;
fi.Read ((void *)&head,sizeof (head));
fi.Close ();
this->SetChannel (head.cChannel);
this->SetSample (head.cSample);
this->SetBit (head.cBit);
return true;
}
//////////////////////////////////////////////////////////////////////
BOOL CWaveOut::Play(char* buf,UINT uSize)
{
if (!m_bDevOpen)
return false;
if (GetBufferNum()>PLAY_DELAY)
{
AfxMessageBox("Delay too big pass over!");
return true;
}
char* p;
LPWAVEHDR pwh=new WAVEHDR;
if(!pwh)
{
AfxMessageBox("alloc WAVEHDR memoyr error!");
return false;
}
p=new char[uSize];
if(!p)
{
AfxMessageBox("alloc data memoyr error!");
return false;
}
CopyMemory(p,buf,uSize);
ZeroMemory(pwh,sizeof(WAVEHDR));
pwh->dwBufferLength=uSize;
pwh->lpData=p;
m_mmr=waveOutPrepareHeader(m_hOut,pwh,sizeof(WAVEHDR));
if (m_mmr)
{
AfxMessageBox("waveOutPrepareHeader error!");
return false;
}
m_mmr=waveOutWrite(m_hOut,pwh,sizeof(WAVEHDR));
if (m_mmr)
{
AfxMessageBox("waveOutWrite error!");
return false;
}
m_iBufferNum ++;
return true;
}
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Glogal Thread procedure for the CWaveIn class
// It cannot be included inside the Class
//
// The LPARAM is the Class pointer (this) it can be the base class CWaveIn ptr or a derived new class
// The value of this parametre can change according the Topmost class of the process
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
DWORD WINAPI CWaveOut::AudioOutThreadProc(LPVOID lpParameter)
{
CWaveOut *pWaveIn;
pWaveIn = (CWaveOut *)lpParameter;
MSG msg;
while(GetMessage(&msg,0,0,0))
{
switch(msg.message )
{
case WOM_OPEN:
break;
case WOM_CLOSE:
break;
case WOM_DONE:
WAVEHDR* pwh=(WAVEHDR*)msg.lParam;
waveOutUnprepareHeader((HWAVEOUT)msg.wParam,pwh,sizeof(WAVEHDR));
pWaveIn->BufferSub ();
delete []pwh->lpData;//删除Play调用时分配的内存
delete pwh;
break;
}
}
TRACE("AudioInThreadProc exit.\n");
return msg.wParam;
}
//////////////////////////////////////////////////////////////////////