减少AVCaptureSession中音频的延迟/滞后?

时间:2014-02-03 17:08:53

标签: audio core-audio osx-mavericks latency avcapturesession

我正在尝试将音频从任意输入(USB音频设备)路由到任意输出(另一个USB音频设备)。考虑交叉点开关。没有记录,没有处理,只需将输入A连接到输出B.

我已经能够使用以下概念验证代码完成此操作,但输入和输出之间的延迟/延迟过大(几乎1秒)。我看到一个帖子(某处)声称AVFoundation使用相当大的缓冲区来处理音频,这是造成延迟的原因,但我还没有找到任何可以改变缓冲区大小的内容。

任何人都可以提出减少滞后的解决方案,

OR:

提供一个关于如何集成一些(尽可能少的)Core Audio(或其他)代码来实现我的目标的建议?

在以下代码中,“IN_1”和“OUT_0,1”是在“音频MIDI设置”控制面板中创建的聚合设备。出于测试目的,它全部在我的测试应用程序委托中实现。

- (void) setUpCaptureSession
{
    NSError * error;
    self.captureSession = [AVCaptureSession new];
    AVCaptureSession * session = self.captureSession;
    AVCaptureDevice * inputDevice = [self deviceWithLocalizedName:@"IN_1"];
    AVCaptureDevice * outputDevice = [self deviceWithLocalizedName: @"OUT_0,1"];
    session.sessionPreset = AVCaptureSessionPresetLow;
    if(inputDevice)
        {
        AVCaptureInput * input = [AVCaptureDeviceInput deviceInputWithDevice: inputDevice error:&error];
        if([session canAddInput: input])
            {
            [session addInput: input];
            }
        }
    if(outputDevice)
        {
        AVCaptureAudioPreviewOutput * output = [AVCaptureAudioPreviewOutput new];
        output.outputDeviceUniqueID = outputDevice.uniqueID;
        output.volume = 1.0;
        if([session canAddOutput: output])
            {
            [session addOutput: output];
            }
        }
    [[NSNotificationCenter defaultCenter] addObserver: self  selector: @selector(handleCaptureSessionError:) name: AVCaptureSessionRuntimeErrorNotification object: self.captureSession];
}

- (AVCaptureDevice *) deviceWithLocalizedName:(NSString *) name
{
    AVCaptureDevice * result = nil;
    NSArray * devices = [AVCaptureDevice devices];
    for(AVCaptureDevice * device in devices)
        {
        if([device.localizedName isCaseInsensitiveLike: name])
            {
            result = device;
            break;
            }
        }
    return result;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
/* NOTE: latency does NOT reduce if the following are run on a background queue */
    [self setUpCaptureSession];
    [self.captureSession startRunning];
}

1 个答案:

答案 0 :(得分:2)

对于低延迟音频,请使用具有短缓冲区的音频单元进行录制和播放,这可能允许延迟数十毫秒或更短,具体取决于硬件。