【问题标题】:Reduce latency/lag in audio in AVCaptureSession?减少 AVCaptureSession 中的音频延迟/滞后?
【发布时间】:2014-02-03 17:08:53
【问题描述】:

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

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

谁能提出减少延迟的解决方案,

或者:

就如何集成一些(尽可能少的)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];
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多