Table of Contents

Class WaveFileWriter

Namespace
NAudio.Wave
Assembly
NAudio.Core.dll

This class writes WAV data to a .wav file on disk

public class WaveFileWriter : Stream, IAsyncDisposable, IDisposable
Inheritance
WaveFileWriter
Implements
Inherited Members
Extension Methods

Constructors

WaveFileWriter(Stream, WaveFormat)

Creates a WaveFileWriter that writes to a stream.

public WaveFileWriter(Stream outStream, WaveFormat format)

Parameters

outStream Stream

Stream to be written to

format WaveFormat

Wave format to use

WaveFileWriter(Stream, WaveFormat, WaveFileWriterOptions)

Creates a WaveFileWriter that writes to a stream with the given configuration.

public WaveFileWriter(Stream outStream, WaveFormat format, WaveFileWriterOptions options)

Parameters

outStream Stream

Stream to be written to

format WaveFormat

Wave format to use

options WaveFileWriterOptions

Writer configuration; null uses defaults.

Remarks

The supplied stream is not owned by the writer: disposing the writer finalizes the WAV header and flushes the stream, but leaves it open for the caller to dispose. Use the filename constructor if you want the writer to own and close the underlying file.

WaveFileWriter(string, WaveFormat)

Creates a new WaveFileWriter

public WaveFileWriter(string filename, WaveFormat format)

Parameters

filename string

The filename to write to

format WaveFormat

The Wave Format of the output data

WaveFileWriter(string, WaveFormat, WaveFileWriterOptions)

Creates a new WaveFileWriter with the given configuration.

public WaveFileWriter(string filename, WaveFormat format, WaveFileWriterOptions options)

Parameters

filename string

The filename to write to

format WaveFormat

The Wave Format of the output data

options WaveFileWriterOptions

Writer configuration; null uses defaults.

Properties

CanRead

When overridden in a derived class, gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

true if the stream supports reading; otherwise, false.

CanSeek

When overridden in a derived class, gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

true if the stream supports seeking; otherwise, false.

CanWrite

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

true if the stream supports writing; otherwise, false.

Filename

The wave file name or null if not applicable

public string Filename { get; }

Property Value

string

Length

Number of bytes of audio in the data chunk

public override long Length { get; }

Property Value

long

Position

When overridden in a derived class, gets or sets the position within the current stream.

public override long Position { get; set; }

Property Value

long

The current position within the stream.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking.

ObjectDisposedException

Methods were called after the stream was closed.

TotalTime

Total time (calculated from Length and average bytes per second)

public TimeSpan TotalTime { get; }

Property Value

TimeSpan

WaveFormat

WaveFormat of this wave file

public WaveFormat WaveFormat { get; }

Property Value

WaveFormat

Methods

AddChunk(IWaveChunkWriter)

Adds a chunk via an IWaveChunkWriter implementation. The writer's Position decides placement.

public void AddChunk(IWaveChunkWriter chunk)

Parameters

chunk IWaveChunkWriter

AddChunk(string, byte[], ChunkPosition)

Adds a raw RIFF chunk to be written. Before-data chunks must be added before any audio is written; after-data chunks are buffered until the writer is closed.

public void AddChunk(string chunkId, byte[] data, ChunkPosition position)

Parameters

chunkId string

Four-character chunk identifier (e.g. "bext").

data byte[]

Chunk payload. Word-alignment padding is handled by the writer.

position ChunkPosition

Where in the file the chunk should be placed.

AddCue(int, string)

Adds a cue point with a label. The cues are written as a pair of cue and LIST/adtl chunks after the data chunk at close time.

public void AddCue(int position, string label)

Parameters

position int

Sample position of the cue point.

label string

Text label (stored as UTF-8).

AddCue(int, string, int)

Adds a cue point with a label and a region length in samples. An ltxt sub-chunk is emitted alongside the label so the cue reads back as a region rather than a point marker.

public void AddCue(int position, string label, int length)

Parameters

position int

Sample position of the cue point.

label string

Text label (stored as UTF-8).

length int

Length in samples of the region starting at position.

CreateWaveFile(string, IWaveProvider)

Creates a Wave file by reading all the data from an IWaveProvider. BEWARE: the source MUST return 0 from its Read method when it is finished, or the Wave File will grow indefinitely.

public static void CreateWaveFile(string filename, IWaveProvider source)

Parameters

filename string

The filename to use

source IWaveProvider

The source audio

CreateWaveFile16(string, ISampleProvider)

Creates a 16 bit Wave File from an ISampleProvider. BEWARE: the source must not return data indefinitely.

public static void CreateWaveFile16(string filename, ISampleProvider source)

Parameters

filename string

The filename to write to

source ISampleProvider

The source sample provider

Dispose(bool)

Actually performs the close, making sure the header contains the correct data

protected override void Dispose(bool disposing)

Parameters

disposing bool

True if called from Dispose()

Flush()

Ensures data is written to disk Also updates header, so that WAV file will be valid up to the point currently written

public override void Flush()

Read(byte[], int, int)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if count is 0 or the end of the stream has been reached.

Exceptions

ArgumentException

The sum of offset and count is larger than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

Seek(long, SeekOrigin)

When overridden in a derived class, sets the position within the current stream.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

A byte offset relative to the origin parameter.

origin SeekOrigin

A value of type SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

The new position within the current stream.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the stream was closed.

SetLength(long)

When overridden in a derived class, sets the length of the current stream.

public override void SetLength(long value)

Parameters

value long

The desired length of the current stream in bytes.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the stream was closed.

Write(byte[], int, int)

Appends bytes to the WaveFile (assumes they are already in the correct format)

public override void Write(byte[] data, int offset, int count)

Parameters

data byte[]

the buffer containing the wave data

offset int

the offset from which to start writing

count int

the number of bytes to write

Write(ReadOnlySpan<byte>)

Appends bytes to the WaveFile from a span (assumes they are already in the correct format)

public override void Write(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

the span containing the wave data

WriteSample(float)

Writes a single sample to the Wave file

public void WriteSample(float sample)

Parameters

sample float

the sample to write (assumed floating point with 1.0f as max value)

WriteSamples(short[], int, int)

Writes 16 bit samples to the Wave file

public void WriteSamples(short[] samples, int offset, int count)

Parameters

samples short[]

The buffer containing the 16 bit samples

offset int

The offset from which to start writing

count int

The number of 16 bit samples to write

WriteSamples(float[], int, int)

Writes 32 bit floating point samples to the Wave file They will be converted to the appropriate bit depth depending on the WaveFormat of the WAV file

public void WriteSamples(float[] samples, int offset, int count)

Parameters

samples float[]

The buffer containing the floating point samples

offset int

The offset from which to start writing

count int

The number of floating point samples to write

WriteWavFileToStream(Stream, IWaveProvider)

Writes to a stream by reading all the data from an IWaveProvider. BEWARE: the source MUST return 0 from its Read method when it is finished, or the Wave File will grow indefinitely.

public static void WriteWavFileToStream(Stream outStream, IWaveProvider source)

Parameters

outStream Stream

The stream the method will output to

source IWaveProvider

The source audio