More MIDI Time Math

Here I go again with time math. This time, I’m working on MIDI export and tempo.

Take a look at the tempo indication below:

I have a tempo model, modeled after modern metronome markings composed of three parts: a part to the left of the ‘=’ , the value to the right of the ‘=’, and the implied clock-time denominator – usually 1 minute.

In software the left hand side (LHS) is represented as a rational number: 1/4 for quarter note, 1/8 for eighth note, etc… The right hand side has a numerator (num) and, a implied denominator I’m calling clock-unit (CU) usually equal to 60 seconds.. This model allows modeling of tempos for meters like 6/8 when a dotted eighth gets a beat. In this case LHS=3/8, a dotted half.

With these definitions, I now present, without derivation, formulas for

  • converting this model to the MIDI tempo value, which is expressed as a number of microseconds per quarter note
  • converting number of beats to MIDI ticks
  • converting ticks to beats
  • converting the MIDI tempo value back to the num value in the model

First the formulas for exporting to MIDI files:

tempo [microseconds/quarter note] = 10^6 * CU / (4 * LHS * num)

ticks = beats * division[ticks/quarter note] * 4 * LHS

For importing MIDI files:

Beats = ticks / (division * 4 * LHS)

num = 10^6 * CU / (tempo [microseconds/quarter note] * 4 * LHS )