Actual note duration from MIDI duration

后端 未结 1 1338
悲哀的现实
悲哀的现实 2021-02-10 09:10

I\'m currently implementing an application to perform some tasks on MIDI files, and my current problem is to output the notes I\'ve read to a LilyPond file.

I\'ve merge

1条回答
  •  余生分开走
    2021-02-10 10:03

    By default a MIDI file is set to a tempo of 120 bpm and the MThd chunk in the file will tell you the resolution in terms of "pulses per quarter note" (ppqn).

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

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

    With these two values you should find what you need. Beware that the duration in the midi file can be approximate, especially if that MIDI file it's the recording of a human player.

    I've put together a C library to read/write midifiles a long time ago: https://github.com/rdentato/middl in case it may be helpful (it's quite some time I don't look at the code, feel free to ask if there's anything unclear).

    I would suggest to follow this approach:

    1. choose a "minimal note" that is compatible with your division (e.g. 1/128) and use it as a sort of grid.
    2. Align each note to the closest grid line (i.e. to the closest integer multiple of the minimal node)
    3. Convert it to standard notation (e.g a quarter note, a dotted eight note, etc...).

    In your case, take 1/32 as minimal note and 384 as division (that would be 48 ticks). For your note of 376 tick you'll have 376/48=7.8 which you round to 8 (the closest integer) and 8/32 = 1/4.

    If you find a note whose duration is 193 ticks you can see it's a 1/8 note as 193/48 is 4.02 (which you can round to 4) and 4/32 = 1/8.

    Continuing this reasoning you can see that a note of duration 671 ticks should be a double dotted quarter note.

    In fact, 671 should be approximated to 672 (the closest multiple of 48) which is 14*48. So your note is a 14/32 -> 7/16 -> (1/16 + 2/16 + 4/16) -> 1/16 + 1/8 + 1/4.

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

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

    Triplets and other groups would make things a little bit more complex. It's not by chance that the most common division values are 96 (3*2^5), 192 (3*2^6) and 384 (3*2^7); this way triplets can be represented with an integer number of ticks.

    You might have to guess or simplify in some situations, that's why no "midi to standard notation" program can be 100% accurate.

    0 讨论(0)
提交回复
热议问题