Search this Site



Vivisecting A MidiFile

You're here : Midi >Learning

OK, you asked for it... I'll try to be brief for bandwidth's sake, though the subject is worth several pages (Editor's note : What are U waiting for ? ,o))

A midifile consists in a header followed by one track (format 0) or several ones (format 1). Each part of the file is called a Chunk. The Header Chunk looks like this (all the numbers are hexadecimal, this is specified by the suffix "h") :

  • 4Dh 54h 68h 64h : MThd in Ascii
  • 00h 00h 00h 06h : number of bytes that will follow the header
  • 00h 00h : format of the midifile (0, 1 or 2)
  • 00h 01h : number of tracks (always 1 for the format 0)
  • 01h 80h : beat resolution

Then comes the Chunk of first or only track :

  • 4Dh 54h 72h 6Bh : MTrk in Ascii
  • nn nn nn nn : : number of left bytes till the end of the track (shown as a variable length number).

Each event of the track is always preceded by a "delta time" even if this one must be equal to 0. Some events are called 'meta-event' because they aren't Midi messages, but a text, a change of signature or tempo, a mark, etc. They can be recognized by the first byte FFh, followed by a meta-event code, its length (in variable length number), and its data. Here are some of these codes :

  • 01h : text
  • 02h : copyright information (it's getting interesting !) formatted as "©", Year, Author...
  • 03h : : track name
  • 04h : instrument name
  • 05h : lyrics...
  • 2Fh : end of track, this one is necessary.


Back to Cubase.

We can insert some text in the List Edit (for example "Test"). When exported in the midifile, it's converted into a text meta-event and we will find in the file this serie of bytes :

  • FFh : meta-event
  • 01h : text
  • 05h : reste 5 octets
  • 54h : "T"
  • 65h : "e"
  • 73h : "s"
  • 74h : "t"

Method for inserting a copyright into a midifile :

  • Write it in the List Edit of Cubase, at the very beginning of the song and on the first track,
  • Export to midifile,
  • Open the file with a hexadecimal editor,
  • Look for the serie of bytes correspondint to the text meta-event,
  • Change FFh 01h (text) to FFh 02h (copyright,
  • Save, and voila ...

On purpose I cut corners on the famous variable length numbers, in order not to triple the weight of this e-mail, but remember that it only relates to values higher than 127.

Hexadecimal regards...

Top of page
Jihem, on the 12-06-2000

Page viewed 8524 times