21

I want to know how to convert MIDI ticks to actual playback seconds.

For example, if the MIDI PPQ (Pulses per quarter note) is 1120, how would I convert it into real world playback seconds?

kikones34
  • 371
  • 9
  • 13
Taha
  • 301
  • 1
  • 3
  • 11
  • 1
    i m not getting you... Lets say i have a music having a constant tempo of 424 BPM...Now i have fluctuating midi ticks in PPQ..for example 70272,70344,70372 etc...now for every miditick i want to have an equivalent real world playback second.... if i take the for example 145240 PPQ(midi tick)..(sample midi tick taken from sample midi music file ) ...then ur formula is giving me this 0.9116185787866357 no of Milliseconds...which converts to 0.0009116185787866358 in Seconds...which puzzles me cuz such 145240 midi tick is produced after 1 second on playback of midi music.... Plz help – Taha Jan 11 '10 at 21:00

2 Answers2

31

The formula is 60000 / (BPM * PPQ) (milliseconds).

Where BPM is the tempo of the track (Beats Per Minute).

(i.e. a 120 BPM track would have a MIDI time of (60000 / (120 * 192)) or 2.604 ms for 1 tick.

If you don't know the BPM then you'll have to determine that first. MIDI times are entirely dependent on the track tempo.

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
  • It would be great if this answer explained how to compute BPM. – Oddthinking Dec 10 '11 at 10:32
  • 4
    @Oddthinking: Compute it from what? – Aaronaught Dec 10 '11 at 16:53
  • 7
    BPM is not a native concept to MIDI. It is derived from the Microseconds Per Quarter Note (from SET_TEMPO) and the Time Signature. See [this blog post](http://www.lastrayofhope.com/2009/12/23/midi-delta-time-ticks-to-seconds/) for more details of how it is computed. – Oddthinking Dec 11 '11 at 00:19
  • 1
    I came to this question with a similar problem. What made it worse is the 6 minute piece I was looking at had 80 tempo changes in it, so assuming the tempo doesn't change is another danger. – Oddthinking Dec 11 '11 at 00:21
  • @Oddthinking: I still don't really understanding what you're getting at. Of course BPM isn't native to MIDI; MIDI time is measured in PPQ (or "ticks per beat"). In order to convert between that and actual time, you need to *know* the tempo, which is measured in BPM. You can't *calculate* the tempo unless you already know the playback time, which is the opposite of what this question asked? – Aaronaught Dec 11 '11 at 05:40
  • 4
    I was reading a MIDI file, and trying to calculate the relative time of each event. Sounds like the original question. The instrument's track provides the tick number of the event. You also need to find in the MIDI file's header which provides *either* the ticks per quarter note OR the ticks per second. If the former, then to answer the original question you ALSO need to look at Track 0 which provides 0, 1 or more SET TEMPOs. They *provide* the Microseconds/Quarter-Note. If you want to compute BPM (not necessary) you can do it from there. You don't need any prior knowledge. – Oddthinking Dec 11 '11 at 11:05
20

You need two pieces of information:

  • PPQ (pulses per quarter note), which is defined in the header of a midi file, once.
  • Tempo (in microseconds per quarter note), which is defined by "Set Tempo" meta events and can change during the musical piece.

Ticks can be converted to playback seconds as follows:

ticks_per_quarter = <PPQ from the header>
µs_per_quarter = <Tempo in latest Set Tempo event>
µs_per_tick = µs_per_quarter / ticks_per_quarter
seconds_per_tick = µs_per_tick / 1.000.000
seconds = ticks * seconds_per_tick

Note that PPQ is also called "division" or "ticks per quarter note" in the document linked above.

Note that Tempo is commonly represented in BPM (a frequency) but raw MIDI represents it in µs per quarter (a period).

Victor Basso
  • 5,556
  • 5
  • 42
  • 60
  • The given link now 404s. – Stephane May 26 '20 at 13:25
  • Are you sure the `Tempo in latest Set Tempo event` is expressed in `µs_per_quarter` and not in `µs_per_beat` ? I can see a `Tone.js` MIDI interface that shows `(property) IMidiSetTempoEvent.setTempo: { microsecondsPerBeat: number; }` – Stephane May 26 '20 at 13:29
  • You are right with the tempo being expressed in `µs_per_quarter` https://www.recordingblogs.com/wiki/midi-set-tempo-meta-message and there is something I'm missing with the `Tone.js` interface then. – Stephane May 26 '20 at 13:35
  • I located the API and I created an issue https://github.com/chrisguttandin/midi-json-parser-worker/issues/329 – Stephane May 26 '20 at 13:47