/// <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 WaveFormat waveFormat) { byte[] actualSignature = reader.ReadBytes(4); bool signatureIsValid = (actualSignature[0] == WaveFormat.Signature[0]) && // 'f' (actualSignature[1] == WaveFormat.Signature[1]) && // 'm' (actualSignature[2] == WaveFormat.Signature[2]) && // 't' (actualSignature[3] == WaveFormat.Signature[3]); // ' ' if(!signatureIsValid) { throw new IOException( "Offset mismatch or binary data does not contain a wave format structure" ); } // Obtain the total length of the chunk // The old style format info for uncompressed audio was always 16 bytes long int chunkLength = reader.ReadInt32(); // Read the remainder of the wave format info structure waveFormat = new WaveFormat(); waveFormat.FormatTag = reader.ReadUInt16(); waveFormat.ChannelCount = reader.ReadUInt16(); waveFormat.SamplesPerSecond = reader.ReadUInt32(); waveFormat.AverageBytesPerSecond = reader.ReadUInt32(); waveFormat.BlockAlignment = 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>Write a format chunk using the old WaveFormat structure</summary> /// <param name="waveFormat">WaveFormat structure to write into the chunk</param> public void WriteChunk(ref WaveFormat waveFormat) { WaveFormat.Save(this.writer, ref waveFormat); }
/// <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> /// <remarks> /// The default header has a total size of 24 bytes, 16 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 WaveFormat waveFormat) { Save(writer, WaveFormat.Size, ref waveFormat); }
/// <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="waveFormat">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 WaveFormat waveFormat) { writer.Write(Signature); // 'f', 'm', 't', ' ' writer.Write((Int32)chunkSize); writer.Write((UInt16)waveFormat.FormatTag); writer.Write((UInt16)waveFormat.ChannelCount); writer.Write((UInt32)waveFormat.SamplesPerSecond); writer.Write((UInt32)waveFormat.AverageBytesPerSecond); writer.Write((UInt16)waveFormat.BlockAlignment); }