MIDIHeader ParseHeader(FileStream inFile) { MIDIHeader header = new MIDIHeader(); // MThd { byte[] _mthd = new byte[4]; inFile.Read(_mthd, 0, 4); Array.Reverse(_mthd); header.MThd = BitConverter.ToUInt32(_mthd, 0); if (header.MThd != (UInt32)MIDI_EVENT_TYPES.HEADER_CHUNK_ID) { throw new Exception("wrong midi header"); } } //header length; { byte[] _length = new byte[4]; inFile.Read(_length, 0, 4); Array.Reverse(_length); header.header_length = BitConverter.ToUInt32(_length, 0); if (header.header_length != (UInt32)MIDI_EVENT_TYPES.HEADER_CHUNK_SIZE) { throw new Exception("wrong header chunk size"); } } // format { byte[] _format = new byte[2]; inFile.Read(_format, 0, 2); Array.Reverse(_format); header.format = BitConverter.ToUInt16(_format, 0); } // num_of_tracks { byte[] _num_of_tracks = new byte[2]; inFile.Read(_num_of_tracks, 0, 2); Array.Reverse(_num_of_tracks); header.number_of_tracks = BitConverter.ToUInt16(_num_of_tracks, 0); } // division { byte[] _division = new byte[2]; inFile.Read(_division, 0, 2); Array.Reverse(_division); header.division = BitConverter.ToUInt16(_division, 0); } return(header); }
public void ReadMIDIFile(string path) { Debug.Log(path); this.midiFile = File.OpenRead(path); this.header = ParseHeader(midiFile); this.tracks = new List <MIDITrack>(); this.currentNotes = new Dictionary <uint, MIDINote>(); for (int i = 0; i < header.number_of_tracks; i++) { MIDITrack track = ParseTrack(midiFile); this.tracks.Add(track); } }
MIDIHeader ParseHeader(FileStream inFile) { MIDIHeader header = new MIDIHeader (); // MThd { byte[] _mthd = new byte[4]; inFile.Read(_mthd, 0, 4); Array.Reverse(_mthd); header.MThd = BitConverter.ToUInt32(_mthd, 0); if(header.MThd != (UInt32)MIDI_EVENT_TYPES.HEADER_CHUNK_ID){ throw new Exception("wrong midi header"); } } //header length; { byte[] _length = new byte[4]; inFile.Read(_length, 0, 4); Array.Reverse(_length); header.header_length = BitConverter.ToUInt32(_length, 0); if(header.header_length != (UInt32)MIDI_EVENT_TYPES.HEADER_CHUNK_SIZE){ throw new Exception("wrong header chunk size"); } } // format { byte[] _format = new byte[2]; inFile.Read(_format, 0, 2); Array.Reverse(_format); header.format = BitConverter.ToUInt16(_format, 0); } // num_of_tracks { byte[] _num_of_tracks = new byte[2]; inFile.Read(_num_of_tracks, 0, 2); Array.Reverse(_num_of_tracks); header.number_of_tracks = BitConverter.ToUInt16(_num_of_tracks, 0); } // division { byte[] _division = new byte[2]; inFile.Read(_division, 0, 2); Array.Reverse(_division); header.division = BitConverter.ToUInt16(_division, 0); } return header; }
public void ReadMIDIFile(string path) { Debug.Log(path); this.midiFile = File.OpenRead(path); this.header = ParseHeader(midiFile); this.tracks = new List<MIDITrack>(); this.currentNotes = new Dictionary<uint, MIDINote>(); for(int i = 0; i < header.number_of_tracks; i++){ MIDITrack track = ParseTrack(midiFile); this.tracks.Add(track); } }