MID Signature Format Specification & Recovery Example

MIDI Audio Signature Format: Specification & MID Recovery Example

MIDI (Musical Instrument Digital Interface) is a technical standard that describes a protocol, digital interface & connectors, and allows electronic musical instruments, computers and other related devices to connect and communicate with one another.

MIDI Audio files must have a signature (tag) MThd (hex: 4D 54 68 64) at the beginning of the audiofile.

MIDI files are organized into data chunks. Each chunk is prefixed with an 8 byte header: 4 byte signature (MThd or MTrk) used to identify the type of chunk followed by a 4 byte size which defines the chunk's length as number of bytes following this header. All data values are stored in Big-Endian (most significant byte first) format. By searching for data chunks and summarizing sizes for all found data chunks we can calculate total MIDI file size.

Let's examine the example

When inspecting example.mid file's text data using any Hex Viewer, like Active@ Disk Editor, which is included in Active@ File Recovery package, we can see it starts with a tag MThd (hex: 4D, 54, 68, 64). Next to it, at offset 4, there is a header chunk data size, which is 6 (hex: 00, 00, 00, 06), big-endian (highest byte first). Next data chunk starts at offset 14 (4+4+6). It is a track, signature MTrk (hex: 4D, 54, 72, 6B), following the data length: 98 (hex: 00 00 00 62). Next data chunk starts at offset 120 (14+4+4+98). It is a track, signature MTrk (hex: 4D, 54, 72, 6B), following by a data length: 4,643 (hex: 00 00 12 23). Searching chunks further the same way we'll pick up all audio file data.

MIDI audiofile - signature inspection

More info:

MIDI files Header Chunk:

offset size description
0 4 signature, must be 4D 54 68 64 hex ("MThd")
4 4 chunk size (length of the data), big-endian
8 2 format type (0-2)
10 2 number of tracks (1-65,535)
12 2 time division, either ticks per beat or frames per second

MIDI files Track Chunk:

offset size description
0 4 signature, must be 4D 54 72 6B ("MTrk")
4 4 chunk size (length of the data), big-endian
8 data size track event data, contains a stream of MIDI events that define information about the sequence and how it is played

MIDI Audio Specifications

Active@ File Recovery Custom Scripting Example

This example just specifies MIDI start signature and calculates file size based on size of all found data chunks.
Syntax of the signature definition language you can read here.

[MID_HEADER]
DESCRIPTION=MIDI Audio
EXTENSION=mid
BEGIN=MID_BEGIN
SCRIPT=MID_SCRIPT

[MID_BEGIN]
MThd=0|0

[MID_SCRIPT]
next:
	temp = read(dword, size)
	if (temp == "MThd") goto valid
	if (temp != "MTrk") goto exit

valid:
	size = sum(size, 4)
	temp = read(dword, size)
	size = sum(size, 4)
	temp = endian(dword, temp)
	size = sum(size, temp)
	goto next