/// <summary> /// Initializes a new instance of the MidiEventArgs class with the /// specified Midi event. /// </summary> /// <param name="e"> /// The Midi event for this event. /// </param> public MidiEventArgs(MidiEvent e) { evt = e; }
/// <summary> /// Reads the data for the next track from the Midi file. /// </summary> /// <param name="trackNum"> /// The track number. /// </param> private void ReadNextTrack(int trackNum) { int status = 0; int runningStatus = 0; // Read length of track. binReader.ReadBytes(LengthByteCount); // Continue reading Midi events until the end of the track. while (true) { // Next Midi message in track. IMidiMessage msg = null; // Ticks for next Midi event. int ticks = ReadVariableLengthQuantity(); // Read status byte for the next Midi message. status = binReader.ReadByte(); // If this is a status byte. if ((status & StatusFlag) == StatusFlag) { // If the next Midi message is a channel message. if (ChannelMessage.IsChannelMessage(status)) { // Read channel message from the Midi file. msg = ReadChannelMessage(status); // Update running status. runningStatus = status; } // Else if the next Midi message is a meta message. else if (MetaMessage.IsMetaMessage(status)) { // Read the type of meta message. MetaType mType = (MetaType)binReader.ReadByte(); // If this is the end of the track. if (mType == MetaType.EndOfTrack) { // Make sure end of track message has the same // ticks value as the end of track message in the // file. tracks[trackNum].Slide(tracks[trackNum].Count - 1, ticks); // Break out of loop - finished with this track. break; } // Read the length of the meta message data. int length = ReadVariableLengthQuantity(); // Read the meta message data. byte[] data = binReader.ReadBytes(length); // Create meta message. msg = new MetaMessage(mType, data); } // Else if the next Midi message is a system exclusive // message. else if (SysExMessage.IsSysExMessage(status)) { // The type of system exclusive message. SysExType type = (SysExType)status; // Read the length of the system exclusive data. int length = ReadVariableLengthQuantity(); // Read the system exclusive data. byte[] data = binReader.ReadBytes(length); // Create system exclusive message. msg = new SysExMessage(type, data); } } // Assumes running status. else { // Create channel message. msg = ReadChannelMessage(runningStatus, status); } // Create the next Midi event and store it in the specified // track. MidiEvent e = new MidiEvent(msg, ticks); tracks[trackNum].Add(e); } }
/// <summary> /// /// </summary> /// <param name="tracks"></param> /// <returns></returns> public static Track Merge(ArrayList tracks) { Track mergedTrack = new Track(); Track currentTrack; ArrayList trackList = new ArrayList(); ArrayList events = new ArrayList(); ArrayList trackIndexes = new ArrayList(); for (int i = 0; i < tracks.Count; i++) { currentTrack = (Track)tracks[i]; if (currentTrack.Count > 1) { trackList.Add(currentTrack); trackIndexes.Add(0); events.Add(currentTrack[0]); } } while (events.Count > 0) { int n = 0; MidiEvent e1 = (MidiEvent)events[0]; MidiEvent e2; int ticks = e1.Ticks; for (int i = 1; i < events.Count; i++) { e1 = (MidiEvent)events[i]; if (e1.Ticks < ticks) { ticks = e1.Ticks; n = i; } } e1 = (MidiEvent)events[n]; mergedTrack.Add(e1); for (int i = 0; i < events.Count; i++) { e2 = (MidiEvent)events[i]; e2.Ticks -= e1.Ticks; events[i] = e2; } int counter = (int)trackIndexes[n] + 1; currentTrack = (Track)trackList[n]; if (counter < currentTrack.Count - 1) { events[n] = currentTrack[counter]; trackIndexes[n] = counter; } else { trackList.RemoveAt(n); trackIndexes.RemoveAt(n); events.RemoveAt(n); } } return(mergedTrack); }