MP3 to MIDI Key Signature: Two Bytes, Thirty Keys, Twelve Note Sets

The key signature meta event is two data bytes: a signed count of sharps or flats, and a flag for major or minor. Fifteen counts and two flags name thirty keys, and three of those fifteen signatures hold the same seven notes as another one.

The two bytes, answered

  • The field is FF 59 02 sf mi. sf is signed: negative counts flats, positive counts sharps. mi is 0 for a major key and 1 for a minor one.
  • Fifteen counts, thirty keys. sf runs from minus seven to seven, and the same integer names one key on each flag.
  • Three of the fifteen are duplicates. Seven sharps and five flats describe one set of seven pitch classes, six sharps and six flats a second, five sharps and seven flats a third.
  • No key name is stored. The file holds a count and a flag, and the reader turns that pair into a name.

Two bytes, and neither one is a key name

The event is written FF 59 02 sf mi. The specification enumerates its ends, not its middle: minus seven is seven flats, zero is the key of C, seven is seven sharps. The second byte is a flag, 0 for a major key and 1 for a minor one. Neither byte is a key name; the reader does the arithmetic.

Fifteen counts, two flags, thirty keys

Fifteen counts multiplied by two flags is thirty keys, and the count alone is not the key: the same integer names one key on each flag. The flag has exactly two values, so the file distinguishes exactly two modes and has no value for a mode outside that pair.

Three of the fifteen signatures hold the same seven notes

A signature describes a set of seven pitch classes. Sharps are added in one fixed order and flats in its reverse, so the two ends of the range overlap: seven sharps and five flats describe one set, six sharps and six flats a second, five sharps and seven flats a third. Fifteen counts therefore cover only twelve distinct sets.

The note list and the signature answer different questions. The note list reports which pitch classes are played; the signature reports how the file spells them. Two files with identical note lists can carry different signatures: the count is a spelling decision, not a measurement of the audio.

The other meta events that describe rather than play

The specification defines fifteen meta events. Seven are free text, among them a copyright notice, a track name, an instrument name, a lyric, a marker and a cue point. Of the eight numeric ones, the key signature is the only one describing harmony.

SMPTE Offset is written FF 54 05 hr mn se fr ff: the hour byte uses the SMPTE format, and the last field holds fractional frames in hundredths of a frame even when the track's delta-times use a different subdivision. In a format 1 file it must sit with the tempo map and means nothing in any other track. MIDI Channel Prefix is FF 20 01 cc, a channel from 0 to 15 effective until the next event carrying a channel. Sequencer Specific is FF 7F, beginning with a manufacturer ID: one byte, or three when the first byte is zero.

Exactly one meta event is called not optional, and it is End of Track: a track to be looped or concatenated needs an exact ending point.

What to check after a conversion

Read the event list, not the piano roll. A missing key signature is not a fault; the note list is still complete. A present one should be read as a pair: a count of zero is C major on one flag and a different key on the other.

Related