NAudio.Wave.WaveFormat.Serialize C# (CSharp) Method

Serialize() public method

Writes this WaveFormat object to a stream
public Serialize ( BinaryWriter writer ) : void
writer System.IO.BinaryWriter the output stream
return void
        public virtual void Serialize(BinaryWriter writer)
        {
            writer.Write((int)(18 + extraSize)); // wave format length
            writer.Write((short)Encoding);
            writer.Write((short)Channels);
            writer.Write((int)SampleRate);
            writer.Write((int)AverageBytesPerSecond);
            writer.Write((short)BlockAlign);
            writer.Write((short)BitsPerSample);
            writer.Write((short)extraSize);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Createa a new BwfWriter
        /// </summary>
        /// <param name="filename">Rarget filename</param>
        /// <param name="format">WaveFormat</param>
        /// <param name="bextChunkInfo">Chunk information</param>
        public BwfWriter(string filename, WaveFormat format, BextChunkInfo bextChunkInfo)
        {
            this.format = format;
            writer      = new BinaryWriter(File.OpenWrite(filename));
            writer.Write(Encoding.UTF8.GetBytes("RIFF")); // will be updated to RF64 if large
            writer.Write(0);                              // placeholder
            writer.Write(Encoding.UTF8.GetBytes("WAVE"));

            writer.Write(Encoding.UTF8.GetBytes("JUNK")); // ds64
            writer.Write(28);                             // ds64 size
            writer.Write(0L);                             // RIFF size
            writer.Write(0L);                             // data size
            writer.Write(0L);                             // sampleCount size
            writer.Write(0);                              // table length
            // TABLE appears here - to store the sizes of other huge chunks other than

            // write the broadcast audio extension
            writer.Write(Encoding.UTF8.GetBytes("bext"));
            var codingHistory = Encoding.ASCII.GetBytes(bextChunkInfo.CodingHistory ?? "");
            var bextLength    = 602 + codingHistory.Length;

            if (bextLength % 2 != 0)
            {
                bextLength++;
            }
            writer.Write(bextLength); // bext size
            var bextStart = writer.BaseStream.Position;

            writer.Write(GetAsBytes(bextChunkInfo.Description, 256));
            writer.Write(GetAsBytes(bextChunkInfo.Originator, 32));
            writer.Write(GetAsBytes(bextChunkInfo.OriginatorReference, 32));
            writer.Write(GetAsBytes(bextChunkInfo.OriginationDate, 10));
            writer.Write(GetAsBytes(bextChunkInfo.OriginationTime, 8));
            writer.Write(bextChunkInfo.TimeReference); // 8 bytes long
            writer.Write(bextChunkInfo.Version);       // 2 bytes long
            writer.Write(GetAsBytes(bextChunkInfo.UniqueMaterialIdentifier, 64));
            writer.Write(bextChunkInfo.Reserved);      // for version 1 this is 190 bytes
            writer.Write(codingHistory);
            if (codingHistory.Length % 2 != 0)
            {
                writer.Write((byte)0);
            }
            Debug.Assert(writer.BaseStream.Position == bextStart + bextLength, "Invalid bext chunk size");

            // write the format chunk
            writer.Write(Encoding.UTF8.GetBytes("fmt "));
            format.Serialize(writer);

            writer.Write(Encoding.UTF8.GetBytes("data"));
            dataChunkSizePosition = writer.BaseStream.Position;
            writer.Write(-1); // will be overwritten unless this is RF64
            // now finally the data chunk
        }
All Usage Examples Of NAudio.Wave.WaveFormat::Serialize