2

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?

nerdinand
  • 876
  • 12
  • 21

2 Answers2

0

In Swift, functions and blocks are treated almost equally, with the unified notion of closures. I suggest you read up (along with the rest of the documentation) in order to understand closure syntax and semantics.

Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
0

here is a example from my working taae swift project

var receiverCallback: AEAudioReceiverCallback! {
    return  { (receiver:AnyObject?, audioController:AEAudioController?, source:UnsafeMutablePointer<Void>, time:UnsafePointer<AudioTimeStamp>, frames:UInt32, audio:UnsafeMutablePointer<AudioBufferList>) -> Void in
        //do some thing with audio data here
    }
}
z33
  • 1,193
  • 13
  • 24