calculate tempo from two consecutive notes

18 views
Skip to first unread message

Mike

unread,
Dec 20, 2020, 3:44:41 PM12/20/20
to KeyKit
Hi,

Maths is not my strong point so this might be way more complex than I think.

If I have an incoming stream of 16th notes locked to an unknown tempo, shouldn't i be able to calculate the tempo after the 2nd hit?

Cool edit pro has a function called extract BPM from selection where you hilight a portion of a wave, tell it how many bars you have hilighted (e.g 0.25) and it works out the BPM.

I am trying to do the same with keykit at the earliest opportunity. In this case the second 16th note.

Really struggling as all the functions within the library use the tempo in the algorithm.

I have looked at millisince() and seconds(). I know that latestNote.time minus previousNote.time give me the difference in clicks, but clicks are a factor of the tempo so I am spinning around in my head and getting no closer.

thx for any help.

Mike

Tim Thompson

unread,
Dec 20, 2020, 6:21:48 PM12/20/20
to Mike, KeyKit
It's tricky to think about.  The various things involved:

- 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...

--
You received this message because you are subscribed to the Google Groups "KeyKit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keykit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/keykit/f581da2f-f875-4bc9-8af5-b36b16fff622n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "KeyKit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keykit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/keykit/f581da2f-f875-4bc9-8af5-b36b16fff622n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages