Audio 在AVCaptureSession中减少音频延迟/延迟?

Audio 在AVCaptureSession中减少音频延迟/延迟?,audio,core-audio,osx-mavericks,latency,avcapturesession,Audio,Core Audio,Osx Mavericks,Latency,Avcapturesession,我正在尝试将音频从任意输入(USB音频设备)路由到任意输出(另一个USB音频设备)。从交叉点开关的角度考虑。无需记录,无需处理,只需将输入A连接到输出B 我已经能够使用下面的概念验证代码来实现这一点,但是输入和输出之间的延迟/延迟过大(几乎1秒)。我在某个地方看到过一篇帖子,声称AVFoundation使用了相当大的音频缓冲区,这是造成延迟的原因,但我没有发现任何地方可以更改缓冲区大小 有没有人能提出一个减少滞后的解决方案 或: 就如何集成一些(尽可能少的)核心音频(或其他)代码以实现我的目标提

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

我已经能够使用下面的概念验证代码来实现这一点,但是输入和输出之间的延迟/延迟过大(几乎1秒)。我在某个地方看到过一篇帖子,声称AVFoundation使用了相当大的音频缓冲区,这是造成延迟的原因,但我没有发现任何地方可以更改缓冲区大小

有没有人能提出一个减少滞后的解决方案

或:

就如何集成一些(尽可能少的)核心音频(或其他)代码以实现我的目标提供建议

在下面的代码中,“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];
}

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