/// <summary>
        /// Creates a new WAVDataChunk from a binary reader.<para/>
        /// ENSURE YOU'RE CALLING IN THE RIGHT ORDER: WAVHeader =&gt; WAVFormatChunk =&gt; WAVDataChunk.
        /// </summary>
        /// <param name="reader">The <see cref="BinaryReader"/> to take the data from.</param>
        /// <returns></returns>
        public static WAVDataChunk CreateFromStream(BinaryReader reader, WAVFormatChunk fmtChunk)
        {
            WAVDataChunk data = new WAVDataChunk(fmtChunk);

            // Make sure we're in the right place.
            char[] id         = reader.ReadChars(4);
            string idAsString = string.Concat(id);

            while (idAsString != ID)
            {
                Console.WriteLine("Unexpected ID when trying to create format chunk (got [" + idAsString + "], expecting [" + ID + "] || CURRENT POS: " + reader.BaseStream.Position + ")");
                int size = reader.ReadInt32();
                reader.BaseStream.Seek(size, SeekOrigin.Current);
                id         = reader.ReadChars(4);
                idAsString = string.Concat(id);
            }

            uint length = reader.ReadUInt32();

            data.Data = new short[length / 2];
            byte[] raw = new byte[length];
            reader.Read(raw, 0, raw.Length);
            for (int i = 0; i < raw.Length; i += 2)
            {
                byte  b0  = raw[i];
                byte  b1  = raw[i + 1];
                short val = (short)((b1 << 8) + b0);
                data.Data[i / 2] = val;
            }
            return(data);
        }
Exemple #2
0
        /// <summary>
        /// Creates a new WAVFormatChunk from a binary reader.<para/>
        /// ENSURE YOU'RE CALLING IN THE RIGHT ORDER: WAVHeader =&gt; WAVFormatChunk =&gt; WAVDataChunk.
        /// </summary>
        /// <param name="reader">The <see cref="BinaryReader"/> to take the data from.</param>
        /// <returns></returns>
        public static WAVFormatChunk CreateFromStream(BinaryReader reader)
        {
            WAVFormatChunk fmtChunk = new WAVFormatChunk();

            // Make sure we're in the right place.
            char[] id         = reader.ReadChars(4);
            string idAsString = string.Concat(id);

            while (idAsString != ID)
            {
                Console.WriteLine("Unexpected ID when trying to create format chunk (got [" + idAsString + "], expecting [" + ID + "] || CURRENT POS: " + reader.BaseStream.Position + ")");
                int size = reader.ReadInt32();
                reader.BaseStream.Seek(size, SeekOrigin.Current);
                id         = reader.ReadChars(4);
                idAsString = string.Concat(id);
            }

            int objSize = reader.ReadInt32();

            // Now here is where things get important. Size *is* variable, contrary to what I thought. It just has a default value that may be overwritten.
            long currentPos = reader.BaseStream.Position;

            reader.ReadUInt16();             // Skip format tag. This is so that casting works fine.
            fmtChunk.Channels   = reader.ReadUInt16();
            fmtChunk.SampleRate = reader.ReadUInt32();
            reader.ReadUInt32();            // Skip avg bytes / sec
            reader.ReadUInt16();            // Skip frame size
            reader.ReadUInt16();            //fmtChunk.BitsPerSample = reader.ReadUInt16();

            // And skip the necessary amount of bytes
            reader.BaseStream.Seek(currentPos + objSize, SeekOrigin.Begin);

            return(fmtChunk);
        }
 /// <summary>
 /// Construct a new WEM Data chunk. Requires a pre-existing Format Chunk
 /// </summary>
 /// <param name="formatInfo"></param>
 public WAVDataChunk(WAVFormatChunk formatInfo)
 {
     FormatChunk = formatInfo;
 }