Frage

I'm trying to write a C++ program that reads through a MIDI in hex format and extrapolates necessary data, as per this tutorial. In the track chunk information, the first information is the fact that you are looking at a track "MTrk". The second 4 bytes refer to the chunk size.

From what I understand, the chunk size refers to the amount of bytes before the start of the next track chunk, or else the end of the file. However, in the midi files that I've been working with, my chunk size is often too small. That is to say, let's say that the chunk size is 40 bytes: the next "MTrk" doesn't occur until maybe 70 bytes after the current one (I did account for the fact that "MTrk" and the chunk size itself are not part of the chunk size). What, then, are these additional 30 bytes? Should I truncate the bytes that occur after chunk size? Or should I ignore chunk size and keep reading until I encounter the next track (or FF2F00, which indicates the end of a track)?

After having read in a MIDI file, I recorded the following as an output:

4D546864000000060001000300604D54726B0000000B00FFFFFFFF510306FFFFFF8A1B00FFFFFFFF2F004D54726B

Below I have separated the meaningful parts:

4D546864
00000006
0001
0003
0060 

(END OF HEADER)

4D54726B
0000000B

(CHUNK SIZE = 11 bytes)

00FFFFFFFF510306FFFFFF8A1B00FFFFFFFF2F00

(BUT HERE WE SEE 20 bytes)

4D54726B

Here is the MIDI file I used.

War es hilfreich?

Lösung

I downloaded the file from the link provided and it looks quite normal to me. Let's break down the chunks that it defines.

The first chunk has a size of 6 bytes: 00 01 00 03 60, which tell us that it's a MIDI type 1 file with 3 tracks, and time division of 96

The next chunk (MTrk) has a length of 11 bytes: 00 ff 51 03 06 8a 1b 00 ff 2f 00. In this case, it only contains a single MIDI meta event for the tempo, and then an end of track message.

After that comes the second track, which has a length of 02 92 bytes (658 in base 10). A few MIDI meta events defining the track name and instrument come, and then regular MIDI data. Finally comes the last track...

I'm not sure where exactly your count got off, are you sure that you're not counting the 4 byte header (ie, MTrk) against the total number of bytes in the chunk? The chunk length does not include the 8 bytes needed for the name or length.

For future reference, the hexdump utility is very useful for viewing such files, particularly with -C. For this file, it would display the data like this:

$> hexdump -C ArminvanBuurenFerryCorsten_-_Bruteversion4__iCarroller_20130206094335.mid 
00000000  4d 54 68 64 00 00 00 06  00 01 00 03 00 60 4d 54  |MThd.........`MT|
00000010  72 6b 00 00 00 0b 00 ff  51 03 06 8a 1b 00 ff 2f  |rk......Q....../|
00000020  00 4d 54 72 6b 00 00 02  92 00 ff 03 0c 53 61 77  |.MTrk........Saw|
... etc.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top