使用AVAudioEngine在音乐应用程序中分离信号

今天,我了解到,您基本上可以通过使用辅助方法在AVAudioEngine中拆分信号。

您为什么需要这样做?

在普通的AVAudioEngine应用中,所有节点都是一对一连接的。 文件播放器先连接到效果,然后再连接到输出。

并行运行而不是一条长链,有许多声音效果可供探索。 您还可以通过创造性地使用子混音器来施加更多控制。

就我而言,我想实现一种功能,以记录与音频源混合的输入,而不会听到(监视)输出混合中的输入。

您可以使用矩阵混合器

当我偶然发现AVAudioEngine中的多重连接方法时,通过旧的C API在AVAudioUnit中手动配置矩阵混合器的过程非常顺利。 我实际上是想了解默认的connect方法是如何在后台运行的,以查看是否可以在设置流描述时随时节省自己的时间。

绝对有可能,但是还有很多工作要做。

使用这种多重连接方法更容易

这是有问题的方法:

func connect(_ sourceNode: AVAudioNode, 
to destNodes: [AVAudioConnectionPoint],
fromBus sourceBus: AVAudioNodeBus,
format: AVAudioFormat?)

文档说的不多,但是标题有更多信息。

 Use this method to establish connections from a source node to multiple destination nodes. Connections made using this method are either one-to-one (when a single destination connection is specified) or one-to-many (when multiple connections are specified), but never many-to-one. 

编写AVAudioConnectionPoint集合只是用您最终希望与之连接的节点初始化对象的问题:

 let mixerPoint = AVAudioConnectionPoint(node: mixer, bus: mixer.nextAvailableInputBus) 
let subMixerPoint = AVAudioConnectionPoint(node: subMixerPoint, bus: subMixer.nextAvailableInputBus)

然后很容易连接

 engine.connect(input, to: [mixerPoint, subMixerPoint], fromBus: 0, format: input.outputFormat(forBus: 0)) 

iOS 9上无需监控即可录制的特定用例

在iOS 9中,除非将AVAudioEngine连接到输出,否则它不会通过其信号路径移动任何声音。 如果您想实时记录而不进行监视,这将是一个挑战。 要激活任何静音子混音器,只需将其音量设置为0,然后连接到输出即可。