MIDI file reading
The simplest code for MIDI file reading is:
var file = MidiFile.Read("Some great song.mid");
After that you have an instance of the MidiFile. Its Chunks property returns a collection of chunks within the MIDI file read. Using this collection, you can manage chunks (add new, delete existing one and so on).
MIDI file reading process can be finely adjusted via ReadingSettings. For example, if we want to abort reading on unknown chunk (about reading custom chunks see Custom chunks article), you can set corresponding policy:
var file = MidiFile.Read("Some great song.mid", new ReadingSettings
{
UnknownChunkIdPolicy = UnknownChunkIdPolicy.Abort
});
ReadingSettings
has a lot of useful properties. You can read documentation on all of them to learn how you can adjust MIDI file reading.
Reading corrupted files
DryWetMIDI allows to read MIDI files with various violations of SMF standard. Example below shows how to read a MIDI file with different errors:
var file = MidiFile.Read("Some great song.mid", new ReadingSettings
{
InvalidChannelEventParameterValuePolicy = InvalidChannelEventParameterValuePolicy.ReadValid,
InvalidChunkSizePolicy = InvalidChunkSizePolicy.Ignore,
InvalidMetaEventParameterValuePolicy = InvalidMetaEventParameterValuePolicy.SnapToLimits,
MissedEndOfTrackPolicy = MissedEndOfTrackPolicy.Ignore,
NoHeaderChunkPolicy = NoHeaderChunkPolicy.Ignore,
NotEnoughBytesPolicy = NotEnoughBytesPolicy.Ignore,
UnexpectedTrackChunksCountPolicy = UnexpectedTrackChunksCountPolicy.Ignore,
UnknownChannelEventPolicy = UnknownChannelEventPolicy.SkipStatusByteAndOneDataByte,
UnknownChunkIdPolicy = UnknownChunkIdPolicy.ReadAsUnknownChunk,
UnknownFileFormatPolicy = UnknownFileFormatPolicy.Ignore,
});
Please read more about these properties in the API section to learn about what options are available to handle each error. If some policies are set to Abort
, an instance of corresponding exception will be thrown. Types of all such exceptions are derived from MidiException and listed in the Exceptions section of Read
methods (by file path or by stream) on API documentation.