Class WaveFormat
Represents a Wave file format
public class WaveFormat
- Inheritance
-
WaveFormat
- Derived
- Inherited Members
- Extension Methods
Remarks
Not an interop type. It once carried [StructLayout] and was marshalled directly as a
WAVEFORMATEX, but a class hierarchy cannot be marshalled correctly under Native AOT — the
inherited fields are dropped, so a subclass decodes from offset 0 with its base fields left at
their defaults. Use ToWaveFormatExBytes(), MarshalToPtr(WaveFormat) or
MarshalFromPtr(nint) to cross a native boundary.
See https://github.com/naudio/NAudio/issues/1432.
Constructors
WaveFormat()
Creates a new PCM 44.1Khz stereo 16 bit format
public WaveFormat()
WaveFormat(BinaryReader)
Reads a new WaveFormat object from a stream
public WaveFormat(BinaryReader br)
Parameters
brBinaryReaderA binary reader that wraps the stream
WaveFormat(int, int)
Creates a new 16 bit wave format with the specified sample rate and channel count
public WaveFormat(int sampleRate, int channels)
Parameters
WaveFormat(int, int, int)
Creates a new PCM format with the specified sample rate, bit depth and channels
public WaveFormat(int rate, int bits, int channels)
Parameters
Fields
averageBytesPerSecond
for buffer estimation
protected int averageBytesPerSecond
Field Value
bitsPerSample
number of bits per sample of mono data
protected short bitsPerSample
Field Value
blockAlign
block size of data
protected short blockAlign
Field Value
channels
number of channels
protected short channels
Field Value
extraSize
number of following bytes
protected short extraSize
Field Value
sampleRate
sample rate
protected int sampleRate
Field Value
waveFormatTag
format type
protected WaveFormatEncoding waveFormatTag
Field Value
Properties
AverageBytesPerSecond
Returns the average number of bytes used per second
public int AverageBytesPerSecond { get; }
Property Value
BitsPerSample
Returns the number of bits per sample (usually 16 or 32, sometimes 24 or 8) Can be 0 for some codecs
public int BitsPerSample { get; }
Property Value
BlockAlign
Returns the block alignment
public virtual int BlockAlign { get; }
Property Value
Channels
Returns the number of channels (1=mono,2=stereo etc)
public int Channels { get; }
Property Value
Encoding
Returns the encoding type used
public WaveFormatEncoding Encoding { get; }
Property Value
ExtraSize
Returns the number of extra bytes used by this waveformat. Often 0, except for compressed formats which store extra data after the WAVEFORMATEX header
public int ExtraSize { get; }
Property Value
SampleRate
Returns the sample rate (samples per second)
public int SampleRate { get; }
Property Value
Methods
ConvertLatencyToByteSize(int)
Gets the size of a wave buffer equivalent to the latency in milliseconds.
public int ConvertLatencyToByteSize(int milliseconds)
Parameters
millisecondsintThe milliseconds.
Returns
CreateALawFormat(int, int)
Creates an A-law wave format
public static WaveFormat CreateALawFormat(int sampleRate, int channels)
Parameters
Returns
- WaveFormat
Wave Format
CreateCustomFormat(WaveFormatEncoding, int, int, int, int, int)
Creates a WaveFormat with custom members
public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
Parameters
tagWaveFormatEncodingThe encoding
sampleRateintSample Rate
channelsintNumber of channels
averageBytesPerSecondintAverage Bytes Per Second
blockAlignintBlock Align
bitsPerSampleintBits Per Sample
Returns
CreateIeeeFloatWaveFormat(int, int)
Creates a new 32 bit IEEE floating point wave format
public static WaveFormat CreateIeeeFloatWaveFormat(int sampleRate, int channels)
Parameters
Returns
CreateMuLawFormat(int, int)
Creates a Mu-law wave format
public static WaveFormat CreateMuLawFormat(int sampleRate, int channels)
Parameters
Returns
- WaveFormat
Wave Format
Equals(object)
Compares with another WaveFormat object
public override bool Equals(object obj)
Parameters
objobjectObject to compare to
Returns
- bool
True if the objects are the same
FromFormatChunk(BinaryReader, int)
Reads in a WaveFormat (with extra data) from a fmt chunk (chunk identifier and length should already have been read)
public static WaveFormat FromFormatChunk(BinaryReader br, int formatChunkLength)
Parameters
brBinaryReaderBinary reader
formatChunkLengthintFormat chunk length
Returns
- WaveFormat
A WaveFormatExtraData
GetHashCode()
Provides a Hashcode for this WaveFormat
public override int GetHashCode()
Returns
- int
A hashcode
MarshalFromPtr(nint)
Helper function to retrieve a WaveFormat structure from a pointer
public static WaveFormat MarshalFromPtr(nint pointer)
Parameters
pointernintWaveFormat structure
Returns
MarshalToPtr(WaveFormat)
Helper function to marshal WaveFormat to an IntPtr
public static nint MarshalToPtr(WaveFormat format)
Parameters
formatWaveFormatWaveFormat
Returns
- nint
IntPtr to WaveFormat structure (needs to be freed by callee)
Serialize(BinaryWriter)
Writes this WaveFormat object to a stream
public virtual void Serialize(BinaryWriter writer)
Parameters
writerBinaryWriterthe output stream
ToString()
Reports this WaveFormat as a string
public override string ToString()
Returns
- string
String describing the wave format
ToWaveFormatExBytes()
Renders this WaveFormat as a native WAVEFORMATEX block — the fixed 18-byte header (cbSize always present) followed by cbSize bytes of format-specific extra data. Use this when writing into a buffer you already own; MarshalToPtr(WaveFormat) wraps it for the common case of needing a freshly allocated unmanaged block.
public byte[] ToWaveFormatExBytes()
Returns
- byte[]
The WAVEFORMATEX bytes, always at least 18 long.