I'm trying to use The Amazing Audio Engine to record some audio using Swift on OS X. To do this, I need to implement a callback function that will receive the audio and do something with it. The documentation has some examples on how to do this with Objective-C:
@interface MyAudioReceiver : NSObject <AEAudioReceiver>
@end
@implementation MyAudioReceiver
static void receiverCallback(__unsafe_unretained MyAudioReceiver *THIS,
__unsafe_unretained AEAudioController *audioController,
void *source,
const AudioTimeStamp *time,
UInt32 frames,
AudioBufferList *audio) {
// Do something with 'audio'
}
-(AEAudioReceiverCallback)receiverCallback {
return receiverCallback;
}
@end
...
id<AEAudioReceiver> receiver = [[MyAudioReceiver alloc] init];
or
id<AEAudioReceiver> receiver = [AEBlockAudioReceiver audioReceiverWithBlock:
^(void *source,
const AudioTimeStamp *time,
UInt32 frames,
AudioBufferList *audio) {
// Do something with 'audio'
}];
This is as far as I got:
var audioController: AEAudioController? = nil
audioController = AEAudioController(audioDescription: AEAudioStreamBasicDescriptionInterleaved16BitStereo, inputEnabled: true)
do {
try audioController?.start()
} catch {
NSLog("An error happened while starting AEAudioController.")
}
let receiver = MyAudioReceiver();
audioController?.addInputReceiver(receiver)
class MyAudioReceiver : NSObject, AEAudioReceiver {
var receiverCallback: AEAudioReceiverCallback! {
// what do I do here?
}
}
Now I'm getting an error in the receiverCallback
property. Am I on the right track here or is my approach completely wrong?
I can't figure out how to do the exact same thing in Swift. How would I do that?