Accurate tempo/BPM-based timing in your animations and music apps

Posted by Gary Duke, Posted on February 2, 2012

2 votes

Accurate tempo/BPM-based timing in your animations and general beat-sequenced stuff.

I am using this logic in a spiffy metronome that I am porting over to iOS using Corona. I've used this rock-solid tempo pulse logic in my music apps for various platforms since 1998 and it has never done me wrong.

You can use this code in your apps that need to synchronize sounds or animations to background music or wave file loops-- whatever. Just be sure that if you are looping music, that you use a wave file, not an mp3, and that you triple check the number of samples in the wave file so you know it will loop exactly to the tempo you need.

If you use this logic, your app will have better timing than many XCode/Objective C projects. Why? Because it's often well-thought out business logic in a program that matters most, not the language or environment or API.

So check out my solution to a fellow developer's tempo problems in this thread here:

http://developer.anscamobile.com/forum/2011/08/15/drum-toy-0#comment-84062

If you make use of my code/this post in your Corona (or other platform) app, please give me a shout!

Cheers!

Gary


Replies

noeguer
User offline. Last seen 26 min 36 sec ago. Offline
Joined: 23 Dec 2011

I have check this tempo algorith with the tempo of a real hardware device, and they lose bit a bit the syncronization.

Gary Duke's picture
Gary Duke
User offline. Last seen 1 week 3 days ago. Offline
Joined: 24 Nov 2011

Hello noeguer,

Thanks for commenting!

What you need to understand is that *any* two devices will eventually go out of sync. That is why midi has clock sync function, and why audio recorded separately from video almost always goes out of sync unless the timing is synched via some sort of protocol.

All hardware uses timing crystals to keep time and all have margins of error. It's often just a matter of how good the timing crystal is and how well its calibrated.

That "real hardware device" may have a cheap timing crystal or some programming errors. I once had a Dr Rhythm drum machine with hideous timing :) You may want to check both tempos against logic on a Mac which should be pretty accurate.

My algorithm makes the tempo as accurate as the processor clock, which is all one can do. you can't get anymore accurate on iOS without slaving to a MIDI clock sync from a master device.

Trust me, this algorithm is as accurate as any iOS metronome, and is more accurate than the algorithms many use.

Cheers,

Gary

noeguer
User offline. Last seen 26 min 36 sec ago. Offline
Joined: 23 Dec 2011

Well, the real devices is access virus ti...i dont think it has any error in its clock, any way thanks for the code. It is really cool. Thanks a lot

Gary Duke's picture
Gary Duke
User offline. Last seen 1 week 3 days ago. Offline
Joined: 24 Nov 2011

You virus TI and the clock of every other device will all go out of sync by some fractional degree over time. It's just how electronics work.

I also looked into building a hardware version... and believe me the cost of the crystal goes up considerably with higher accuracy and yet still there is a margin of error, and calibration is required which will improve the error, but two hardware devices using different quartz crystals and different programming and calibration will go out of sync. That is why you often see video out of sync with audio even in broadcast situations.

What I can say is, you are quite welcome for the algorithm. I hope you do great things with it. There is no better way to get better timing on iOS, or any platform for that matter, without some kind of MIDI clock sync signal.

noeguer
User offline. Last seen 26 min 36 sec ago. Offline
Joined: 23 Dec 2011

You know more than me, i trust you :) It is great and easy to add to the code. Congrat!