我正在尝试使用The Amazing Audio Engine在 OS X 上使用 Swift 录制一些音频。为此,我需要实现一个回调函数,该函数将接收音频并对其进行处理。该文档有一些关于如何使用 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];
或者
id<AEAudioReceiver> receiver = [AEBlockAudioReceiver audioReceiverWithBlock:
^(void *source,
const AudioTimeStamp *time,
UInt32 frames,
AudioBufferList *audio) {
// Do something with 'audio'
}];
据我所知:
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?
}
}
现在我在receiverCallback
属性中遇到错误。我是在正确的轨道上还是我的方法完全错误?
我不知道如何在 Swift 中做同样的事情。我该怎么做?