I am writing a midi generator using python and the mido library. I am just struggling to understand the point of the key_signature and time_signature meta messages. I have kept these in my data but if I remove them from the songs messages and play it, they sound identical. I previously thought that key_signature messages were important in defining which notes where sharp etc however on consulting https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies I see that the note_on messages already specify whether it is sharp or flat. Equally I thought the time_signature messages were important in setting overall timing ( I know little about music theory) but then discovered mid.ticks_per_beat seems to do this instead.

All I am trying to ascertain is whether these messages are important to keep if generating new music that looks at patterns between many songs. Equally if someone could elaborate on the importance of mid.ticks_per_beat and how I should combine these values across multiple songs when generating new music, this would be thoroughly appreciated.

Thank you

1 Answer 1


So far as the MIDI data is concerned, none of the meta messages are necessary, but they are useful when converting the MIDI to music staff notation.

The key signature is important for defining the correct "spelling" of the notes - for example whether MIDI note 61 should be written as a C sharp or a D flat. It makes no difference to the sound, but the "wrong" spelling in staff notation looks musically illiterate, and is hard to read because it obfuscates identifying the chords and scales used in the music.

The time signature is important for defining the overall rhythm (e.g. whether the piece is a march or a waltz) which is independent of the tempo, and indicates the position of the bar lines in the staff notation.

The ticks per quarter note defines how far you can subdivide a beat evenly. In popular music you are unlikely to find subdivisions much beyond 3 or 4, and a relatively low number of ticks per beat may (slightly) reduce the size of the MIDI file by reducing the number of bytes needed to store the "delta times" between the notes.

Originally, 60 or 120 TPQN were fairly common choices, which split evenly into 2, 3, 4, 5, and 6 subdivisions (and 120 also into 8). 72 TPQN splits into 2, 3, 4, 6, 8, and 9. A common standard for modern software is 480 TPQN, which is a reasonable compromise between allowing finer subdivisions without increasing the file size too much. Some software uses higher values like 5040 TPQN, and/or allows the user to choose what value to use. Software that reads MIDI files should be able to handle any (reasonable) number of TPQN. Unless you have a reason to do something different, I would use 480.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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