/// <summary>Write the track to the output stream.</summary> /// <param name="outputStream">The output stream to which the track should be written.</param> internal void Write(Stream outputStream) { Validate.NonNull("outputStream", outputStream); // Make sure we have an end of track marker if we need one if (!HasEndOfTrack && m_requireEndOfTrack) { throw new InvalidOperationException("The track cannot be written until it has an end of track marker."); } using (MemoryStream memStream = new MemoryStream()) { // Get the event data and write it out for (int i = 0; i < m_events.Count; i++) { m_events[i].Write(memStream); } // Tack on the header and write the whole thing out to the main stream. MTrkChunkHeader header = new MTrkChunkHeader(memStream.ToArray()); header.Write(outputStream); } }
/// <summary>Reads a MIDI stream into a new MidiSequence.</summary> /// <param name="inputStream">The stream containing the MIDI data.</param> /// <returns>A MidiSequence containing the parsed MIDI data.</returns> public static MidiSequence Open(Stream inputStream) { Validate.NonNull("inputStream", inputStream); // Read in the main MIDI header MThdChunkHeader mainHeader = MThdChunkHeader.Read(inputStream); // Read in all of the tracks MTrkChunkHeader[] trackChunks = new MTrkChunkHeader[mainHeader.NumberOfTracks]; for (int i = 0; i < mainHeader.NumberOfTracks; i++) { trackChunks[i] = MTrkChunkHeader.Read(inputStream); } // Create the MIDI sequence MidiSequence sequence = new MidiSequence(mainHeader.Format, mainHeader.Division); for (int i = 0; i < mainHeader.NumberOfTracks; i++) { sequence.Tracks.Add(MidiParser.ParseToTrack(trackChunks[i].Data)); } return(sequence); }