Changing the key signature of MIDI files

Key transpositions of MIDI files are relatively easy. The note value is the second byte of a NoteOn or NoteOff message:

&h90+ChanNo  Note  Velocity
&h80+ChanNo  Note  Velocity

The quantity Note takes integer values from &h00 (0) to &h7F (127) following the chromatic scale. Middle C corresponds to Note = &h3C (60). Therefore, a transposition of a MIDI file consists simply to rewriting the file with offset Note values:

Note = Note + N,

where N is an integer. The purpose of this article is to show how to choose N to achieve a desired key signature.

A well formattted MIDI file includes a message that defines the key signature in the form

&hFF &h59 &h02 SF MI

The first byte indicates a non-MIDI message, the second byte designates that the message contains a key signature and the third byte states that two data bytes follow. The value of MI determines whether the key is major (MI = 0) or minor (MI = 1). The value of SF determines the key signature according to Table 1.

Table 1

Table 1.

Table 2 shows the relationship between the note displacement N and the key. A transposition represents a movement up or down the table by a number of rows equal to N. For example, to transpose a piece from the key of C to F, we need to move either up seven rows (N = -7) or down five rows (N = +5). The final key has one flat. Here are two examples of how to use the tables:

  • Transpose a piece with a key signature with four sharps (E major) to C major. Inspection of Table 2 shows that we need to change the Note values by either N = -4 or N = +8.
  • The goal is to transpose a MIDI file in E major (four sharps) to use as an accompaniment for a recorder, an instrument on which it is difficult to play black-key notes. At the same time, we want to keep N as small as possible to avoid a radical change in the accompaniment sound. The key of G is close and has only one sharp, so we pick N = +3.
Table 2

Table 2.


[1] Find out more about KBD-Infinity: Home page.

[2] If you have comments or questions, please contact us at

Comments are closed.