Actual Recording Duration versus MIDI Duration

I am currently implementing an application to perform some tasks on MIDI files, and my current problem is to output notes that I read into a LilyPond file.

I combined the note_on and note_off events with single note objects with an absolute start and an absolute duration, but I don’t see how to convert this duration to an actual musical notation. I guessed that the duration of 376 is a quarter of the notes in the file that I read because I know the song, and obviously 188 is the eighth note, but this, of course, does not generalize to all MIDI files.

Any ideas?

+4
source share
1 answer

By default, the MIDI file is set to 120 bpm, and the MThd fragment in the file tells you the resolution in terms of β€œpulses per quarter note” (ppqn).

If ppqn, say 96, than delta 96 ticks, is a quarter note.

If you are interested in the actual duration (in seconds) of each sound, you should also consider the "tempo" that can be changed by the event "FF 51 03 tt tt tt"; three bytes - microseconds for a quarter note.

With these two values ​​you should find what you need. Remember that the duration in a midi file can be approximate, especially if this MIDI file is a recording of a human player.

I put together a C library for reading / writing midi files a long time ago: https://github.com/rdentato/middl in case it can be useful (for a long time I have not looked at the code, feel free to ask if there is anything- something incomprehensible).

I suggest following this approach:

  • select the "minimum note" compatible with your division (for example, 1/128) and use it as a kind of grid.
  • Align each note with the nearest grid (i.e., to the nearest integer edge of the minimum node)
  • Convert it to standard notation (e.g. quarter note, octal note, etc.).

In your case, take 1/32 as the minimum note and 384 as the division (this will be 48 ticks). For your 376 tick mark, you will have 376/48 = 7.8, which you round to 8 (nearest integer) and 8/32 = 1/4.

If you find a note with a duration of 193 ticks, you can see it as 1/8 notes, since 193/48 is 4.02 (which you can round to 4) and 4/32 = 1/8.

Continuing this argument, you can see that the 671 tick mark should be a two-digit record with a quadratic point.

In fact, 671 should be close to 672 (the nearest multiple of 48), which is 14 * 48. So your note is 14/32 β†’ 7/16 β†’ (1/16 + 2/16 + 4/16) β†’ 1/16 + 1/8 + 1/4.

If you are comfortable using binary numbers, you may notice that 14 is 1110 and from there, directly due to the presence of 1/16, 1/4 and 1/8.

As another example, a note of 480 ticks of duration is a quarter note tied to a 1/16 record, since 480 = 48 * 10 and 10 1010 in binary format.

Threes and other groups will make things a little more complicated. It is no coincidence that the most common division values ​​are 96 (3 * 2 ^ 5), 192 (3 * 2 ^ 6) and 384 (3 * 2 ^ 7); thus, triplets can be represented by an integer number of ticks.

You may have to guess or simplify in some situations, so no midi to standard notation program can be 100% accurate.

+7
source

Source: https://habr.com/ru/post/1304435/


All Articles