|
waveout系列函数如何播放连续的PCM片段
[复制链接]
有一个库,它把一个句话的PCM数据分多次通过回调函数传给我;我把PCM数据存成文件再播放,是可以的;
我想实时播放,试了下在回调函数中去播PCM片段,只听到“咔咔”的不连续的声音。
附上播放PCM片段的函数PlayPCM:
UINT PlayPCM(void* pcmdata, int nLen)
{
HWAVEOUT hwo;
WAVEHDR whdr;
MMRESULT mmres;
HANDLE hDoneEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("DONE_EVENT"));
UINT devId = 0;
WAVEFORMATEX wFormatEx;
wFormatEx.wFormatTag = WAVE_FORMAT_PCM;
wFormatEx.nChannels = 1;
wFormatEx.wBitsPerSample = 16;
wFormatEx.nSamplesPerSec = 16000;
wFormatEx.nBlockAlign = wFormatEx.wBitsPerSample * wFormatEx.nChannels / 8;
wFormatEx.nAvgBytesPerSec = wFormatEx.nSamplesPerSec* wFormatEx.nBlockAlign;
// Open audio device
for (devId = 0; devId < waveOutGetNumDevs(); devId++) {
mmres = waveOutOpen(&hwo, devId, &wFormatEx, (DWORD) hDoneEvent,
0, CALLBACK_EVENT);
if (mmres == MMSYSERR_NOERROR) {
break;
}
}
if (mmres != MMSYSERR_NOERROR)
{
return mmres;
}
// Initialize wave header
ZeroMemory(&whdr, sizeof(WAVEHDR));
whdr.lpData = (LPSTR)pcmdata;
whdr.dwBufferLength = nLen;
mmres = waveOutPrepareHeader(hwo, &whdr, sizeof(WAVEHDR));
if (mmres != MMSYSERR_NOERROR)
{
return mmres;
}
mmres = waveOutWrite(hwo, &whdr, sizeof(WAVEHDR));
if (mmres != MMSYSERR_NOERROR)
{
return mmres;
}
// Wait for audio to finish playing
while (!(whdr.dwFlags & WHDR_DONE))
{
WaitForSingleObject(hDoneEvent, INFINITE);
}
// Clean up
mmres = waveOutUnprepareHeader(hwo, &whdr, sizeof(WAVEHDR));
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
mmres = waveOutClose(hwo);
if (mmres != MMSYSERR_NOERROR)
{
return mmres;
}
return MMSYSERR_NOERROR;
}
|
|