public void TestLoadStandardWaveFile() { using (MemoryStream memoryStream = new MemoryStream()) { { byte[] testData = new byte[] { 0x66, 0x6D, 0x74, 0x20, 0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x22, 0x56, 0x00, 0x00, 0x27, 0x57, 0x00, 0x00, 0x00, 0x04 }; memoryStream.Write(testData, 0, testData.Length); } memoryStream.Position = 0; { WaveFormat restored; BinaryReader reader = new BinaryReader(memoryStream); int restoredFileLength = WaveFormat.Load(reader, out restored); Assert.AreEqual(50, restoredFileLength); Assert.AreEqual(2, restored.FormatTag); Assert.AreEqual(2, restored.ChannelCount); Assert.AreEqual(22050, restored.SamplesPerSecond); Assert.AreEqual(22311, restored.AverageBytesPerSecond); Assert.AreEqual(1024, restored.BlockAlignment); } } }
public void TestLoadVersusSave() { WaveFormat original; original.FormatTag = 0x1A2B; original.ChannelCount = 42; original.SamplesPerSecond = 123456789; original.AverageBytesPerSecond = 987654321; original.BlockAlignment = 1928; using (MemoryStream memoryStream = new MemoryStream()) { { BinaryWriter writer = new BinaryWriter(memoryStream); WaveFormat.Save(writer, ref original); } memoryStream.Position = 0; { WaveFormat restored; BinaryReader reader = new BinaryReader(memoryStream); int restoredFileLength = WaveFormat.Load(reader, out restored); Assert.AreEqual(WaveFormat.Size, restoredFileLength); Assert.AreEqual(original.FormatTag, restored.FormatTag); Assert.AreEqual(original.ChannelCount, restored.ChannelCount); Assert.AreEqual(original.SamplesPerSecond, restored.SamplesPerSecond); Assert.AreEqual(original.AverageBytesPerSecond, restored.AverageBytesPerSecond); Assert.AreEqual(original.BlockAlignment, restored.BlockAlignment); } } }
/// <summary>Reads the WaveFormat information structure from a binary stream</summary> /// <param name="reader">Reader from which to read the WaveFormat structure</param> /// <param name="waveFormatEx">Wave format structure to be written to the file</param> /// <returns>The total size of the structure as indicated in the size field</returns> public static int Load(BinaryReader reader, out WaveFormatEx waveFormatEx) { int chunkLength = WaveFormat.Load(reader, out waveFormatEx.WaveFormat); waveFormatEx.BitsPerSample = reader.ReadUInt16(); waveFormatEx.ExtraInformationSize = reader.ReadUInt16(); // Done, return the chunk's indicated length, the caller might be interested // in this number to either skip additional data or process it return(chunkLength); }
public void TestLoadNonWaveData() { using (MemoryStream memoryStream = new MemoryStream()) { { byte[] testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; memoryStream.Write(testData, 0, testData.Length); } memoryStream.Position = 0; { WaveFormat restored; BinaryReader reader = new BinaryReader(memoryStream); Assert.Throws <IOException>( delegate() { WaveFormat.Load(reader, out restored); } ); } } }