Is it possible to use AVAudioEngine to monitor an input device through an output device?

I understand that with AVAudioEngine, I can access .inputNode and .outputNode.

I also understand that it is possible to point audio at a particular output device:
Code Block
var outputDeviceID: AudioDeviceID = 50  // External display speakers
guard let outputUnit: AudioUnit = engine.outputNode.audioUnit else { return }
AudioUnitSetProperty(outputUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 1, &outputDeviceID, UInt32(MemoryLayout<AudioDeviceID>.size))


Yet, if I try to capture, or even simply access the .inputNode, this will fail.

Doing this:
Code Block
let input = engine.inputNode
let output = engine.outputNode


and then setting a custom outputDeviceID as show above, will cause this error:
Code Block
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.coreaudio.avfaudio Code=-10875 "(null)" UserInfo={false condition=IsFormatSampleRateAndChannelCountValid(outputHWFormat)}


I could not find any documentation about this, but I did find others on StackOverflow who had similar issues and came to the conclusion that it cannot be done this way.

I am looking for an alternative.

My goal is to simply connect Input Device A with Output Device B, where B is not necessarily the System Output device.

I saw one suggestion to create an AudioUnit to pass the data through, but I am not clear on how to do that without explicitly referring to engine.inputNode, which will cause the above error.

I know that this has to be possible because applications like Logic Pro and GarageBand have preference panes where one can select an input device and output device and do live monitoring with very low latency. While I would prefer an option that has no latency, I'd be interested in whatever is possible.

Thank you!
Is it possible to use AVAudioEngine to monitor an input device through an output device?
 
 
Q