inline BOOL CAudioChannel::ConvertPCMData(unsigned char * Srcdata, unsigned char * Dstdata, WAVEFORMATEX wfSrc, WAVEFORMATEX wfDsc,
DWORD hadID, DWORD dwSize, long& DestLength)
{
IVS_LOG(LOG_INFO, "Start conver PCM data.");
HACMDRIVERID hadid = (HACMDRIVERID)hadID;
//源音频格式
wfSrc.cbSize = 0;
wfSrc.wFormatTag = wfSrc.wFormatTag;
wfSrc.nChannels = wfSrc.nChannels;
wfSrc.nSamplesPerSec = wfSrc.nSamplesPerSec;
wfSrc.wBitsPerSample = wfSrc.wBitsPerSample;
wfSrc.nBlockAlign = wfSrc.nBlockAlign;
wfSrc.nAvgBytesPerSec = wfSrc.nAvgBytesPerSec;
// convert the intermediate PCM format to the final format
HACMDRIVER had = NULL;
MMRESULT mmr;
mmr = acmDriverOpen(&had, hadid, 0);
if (mmr)
{
IVS_LOG(LOG_ERR,"Convert PCM data fail,acmDriverOpen fail.");
return FALSE;
}
HACMSTREAM hstr;
mmr = acmStreamOpen(&hstr,NULL, &wfSrc, &wfDsc, NULL, NULL, 0, ACM_STREAMOPENF_NONREALTIME);
if (mmr)
{
IVS_LOG(LOG_ERR, "acmStreamOpen fail.");
acmDriverClose(had, 0);
return FALSE;
}
ACMSTREAMHEADER strhdr;
memset(&strhdr, 0, sizeof(strhdr));
strhdr.cbStruct = sizeof(strhdr);
strhdr.pbSrc = Srcdata;
strhdr.cbSrcLength = dwSize;
strhdr.pbDst = Dstdata;
strhdr.cbDstLength = dwSize;
mmr = acmStreamPrepareHeader(hstr, &strhdr, 0);
if (mmr)
{
IVS_LOG(LOG_ERR, "Failed to do PCM to driver format conversion.");
acmStreamClose(hstr, 0);
acmDriverClose(had, 0);
return FALSE;
}
mmr = acmStreamConvert(hstr, &strhdr, 0);
if (mmr)
{
IVS_LOG(LOG_ERR,"Failed to do PCM to driver format conversion.");
acmStreamUnprepareHeader(hstr,&strhdr,0);
acmStreamClose(hstr, 0);
acmDriverClose(had, 0);
return FALSE;
}
DestLength = (long)strhdr.cbDstLengthUsed;
acmStreamUnprepareHeader(hstr,&strhdr,0);
acmStreamClose(hstr, 0);
acmDriverClose(had, 0);
return TRUE;
}