c++ sound 如何使用WASAPI共享模式獲得低於10ms的延遲?



windows 10 sound api (1)

根據微軟的說法,從Windows 10開始,使用共享模式WASAPI的應用程序可以請求小於10毫秒的緩衝區大小(請參閱https://msdn.microsoft.com/zh-cn/library/windows/hardware/mt298187%28v=vs。 85%29.aspx )。

根據文章,實現如此低的延遲需要一些驅動程序更新,我做了。 使用獨占模式渲染和捕獲流,我測量了大約13ms的總往返延遲(使用硬件回送電纜)。 這意味著至少有一個終端成功實現了<10ms的延遲。 (這個假設是否正確?)

本文提到,應用程序可以使用新的IAudioClient3接口,使用IAudioClient3::GetSharedModeEnginePeriod()查詢Windows音頻引擎支持的最小緩衝區大小。 然而,這個函數總是在我的系統上返回10ms,並且使用IAudioClient::Initialize()或者IAudioClient3::InitializeSharedAudioStream()以低於10ms的周期初始化音頻流的任何嘗試總是導致AUDCLNT_E_INVALID_DEVICE_PERIOD

可以肯定的是,我還禁用了音頻驅動程序中的任何效果處理。 我錯過了什麼? 是否有可能從共享模式獲得低延遲? 請參閱下面的一些示例代碼。

#include <windows.h>
#include <atlbase.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <iostream>

#define VERIFY(hr) do {                                    \
  auto temp = (hr);                                        \
  if(FAILED(temp)) {                                       \
    std::cout << "Error: " << #hr << ": " << temp << "\n"; \
    goto error;                                            \
  }                                                        \
} while(0)


int main(int argc, char** argv) {

  HRESULT hr;
  CComPtr<IMMDevice> device;
  AudioClientProperties props;
  CComPtr<IAudioClient> client;
  CComPtr<IAudioClient2> client2;
  CComPtr<IAudioClient3> client3;
  CComHeapPtr<WAVEFORMATEX> format;
  CComPtr<IMMDeviceEnumerator> enumerator; 

  REFERENCE_TIME minTime, maxTime, engineTime;
  UINT32 min, max, fundamental, default_, current;

  VERIFY(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED));
  VERIFY(enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator)));
  VERIFY(enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &device));
  VERIFY(device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&client)));
  VERIFY(client->QueryInterface(&client2));
  VERIFY(client->QueryInterface(&client3));

  VERIFY(client3->GetCurrentSharedModeEnginePeriod(&format, &current));

  // Always fails with AUDCLNT_E_OFFLOAD_MODE_ONLY.
  hr = client2->GetBufferSizeLimits(format, TRUE, &minTime, &maxTime);
  if(hr == AUDCLNT_E_OFFLOAD_MODE_ONLY)
    std::cout << "GetBufferSizeLimits returned AUDCLNT_E_OFFLOAD_MODE_ONLY.\n";
  else if(SUCCEEDED(hr))
    std::cout << "hw min = " << (minTime / 10000.0) << " hw max = " << (maxTime / 10000.0) << "\n";
  else
    VERIFY(hr);

  // Correctly? reports a minimum hardware period of 3ms and audio engine period of 10ms.
  VERIFY(client->GetDevicePeriod(&engineTime, &minTime));
  std::cout << "hw min = " << (minTime / 10000.0) << " engine = " << (engineTime / 10000.0) << "\n";

  // All values are set to a number of frames corresponding to 10ms.
  // This does not change if i change the device's sampling rate in the control panel.
  VERIFY(client3->GetSharedModeEnginePeriod(format, &default_, &fundamental, &min, &max));
  std::cout << "default = " << default_ 
            << " fundamental = " << fundamental 
            << " min = " << min 
            << " max = " << max 
            << " current = " << current << "\n";

  props.bIsOffload = FALSE;
  props.cbSize = sizeof(props);
  props.eCategory = AudioCategory_ForegroundOnlyMedia;
  props.Options = AUDCLNT_STREAMOPTIONS_RAW | AUDCLNT_STREAMOPTIONS_MATCH_FORMAT;

  // Doesn't seem to have any effect regardless of category/options values.
  VERIFY(client2->SetClientProperties(&props));

  format.Free();
  VERIFY(client3->GetCurrentSharedModeEnginePeriod(&format, &current));
  VERIFY(client3->GetSharedModeEnginePeriod(format, &default_, &fundamental, &min, &max));
  std::cout << "default = " << default_ 
            << " fundamental = " << fundamental 
            << " min = " << min 
            << " max = " << max 
            << " current = " << current << "\n";

error:
  CoUninitialize();
  return 0;
}

在上面的註釋中,按照漢斯,請仔細檢查您是否已按照低延遲音頻的說明操作。

我會重新啟動機器,以確保; Windows可以有點挑剔這樣的事情。