2

我正在尝试使用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 中做同样的事情。我该怎么做?

4

2 回答 2

0

在 Swift 中,函数和块的处理几乎是平等的,具有统一的闭包概念。我建议您阅读(连同文档的其余部分)以了解闭包语法和语义。

于 2015-11-21T15:06:26.920 回答
0

这是我正在工作的 taae swift 项目中的一个示例

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
    }
}
于 2016-01-18T13:28:12.840 回答