UAC开发调试笔记
+ -

Windows系统获取音频设备属性信息

2022-09-02 487 0
#include <iostream>



#include <iostream>
#include <windows.h>
#include <Audioclient.h>

#include <MMSystem.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <Functiondiscoverykeys_devpkey.h>//PKEY_Device_FriendlyName


//-----------------------------------------------------------
// Record an audio stream from the default audio capture
// device. The RecordAudioStream function allocates a shared
// buffer big enough to hold one second of PCM audio data.
// The function uses this buffer to stream data from the
// capture device. The main loop runs every 1/2 second.
//-----------------------------------------------------------

// REFERENCE_TIME time units per second and per millisecond
#define REFTIMES_PER_SEC  10000000
#define REFTIMES_PER_MILLISEC  10000 //100ns

//以下代码来自MSDN
#define EXIT_ON_ERROR(hres)  \
              if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk)  \
              if ((punk) != NULL)  \
                { (punk)->Release(); (punk) = NULL; }

const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);

void PrintEndpointNames()
{
    HRESULT hr = S_OK;
    IMMDeviceEnumerator* pEnumerator = NULL;
    IMMDeviceCollection* pCollection = NULL;
    IMMDevice* pEndpoint = NULL;
    IPropertyStore* pProps = NULL;
    LPWSTR pwszID = NULL;

    hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL,CLSCTX_ALL, IID_IMMDeviceEnumerator,(void**)&pEnumerator);
    if (FAILED(hr))
    { 
        goto Exit; 
    }


    hr = pEnumerator->EnumAudioEndpoints( eAll, DEVICE_STATE_ACTIVE,&pCollection);
    if (FAILED(hr))
    {
        goto Exit;
    }


    UINT  count;
    hr = pCollection->GetCount(&count);
    if (FAILED(hr))
    {
        goto Exit;
    }

    if (count == 0)
    {
        printf("No endpoints found.\n");
    }

    for (ULONG i = 0; i < count; i++)
    {
        hr = pCollection->Item(i, &pEndpoint);
        if (FAILED(hr))
        {
            goto Exit;
        }

        hr = pEndpoint->GetId(&pwszID);
        if (FAILED(hr))
        {
            goto Exit;
        }

        hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
        if (FAILED(hr))
        {
            goto Exit;
        }

        PROPVARIANT varName;
        PropVariantInit(&varName);

        hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
        if (FAILED(hr))
        {
            goto Exit;
        }

       printf("Endpoint %d: \"%S\" (%S)\n",  i, varName.pwszVal, pwszID);
       CoTaskMemFree(pwszID);
       pwszID = NULL;


        PropVariantClear(&varName);
        if (pProps != NULL)
        {
            pProps->Release();
            pProps = NULL;
        }
        if (pEndpoint != NULL)
        {
            pEndpoint->Release();
            pEndpoint = NULL;
        }

    }
    if (pEnumerator != NULL)
    {
        pEnumerator->Release();
        pEnumerator = NULL;
    }
    if (pCollection != NULL)
    {
        pCollection->Release();
        pCollection = NULL;
    }

    return;

Exit:
    printf("Error!\n");
    CoTaskMemFree(pwszID);
    SAFE_RELEASE(pEnumerator);
    SAFE_RELEASE(pCollection);
    SAFE_RELEASE(pEndpoint);
    SAFE_RELEASE(pProps);
}

int main()
{
    CoInitialize(NULL);
    PrintEndpointNames();

    CoUninitialize();
   return  getchar();
}

可获取的设备属性信息包括:
头文件定义路径为:

C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\functiondiscoverykeys_devpkey.h

内容为:


PKEY_Device_DeviceDesc,            
PKEY_Device_HardwareIds,           
PKEY_Device_CompatibleIds,         
PKEY_Device_Service,               
PKEY_Device_Class,                 
PKEY_Device_ClassGuid,             
PKEY_Device_Driver,                
PKEY_Device_ConfigFlags,           
PKEY_Device_Manufacturer,          
PKEY_Device_FriendlyName,          
PKEY_Device_LocationInfo,          
PKEY_Device_PDOName,               
PKEY_Device_Capabilities,          
PKEY_Device_UINumber,              
PKEY_Device_UpperFilters,          
PKEY_Device_LowerFilters,          
PKEY_Device_BusTypeGuid,           
PKEY_Device_LegacyBusType,         
PKEY_Device_BusNumber,             
PKEY_Device_EnumeratorName,        
PKEY_Device_Security,              
PKEY_Device_SecuritySDS,           
PKEY_Device_DevType,               
PKEY_Device_Exclusive,             
PKEY_Device_Characteristics,       
PKEY_Device_Address,               
PKEY_Device_UINumberDescFormat,    
PKEY_Device_PowerData,             
PKEY_Device_RemovalPolicy,         
PKEY_Device_RemovalPolicyDefault,  
PKEY_Device_RemovalPolicyOverride, 
PKEY_Device_InstallState,          
PKEY_Device_LocationPaths,         
PKEY_Device_BaseContainerId,       


PKEY_Device_DevNodeStatus,         
PKEY_Device_ProblemCode,           




PKEY_Device_EjectionRelations,     
PKEY_Device_RemovalRelations,      
PKEY_Device_PowerRelations,        
PKEY_Device_BusRelations,          
PKEY_Device_Parent,                
PKEY_Device_Children,              
PKEY_Device_Siblings,              
PKEY_Device_TransportRelations,    




PKEY_Device_Reported,              
PKEY_Device_Legacy,                
PKEY_Device_InstanceId,            

PKEY_Device_ContainerId,           

PKEY_Device_ModelId,               

PKEY_Device_FriendlyNameAttributes,
PKEY_Device_ManufacturerAttributes,

PKEY_Device_PresenceNotForDevice,  
PKEY_Device_SignalStrength,        
PKEY_Device_IsAssociateableByUserAc



PKEY_Numa_Proximity_Domain,        
PKEY_Device_DHP_Rebalance_Policy,  
PKEY_Device_Numa_Node,             
PKEY_Device_BusReportedDeviceDesc, 

PKEY_Device_InstallInProgress,     



PKEY_Device_DriverDate,            
PKEY_Device_DriverVersion,         
PKEY_Device_DriverDesc,            
PKEY_Device_DriverInfPath,         
PKEY_Device_DriverInfSection,      
PKEY_Device_DriverInfSectionExt,   
PKEY_Device_MatchingDeviceId,      
PKEY_Device_DriverProvider,        
PKEY_Device_DriverPropPageProvider,
PKEY_Device_DriverCoInstallers,    
PKEY_Device_ResourcePickerTags,    
PKEY_Device_ResourcePickerException
PKEY_Device_DriverRank,            
PKEY_Device_DriverLogoLevel,       
PKEY_Device_NoConnectSound,        
PKEY_Device_GenericDriverInstalled,
PKEY_Device_AdditionalSoftwareReque



PKEY_Device_SafeRemovalRequired,   
PKEY_Device_SafeRemovalRequiredOver




PKEY_DrvPkg_Model,                 
PKEY_DrvPkg_VendorWebSite,         
PKEY_DrvPkg_DetailedDescription,   
PKEY_DrvPkg_DocumentationLink,     
PKEY_DrvPkg_Icon,                  
PKEY_DrvPkg_BrandingIcon,          



PKEY_DeviceClass_UpperFilters,     
PKEY_DeviceClass_LowerFilters,     
PKEY_DeviceClass_Security,         
PKEY_DeviceClass_SecuritySDS,      
PKEY_DeviceClass_DevType,          
PKEY_DeviceClass_Exclusive,        
PKEY_DeviceClass_Characteristics,  



PKEY_DeviceClass_Name,             
PKEY_DeviceClass_ClassName,        
PKEY_DeviceClass_Icon,             
PKEY_DeviceClass_ClassInstaller,   
PKEY_DeviceClass_PropPageProvider, 
PKEY_DeviceClass_NoInstallClass,   
PKEY_DeviceClass_NoDisplayClass,   
PKEY_DeviceClass_SilentInstall,    
PKEY_DeviceClass_NoUseClass,       
PKEY_DeviceClass_DefaultService,   
PKEY_DeviceClass_IconPath,         


PKEY_DeviceClass_ClassCoInstallers,
PKEY_DeviceInterface_FriendlyName, 
PKEY_DeviceInterface_Enabled,      
PKEY_DeviceInterface_ClassGuid,
HID人机交互QQ群:564808376    UAC音频QQ群:218581009    UVC相机QQ群:331552032    BOT&UASP大容量存储QQ群:258159197    STC-USB单片机QQ群:315457461    USB技术交流QQ群2:580684376    USB技术交流QQ群:952873936   

0 篇笔记 写笔记

USB-UAC麦克风 音频控制接口
音频控制接口描述符描述了设备的结构(拓扑结构),并通过特定类请求对音频的控制。UAC麦克风 音频控制接口描述符偏移地址字段长度值描述0bLength10x09接口描术符的长度1bDescriptorType10x04描述符的类型接口描述符2bInterfaceNumber10x00接口ID号3bA......
UAC 类特定音频控制接口头描述符
UAC类特定音频控制头接口描述符这个名字有点绕,其实这个描述符是前接标准的音频控制接口描述符,后续关于音频控制的所有相关描述符,起着承上起下的作用。当然也可以认为是音频控制相关描述符的前导。这是因为UAC类特定音频控制接口描述符含有一个关键的字段wTotalLength,用于包含音频控制所有接口描述......
华为UAC耳机 音频控制接口
音频控制接口占用接口ID=0,音频控制接口的描述符结构布局如下:USB标准接口描述符UAC音频控制接口头描述符IDSId描述    音频控制输入终端描述符1USB Streaming   ......
UAC 音频数据格式FORMAT_TYPE_3
下面来介绍USB Audio Data Formats 的第一类音频数格式 FORMAT_TYPE_III = 0x03Audio Data Format Type III Codes 其下又分为5种,分别为:NamewFormatTagTYPE_III_UNDEFINED0x2000IEC1937......
UAC 音频控制
一个USB设备可能包含多个配置。像手机一样,当手机通过USB线缆接入PC机后,会弹出一个选择对话框:让用户选择。当然一个USB设备只能工作在一种配置描述符下。对于每一个USB配置描述符,可能含有多个USB接口描述描述符,同时这些接口描述符可能每个接口描述符又包含多个转换接口描述符。这些接口描述符可能......
UAC 标准音频流接口描述符
UAC音频流接口描述符包含与音频数据流相关的描述符信息。标准音频流接口描述符,Standard AS Interface Descriptor是音频流接口描述符的第一个描述符。该描述符是标准的接口描述符,故数据结构定义如下:typedef struct _USB_INTERFACE_DESCRIPT......
UAC 音频数据格式FORMAT_TYPE_I
下面来介绍USB Audio Data Formats 的第一类音频数格式 FORMAT_TYPE_I = 0x01Audio Data Format Type I Codes 其下又分为5种,分别为:NamewFormatTagTYPE_I_UNDEFINED0x0000PCM0x0001PCM8......
UAC 音频数据格式FORMAT_TYPE_2
下面来介绍USB Audio Data Formats 的第一类音频数格式 FORMAT_TYPE_II = 0x02Audio Data Format Type II Codes 其下又分为5种,分别为:NamewFormatTagTYPE_II_UNDEFINED0x1000MPEG0x1001......
USB-UAC麦克风 音频流接口
音频流接口有2个转换接口。UAC麦克风 零带宽转换接口转换接口0是零带宽设置,用于在麦克风未使用。这是通电后的默认设置。实现了零带宽通过指定接口的此备用设置没有与之关联的端点(bNumEndpoints=0)。偏移地址字段长度值描述0bLength10x091bDescriptorType10x04......
USB音箱 UAC音频控制模块
从UAC规范可知,类示于UVC规范,UAB的两个主要模块分别为音频控制模块和音频流模块。这两个模块分别对应一系列的相关描述符。其中UAC的音频控制模块顾名思义,就是实现的是音频的控制功能,同时显示音频的拓扑结构。而音频流模块主要实现的是数据传输。从音频控制接口头描述符(Audio Control ......
UAC 音频通道集描述符
UAC音频通道集描述符全称Audio Channel Cluster Descriptor。音频通道集描述符是为了引入一组具有相同特性(如采样)的音频通道的描述符。音频通道描述符主要包括两方面的内容:bNrChannels:音频通道的数量。bmChannelConfig:位图字段,用于指定通道占用的......
UAC 音频流端点控制请求
音频流端点控制支持如下选择子:ControlSelector ValueEP_CONTROL_UNDEFINED0x00SAMPLING_FREQ_CONTROL0x01PITCH_CONTROL0x02SAMPLING_FREQ_CONTROL采样......
UAC 类特定音频流接口描述符
类特定音频流接口描述符:Class-Specific AS Interface Descriptor.UAC类特定音频流接口描述符 - UVC1.0结构体定义如下:typedef struct _USB_ASI_DESCRIPTOR{ UINT8 bLength; UINT8......
USB音箱 UAC音频流模块
音频流模块主要用于数据的传输,这里只有一个音频流接口,并且这个接口有一个转换接口。当转换接口为0时,在UAC规范中定义为无数据流传输,即设备关闭,故无任何USB端点信息。从Audio Streaming Format Type Descriptor可知,设备支持的是PCM1格式,2通道,每2通道1......
UAC 标准等时音频数据端点描述符
标准等时音频数据端点描述符:Standard AS Isochronous Audio Data Endpoint Descriptor,用于描述音频数据流的传递方式及方法。标准等时音频数据端点描述符数据结构定义如下:typedef struct _USB_AUDIO_ENDPOINT_DESCRI......
关注公众号
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!