I need help understanding this MIDI output, so that I may use this output in a machine learning task to generate music. The thing is that my background in statistics and computer science isn't enough for me to understand what I am reading below. Could anyone help out? Below is a snippet of the output. The original goes on for about 400 lines or so.

Track 0: 
<meta message key_signature key='F' time=0>
<meta message time_signature numerator=8 denominator=4 clocks_per_click=24 notated_32nd_notes_per_beat=8 time=0>
<meta message set_tempo tempo=599704 time=0>
<meta message set_tempo tempo=666337 time=41088>
<meta message set_tempo tempo=799605 time=96>
<meta message set_tempo tempo=999506 time=192>
<meta message set_tempo tempo=468519 time=96>
<meta message end_of_track time=768>
Track 1: 
program_change channel=0 program=0 time=0
note_on channel=0 note=62 velocity=64 time=0
note_on channel=0 note=62 velocity=0 time=48
note_on channel=0 note=70 velocity=64 time=0
note_on channel=0 note=70 velocity=0 time=48
note_on channel=0 note=69 velocity=64 time=0
note_on channel=0 note=69 velocity=0 time=192
note_on channel=0 note=67 velocity=64 time=0
note_on channel=0 note=67 velocity=0 time=96
<meta message end_of_track time=0>

If it helps in any way, below is the code I used [in Python] to access and convert the MIDI file into what you see above.

from mido import MidiFile

mid = mido.MidiFile('can1.mid')

for i, track in enumerate(mid.tracks):
    print('Track {}: {}'.format(i, track.name))
    for msg in track:
  • 2
    This is just a representation of the contents of a Standard MIDI File. I suggest you read the specification.
    – CL.
    Jan 10 '18 at 7:48

The most important messages in a MIDI file for most purposes (and probably for your purposes) are the ones that play the notes of the musical piece.

Usually, these are expressed as note-on messages ('start playing a note at this time') and note-off messages ('stop playing a note at this time'). Note-on messages have a velocity, analogous to how hard the player struck the key, or plucked the string, etc.

Additionally, MIDI has 16 'channels' that can be used to play up to 16 different instruments.

So, let's take the first note-on message in your sample file:

note_on channel=0 note=62 velocity=64 time=0

'Channel=0' here means the first MIDI channel - what we'd normally call channel '1'. As the mido documentation says, "Mido numbers channels 0 to 15 instead of 1 to 16".

note=62 means that we're playing MIDI note number '62'. What does this mean? Consulting a chart of MIDI note numbers will show us that it's D in octave 4.

'velocity=64' means it's being hit with medium strength - velocities range from 0 to 127.

'time=0' means that it's being played right at the start of the piece.

So, that's the information about how the note starts - what about where it stops? There don't seem to be any note off messages - but it turns out that a note-on message with a velocity of 0 should be treated as a note off. So that note stops at 'time=48'. http://mido.readthedocs.io/en/latest/midi_files.html#about-the-time-attribute tells us that is a time in 'ticks'.

http://mido.readthedocs.io/en/latest/midi_files.html#tempo-and-beat-resolution tells us how to map that time to a time in seconds - you need to use ticks_per_beat in the MidiFile object, and then work out how fast each beat is using the tempo. The set_tempo messages in this file are a bit strange - you have two different tempos set at time=96. When learning about machine learning, you'll have read about the importance of cleaning your data, and that will be as true for MIDI files as anything else!

I hope that's a useful start. MIDI is a very well-documented file format and you should be able to find lots of information about most standard MIDI message types around the web.

  • 1
    The time stamps are relative.
    – CL.
    Jan 10 '18 at 14:31
  • 1
    @CL. aha... that would make more sense for the tempo messages.
    – topo morto
    Jan 10 '18 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.