Inter-app Audio and AVAudioEngine?

I've been a good boy and rewritten all my musical instrument apps that were originally using OpenAL to now exclusively use AVAudioEngine. Works fantastically and is an incredibly flexible architecture that allows me to do so much more that I was ever able to do on OpenAL.


Now my users really want me to add Inter-App Audio support to my apps so they can record the output of my instruments.


Considering the push by Apple to switch to AvAudioEngine, as well as their evangelism on Inter-App Audio, there has to be a relatively straightforward way to do this, or at least some sample code on bridging AVAudioEngine to Inter-App Audio. I would imagine it involves installing a tap on the main mixer, but after that I have no clue how to proceed or even how to start.


Does anyone have any example code for this or can make a recommendation as to how to go about making this work? I'm sure I'm not the only developer who wants to do this, and I'd think Apple would want this to be readily available as well.


Thank you,


Michael Eskin

AppCordions.com

Replies

Hi Michael,


Be really interested to hear if you made any headway on this as I'm in a similar situation as of yesterday. I've managed to publish my AU so it shows up in IAA apps as an instrument but no luck re-routing the audio to the host thus far. I'm getting the route change notification when the IAA host discovers my app, but Im kinda scratching around figuring how to deal with it.


Documentation / examples are you probably know are very hard to find and there's virtually no Swift examples. Seems like crossing the border from AVFoundation to Core Audio isn't as straightforward as you'd hope it might be! :-)


Cheers, -Rob


edit - I'm going to ask this question over on the AVFoundation and see if we get any luck there.

Hi Michael,


I'm using 3rd party audio libraries for playing,recording etc for host app. This libraries hard to integrate with IAA.

I see your post when i was searching AVAudioEngine. If you succeed integrating AVAudioEngine with IAA or if you have found any clue to do this

i'll change my audio engine to AVAudioEngine.


Thanks

Umut

FWIW, I got this working in Swift without any 3rd-party libraries, but I have to say getting the IAA host property change notification (AudioUnitAddPropertyListener) to work was not pretty. If you're not familiar with dealing with UnsafeMutableRawPointers and bridging between Swift classes and C functions, I'd suggest you don't go there... -Rob

Hi Rob,

That's exactly the problem I'm having. I've coded my app in Swift 3 and want

to use IAA. I was following an objective C tutorial on doing this and got

stuck on the UnsafeMutableRawPointers, specifically the fourth parameter to

AudioOutputUnitPublish, inOutputUnit.

It would be a great help if you have some example code to share. I might be

able to figure it out from there.

Cheers Matt,

Well, this is a super old post, but I just figured out one (possibly terrible) way of doing this by creating a semaphore and passing around pointers...


        var sem = DispatchSemaphore(value: 0)
        DispatchQueue.global(qos: .background).async {
            while true {
                sem.wait()
               
                do {
                    try AVAudioSession.sharedInstance().setActive(true)
                    try self.engine.start()
                }
                catch {
                    print("unable to start Audio Engine!!!: \(error)")
                }
            }
        }
       
        let proc: AudioUnitPropertyListenerProc = { (inRefCon, inUnit, inID, inScope, inElement) in
            let sem = inRefCon.assumingMemoryBound(to: DispatchSemaphore.self).pointee
            sem.signal()
        }
       
        AudioUnitAddPropertyListener(self.engine.outputNode.audioUnit!,kAudioUnitProperty_IsInterAppConnected, proc,UnsafeMutableRawPointer(&sem))