Esempio n. 1
0
        /// <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 Import(Stream inputStream)
        {
            // Validate input
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            if (!inputStream.CanRead)
            {
                throw new ArgumentException("Stream must be readable.", "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.AddTrack(MidiParser.ParseToTrack(trackChunks[i].Data));
            }
            return(sequence);
        }
        /// <summary>Write the track to the output stream.</summary>
        /// <param name="outputStream">The output stream to which the track should be written.</param>
        public void Write(Stream outputStream)
        {
            // Validate the stream
            if (outputStream == null)
            {
                throw new ArgumentNullException("outputStream");
            }
            if (!outputStream.CanWrite)
            {
                throw new ArgumentException("Cannot write to stream.", "outputStream");
            }

            // Make sure we have an end of track marker if we need one
            if (!HasEndOfTrack && _requireEndOfTrack)
            {
                throw new InvalidOperationException("The track cannot be written until it has an end of track marker.");
            }

            // Get the event data and write it out
            MemoryStream memStream = new MemoryStream();

            for (int i = 0; i < _events.Count; i++)
            {
                _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);
        }
 public static MidiSequence Import(Stream inputStream)
 {
     if (inputStream == null)
     {
         throw new ArgumentNullException("inputStream");
     }
     if (!inputStream.CanRead)
     {
         throw new ArgumentException("Stream must be readable.", "inputStream");
     }
     MThdChunkHeader header = MThdChunkHeader.Read(inputStream);
     MTrkChunkHeader[] headerArray = new MTrkChunkHeader[header.NumberOfTracks];
     for (int i = 0; i < header.NumberOfTracks; i++)
     {
         headerArray[i] = MTrkChunkHeader.Read(inputStream);
     }
     MidiSequence sequence = new MidiSequence(header.Format, header.Division);
     for (int j = 0; j < header.NumberOfTracks; j++)
     {
         sequence.AddTrack(MidiParser.ParseToTrack(headerArray[j].Data));
     }
     return sequence;
 }
Esempio n. 4
0
        /// <summary>Write the track to the output stream.</summary>
        /// <param name="outputStream">The output stream to which the track should be written.</param>
        public void Write(Stream outputStream)
        {
            // Validate the stream
            if (outputStream == null) throw new ArgumentNullException("outputStream");
            if (!outputStream.CanWrite) throw new ArgumentException("Cannot write to stream.", "outputStream");

            // Make sure we have an end of track marker if we need one
            if (!HasEndOfTrack && _requireEndOfTrack) throw new InvalidOperationException("The track cannot be written until it has an end of track marker.");

            // Get the event data and write it out
            MemoryStream memStream = new MemoryStream();
            for(int i=0; i<_events.Count; i++) _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);
        }
Esempio n. 5
0
        /// <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 Import(Stream inputStream)
        {
            // Validate input
            if (inputStream == null) throw new ArgumentNullException("inputStream");
            if (!inputStream.CanRead) throw new ArgumentException("Stream must be readable.", "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.AddTrack(MidiParser.ParseToTrack(trackChunks[i].Data));
            }
            return sequence;
        }