www.pudn.com > hmmPlatform.rar > BufferWaveOut.cpp


////////////////////////////////////////////////////////////////////////// 
// class CBufferWaveOut 
// 
// 功能:	实现内存中的裸音频数据的播放 
// 创建人:	陈文凯 (chwkai@gmail.com) 
// 创建日期:2005年5月19日 
// 修改人: 
// 修改日期: 
// 版本 
 
#include "StdAfx.h" 
#include ".\bufferwaveout.h" 
 
CBufferWaveOut::CBufferWaveOut(void) 
{ 
	// 重置指针 
	this->m_pAllocBuffer = NULL; 
	this->m_pNextBuffer = NULL; 
	this->m_pPreBuffer = NULL; 
} 
 
CBufferWaveOut::~CBufferWaveOut(void) 
{ 
	// 停止播放,并释放所占用资源 
	this->Stop(); 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 打开输出设备,设置回调方式,设置format信息 
// Prepare所用到的缓存 
BOOL CBufferWaveOut::Init(WAVEFORMATEX fmt, DWORD hWnd) 
{ 
	BOOL bRet = FALSE; 
 
	// 打开输出设备 
	if (bRet = CWaveOut::Init(fmt, hWnd)) 
	{ 
		// 预备全部缓冲块 
		this->m_pNextBuffer = this->m_pAllocBuffer;	 
 
		while (this->m_pNextBuffer != NULL) 
		{ 
			waveOutPrepareHeader(this->m_hWaveOut, this->m_pNextBuffer, sizeof(WAVEHDR)); 
 
			this->m_pNextBuffer = this->m_pNextBuffer->lpNext; 
		} 
	} 
 
	return bRet; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 开始播放音频裸数据 
void CBufferWaveOut::Start() 
{ 
	if (this->m_pAllocBuffer != NULL) 
	{ 
		// 发送第一个缓冲快 
		this->m_pPreBuffer = this->m_pAllocBuffer; 
		this->m_pNextBuffer = this->m_pPreBuffer->lpNext; 
		waveOutWrite(this->m_hWaveOut, this->m_pPreBuffer, sizeof(WAVEHDR)); 
	} 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 设定播放的缓存区,设定format信息,调用init进行初始化 
BOOL CBufferWaveOut::LoadBuffer( WAVEHDR* pBuffer , WAVEFORMATEX fmt, DWORD hWnd) 
{ 
	BOOL bRet = FALSE; 
 
	if (pBuffer != NULL) 
	{ 
		// 停止当前播放 
		this->Stop(); 
 
		// 调用init进行初始化 
		bRet = this->Init(fmt, hWnd); 
 
		// 设定缓存 
		this->m_pAllocBuffer = pBuffer; 
	} 
 
	return bRet; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 处理WOU_DONE消息 
LRESULT CBufferWaveOut::OnDone(DWORD dwParam) 
{ 
	// 维护缓冲链表 
	this->m_pPreBuffer->lpNext = this->m_pNextBuffer; 
 
	// 发送待播放的缓冲 
	if (this->m_pNextBuffer != NULL) 
	{ 
		this->m_pPreBuffer = this->m_pNextBuffer; 
		this->m_pNextBuffer = this->m_pNextBuffer->lpNext; 
 
		waveOutWrite(this->m_hWaveOut, this->m_pPreBuffer, sizeof(WAVEHDR)); 
	} 
	else 
	{ 
		// 缓冲播放完毕后结束 
		this->Stop(); 
	} 
 
	return 0; 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 停止播放,关闭输出设备, UnPrepare所用到的缓冲块 
void CBufferWaveOut::Stop() 
{ 
	// 停止播放 
	CWaveOut::Stop(); 
	 
	// UnPrepare 全部缓冲块 
	this->m_pNextBuffer = this->m_pAllocBuffer; 
	while (this->m_pNextBuffer != NULL) 
	{ 
		waveOutUnprepareHeader(this->m_hWaveOut, this->m_pNextBuffer, sizeof(WAVEHDR)); 
 
		this->m_pNextBuffer = this->m_pNextBuffer->lpNext; 
	} 
 
	// 关闭输出设备 
	CWaveOut::CloseDev(); 
 
	// 释放所占用资源,重置属性 
	this->Dispose(); 
} 
 
////////////////////////////////////////////////////////////////////////// 
// 释放所占用资源,重置属性 
void CBufferWaveOut::Dispose() 
{ 
	// 重置指针 
	this->m_pAllocBuffer = NULL; 
	this->m_pNextBuffer = NULL; 
	this->m_pPreBuffer = NULL; 
 
	// 重置基类参数 
	CWaveOut::Dispose(); 
}