3

I'm trying to open an exclusive stream with an output device using WASAPI. I'm having trouble choosing an acceptable format, since there appear to be no hints as to what formats are accepted by a given device.

In my case, IAudioClient::GetMixFormat(), which would otherwise return a sort of default format for the device, returns a format that can't be used in exclusive mode (IAudioClient::IsFormatSupported() returns AUDCLNT_E_UNSUPPORTED_FORMAT). I don't know where to go from there. There's a ridiculous number of combinations of wave format parameters - do I literally have to iterate through every one of them until something works?

Roman R.
  • 68,205
  • 6
  • 94
  • 158
NmdMystery
  • 2,778
  • 3
  • 32
  • 60

1 Answers1

6

Well, I asked the MSDN forums and they came up with a good answer.

You need to check the device's default device format via IMMDevice::OpenPropertyStore(), and subsequently IPropertyStore::GetValue(), not IAudioClient::GetMixFormat(). Here is the code that retrieved an acceptable WAVEFORMATEX structure:

//CoInitialize/Enumerate devices

IPropertyStore* store = nullptr;

hr = device->OpenPropertyStore(STGM_READ, &store);

if (FAILED(hr)) {
    ExitProcess(1);
}

PROPVARIANT prop;

hr = store->GetValue(PKEY_AudioEngine_DeviceFormat, &prop);

if (FAILED(hr)) {
    ExitProcess(2);
}

hr = device->Activate (
    __uuidof(IAudioClient), 
    CLSCTX_ALL,
    NULL,
    (void**)&audioClient
);

device->Release();
device = nullptr;

if (FAILED(hr)) {
    ExitProcess(3);
}

hr = audioClient->IsFormatSupported (
    AUDCLNT_SHAREMODE_EXCLUSIVE,
    (PWAVEFORMATEX)prop.blob.pBlobData,
    NULL
);

if (FAILED(hr)) {
    ExitProcess(4);
}

The final value of hr is S_OK.

NmdMystery
  • 2,778
  • 3
  • 32
  • 60
  • That usually works but keep in mind it might still fail. I've actually seen it fail on a virtualbox running windows 7 (i.e. the deviceformat isn't supported by the device =)). IMHO the only really reliable way to determine the format is to keep calling IsFormatSupported untill you find something you like. – Sjoerd van Kreel May 26 '16 at 22:19