Search code examples
swiftavaudioengine

Getting CPU clock time AVAudioEngine


How does one get the clock time or sample accurate time on the running AVAudioEngine?


Solution

  • From the docs:

    The AVAudioEngine class defines a group of connected AVAudioNode objects, known as audio nodes. You use audio nodes to generate audio signals, process them, and perform audio input and output.

    For clock and/or sample time, you need to call lastRenderTime on a valid AVAudioNode instance.

    The lastRenderTime method returns an AVAudioTime object. With the AVAudioTime, you can retrieve time:

    1. sampleTime will return the sample time is sampleTimeValid returns YES.
    2. hostTime will return host time if hostTimeValid returns `YES.