0

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.

JiminyKirket
  • 34
  • 1
  • 2

1 Answers1

0

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)

E_net4
  • 27,810
  • 13
  • 101
  • 139
punkbit
  • 7,347
  • 10
  • 55
  • 89
  • This is not using, Timer, but AKMetronome, part of AudioKit: https://audiokit.io/. I actually forgot about this question, and figured out a solution since then, so I'll edit my question. – JiminyKirket May 05 '20 at 17:52
  • @JiminyKirket I meant for the `timestamp `. I had the same issue, you may find this one interesting regarding AVaudioTime and latency compensation ( https://github.com/AudioKit/AudioKit/blob/master/Examples/iOS/LoopbackRecording/LoopbackRecording/ViewController.swift ). Hopefully, you'll share your findings aswell, thanks – punkbit May 05 '20 at 17:56
  • Ahhh I see. I didn't realize that link pertained to AudioKit. I will definitely look into that. I'm just getting the timestamp by getting `systemUptime` in `AKMetronome.callback` and comparing that to the expected beat length based on BPM... Making the smallest buffer size basically fixed the problem, but your link looks like it might have a better answer, so thanks in advance. – JiminyKirket May 05 '20 at 18:17
  • 1
    I'll just add a comment here for future readers, so the `systemUptime` is a property of `ProcessInfo.processInfo` (https://developer.apple.com/documentation/foundation/processinfo), measuring time accurately is difficult and there's a lot of discussions but it's used by the author of AudioKit here (https://github.com/AudioKit/SamplerDemo/blob/master/iOS/SamplerDemo/Conductor.swift), so it's a good practice to follow the original author, I think. – punkbit May 05 '20 at 18:26