Microsoft Core Audio API:从默认端点获取WAVEFORMATEX信息

时间:2015-10-27 05:12:55

标签: c++ audio

我正在研究游戏的音频引擎和被调用的Microsoft Core Audio API之间的交互。我试图弄清楚,游戏如何从默认端点获得'WAVEFORMATEX'信息。我看到在游戏开始的某个时刻,IsFormatSupported()[https://msdn.microsoft.com/en-us/library/windows/desktop/dd370876(v=vs.85).aspx]被调用,其中* pFormat(IsFormatSupported的第二个参数)填充了默认端点的格式信息(即通道,位样本,采样率等) 。我也看到游戏在此之前没有调用GetMixFormat()[https://msdn.microsoft.com/en-us/library/windows/desktop/dd370872(v=vs.85).aspx]

但是,有一系列与IMMDeviceEnumerator(EnumAudioEndpoints,QuryInterface,AddRef等),IMMDeviceCollection(GetCount,Item)和IMMDevice(QueryInterface,AddRef等)相关的调用。查看这些文档,似乎没有直接的方法来获取'format'(WAVEFORMATEX)信息。虽然调用MMDevice :: OpenPropertyStore()并随后调用'GetId()',但我没有看到使用'PKEY_AudioEngine_DeviceFormat'参数调用'GetValue()',这可能提供了'format'信息。因此,我对游戏如何获得“格式”信息感到有些困惑。上述任何一个电话都可以间接获得'格式'信息吗?

我正在使用API​​监视器应用程序[http://www.rohitab.com/downloads]进行分析,并启用了“音频和视频”过滤器。

1 个答案:

答案 0 :(得分:0)

实际上可以从IsFormatSupported获得混音器格式。

HRESULT IsFormatSupported(
  [in]        AUDCLNT_SHAREMODE ShareMode,
  [in]  const WAVEFORMATEX      *pFormat,
  [out]       WAVEFORMATEX      **ppClosestMatch //< Audio format suggested by the Windows audio engine
);

IsFormatSupported用于格式否定,在Windows中,如果音频图中有一个能够进行转换的音频处理对象,则可以打开与混音器格式不同的音频格式。 但是,如果您提供的pFormat不是调音台格式且没有可用的转化,则Windows会为ppClosestMatch分配一个已使用调音台格式填充的已分配WAVEFORMATEX。 这里我测试的示例代码能够从IsFormatSupported获取混合器格式(为简单起见,没有错误处理):

CComPtr<IMMDeviceEnumerator> pEnum;
pEnum.CoCreateInstance(__uuidof(MMDeviceEnumerator));
CComPtr<IMMDevice> pDevice;
pEnum->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
CComPtr<IAudioClient> pAudioClient;
pDevice->Activate( __uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&pAudioClient);
// Provide an empty weveformatex
WAVEFORMATEX wfxEmpty = {};
WAVEFORMATEX *pClosestWfx;
HRESULT hr = pAudioClient->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, &wfxEmpty, &pClosestWfx);
if (hr == S_FALSE) {
    // The audio engine suggest us a format in pClosestWfx
    // which is the mixer format because we did not provide a valid input format.
    // the current application did not use any windows API to get the mixer format directly
    // ...
    CoTaskMemFree(pClosestWfx);
}

这是获得混音器格式的另一种方法。