Using AVFoundation and AVAudioNode how do I take one audio source and route it to multiple destinations?

Viewed 238

I am trying to do something which in theory should be very simple, i.e. take the audio produced from a single AVAudioNode and route it to multiple outputs which are also AVAudioNodes. However, I do not see an classes that do this. This is essentially like a splitter object.

I want to do something like this:

import AVFoundation
class Router{
func connect(){
        let node1 = mixerNode()
        let node2 = mixerNode()
        let node3 = mixerNode()
        let engine = AVAudioEngine()
        let format = node1.outputFormat(forBus: 0)
        engine.attach(node1)
        engine.attach(node2)
        engine.attach(node3)
        engine.connect(node1, to: node2, format: format)
        engine.connect(node1, to: node3, format: format)
        let connections = engine.outputConnectionPoints(for: node1, outputBus: 0)
        print("Num connections = \(connections.count)")
}

However, the console shows that node1 is only attached to one node as it can only have only one output. Is there an object which allows multiple outputs?

1 Answers

I figured out how to do this. There is no "splitter" node. However, AVAudioEngine does allow you to route any one node to multiple output destinations by using AVAudioConnection like this:

   import AVFoundation
   class someClass{ 
        func connectMultipleNodes(){
            let engine = AVAudioEngine()
            let node1 = AVAudioNode()
            let node2 = AVAudioNode()
            let node3 = AVAudioNode()
            engine.attach(node1)
            engine.attach(node2)
            engine.attach(node3)
            let format = node1.outputFormat(forBus: 0)
            let connection1 = AVAudioConnectionPoint(node: node2, bus: 0)
            let connection2 = AVAudioConnectionPoint(node: node3, bus: 0)
            let connections = [connection1, connection2]
            engine.connect(node1, to: connections, fromBus: 0, format: format)
       }
   }
Related