如何获取物理设备支持的音频格式(WinAPI,Windows)

How to get Audio Formats supported by physical device (WinAPI, Windows)

我有一个音频设备(一个 USB 麦克风),我想知道它原生支持哪些音频格式(位深度和采样率),在 OS X 上有一个不错的 kAudioStreamPropertyAvailablePhysicalFormats Core Audio 属性,但我在 Windows.

上找不到类似的东西

我知道有这个问题Windows Core Audio Api get all supported formats on capture device但是

Hokay,这里有一些示例代码供您使用。信息就在那里,散布在网络上,但你必须搜索它。 Google 我在下面调用的函数和一些奇怪的清单常量以了解更多信息。用记事本编写的代码可能无法编译。

下面的代码查询默认的输入/输出设备。要获取任何特定系统上安装的所有设备的 device_id,请调用 waveInGetNumDevswaveOutGetNumDevs 并从 0 开始计数。

#define INITGUID

#include "Ks.h"
#include "KsMedia.h"
#include "mmdeviceapi.h"


// Open a query handle for the default input or output device
// Call Closehandle when done.
static HANDLE QueryOpen (bool input)
{
    DWORD device_id, device_status;
    DWORD err = (input) ?
        waveInMessage ((HWAVEIN) (INT_PTR) WAVE_MAPPER,
            DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR) &device_id, (DWORD_PTR) &device_status) :
        waveOutMessage ((HWAVEOUT) (INT_PTR) WAVE_MAPPER,
            DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR) &device_id, (DWORD_PTR) &device_status);
    if (err)
        return INVALID_HANDLE_VALUE;

    DWORD devicePathSize;
    DWORD mm_result = (input) ? 
        waveInMessage ((HWAVEIN) (INT_PTR) device_id, DRV_QUERYDEVICEINTERFACESIZE,
            (DWORD_PTR) &devicePathSize, 0) :
        waveOutMessage ((HWAVEOUT) (INT_PTR) device_id, DRV_QUERYDEVICEINTERFACESIZE,
            (DWORD_PTR) &devicePathSize, 0);
    if (mm_result != 0)
        return INVALID_HANDLE_VALUE;

    /* apparently DRV_QUERYDEVICEINTERFACE returns a unicode interface path, although this is undocumented */
    WCHAR *devicePath = (WCHAR *) malloc (devicePathSize);

    mm_result = (input) ?
        waveInMessage ((HWAVEIN) (INT_PTR) device_id, DRV_QUERYDEVICEINTERFACE,
            (DWORD_PTR) devicePath, devicePathSize) :
        waveOutMessage ((HWAVEOUT) (INT_PTR) device_id, DRV_QUERYDEVICEINTERFACE,
            (DWORD_PTR) devicePath, devicePathSize);

    HANDLE result = (mm_result == 0) ? CreateFileW (devicePath, FILE_SHARE_READ | FILE_SHARE_WRITE,
        0, NULL, OPEN_EXISTING, 0, NULL) : INVALID_HANDLE_VALUE;

    free (devicePath);
    return result;
}


// Interrogate the default input / output device (demo code)
void InterrogateDefaultDevice (bool input)
{
    HANDLE hQuery = QueryOpen (input);
    if (hQuery == INVALID_HANDLE_VALUE)
        return;

    int pin_count = GetKSFilterPinCount (hQuery);

    for (int pinId = 0; pinId < pin_count; ++pinId)
    {
        KSPIN_COMMUNICATION communication = GetKSFilterPinPropertyCommunication (hQuery, pinId);
        KSPIN_DATAFLOW dataflow = GetKSFilterPinPropertyDataflow (h, pinId);

        if ((communication == KSPIN_COMMUNICATION_SINK || communication == KSPIN_COMMUNICATION_BOTH) &&
            (KSFilterPinPropertyIdentifiersInclude (hQuery, pinId, KSPROPERTY_PIN_INTERFACES,
                &KSINTERFACESETID_Standard, KSINTERFACE_STANDARD_STREAMING) ||
             KSFilterPinPropertyIdentifiersInclude (hQuery, pinId, KSPROPERTY_PIN_INTERFACES,
                &KSINTERFACESETID_Standard, KSINTERFACE_STANDARD_LOOPED_STREAMING)) &&
             KSFilterPinPropertyIdentifiersInclude (hQuery, pinId, KSPROPERTY_PIN_MEDIUMS,
                &KSMEDIUMSETID_Standard, KSMEDIUM_STANDARD_DEVIO))
        {
            KSMULTIPLE_ITEM *item = NULL;

            if (WdmGetPinPropertyMulti (hQuery, pinId, KSPROPERTY_PIN_DATARANGES, &item))
            {
                KSDATARANGE_AUDIO *dr = (KSDATARANGE_AUDIO *) (item + 1);

                for (ULONG i = 0; i < item->Count; ++i )
                {
                    printf ("%ul - %ul Hz (%ul - %ul bits per sample, upto %ul channels)\n",
                        dr->MinimumSampleFrequency, dr->MaximumSampleFrequency,
                        dr->MinimumBitsPerSample, dr->MaximumBitsPerSample, dr->MaximumChannels);

                    dr = (KSDATARANGE_AUDIO *) ((BYTE *) dr + dr->DataRange.FormatSize);
                }

                free (item);
            }
        }
    }

    CloseHandle (hQuery);
}