I noticed that when launching my app and creating a fresh AVAudioEngine, engine.inputNode.lastRenderTime is usually > 0. It is nil before calling engine.start(), as expected. However, I would've expected it to start counting from 0 when calling engine.start() for the first time. Calling engine.prepare() and/or engine.reset(), either before or after the .start() call, doesn't change that.
What am I missing? There are several code examples out there that seem to assume that inputNode.lastRenderTime starts counting at zero, such as Analyzing Audio to Classify Sounds. In this part of the Apple documentation, the code snippet states:
However, given that lastRenderTime is not guaranteed to start at zero, I've found the correct code to be:
This requires, however, adding the tap after the engine was started, since otherwise, lastRenderTime will be nil.
Is there a way to reset lastRenderTime to zero so that I don't have to include offsetTime in my code?
What am I missing? There are several code examples out there that seem to assume that inputNode.lastRenderTime starts counting at zero, such as Analyzing Audio to Classify Sounds. In this part of the Apple documentation, the code snippet states:
Code Block audioEngine.inputNode.installTap(onBus: inputBus, bufferSize: 8192, format: inputFormat) { buffer, time in self.analysisQueue.async { self.streamAnalyzer.analyze(buffer, atAudioFramePosition: time.sampleTime) } }
However, given that lastRenderTime is not guaranteed to start at zero, I've found the correct code to be:
Code Block let offsetTime = inputNode.lastRenderTime?.sampleTime ?? 0 audioEngine.inputNode.installTap(onBus: inputBus, bufferSize: 8192, format: inputFormat) { buffer, time in self.analysisQueue.async { self.streamAnalyzer.analyze(buffer, atAudioFramePosition: time.sampleTime - offsetTime) } }
This requires, however, adding the tap after the engine was started, since otherwise, lastRenderTime will be nil.
Is there a way to reset lastRenderTime to zero so that I don't have to include offsetTime in my code?