/// <summary> /// Creates a new WEMDataChunk from a binary reader.<para/> /// ENSURE YOU'RE CALLING IN THE RIGHT ORDER: WEMHeader => WEMFormatChunk => WEMDataChunk. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> to take the data from.</param> /// <returns></returns> public static WEMDataChunk CreateFromStream(BinaryReader reader, WEMFormatChunk fmtChunk) { WEMDataChunk data = new WEMDataChunk(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); }
/// <summary> /// Creates a new WEMFormatChunk from a binary reader.<para/> /// ENSURE YOU'RE CALLING IN THE RIGHT ORDER: WEMHeader => WEMFormatChunk => WEMDataChunk. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> to take the data from.</param> /// <returns></returns> public static WEMFormatChunk CreateFromStream(BinaryReader reader) { WEMFormatChunk fmtChunk = new WEMFormatChunk(); // Make sure we're in the right place. char[] id = reader.ReadChars(4); string idAsString = string.Concat(id); //Console.WriteLine(idAsString); 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 WEMDataChunk(WEMFormatChunk formatInfo) { FormatChunk = formatInfo; }