- The tempo() function returns or sets the current tempo in terms of microseconds per "beat".
- The notion of a "beat" in KeyKit is defined by the value of the global variable Clicks (default is 96, meaning a beat is 96 clicks).
- The .time values of notes in KeyKit are in terms of clicks. This includes notes received from realtime MIDI input.
Let's say you have two notes obtained from some realtime MIDI input, p1 and p2. You want to find out the tempo that they imply, based on their actual clock time. The tricky aspect is that this implied tempo is unrelated to the current tempo in KeyKit, BUT you need to take into account that the .time values of those 2 notes are in terms of Clicks, which ARE relative to the current tempo in KeyKit. So, the important first step is to convert those .time values into microseconds, using the current tempo() and Clicks:
microsecs1 = p1.time * tempo() / float(Clicks)
microsecs2 = p2.time * tempo() / float(Clicks)
If you want the clock time between those 2 notes to be considered a "beat", the BPM would be:
bpm = 60 * 1000000.0 / (microsecs2 - microsecs1)
If you wanted to then change KeyKit's notion of tempo to reflect this bpm, you would do:
tempo ( 60 * 1000000.0 / bpm )
I hope I got that right.
...Tim...