Home > Net >  Issue In Hand Compiled Midi File
Issue In Hand Compiled Midi File

Time:10-19

I'm trying to learn how MIDI files work, and so I'm trying to hand compile a small MIDI track, writing the bytes myself. Here are all of the bytes I've done so far:

/*-----Header-Data----//-------Value-|-Description--------*/

0x4d, 0x54, 0x68, 0x64, // MThd | ASCII Header Chunk Type
0x00, 0x00, 0x00, 0x06, // 6 | 32 Bit Header Size
0x00, 0x00, // 0 | 16 Bit File Format | Single Track
0x00, 0x01, // 1 | Number Of Track Chunks
0b00000000_1100010_0, // 98_0 | 98 Ticks Per Quarter Note

/*-----Track-Data----//-------Value-|-Description--------*/

0x4d, 0x54, 0x72, 0x6b, // MTrk | ASCII Track Chunk Type
0x00, 0x00, 0x00, 0x04, // 4 | Track Length

/*----Event-Data---//-----Value-|-Description------*/

0x00, // 0 | Elapsed Time From The Previous Event
0b1001, // 9 | Note On Event
0b0000, // 0 | Channel 1 - Piano
0b0011_1100, // 60 | Note - Middle C
0b00100_0000, // 64 | Velocity

As of now, I only want to play a single note and get that working. However, the file above does not play properly (I'm on Windows).

I've been following these two websites to learn the specification:

I would appreciate it if someone could help me in understanding why the above bytes don't work. Thanks in advance.

CodePudding user response:

I see two problems:

  1. You missed End of Track event at the end of track chunk.

  2. What do you mean?

    However, the file above does not play properly

    In your file you have only Note On event. So you press a key and then the file immediately ends, so no sound produced. You need Note Off event also.

Here the simple file with one note with length of 100 MIDI ticks:

0x4d 0x54 0x68 0x64 // MThd
0x00 0x00 0x00 0x06
0x00 0x00
0x00 0x01
0x00 0x60

0x4d 0x54 0x72 0x6b // MTrk
0x00 0x00 0x00 0xc

0x00 0x90 0x3c 0x40 // Note On
0x64 0x80 0x3c 0x00 // Note Off
0x00 0xff 0x2f 0x00 // End of Track
  • Related