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

Jer*_*ner 5 macos core-audio

我有一个基于 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();
}
Run Code Online (Sandbox Code Playgroud)

Ruu*_*ema 4

有多种方法可以处理通道映射(您基本上正在描述),但我怀疑这对于您的情况是否是“更好”的方法。

使用音频单元的 AudioToolbox 框架涵盖了此类功能。在这种情况下,尤其是 kAudioUnitSubType_HALOutput AudioUnit (AUComponent.h) 很有趣。

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

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

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

因此,就您的情况而言,我认为通过忽略不需要的样本来进行手动通道映射会更容易。另外,我不确定 CoreAudio 是否提供“切片”输出缓冲区。当然,请考虑自己让他们保持沉默。

编辑

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

为此,请查看AudioHardware.h 中的AudioHardwareIOProcStreamUsage和两者kAudioDevicePropertyIOProcStreamUsage

我发现 Apple 的 HALLLab 实用程序对于了解实际流非常有用。(https://developer.apple.com/download/more/并搜索“Audio Tools for Xcode”)