waveOutOpen 函数说明
(2008-12-21 13:33:10)
标签:
it |
使用心得:首先打开声音输出设备,然后初始化输出内存,并保证不要使用缓存;(调用waveOutPrepareHeader设置内存),然后通过回调函数进行状态通知。通过waveOutWrite进行声音播放;
最后调用waveOutUnprepareHeader释放内存,waveOutReset(m_hWaveOut);
waveOutClose(m_hWaveOut);关闭音频设备
//声明:
waveOutOpen(
lphWaveOut:
PHWaveOut; {用于返回设备句柄的指针; 如果
dwFlags=WAVE_FORMAT_QUERY, 这里应是 nil}
uDeviceID:
UINT; {设备ID; 可以指定为: WAVE_MAPPER,
这样函数会根据给定的波形格式选择合适的设备}
lpFormat: PWaveFormatEx; {TWaveFormat 结构的指针; TWaveFormat
包含要申请的波形格式}
dwCallback:
DWORD {回调函数地址或窗口句柄; 若不使用回调机制, 设为
nil}
dwInstance:
DWORD {给回调函数的实例数据;
不用于窗口}
dwFlags:
DWORD {打开选项}
):
MMRESULT; {成功返回 0;
可能的错误值见下:}
MMSYSERR_BADDEVICEID = 2; {设备ID超界}
MMSYSERR_ALLOCATED =4; {指定的资源已被分配}
MMSYSERR_NODRIVER = 6; {没有安装驱动程序}
MMSYSERR_NOMEM = 7; {不能分配或锁定内存}
WAVERR_BADFORMAT = 32;{设备不支持请求的波形格式}
//TWaveFormatEx
结构:
TWaveFormatEx = packedrecord
wFormatTag:
Word; {指定格式类型; 默认 WAVE_FORMAT_PCM =
1;}
nChannels:
Word; {指出波形数据的通道数; 单声道为 1, 立体声为
2}
nSamplesPerSec: DWORD; {指定样本速率(每秒的样本数)}一般为8000
nAvgBytesPerSec: DWORD; {指定数据传输的平均速率(每秒的字节数)} 每秒的字节数:
nBlockAlign:
Word; {指定块对齐(单位字节),
块对齐是数据的最小单位}
wBitsPerSample:
Word; {采样大小(字节)}每个样本的BIT数目,一般为16
cbSize:
Word; {应该是该结构的大小}
end;
{16 位立体声 PCM 的块对齐是 4
字节(每个样本2字节, 2个通道)}
//打开选项 dwFlags
的可选值:
WAVE_FORMAT_QUERY = $0001; {只是判断设备是否支持给定的格式,
并不打开}
WAVE_ALLOWSYNC = $0002; {当是同步设备时必须指定}
CALLBACK_WINDOW = $00010000; {当 dwCallback
是窗口句柄时指定}
CALLBACK_FUNCTION = $00030000; {当 dwCallback 是函数指针时指定}
//如果选择窗口接受回调信息,
可能会发送到窗口的消息有:
MM_WOM_OPEN =
$3BB;
MM_WOM_CLOSE = $3BC;
MM_WOM_DONE = $3BD;
//如果选择函数接受回调信息,
可能会发送给函数的消息有:
WOM_OPEN =
MM_WOM_OPEN;
WOM_CLOSE = MM_WOM_CLOSE;
WOM_DONE = MM_WOM_DONE;
举例如下:
//回调函数
static void CALLBACK
SpeakerCallback (HWAVEOUT _waveoutdev, UINT uMsg, DWORD
dwInstance,
DWORD dwParam1, DWORD dwParam2)
{
WAVEHDR *wHdr;
streamUtil * parentStream
=NULL;
switch (uMsg)
{
case WOM_OPEN:
// ms_message("SpeakerCallback : WOM_OPEN");
DEBUG_HELPER->log("streamUtil",LM_DEBUG,"RECEIVE
WOM_OPEN\n");
break;
case WOM_CLOSE:
DEBUG_HELPER->log("streamUtil",LM_DEBUG,"RECEIVE
WOM_CLOSE\n");
// ms_message("SpeakerCallback : WOM_CLOSE");
break;
case WOM_DONE:
parentStream = (streamUtil*)dwInstance;
wHdr =
(WAVEHDR *) dwParam1;
// waveOutUnprepareHeader
(parentStream->m_hWaveOut, wHdr, sizeof (WAVEHDR))
;
break;
default:
break;
}
}
WAVEFORMATEX waveform;
waveform.wFormatTag = WAVE_FORMAT_PCM;
waveform.nChannels = channel;
waveform.nSamplesPerSec =rate;//8000
waveform.nAvgBytesPerSec=16000;
waveform.nBlockAlign =2;
waveform.wBitsPerSample =bits;
//16
waveform.cbSize =0;
//初始化回调标志
dwFlag = CALLBACK_FUNCTION;
if
(devNumber != WAVE_MAPPER)
dwFlag = WAVE_MAPPED | CALLBACK_FUNCTION;
//打开设备
if
(waveOutOpen(&m_hWaveOut,devNumber,&waveform,(DWORD)
SpeakerCallback,(DWORD)this,dwFlag)) {
DEBUG_HELPER->log("streamUtil",LM_ERROR,"Failed to
open deice. (waveOutOpen:0x%i)\n", mr);
return -1;
}
//
//缓存的大小
m_bufLength = (rate/8000) *
INP_BUFFER_SIZE;
for(int
i=0;i<OUT_BUFFER_NUM;i++)
{
//初始化输出缓存
//m_outHdr[i]=
reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
// m_outBuffer[i]
= (PBYTE)malloc(1 * m_bufLength);
memset(m_outBuffer[i],0,m_bufLength);
m_outHdr[i].lpData = (LPSTR)(m_outBuffer[i] );
m_outHdr[i].dwBufferLength = m_bufLength ;
m_outHdr[i].dwBytesRecorded
= 0 ;
m_outHdr[i].dwUser = 0 ;
m_outHdr[i].dwFlags = 0 ;
// m_outHdr[i].lpNext = NULL ;
m_outHdr[i].reserved = 0 ;
m_outHdr[i].dwLoops
=0;
waveOutPrepareHeader
(m_hWaveOut, &(m_outHdr[i]), sizeof (WAVEHDR))
;
}