private void BrowseBTN_Click(object sender, EventArgs e) { if (OpenMIDIDialog.ShowDialog() == DialogResult.OK) { using (FileStream midiReader = new FileStream(OpenMIDIDialog.FileName, FileMode.Open)) { midiReader.Seek(4, SeekOrigin.Begin); byte[] headerLength = new byte[4]; midiReader.Read(headerLength, 0, headerLength.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(headerLength); } int headerLengthInt = BitConverter.ToInt32(headerLength, 0); if (headerLengthInt != 6) { MessageBox.Show(this, "Error: " + Path.GetFileName(OpenMIDIDialog.FileName) + " is not a MIDI file created under the MIDI 1.0 specification, and won't be opened for reading.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MIDIFileBox.Text = OpenMIDIDialog.FileName; } } } }
private void BrowseBTN_Click(object sender, EventArgs e) { if (OpenMIDIDialog.ShowDialog() == DialogResult.OK) { using (FileStream midiReader = new FileStream(OpenMIDIDialog.FileName, FileMode.Open)) { midiReader.Seek(4, SeekOrigin.Begin); byte[] headerSize = new byte[4]; midiReader.Read(headerSize, 0, headerSize.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(headerSize); } int headerSizeInt = BitConverter.ToInt32(headerSize, 0); byte[] midiFormat = new byte[2]; midiReader.Read(midiFormat, 0, midiFormat.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(midiFormat); } ushort formatNum = BitConverter.ToUInt16(midiFormat, 0); if (headerSizeInt != 6) { MessageBox.Show(this, Path.GetFileName(OpenMIDIDialog.FileName) + " is not a MIDI file created under the MIDI 1.0 specification, and won't be opened for splitting.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (formatNum != 1) { MessageBox.Show(this, Path.GetFileName(OpenMIDIDialog.FileName) + " is not a Format 1 MIDI file, and won't be opened for splitting.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MIDIListView.Items.Clear(); MIDIPathBox.Text = OpenMIDIDialog.FileName; byte[] totalTracks = new byte[2]; midiReader.Read(totalTracks, 0, totalTracks.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(totalTracks); } ushort totalTracksShort = BitConverter.ToUInt16(totalTracks, 0); midiReader.Seek(2, SeekOrigin.Current); if (Settings.Default.ReadTrackNames) { for (ushort i = 0; i < totalTracksShort; i++) { midiReader.Seek(4, SeekOrigin.Current); byte[] trackSize = new byte[4]; midiReader.Read(trackSize, 0, trackSize.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(trackSize); } int trackSizeInt = BitConverter.ToInt32(trackSize, 0); List <byte> tempArray = new List <byte>(); if (trackSizeInt <= 4096) { byte[] trackNameData = new byte[trackSizeInt]; midiReader.Read(trackNameData, 0, trackNameData.Length); tempArray.AddRange(trackNameData); } else // To help speed up search time, read only the first 4096 bytes of data in larger track sizes (the track name is usually located within the first 4096 bytes of data). { byte[] trackNameData = new byte[4096]; midiReader.Read(trackNameData, 0, trackNameData.Length); midiReader.Seek(trackSizeInt - 4096, SeekOrigin.Current); tempArray.AddRange(trackNameData); } byte[] trackData = tempArray.ToArray(); byte[] searchPattern = { 0xFF, 0x03 }; List <int> trackNameIndex = new List <int>(); trackNameIndex = KMPSearch(trackData, searchPattern); string trackNameStr; if (trackNameIndex.Count > 0) { int lengthIndex = trackNameIndex.ElementAt(trackNameIndex.Count - 1) + 2; byte trackNameByteLength = trackData[lengthIndex]; byte[] trackNameBytes = new byte[(int)trackNameByteLength]; trackNameBytes = SubArray(trackData, lengthIndex + 1, (int)trackNameByteLength); trackNameStr = Encoding.UTF8.GetString(trackNameBytes); } else { trackNameStr = "Track " + (i + 1).ToString(); } string[] newRow = { (i + 1).ToString(), trackNameStr, trackSizeInt.ToString() }; ListViewItem newItem = new ListViewItem(newRow); MIDIListView.Items.Add(newItem); } } else { for (ushort i = 0; i < totalTracksShort; i++) { midiReader.Seek(4, SeekOrigin.Current); byte[] trackSize = new byte[4]; midiReader.Read(trackSize, 0, trackSize.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(trackSize); } int trackSizeInt = BitConverter.ToInt32(trackSize, 0); midiReader.Seek(trackSizeInt, SeekOrigin.Current); string trackNameStr = "Track " + (i + 1).ToString(); string[] newRow = { (i + 1).ToString(), trackNameStr, trackSizeInt.ToString() }; ListViewItem newItem = new ListViewItem(newRow); MIDIListView.Items.Add(newItem); } } } } } }