Corey Ford
Corey Ford

Reputation: 178

Writing Midi File From Scratch Using Hexadecimal

I am trying to write a midi file from scratch. It will play a C major arpeggio of 3 crotchets. I've been using sublime text to save using hex encoding. The result I then import into logic (after changing the extension to .mid). So far this has been unsuccessful, and logic simply does nothing.

I gained most of my information from here and have written the following:

4d 54 68 64 00 00 00 06 00 00 00 01 00 18 4d 54 72 6b 00 00 00 1e 80 18 81 3c 7f 80 00 91 3c 00 80 18 81 40 7f 80 00 91 40 00 80 18 81 43 7f 80 00 91 43 00

Here is a breakdown of each byte, it's in note form but summarises my understanding of the file format: Byte breakdown for MIDI file

Could anybody point out why this isn't loading the correct result into Logic. Have I misunderstood the file format itself or is the problem deeper in the encoding.
UPDATE: here is the final, corrected hexadecimal...

4d 54 68 64 00 00 00 06 00 00 00 01 00 18 4d 54 72 6b 00 00 00 22 80 18 91 3c 7f 80 00 81 3c 00 80 18 91 40 7f 80 00 81 40 00 80 18 91 43 7f 80 00 81 43 00 00 ff 2f 00

Upvotes: 2

Views: 1436

Answers (1)

CL.
CL.

Reputation: 180080

The track lacks the end-of-track meta event (type 2F, length 0). So add the bytes 00 FF 2F 00, and adjust the track size accordingly.

The track contains six MIDI messages. All six are note-off messages, so there are not any actual notes.

8x is a note-off message.
9x is a note-on message, but a velocity of 0 actually specifies a note-off. (This is an optimization that is helpful when using running status.)

Upvotes: 1

Related Questions