How long is a MIDI tick in a .mid file please?

Thread Starter

hexreader

Joined Apr 16, 2011
581
I am busy programming PIC controllers to decode simple .mid files to send as raw MIDI data to synth, but despite much googling I am stuck on understanding what a "tick" is.

Google hits reveal plenty about how many ticks to a beat and all about delta-time, but I nothing about how much time a tick takes.

I downloaded a simple silent_night_easy.mid file and understand pretty much every byte of the file, but without knowing how long a tick takes, the timing eludes me.

Will attach .mid file and decode if requested, but without understanding of a MIDI "tick" I suspect such downloads would not add anything.

To be clear - I want to know how to work out the length of a "tick" in seconds/milliseconds/picoseconds. NOT as a fraction of something else undefined.

Can anybody help please?

Many thanks
 

Ya’akov

Joined Jan 27, 2019
9,071
Ticks are relative to the tempo of the song. They must be derived from the SET_TEMPO parameter in the header which is apparently in microseconds per ¼ note. So you have to know the tempo, then you can calculate the length of a tick.

Here's the method:

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: I am not an expert in MIDI. I vaguely remembered this and had to look it up.
 

bertus

Joined Apr 5, 2008
22,270
Hello,

The tempo of a midi file is in the header of the file.
from the wiki:
SMFs are created as an export format of software sequencers or hardware workstations. They organize MIDI messages into one or more parallel tracks and time-stamp the events so that they can be played back in sequence. A header contains the arrangement's track count, tempo and an indicator of which of three SMF formats the file uses. A type 0 file contains the entire performance, merged onto a single track, while type 1 files may contain any number of tracks that are performed synchronously. Type 2 files are rarely used[53] and store multiple arrangements, with each arrangement having its own track and intended to be played in sequence.

https://en.wikipedia.org/wiki/MIDI

Also here can info be found:
https://www.midi.org/specifications/file-format-specifications

Bertus
 

Thread Starter

hexreader

Joined Apr 16, 2011
581
yaakov, bertus

I really appreciate the responses, but sadly it gets me no further forward.

You eloquently and clearly explain how to work out ticks_per_quarter from PPQ and the like

I can see how everything else can be calculated once the value of tick is known, but I still have no idea how long is a tick.

Is there a smaller unit than a tick?

Windows seems to play a good rendition of "silent night" from a simple .mid file. How does windows media player determine how long a tick is?

Apologies if I am just being thick.

Thanks again,

Robin
 

Thread Starter

hexreader

Joined Apr 16, 2011
581
Hmmm ... tempo ???

Cogs still turning in brain....

Here is my manual decode of hex file derived from original silent_night_easy.mid file:

Silent Night MIDI file
Status 27 July 2022 - work in progress - many odd unknowns

:20 0000 00 - 32 bytes address 0x0000 - type 0

4D546864 "MThd" header
00000006 6 bytes follow
0001 file format 1 - multiple tracks synchronous
0002 number of tracks = 2
0100 delta-time ticks per quarter note = 256

4D54726B "MTrk" header - track change

00000019 25 bytes of MIDI data follow - exactly as transmitted

00 ?
FF 58 04 tune signature
03 numerator
02 denominator
18 number of ticks in metronome click
08 number of 32nd notes to the quarter note

00 FF

checksum 53
:20 0020 00

59 02 Key signature
00 00 C major

00 delta time 0
FF 51 03 Set tempo
09 27 C0 tempo

01 delta time 1
FF 2F 00 This event must come at the end of each track
(end of 25 bytes MIDI data)

4D54726B "MTrk"

000002B4 619 bytes of MIDI data follow

00 delta time 0
C0 00 patch change 00
00 delta time 0
B0 79 00 control change (controller, new value)
00 delta time 0
B0

checksum 26
:20 0040 00

40 00 control change (controller, new value)
00 delta time 0
B0 5B 30
00 delta time 0
B0 0A 33
00 delta time 0
B0 07 64
00 delta time 0
90 43 35 note on 67 = G5
00 delta time 0
B0 79 00
00 delta time 0
B0 40 00
00 delta time 0
B0 5B 30
00 delta time 0
B0

checksum 11
:20 0060 00

0A 33
00 delta time 0
B0 07 64
00 delta time 0
FF 03 05 5069616E6F Track name "Piano"
00 delta time 0
90 3C 35 note on 60 = C5
83 00 delta time 384
90 45 38 note on 69 = A5
07 delta time 7
80 43 00 note off
79
90 43 3D note on 67 = G5

checksum 46
:20 0080 00

02 80 45 00 81 7E
90 40 36 note on 64 = E5
00
80 3C 00
00
90 3C 39 note on 60 = C5
05
80 43 00
857B
90 43 41
00
80 3C 00
00
90
 

Thread Starter

hexreader

Joined Apr 16, 2011
581
Hello,

Does this page tell you more about the midi tick?
https://www.recordingblogs.com/wiki/midi-tick

Bertus
Thanks Bertus

Yes, I suspect that it probably will solve my problem

Brain hurts right now, but I will read your link many times and hopefully understand eventually.

This is a much tougher problem than I first imagined.

Amazing how smart other forum members are on an amazing variety of subjects.

I am lucky to have you guys/gals to consult:)
 

Ya’akov

Joined Jan 27, 2019
9,071
yaakov, bertus

I really appreciate the responses, but sadly it gets me no further forward.

You eloquently and clearly explain how to work out ticks_per_quarter from PPQ and the like

I can see how everything else can be calculated once the value of tick is known, but I still have no idea how long is a tick.

Is there a smaller unit than a tick?

Windows seems to play a good rendition of "silent night" from a simple .mid file. How does windows media player determine how long a tick is?

Apologies if I am just being thick.

Thanks again,

Robin
In my post above, note this line: µs_per_tick = µs_per_quarter / ticks_per_quarter

So it's a simple calculation if you know the microseconds per quarter which comes from the latest SET_TEMPO event, and ticks per quarter from the PPQ, just do the math.
 

Thread Starter

hexreader

Joined Apr 16, 2011
581
In my post above, note this line: µs_per_tick = µs_per_quarter / ticks_per_quarter

So it's a simple calculation if you know the microseconds per quarter which comes from the latest SET_TEMPO event, and ticks per quarter from the PPQ, just do the math.
I think I am starting to understand a little, though not there yet.

Appreciate the help

Up to me to read over and over, inwardly digest, and hopefully become enlightened.

Many thanks
 

Thread Starter

hexreader

Joined Apr 16, 2011
581
As a last resort: find the last tick used in the file, play the file and time it. Divide the play time by last tick.
I have a terrible feeling that I may end up doing just that.

Maths and theory is great, but trial and error is quicker and requires less brain power.

Thanks everyone

Happy now :)
 

BobTPH

Joined Jun 5, 2013
8,813
From m your decoded file:

uSec per quarter note = 596928

ticks per quarter note = 256

usec per tick = 596928 / 256 = 2341

The tempo as usually expressed (in beats per minute) is:

60 x 0.596928 = 100 beats per minute, which is is entirely reasonable for s slow song.
 

Ya’akov

Joined Jan 27, 2019
9,071
Not sure what JSON is, but that decode is far better than all of the other decode apps that I tried.

Really appreciate the help

Many thanks
Robin
JSON is Javascript Object Notation. It is a data serialization format that is used all over the place and no longer really implies that Javascript is in use. In Javascript, though, it is effectively an object as it stands and it can be consumed and used right away.
 
Top