iOS音频-AudioUnit(使用特定的AudioUnit)

1,557 阅读8分钟

接上文 iOS音频-AudioUnit(官方文档)

4. 使用特定的 Audio Unit

每个iOS音频单元都有某些共同之处,也有一些独特之处。本文档的前几章描述了常见的方面,其中包括需要在运行时找到音频单元,实例化它,并确保其流格式被适当地设置。本章解释了音频单元之间的差异,并提供了如何使用它们的细节。

在后面的章节中,Identifier Keys for Audio Units 列出了你需要在运行时为每个音频单元找到动态链接库的代码。

Using I/O Units

iOS提供了三个 I/O (输入/输出) 单元。绝大多数音频单元应用程序使用 Remote I/O unit,它连接到输入和输出音频硬件,并提供对单个传入和传出音频样本值的低延迟访问。对于VoIP应用程序,Voice-Processing I/O unit 通过添加回声消除和其他功能来扩展 Remote I/O单元。要将音频发送回应用程序而不是输出音频硬件,请使用 Generic Output unit

Remote I/O Unit

Remote I/O 单元 (子类型 kAudioUnitSubType_RemoteIO) 连接到设备硬件进行输入、输出或同时输入和输出。使用它回放,录音,或低延迟的同步输入和输出不需要回声消除。

设备的音频硬件将其音频流格式强加到 Reteme I/O 单元的外部,如Understanding Where and How to Set Stream Formats中所述。音频单元提供硬件音频格式和应用程序音频格式之间的格式转换,通过包含的格式转换器音频单元来实现。

有关演示如何使用此音频单元的示例代码,请参见示例代码项目 aurioTouch

本音频单元的使用说明如表3-1所示。

Table 3-1 Using the Remote I/O unit

Audio unit featureDetails
Elements一个 input element: element 1. 一个 output element: element 0.
默认情况下,input element 是禁用的,output element 是启用的。如果需要更改此属性,请参考kAudioOutputUnitProperty_EnableIO属性的描述。
Recommended stream format attributeskAudioFormatLinearPCM
AudioUnitSampleType
kAudioFormatFlagsAudioUnitCanonical
Stream format notesRemote I/O单元的外部从音频硬件获取其格式如下:
1. input element (element 1) 的 input scope 从当前活动的音频输入硬件获得流格式。
2. output element (element 0) 的 output scope 从当前激活的输出音频硬件获得流格式。
在 input element 的 output scope 上设置应用程序格式。input element 根据需要在其输入和输出范围之间执行格式转换。对应用程序流格式使用硬件采样率。
如果 output element 的输入范围是由音频单元连接提供的,则它从该连接获取其流格式。但是,如果它是由渲染回调函数提供的,那么就在它上面设置应用程序格式。
ParametersNone in iOS.
Properties参考 I/O Audio Unit Properties.
Property notes你永远不需要在这个音频单元上设置 kAudioUnitProperty_MaximumFramesPerSlice 属性

Voice-Processing I/O Unit

Voice-Processing I/O unit (子类型 kAudioUnitSubType_VoiceProcessingIO) 具有 Remote I/O单元的特性,并为双向双工通信增加了回波抑制。它还增加了自动增益校正、语音处理质量调整和静音功能。这是用于VoIP (Voice over Internet Protocol)应用程序的正确I/O单元。

表3-1中列出的所有考虑事项也适用于 Voice-Processing I/O unit。此外,在 Voice-Processing I/O Audio Unit Properties 中描述了该音频单元可用的特定属性

Generic Output Unit

当将audio processing graph的输出发送到应用程序而不是输出音频硬件时,使用子类型kAudioUnitSubType_GenericOutput 的音频单元。您通常会使用Generic Output单元进行脱机音频处理。就像其他I/O单元一样,通用输出单元集成了格式转换器单元。这允许Generic Output单元在 audio processing graph 中使用的流格式和您想要的格式之间执行格式转换。

Using Mixer Units

iOS提供了两个 mixer 单元。在大多数情况下,您应该使用 Multichannel Mixer单元,它为任何数量的单声道或立体声流提供混合。如果你需要3D Mixer单元的功能,你很可能会使用OpenAL来代替。OpenAL是建立在3D Mixer单元之上的,通过一个非常适合游戏应用开发的更简单的API提供同等的性能。

Multichannel Mixer Unit (多通道混合单元)

Multichannel Mixer unit (子类型 kAudioUnitSubType_MultiChannelMixer ) 接受任意数量的单声道或立体声流,并将它们合并成一个单一的立体声输出。它控制音频增益为每个输入和输出,并让您打开或关闭每个输入单独。从iOS 4.0开始,Multichannel Mixer支持立体声平移每个输入。

有关演示如何使用此音频单元的示例代码,请参见示例代码项目audio Mixer (MixerHost)。

本音频单元的使用说明如表3-2所示。

Table 3-2 Using the Multichannel Mixer unit

Audio unit featureDetails
Elements一个或多个 input element ,每一个都可以是单声道的或立体声的。一个立体声 output element
Recommended stream format attributeskAudioFormatLinearPCM
AudioUnitSampleType
kAudioFormatFlagsAudioUnitCanonical
Stream format notes在 inout scope 上,按如下方式管理流格式:
1. 如果 input bus 是由音频单元连接提供的,它就从该连接获得流格式。
2. 如果 input bus 是由回调函数提供的,请在bus上设置完整的应用程序流格式。使用与回调提供的数据相同的流格式。
在 output scope 中,只设置应用程序采样率
Parameters参考 Multichannel Mixer Unit Parameters.
PropertieskAudioUnitProperty_MeteringMode
Property notes默认情况下,kAudioUnitProperty_MaximumFramesPerSlice 属性被设置为1024,当屏幕锁定和显示休眠时,这个值是不够的。如果你的应用程序在屏幕锁定的情况下播放音频,你必须增加这个属性的值,除非音频输入是激活的。执行如下操作:
1. 如果音频输入是活动的,你不需要为 kAudioUnitProperty_MaximumFramesPerSlice 属性设置一个值。
如果音频输入未激活,将此属性设置为4096。

Using Effect Units

iPodEQ单元 (子类型 kAudioUnitSubType_AUiPodEQ) 是iOS 4中唯一提供的效果单元。这是相同的均衡器使用内置的iPod应用程序。要查看iPod应用程序的用户界面为这个音频单元,去设置> iPod > EQ。这个音频单元提供了一套预设均衡曲线,如Bass Booster, Pop,和Spoken Word。

你必须提供自己的用户界面iPod EQ单元,因为你必须为任何音频单元。 Mixer iPodEQ AUGraph Test 示例代码项目演示了如何使用iPodEQ单元,并展示了一种为它提供用户界面的方法。

本音频单元的使用说明如表3-4所示。

Table 3-4 Using the iPod EQ unit

Audio unit featureDetails
Elements一个单声道或立体声 input element。一个单声道或立体声 output element
Recommended stream format attributes1. kAudioFormatLinearPCM
2. AudioUnitSampleType
3. kAudioFormatFlagsAudioUnitCanonical
Stream format notes在 input scope 上,按照以下方式管理流格式:
1. 如果输入是由音频单元连接提供的,它从该连接获取流格式。
2. 如果输入是由回调函数提供的,请在总线上设置完整的应用程序流格式。使用与回调提供的数据相同的流格式。
在 output scope 上,设置与输入相同的完整流格式
ParametersNone.
PropertieskAudioUnitProperty_FactoryPresetskAudioUnitProperty_PresentPreset
Property notesiPod EQ单元提供了一套预先设定的音调均衡曲线作为工厂预设。通过访问音频单元的' kAudioUnitProperty_FactoryPresets '属性获得可用的EQ设置数组。然后你可以通过将它作为' kAudioUnitProperty_PresentPreset '属性的值来应用一个设置。默认情况下,' kAudioUnitProperty_MaximumFramesPerSlice '属性被设置为1024,当屏幕锁定和显示休眠时,这个值是不够的。如果你的应用程序在屏幕锁定的情况下播放音频,你必须增加这个属性的值,除非音频输入是激活的。执行如下操作:如果音频输入是活动的,你不需要为' kAudioUnitProperty_MaximumFramesPerSlice '属性设置一个值。如果音频输入未激活,将此属性设置为4096。

Identifier Keys for Audio Units

这个表提供了您需要访问每个iOS音频单元的动态链接库的标识符键,以及音频单元的简要描述。

Table 3-5 Identifier keys for accessing the dynamically-linkable libraries for each iOS audio unit

Name and descriptionIdentifier keysCorresponding four-char codes
Converter unitSupports audio format conversions to or from linear PCM.kAudioUnitType_FormatConverter
kAudioUnitSubType_AUConverter
kAudioUnitManufacturer_Apple
aufc
conv
appl
iPod Equalizer unitProvides the features of the iPod equalizer.kAudioUnitType_Effect
kAudioUnitSubType_AUiPodEQ
kAudioUnitManufacturer_Apple
aufx
ipeq
appl
3D Mixer unitSupports mixing multiple audio streams, output panning, sample rate conversion, and more.kAudioUnitType_Mixer
kAudioUnitSubType_AU3DMixerEmbedded
kAudioUnitManufacturer_Apple
aumx
3dem
appl
Multichannel Mixer unitSupports mixing multiple audio streams to a single stream.kAudioUnitType_Mixer
kAudioUnitSubType_MultiChannelMixer
kAudioUnitManufacturer_Apple
aumx
mcmx
appl
Generic Output unitSupports converting to and from linear PCM format; can be used to start and stop a graph.kAudioUnitType_Output
kAudioUnitSubType_GenericOutput
kAudioUnitManufacturer_Apple
auou
genr
appl
Remote I/O unitConnects to device hardware for input, output, or simultaneous input and output.kAudioUnitType_Output
kAudioUnitSubType_RemoteIO
kAudioUnitManufacturer_Apple
auou
rioc
appl
Voice Processing I/O unitHas the characteristics of the I/O unit and adds echo suppression for two-way communication.kAudioUnitType_Output
kAudioUnitSubType_VoiceProcessingIO
kAudioUnitManufacturer_Apple
auou
vpio
appl