Problems with AVAudioEngine Latency Handling on iOS 17 When Using Remote IO
I'm integrating two systems and I'm collaborating on a project where I'm following best practices but I keep running into I'm currently working on an audio processing app using AVAudioEngine on iOS 17, and I've run into a latency scenario when using the Remote I/O audio unit... The question arises when I attempt to play audio from a file while simultaneously processing microphone input. Despite configuring the engine to minimize latency, I'm observing delays that affect the overall experience. The audio output seems to lag a few hundred milliseconds behind the input, which is unacceptable for my use case. To mitigate this, I've made sure to set the audio session category to `.playAndRecord` and adjusted the preferred I/O buffer size. Hereβs the relevant setup code: ```swift import AVFoundation let audioSession = AVAudioSession.sharedInstance() try? audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker]) try? audioSession.setActive(true) let audioEngine = AVAudioEngine() let inputNode = audioEngine.inputNode let outputNode = audioEngine.outputNode let format = inputNode.outputFormat(forBus: 0) inputNode.installTap(onBus: 0, bufferSize: 1024, format: format) { (buffer, time) in // process audio buffer } try? audioEngine.start() ``` Despite using a buffer size of 1024, I still notice a considerable lag. I also checked the `AVAudioSession`'s latency and the value I received was much higher than expected, giving me clues that something might be misconfigured. I tried different buffer sizes and changing the audio sample rates, but the latency continues. I also attempted to monitor the real-time audio processing using the `AVAudioEngine`'s `outputNode` but couldn't identify any obvious bottlenecks. Could anyone provide insights on how to effectively reduce the latency when working with `AVAudioEngine` and `Remote I/O`? Are there any specific settings or configurations I might be overlooking? Any help would be greatly appreciated! My team is using Swift for this CLI tool. This is part of a larger CLI tool I'm building. Am I approaching this the right way?