/// <summary>Read in an MThd chunk from the stream.</summary>
        /// <param name="inputStream">The stream from which to read the MThd chunk.</param>
        /// <returns>The MThd chunk read.</returns>
        public static MThdChunkHeader Read(Stream inputStream)
        {
            Validate.NonNull("inputStream", inputStream);

            // Read in a header from the stream and validate it
            ValidateHeader(ChunkHeader.Read(inputStream));

            // Read in the format, number of tracks, and the division, each a two-byte value
            int format    = ReadTwoByteValue(inputStream);
            int numTracks = ReadTwoByteValue(inputStream);
            int division  = ReadTwoByteValue(inputStream);

            // Create a new MThd header and return it
            return(new MThdChunkHeader((Format)format, numTracks, division));
        }
Пример #2
0
        /// <summary>Read in an MTrk chunk from the stream.</summary>
        /// <param name="inputStream">The stream from which to read the MTrk chunk.</param>
        /// <returns>The MTrk chunk read.</returns>
        public static MTrkChunkHeader Read(Stream inputStream)
        {
            Validate.NonNull("inputStream", inputStream);

            // Read in a header from the stream and validate it
            ChunkHeader header = ChunkHeader.Read(inputStream);

            ValidateHeader(header);

            // Read in the amount of data specified in the chunk header
            byte[] data = new byte[header.Length];
            int    bytesRead;

            for (int offset = 0; offset < data.Length; offset += bytesRead)
            {
                if ((bytesRead = inputStream.Read(data, offset, data.Length - offset)) < 0)
                {
                    throw new InvalidOperationException("Not enough data in stream to fill MTrk chunk.");
                }
            }

            // Return the new chunk
            return(new MTrkChunkHeader(data));
        }