同步AVAudioPlayerNode和AVAudioEngine录音开始

5
我使用 AVAudioEngine 来播放和录制音频。对于我的使用场景,我需要在开始录制音频时立即播放声音。目前,我的录制似乎是在声音播放之前开始的。如何使声音和录音同时开始?理想情况下,我希望录制和声音可以同时开始,而不是在后期进行同步。
以下是我的代码:
class Recorder {
  enum RecordingState {
    case recording, paused, stopped
  }
  
  private var engine: AVAudioEngine!
  private var mixerNode: AVAudioMixerNode!
  private var state: RecordingState = .stopped
    
    

  private var audioPlayer = AVAudioPlayerNode()
  
  init() {
    setupSession()
    setupEngine()
    
  }
    
    
  fileprivate func setupSession() {
      let session = AVAudioSession.sharedInstance()
    try? session.setCategory(.playAndRecord, options: [.mixWithOthers, .defaultToSpeaker])
      try? session.setActive(true, options: .notifyOthersOnDeactivation)
   }
    
    fileprivate func setupEngine() {
      engine = AVAudioEngine()
      mixerNode = AVAudioMixerNode()

      // Set volume to 0 to avoid audio feedback while recording.
      mixerNode.volume = 0

      engine.attach(mixerNode)

    engine.attach(audioPlayer)
        
      makeConnections()

      // Prepare the engine in advance, in order for the system to allocate the necessary resources.
      engine.prepare()
    }

    
    fileprivate func makeConnections() {
       
      let inputNode = engine.inputNode
      let inputFormat = inputNode.outputFormat(forBus: 0)
        print("Input Sample Rate: \(inputFormat.sampleRate)")
      engine.connect(inputNode, to: mixerNode, format: inputFormat)
      
      let mainMixerNode = engine.mainMixerNode
      let mixerFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: inputFormat.sampleRate, channels: 1, interleaved: false)
    
      engine.connect(mixerNode, to: mainMixerNode, format: mixerFormat)

      let path = Bundle.main.path(forResource: "effect1.wav", ofType:nil)!
      let url = URL(fileURLWithPath: path)
      let file = try! AVAudioFile(forReading: url)
      audioPlayer.scheduleFile(file, at: nil)
      engine.connect(audioPlayer, to: mainMixerNode, format: nil)
        
        }
    
    
    //MARK: Start Recording Function
    func startRecording() throws {
        print("Start Recording!")
      let tapNode: AVAudioNode = mixerNode
      let format = tapNode.outputFormat(forBus: 0)

      let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        
      // AVAudioFile uses the Core Audio Format (CAF) to write to disk.
      // So we're using the caf file extension.
        let file = try AVAudioFile(forWriting: documentURL.appendingPathComponent("recording.caf"), settings: format.settings)
       
      tapNode.installTap(onBus: 0, bufferSize: 4096, format: format, block: {
        (buffer, time) in

        try? file.write(from: buffer)
        print(buffer.description)
        print(buffer.stride)
       
        //Do Stuff
        print("Doing Stuff")
      })
    
      
      try engine.start()
        audioPlayer.play()
      state = .recording
    }
    
    
    //MARK: Other recording functions
    func resumeRecording() throws {
      try engine.start()
      state = .recording
    }

    func pauseRecording() {
      engine.pause()
      state = .paused
    }

    func stopRecording() {
      // Remove existing taps on nodes
      mixerNode.removeTap(onBus: 0)
      
      engine.stop()
      state = .stopped
    }
    

    
    
}

不幸的是(也许令人惊讶的是),我认为没有一种准确的方法可以在不测量的情况下确定延迟。 - sbooth
@sbooth 我该如何测量延迟? - coder
最理想的方法是使用环回电缆,将设备的输出连接到其输入。您可以向输出发送测试信号,记录发送时间,然后检测输入中的相同信号。时间差就是往返延迟。虽然有一些使用 AVAudioEngine 进行估算的方法,但在我的实验中,没有一种是样本精确的。 - sbooth
嗯...我可以这样做,但如果用户降低音量会怎么样呢?这会使检测输出信号变得困难。如果您知道使用 AVAudioEngine 估计往返延迟的方法,请在回答中分享它们。大致估算总比什么都不知道好! - coder
这里有一些关于https://dev59.com/oFEG5IYBdhLWcg3wdufF的讨论。 - sbooth
1个回答

0

你试过在安装tap之前启动播放器吗?

// Stop the player to be sure the engine.start calls the prepare function
audioPlayer.stop()
try engine.start()
audioPlayer.play()
state = .recording

tapNode.installTap(onBus: 0, bufferSize: 4096, format: format, block: {
        (buffer, time) in
        try? file.write(from: buffer)
      })

如果在这种情况下,您的录制稍晚一些,可以尝试使用player.outputPresentationLatency进行补偿。根据文档,这是一个最大值。意味着时间可能略低于此值。我希望这值得一试。
print(player.outputPresentationLatency)
// 0.009999999776482582

let nanoseconds = Int(player.outputPresentationLatency * pow(10,9))
let dispatchTimeInterval = DispatchTimeInterval.nanoseconds(nanoseconds)
            
player.play()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + dispatchTimeInterval) {
    self.installTap()
    self.state = .recording
}

当我尝试实现你的解决方案时,我遇到了一个错误:`'AVAudioPlayerNode'类型的值没有'member'prepareToPlay'。请记住,'audioPlayer'的类型是'AVAudioPlayerNode'。 - coder
此外,延迟必须尽可能小,因为我可能会使用样本数量来计算音频中某些声音或模式发生的时间。 - coder
抱歉,AudioEngine.prepare函数已经具有同样的功能。我更正了我的答案。 在回答前我会进行一些测试,因为prepare函数不足够奇怪。也许可以使用回调函数。 - Moose
1
旁白:他不会。 - Rob Keniger
历史学家:叙述者是正确的。 - undefined
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接