Beispiel #1
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 input
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            if (!inputStream.CanRead)
            {
                throw new ArgumentException("Stream must be readable.", "inputStream");
            }

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

            ValidateHeader(header);

            // Read in the data (amount specified in the header)
            byte [] data = new byte[header.Length];
            if (inputStream.Read(data, 0, data.Length) != data.Length)
            {
                throw new InvalidOperationException("Not enough data in stream to read MTrk chunk.");
            }

            // Return the new chunk
            return(new MTrkChunkHeader(data));
        }
Beispiel #2
0
        /// <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 input
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            if (!inputStream.CanRead)
            {
                throw new ArgumentException("Stream must be readable.", "inputStream");
            }

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

            ValidateHeader(header);

            // Read in the format
            int format = 0;

            for (int i = 0; i < 2; i++)
            {
                int val = inputStream.ReadByte();
                if (val < 0)
                {
                    throw new InvalidOperationException("The stream is invalid.");
                }
                format <<= 8;
                format  |= val;
            }

            // Read in the number of tracks
            int numTracks = 0;

            for (int i = 0; i < 2; i++)
            {
                int val = inputStream.ReadByte();
                if (val < 0)
                {
                    throw new InvalidOperationException("The stream is invalid.");
                }
                numTracks <<= 8;
                numTracks  |= val;
            }

            // Read in the division
            int division = 0;

            for (int i = 0; i < 2; i++)
            {
                int val = inputStream.ReadByte();
                if (val < 0)
                {
                    throw new InvalidOperationException("The stream is invalid.");
                }
                division <<= 8;
                division  |= val;
            }

            // Create a new MThd header and return it
            return(new MThdChunkHeader(format, numTracks, division));
        }