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, 0x04, 0x00, 0x20, 0x00//, 0xF4, 0x03, 0x07 }; memoryStream.Write(testData, 0, testData.Length); } memoryStream.Position = 0; { WaveFormatEx restored; BinaryReader reader = new BinaryReader(memoryStream); int restoredFileLength = WaveFormatEx.Load(reader, out restored); Assert.AreEqual(50, restoredFileLength); Assert.AreEqual(2, restored.WaveFormat.FormatTag); Assert.AreEqual(2, restored.WaveFormat.ChannelCount); Assert.AreEqual(22050, restored.WaveFormat.SamplesPerSecond); Assert.AreEqual(22311, restored.WaveFormat.AverageBytesPerSecond); Assert.AreEqual(1024, restored.WaveFormat.BlockAlignment); Assert.AreEqual(4, restored.BitsPerSample); Assert.AreEqual(32, restored.ExtraInformationSize); } } }
/// <summary>Saves the WaveFormat information structure into a binary stream</summary> /// <param name="writer">Writer through which to write the WaveFormat structure</param> /// <param name="chunkSize">Total size of the format info chunk</param> /// <param name="waveFormatEx">Wave format structure to be written to the file</param> /// <remarks> /// Note that this writes chunkSize + 8 bytes. The additional 8 bytes result from /// the chunk's header that provides the chunk signature and size you indicate. /// </remarks> public static void Save( BinaryWriter writer, int chunkSize, ref WaveFormatEx waveFormatEx ) { WaveFormat.Save(writer, chunkSize, ref waveFormatEx.WaveFormat); writer.Write((UInt16)waveFormatEx.BitsPerSample); writer.Write((UInt16)waveFormatEx.ExtraInformationSize); }
/// <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; }
/// <summary>Saves the WaveFormat information structure into a binary stream</summary> /// <param name="writer">Writer through which to write the WaveFormat structure</param> /// <param name="waveFormat">Wave format structure to be written to the file</param> public static void Save(BinaryWriter writer, ref WaveFormatExtensible waveFormat) { WaveFormatEx.Save(writer, WaveFormatExtensible.Size, ref waveFormat.FormatEx); writer.Write((UInt16)waveFormat.ValidBitsPerSample); writer.Write((UInt32)waveFormat.ChannelMask); byte[] subFormatGuidBytes = waveFormat.SubFormat.ToByteArray(); writer.Write(subFormatGuidBytes, 0, 16); }
/// <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); }
/// <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="waveFormat">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 WaveFormatExtensible waveFormat) { int totalSize = WaveFormatEx.Load(reader, out waveFormat.FormatEx); waveFormat.ValidBitsPerSample = reader.ReadUInt16(); waveFormat.ChannelMask = (ChannelMaskFlags)reader.ReadUInt32(); byte[] subFormatGuidBytes = reader.ReadBytes(16); waveFormat.SubFormat = new Guid(subFormatGuidBytes); return(totalSize); }
public void TestLoadVersusSave() { WaveFormatEx original; original.WaveFormat.FormatTag = 0x1A2B; original.WaveFormat.ChannelCount = 42; original.WaveFormat.SamplesPerSecond = 123456789; original.WaveFormat.AverageBytesPerSecond = 987654321; original.WaveFormat.BlockAlignment = 1928; original.BitsPerSample = 9182; original.ExtraInformationSize = 22222; using (MemoryStream memoryStream = new MemoryStream()) { { BinaryWriter writer = new BinaryWriter(memoryStream); WaveFormatEx.Save(writer, ref original); } memoryStream.Position = 0; { WaveFormatEx restored; BinaryReader reader = new BinaryReader(memoryStream); int restoredFileLength = WaveFormatEx.Load(reader, out restored); Assert.AreEqual(WaveFormatEx.Size, restoredFileLength); Assert.AreEqual( original.WaveFormat.FormatTag, restored.WaveFormat.FormatTag ); Assert.AreEqual( original.WaveFormat.ChannelCount, restored.WaveFormat.ChannelCount ); Assert.AreEqual( original.WaveFormat.SamplesPerSecond, restored.WaveFormat.SamplesPerSecond ); Assert.AreEqual( original.WaveFormat.AverageBytesPerSecond, restored.WaveFormat.AverageBytesPerSecond ); Assert.AreEqual( original.WaveFormat.BlockAlignment, restored.WaveFormat.BlockAlignment ); Assert.AreEqual(original.BitsPerSample, restored.BitsPerSample); Assert.AreEqual(original.ExtraInformationSize, restored.ExtraInformationSize); } } }
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; { WaveFormatEx restored; BinaryReader reader = new BinaryReader(memoryStream); Assert.Throws <IOException>( delegate() { WaveFormatEx.Load(reader, out restored); } ); } } }
/// <summary>Write a format chunk using the WaveFormatEx structure</summary> /// <param name="waveFormatEx"> /// Extended WaveFormat structure to write into the chunk /// </param> public void WriteChunk(ref WaveFormatEx waveFormatEx) { WaveFormatEx.Save(this.writer, ref waveFormatEx); }
/// <summary>Saves the WaveFormat information structure into a binary stream</summary> /// <param name="writer">Writer through which to write the WaveFormat structure</param> /// <param name="waveFormatEx">Wave format structure to be written to the file</param> /// <remarks> /// The default header has a total size of 26 bytes, 18 bytes are taken up by /// the actual chunk and 8 additional bytes by the header that specifies the /// chunk's signature and size. /// </remarks> public static void Save(BinaryWriter writer, ref WaveFormatEx waveFormatEx) { Save(writer, WaveFormatEx.Size, ref waveFormatEx); }