public static MidiFileContainer Load(byte[] data)
{
var tracks = new List<MidiTrack> ();
var reader = new MidiDataStreamReader (data);
// Chunk type.
if (new string (reader.ReadChars (4)) != "MThd") {
throw new System.FormatException ("Can't find header chunk.");
}
// Chunk length.
if (reader.ReadBEInt32 () != 6) {
throw new System.FormatException ("Length of header chunk must be 6.");
}
// Format (unused).
reader.Advance (2);
// Number of tracks.
var trackCount = reader.ReadBEInt16 ();
// Delta-time divisions.
var division = reader.ReadBEInt16 ();
if ((division & 0x8000) != 0) {
throw new System.FormatException ("SMPTE time code is not supported.");
}
// Read the tracks.
for (var trackIndex = 0; trackIndex < trackCount; trackIndex++) {
tracks.Add (ReadTrack (reader));
}
return new MidiFileContainer (division, tracks);
}