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, IDisposable, IAsyncDisposable
Inheritance
WaveFileWriter
Implements
Derived
Inherited Members

Constructors

WaveFileWriter(Stream, WaveFormat)

WaveFileWriter that actually 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(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

Properties

CanRead

Returns false: Cannot read from a WaveFileWriter

public override bool CanRead { get; }

Property Value

bool

CanSeek

Returns false: Cannot seek within a WaveFileWriter

public override bool CanSeek { get; }

Property Value

bool

CanWrite

Returns true: Can write to a WaveFileWriter

public override bool CanWrite { get; }

Property Value

bool

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

Gets the Position in the WaveFile (i.e. number of bytes written so far)

public override long Position { get; set; }

Property Value

long

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

CreateWaveFile(string, IWaveProvider)

Creates a Wave file by reading all the data from a WaveProvider BEWARE: the WaveProvider 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 sourceProvider)

Parameters

filename string

The filename to use

sourceProvider IWaveProvider

The source WaveProvider

CreateWaveFile16(string, ISampleProvider)

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

public static void CreateWaveFile16(string filename, ISampleProvider sourceProvider)

Parameters

filename string

The filename to write to

sourceProvider 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

~WaveFileWriter()

Finaliser - should only be called if the user forgot to close this WaveFileWriter

protected ~WaveFileWriter()

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)

Read is not supported for a WaveFileWriter

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

Parameters

buffer byte[]
offset int
count int

Returns

int

Seek(long, SeekOrigin)

Seek is not supported for a WaveFileWriter

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long
origin SeekOrigin

Returns

long

SetLength(long)

SetLength is not supported for WaveFileWriter

public override void SetLength(long value)

Parameters

value long

UpdateHeader(BinaryWriter)

Updates the header with file size information

protected virtual void UpdateHeader(BinaryWriter writer)

Parameters

writer BinaryWriter

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

WriteData(byte[], int, int)

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

[Obsolete("Use Write instead")]
public void WriteData(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

WriteData(short[], int, int)

Writes 16 bit samples to the Wave file

[Obsolete("Use WriteSamples instead")]
public void WriteData(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

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 a WaveProvider BEWARE: the WaveProvider 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 sourceProvider)

Parameters

outStream Stream

The stream the method will output to

sourceProvider IWaveProvider

The source WaveProvider