Set AVAudioEngine Input and Output Devices

avaudioengine, avfoundation, objective-c

Solution

Ok, after re-reading the docs for the 10th time, I noticed `AVAudioEngine` has members inputNode and outputNode (not sure how I missed that!).

The following code seems to do the job:

AudioDeviceID inputDeviceID = 53; // get this using AudioObjectGetPropertyData
AVAudioEngine *engine = [[AVAudioEngine alloc] init];
AudioUnit audioUnit = [[engine inputNode] audioUnit];

OSStatus error = AudioUnitSetProperty(audioUnit,
                                      kAudioOutputUnitProperty_CurrentDevice,
                                      kAudioUnitScope_Global,
                                      0,
                                      &inputDeviceID,
                                      sizeof(inputDeviceID));

I borrowed the non-AVFoundation C code from the CAPlayThrough example.

Problem

I've been playing around with Apple's shiny new `AVFoundation` library, but so far I've unable to set the input or output devices (e.g. a USB sound card) used by an `AVAudioEngine`, and I can't seem to find anything in the documentation to say it's even possible. Does anyone have any experience with this?

Original source