7

I'd like to connect my electric guitar to my app. I have hardware (the Line6 Sonic Port) that passes the audio from my guitar into my iPhone. I've figured out how to get audio playing out to my headphones, but it's audio coming from my headphone mic, not the Lightning Port input. How do I programmatically find the Lightning Port audio input, instead of getting audio via the headphone mic?

Here's what I've tried so far:

self.audioEngine = AVAudioEngine()

let input = self.audioEngine.inputNode
let mixer = self.audioEngine.mainMixerNode
let output = self.audioEngine.outputNode

self.audioEngine.inputNode.installTapOnBus(0, bufferSize: 128, format: input.inputFormatForBus(0)) { (buffer, time) -> Void in
    //
}

self.audioEngine.connect(input, to: mixer, format: input.inputFormatForBus(0))
self.audioEngine.connect(mixer, to: output, format: mixer.inputFormatForBus(0))

self.audioEngine.prepare()
self.audioEngine.startAndReturnError(nil)

When I run this, I hear audio - but it's coming from my headphone mic, not the guitar. How can I connect to audio coming from the lightning port?

For a quick illustration, here is the hardware that I'm using: Line6 Sonic Port

bryanjclark
  • 5,978
  • 2
  • 31
  • 63
  • I haven't used AVAudioEngine before. Neither have I used the Line6 Sonic Port - so could be either of those. I've used the earlier Core Audio frameworks with my Apogee Jam and it "just worked" when connected. Is the headphone connected to the Sonic Port (I see a jack on the side)? Can you change the input node / settings on the input node. – e7mac Jan 22 '15 at 07:52
  • 1
    Does your input device show up in `AVAudioSession.availableInputs`? – sbooth Jan 23 '15 at 00:08

2 Answers2

5

To connect to a particular audio input, you need to configure the sharedInstance of AVAudioSession, by using the setPreferredInput:error: method.

Here's how this is done in Objective-C:

AVAudioSession *sharedSession = [AVAudioSession sharedInstance];
NSArray *availableInputs = [sharedSession availableInputs];
[availableInputs enumerateObjectsUsingBlock:^(AVAudioSessionPortDescription *portDescription, NSUInteger idx, BOOL *stop) {
    if (portDescription.portType == AVAudioSessionPortUSBAudio) {
        [sharedSession setPreferredInput:portDescription error:nil];
    }
}];

To learn more, check out:

bryanjclark
  • 5,978
  • 2
  • 31
  • 63
2

iOS normally routes both audio input and output from the last port that was plugged in. Since you plugged in headphones, that's the port it uses for recording. Unplug the headphones, plug in lightning, and it will use that route for both audio record and play.

hotpaw2
  • 68,014
  • 12
  • 81
  • 143