AudioKit AKMetronome callback timing seems imprecise or quantized

256 views Asked by At

I'm new to AudioKit and digital audio in general, so I'm sure there must be something I'm missing.

I'm trying to get precise timing from AKMetronome by getting the timestamp of each callback. The timing seems to be quantized in some way though, and I don't know what it is.

Example: if my metronome is set to 120, each callback should be exactly 0.5 seconds apart. But if I calculate the difference from one tick to the next, I get this:

0.49145491666786256

0.49166241666534916

0.5104563333334227

0.4917322500004957

0.5104953749978449

0.49178879166720435

0.5103940000008151

0.4916401666669117

It's always one of 2 values, within a very small margin of error. I want to be able to calculate when the next tick is coming so I can trigger animation a few frames ahead, but this makes it difficult. Am I overlooking something?

edit: I came up with a solution since I originally posted this question, but I'm not sure if it's the only or best solution.

I set the buffer to the smallest size using AKSettings.BufferLength.veryShort

With the smallest buffer, the timestamp is always on within a millisecond or two. I'm still not sure though if I'm doing this right, or whether this is the intended behavior of the AKCallback. It seems like the callback should be on time even with a longer buffer.

1

There are 1 answers

4
punkbit On

Are you using Timer to calculate the time difference? From my point of view and based on my findings, the issue is related to the Timer that is not meant to be precise in ios see the thread (Accuracy of NSTimer).

Alternatively, you can look into AVAudioTime (https://audiokit.io/docs/Extensions/AVAudioTime.html)