public WaveFormat(BinaryStream stream) { UInt32 riffChunkSize = Riff.VerifyChunkIdAndGetSize(stream, Riff.RiffHeaderID); Console.WriteLine("Riff Chunk Size {0}", riffChunkSize); Riff.Verify(stream, Wave.WaveHeaderID, "Expected RIFF format to be"); UInt32 formatChunkSize = Riff.VerifyChunkIdAndGetSize(stream, Wave.FmtHeaderID); if (formatChunkSize < 16) { throw new FormatException(String.Format("Expected wave 'fmt ' chunk size to be at least 16 but is {0}", formatChunkSize)); } this.audioFormat = stream.LittleEndianReadUInt16(); this.channelCount = stream.LittleEndianReadUInt16(); this.samplesPerSecond = stream.LittleEndianReadUInt32(); this.avgBytesPerSecond = stream.LittleEndianReadUInt32(); this.blockAlign = stream.LittleEndianReadUInt16(); this.bitsPerSample = stream.LittleEndianReadUInt16(); // skip extra parameters stream.Skip((Int32)formatChunkSize - 16); // this.dataChunkSize = Riff.VerifyChunkIdAndGetSize(stream, Wave.DataHeaderID); }
public static UInt32 VerifyChunkIdAndGetSize(BinaryStream stream, Byte[] expectedChunkID) { Byte[] chunkID = stream.ReadFullSize(4); if (chunkID[0] != expectedChunkID[0] || chunkID[1] != expectedChunkID[1] || chunkID[2] != expectedChunkID[2] || chunkID[3] != expectedChunkID[3]) { throw new FormatException(String.Format( "Expected chunk id to be '{0}{1}{2}{3}' (0x{4:X2}{5:X2}{6:X2}{7:X2}) but got '{8}{9}{10}{11}' (0x{12})", (Char)expectedChunkID[0], (Char)expectedChunkID[1], (Char)expectedChunkID[2], (Char)expectedChunkID[3], expectedChunkID[0], expectedChunkID[1], expectedChunkID[2], expectedChunkID[3], (Char)chunkID[0], (Char)chunkID[1], (Char)chunkID[2], (Char)chunkID[3], chunkID.ToHexString(0, 4))); } UInt32 size = stream.LittleEndianReadUInt32(); Console.WriteLine("RIFF '{0}{1}{2}{3}' size {4}", (Char)chunkID[0], (Char)chunkID[1], (Char)chunkID[2], (Char)chunkID[3], size); return(size); }