c++ - 如何使用 WASAPI 共享模式获得低于 10 毫秒的延迟?

标签 c++ audio audio-processing wasapi

根据微软的说法,从 Windows 10 开始,使用共享模式 WASAPI 的应用程序可以请求小于 10 毫秒的缓冲区大小(参见 https://msdn.microsoft.com/en-us/library/windows/hardware/mt298187%28v=vs.85%29.aspx)。

根据文章,实现如此低的延迟需要一些驱动程序更新,我做到了。使用独占模式渲染和捕获流,我测量了大约 13 毫秒的总往返延迟(使用硬件环回电缆)。这向我表明,至少有一个端点成功地实现了 < 10 毫秒的延迟。 (这个假设正确吗?)

文章提到应用程序可以使用新的IAudioClient3接口(interface)来查询Windows音频引擎支持的最小缓冲区大小,使用IAudioClient3::GetSharedModeEnginePeriod()。但是,此函数在我的系统上总是返回 10 毫秒,并且任何尝试使用 IAudioClient::Initialize()IAudioClient3::InitializeSharedAudioStream() 初始化音频流低于 10 毫秒的周期总是导致 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;
}

最佳答案

上面评论中的 Per Hans,请仔细检查您是否遵循了低延迟音频 here 的说明.

为了确定,我会重新启动机器; Windows 可能对这种东西有点挑剔。

关于c++ - 如何使用 WASAPI 共享模式获得低于 10 毫秒的延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468283/

相关文章:

c++ - 二叉搜索树中的插入错误

javascript - mozChannels/mozSampleRate未定义

c# - WaveChannel32 给了我一个异常 : Offset and length were out of bounds

matlab - 参数均衡器的幅度响应

OpenCV使用傅里叶变换得到频谱图

python - Cython编译的app需要安装python吗?

数据类型的 C++ 错误

c++ - 在 C++ 中将 SQL 查询返回到 vector<double> 中的函数

android - 如何正确暂停AudioTrack并恢复平稳播放?

ios - 将进度保存在 AVPlayer 中以更新 UIProgressView 并稍后从同一位置恢复