CoreAudio枚举HAL AudioID是不一致的

时间:2015-12-14 22:58:03

标签: macos audio core-audio

我正在进行正常的“迭代”过程以获取Cocoa上可用音频设备的列表,并且遇到了一个奇怪的情况。这是一段代码:

ser = serial.Serial('/dev/ttyS0')   # first physical serial port
ser = serial.Serial('/dev/ttyUSB0') # first usb serial port

我这样做是为了获取硬件设备UID的列表,以便我可以选择它们供以后使用。

奇怪的是,在我运行El Capitan的旧MacPro上,输入数组中的音频设备ID与'kAudioHardwarePropertyTranslateUIDToDevice'调用返回的内容不匹配。在其他系统上,ID确实匹配。

我的底层NSLog函数吐出这样的信息:

AudioObjectPropertyAddress propertyAddress =
{
    kAudioHardwarePropertyDevices,
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyElementMaster
};

UInt32 dataSize = 0;
OSStatus status = AudioHardwareServiceGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if(status != kAudioHardwareNoError)
{
    NSLog (@"AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i", status);
    return;
}

UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID));

AudioDeviceID *audioDevices = (AudioDeviceID *)(calloc(dataSize,1));
if(NULL == audioDevices)
{
    NSLog (@"Unable to allocate memory");
    return;
}

status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if(status != kAudioHardwareNoError)
{
    fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %i\n", status);
    free(audioDevices);
    audioDevices = NULL;
    return;
}

CFMutableArrayRef inputDeviceArray = CFArrayCreateMutable(kCFAllocatorDefault, deviceCount, &kCFTypeArrayCallBacks);
if(NULL == inputDeviceArray)
{
    NSLog (@"CFArrayCreateMutable failed");
    free(audioDevices);
    audioDevices = NULL;
    return;
}

// Iterate through all the devices and determine which are input-capable
propertyAddress.mScope = kAudioDevicePropertyScopeInput;
for(UInt32 i = 0; i < deviceCount; ++i)
{
    // Query device UID
    CFStringRef deviceUID = NULL;
    dataSize = sizeof(deviceUID);
    propertyAddress.mSelector = kAudioDevicePropertyDeviceUID;
    status = AudioHardwareServiceGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceUID);
    if(status != kAudioHardwareNoError)
    {
        NSLog (@"AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID) failed: %i", status);
        continue;
    }

    // For fun, let's do a reverse lookup for device ID

    AudioDeviceID ourDeviceID;
    UInt32 dataSize = sizeof(ourDeviceID);
    propertyAddress.mSelector = kAudioHardwarePropertyTranslateUIDToDevice;
    // Just for fun, get the default input device and compare results
    status = AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                                 &propertyAddress,
                                                 sizeof(deviceUID),
                                                 &deviceUID,
                                                 &dataSize,
                                                 &ourDeviceID);
    if (kAudioHardwareNoError != status)
    {
        NSLog (@"Can't reverse lookup audio id");
    }

    if (ourDeviceID != audioDevices[i])
        NSLog (@"Reversed audio id %d Not equal to audioDevice[] of %d", ourDeviceID, audioDevices[i]);

这适用于我系统上的9种音频设备。这在尝试为我的HAL输出设备选择AudioID时引起了一个巨大的问题,但自从转换为'kAudioHardwarePropertyTranslateUIDToDevice'调用后,我似乎已经解决了这个问题。

真正的问题是:为什么这些数字会有所不同?他们不应该总是返回相同的号码吗?

0 个答案:

没有答案