Search code examples
iosaudiokitavaudioengine

AudioKit in combination with an AVAudioSinkNode


I'm trying to use an AVAudioSinkNode in combination with AudioKit. In the code below this is the flow: Input > Fader > Output. While the SinkNode gets attached to the Fader.

The SinkNode works but the audio further up the chain gets muted. I would expect to hear all input audio but instead there's no audio at all.

If I would remove the attach and connect from the start function the normal flow works fine (the input is send to the output). But obviously the sinkNode does not get any audio anymore.

How can I make sure the SinkNode gets its audio while keeping the rest of the flow intact?

let sinkNode: AVAudioSinkNode
let inputMonitoringNode: Fader

private init() {
    engine = AudioEngine()

    inputMonitoringNode = Fader(engine.input!)

    engine.output = inputMonitoringNode

    sinkNode = AVAudioSinkNode() { (timestamp, frames, audioBufferList) in
        // Store buffer to be processed in other thread
        // Not using a tap because it's to slow
    }
}

func start() {
    do {
        try engine.start()
    } catch let err {
        print(err)
    }

    if (!engine.avEngine.attachedNodes.contains(sinkNode)) {
        engine.avEngine.attach(sinkNode)
        engine.avEngine.connect(inputMonitoringNode.avAudioNode, to: sinkNode, format: nil)
    }
}

I've tried attaching the SinkNode to other nodes but with no results. As mentioned in the comments I also tried using a tap but it's too slow for my needs.


Solution

  • Instead of using engine.avEngine.connect(inputMonitoringNode.avAudioNode, to: sinkNode, format: nil) I needed to use the connect function that accepts AVAudioConnectionPoint's.

    engine.avEngine.attach(sinkNode)
    
    var connectionPoints = engine.outputConnectionPoints(for: inputMonitoringNode.avAudioNode, outputBus: 0)
    connectionPoints.append(AVAudioConnectionPoint(node: sinkNode, bus: 0))
    
    engine.avEngine.connect(inputMonitoringNode.avAudioNode, to: connectionPoints, fromBus: 0, format: nil)