如何从聚合CoreAudio设备中排除输入或输出通道



我有一个基于CoreAudio的MacOS/X程序,它允许用户选择输入音频设备和输出音频设备,并且(如果用户没有为输入和输出选择相同的设备(我的程序创建了一个专用聚合音频设备,并使用它来接收音频-音频,处理它,然后发送出去播放。

这一切都很好,但有一个小问题——如果选定的输入设备也有一些与硬件相关的输出,这些输出将显示为聚合设备输出通道的一部分,这不是我想要的行为。类似地,如果选定的输出设备也有一些与其硬件相关的输入,那么这些输入将在聚合设备的输入中显示为输入通道,这也是我不想要的。

我的问题是,有没有办法告诉CoreAudio不要在我正在构建的聚合设备中包括子设备的输入或输出?(我的后备解决方案是修改我的音频渲染回调,以忽略不需要的音频通道,但这似乎不太优雅,所以我很好奇是否有更好的方法来处理它(

我创建聚合设备的功能如下,以防相关:

// This code was adapted from the example code at :  https://web.archive.org/web/20140716012404/http://daveaddey.com/?p=51
ConstCoreAudioDeviceRef CoreAudioDevice :: CreateAggregateDevice(const ConstCoreAudioDeviceInfoRef & inputCadi, const ConstCoreAudioDeviceInfoRef & outputCadi, bool require96kHz, int32 optRequiredBufferSizeFrames)
{
OSStatus osErr = noErr;
UInt32 outSize;
Boolean outWritable;
//-----------------------
// Start to create a new aggregate by getting the base audio hardware plugin
//-----------------------
osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable);
if (osErr != noErr) return ConstCoreAudioDeviceRef();
AudioValueTranslation pluginAVT;
CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio");
AudioObjectID pluginID;
pluginAVT.mInputData      = &inBundleRef;
pluginAVT.mInputDataSize  = sizeof(inBundleRef);
pluginAVT.mOutputData     = &pluginID;
pluginAVT.mOutputDataSize = sizeof(pluginID);
osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT);
if (osErr != noErr) return ConstCoreAudioDeviceRef();
//-----------------------
// Create a CFDictionary for our aggregate device
//-----------------------
CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFStringRef aggregateDeviceNameRef = CFSTR("My Aggregate Device");
CFStringRef aggregateDeviceUIDRef  = CFSTR("com.mycomapany.myaggregatedevice");
// add the name of the device to the dictionary
CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), aggregateDeviceNameRef);
// add our choice of UID for the aggregate device to the dictionary
CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), aggregateDeviceUIDRef);
if (IsDebugFlagEnabled("public_cad_device") == false)
{
// make it private so that we don't have the user messing with it
int value = 1;
CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), CFNumberCreate(NULL, kCFNumberIntType, &value));
}
//-----------------------
// Create a CFMutableArray for our sub-device list
//-----------------------
// we need to append the UID for each device to a CFMutableArray, so create one here
CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
// add the sub-devices to our aggregate device
const CFStringRef  inputDeviceUID =  inputCadi()->GetPersistentUID().ToCFStringRef();
const CFStringRef outputDeviceUID = outputCadi()->GetPersistentUID().ToCFStringRef();
CFArrayAppendValue(subDevicesArray,  inputDeviceUID);
CFArrayAppendValue(subDevicesArray, outputDeviceUID);
//-----------------------
// Feed the dictionary to the plugin, to create a blank aggregate device
//-----------------------
AudioObjectPropertyAddress pluginAOPA;
pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice;
pluginAOPA.mScope    = kAudioObjectPropertyScopeGlobal;
pluginAOPA.mElement  = kAudioObjectPropertyElementMaster;
UInt32 outDataSize;
osErr = AudioObjectGetPropertyDataSize(pluginID, &pluginAOPA, 0, NULL, &outDataSize);
if (osErr != noErr) return ConstCoreAudioDeviceRef();
AudioDeviceID outAggregateDevice;
osErr = AudioObjectGetPropertyData(pluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, &outAggregateDevice);
if (osErr != noErr) return ConstCoreAudioDeviceRef();
//-----------------------
// Set the sub-device list
//-----------------------
pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList;
pluginAOPA.mScope    = kAudioObjectPropertyScopeGlobal;
pluginAOPA.mElement  = kAudioObjectPropertyElementMaster;
outDataSize = sizeof(CFMutableArrayRef);
osErr = AudioObjectSetPropertyData(outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray);
if (osErr != noErr) return ConstCoreAudioDeviceRef();
//-----------------------
// Set the master device
//-----------------------
// set the master device manually (this is the device which will act as the master clock for the aggregate device)
// pass in the UID of the device you want to use
pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice;
pluginAOPA.mScope    = kAudioObjectPropertyScopeGlobal;
pluginAOPA.mElement  = kAudioObjectPropertyElementMaster;
outDataSize = sizeof(outputDeviceUID);
osErr = AudioObjectSetPropertyData(outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &outputDeviceUID);
if (osErr != noErr) return ConstCoreAudioDeviceRef();
//-----------------------
// Clean up
//-----------------------
// release the CF objects we have created - we don't need them any more
CFRelease(aggDeviceDict);
CFRelease(subDevicesArray);
// release the device UID CFStringRefs
CFRelease(inputDeviceUID);
CFRelease(outputDeviceUID);
ConstCoreAudioDeviceInfoRef infoRef = CoreAudioDeviceInfo::GetAudioDeviceInfo(outAggregateDevice);
if (infoRef())
{
ConstCoreAudioDeviceRef ret(new CoreAudioDevice(infoRef, true));
return ((ret())&&(SetupSimpleCoreAudioDeviceAux(ret()->GetDeviceInfo(), require96kHz, optRequiredBufferSizeFrames, false).IsOK())) ? ret : ConstCoreAudioDeviceRef();
}
else return ConstCoreAudioDeviceRef();
}

有一些方法可以处理通道映射(您基本上正在描述(,但我怀疑它是否是一个"更好";就你的情况而言。

使用音频单元的AudioToolbox框架中涵盖了此类功能。特别是kAudioUnitSubType_HALOutputAudioUnit(AUComponent.h(在这种情况下很有趣。

使用这种类型的AudioUnit,您可以以指定的频道格式向特定音频设备发送音频,也可以从特定音频设备接收音频。当所需的通道布局与设备的通道布局不匹配时,可以进行通道映射。

要获得一些技术细节,请查看:https://developer.apple.com/library/archive/technotes/tn2091/_index.html

请注意,许多AudioToolbox正在被AVAudioEngine取代。

所以,在你的情况下,我认为忽略你不需要的样本会更容易进行手动通道映射。此外,我不确定CoreAudio是否提供"切片"输出缓冲区。当然可以考虑让他们自己沉默。

编辑

看看AudioHardware.h中的文档,似乎有一种方法可以启用和禁用特定IOProc的流。当OS X创建聚合时,它会将不同子设备的所有通道放在不同的流中,因此在您的情况下,您应该能够禁用包含输出设备输入的流,反之亦然,禁用包含输入设备输出的流。

为此,请查看AudioHardware.h 中的AudioHardwareIOProcStreamUsagekAudioDevicePropertyIOProcStreamUsage

我发现苹果公司的HALLab实用程序在了解实际流方面非常有用。(https://developer.apple.com/download/more/并搜索";用于Xcode的音频工具"(

最新更新