I am creating an application that uses Audio Units, and while there are many examples of code in Objective-C (including Apple's own aurioTouch and others) I am attempting to code the entire thing in Swift.
I have been able to set up my AUGraph and run some audio through it, but I cannot seem to figure out the syntax for the render callbacks. I have tried a couple of methods:
Method 1: Create an AURenderCallback directly
let render : AURenderCallback = { (
inRefCon: UnsafeMutablePointer<Void>,
ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>,
inTimeStamp: UnsafePointer<AudioTimeStamp>,
inBufNumber: UInt32,
inNumberFrames: UInt32,
ioData: UnsafeMutablePointer<AudioBufferList>) -> OSStatus in
return noErr
}
I do nothing in this callback besides returning noErr at this point since I am just trying to make it work. However, the compiler returns the following error:
(UnsafeMutablePointer, UnsafeMutablePointer, UnsafePointer, UInt32, UInt32, UnsafeMutablePointer) -> OSStatus' is not convertible to 'AURenderCallback
The definition of AURenderCallback in the documentation is this:
typealias AURenderCallback = (UnsafeMutablePointer<Void>,UnsafeMutablePointer<AudioUnitRenderActionFlags>,UnsafePointer<AudioTimeStamp>, UInt32, UInt32,UnsafeMutablePointer<AudioBufferList>) -> OSStatus
It seems to be the same as what I had entered, although it could be that I do not understand what the documentation is asking for.
Method 2: Create a function representing the AURenderCallback
func render(
inRefCon: UnsafeMutablePointer<Void>,
ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>,
inTimeStamp: UnsafePointer<AudioTimeStamp>,
inBufNumber: UInt32,
inNumberFrames: UInt32,
ioData: UnsafeMutablePointer<AudioBufferList>) -> OSStatus {
return noErr
}
This does not give any errors as a function, but when I place it into AURenderCallbackStruct in the inputProc parameter I receive an error:
Cannot find an initializer for type 'AURenderCallbackStruct' that accepts an argument list of type '(inputProc: (UnsafeMutablePointer, UnsafeMutablePointer, UnsafePointer, UInt32, UInt32, UnsafeMutablePointer) -> OSStatus, inputProcRefCon: nil)
I have not found many examples of creating AURenderCallbacks in Swift, and there seems to be a large difference in syntax compared with Objective-C. Any help would be appreciated.