Class WaveFileWriter
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
outStreamStreamStream to be written to
formatWaveFormatWave 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
outStreamStreamStream to be written to
formatWaveFormatWave format to use
optionsWaveFileWriterOptionsWriter configuration;
nulluses 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
filenamestringThe filename to write to
formatWaveFormatThe 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
filenamestringThe filename to write to
formatWaveFormatThe Wave Format of the output data
optionsWaveFileWriterOptionsWriter configuration;
nulluses 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
CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; }
Property Value
CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writing.
public override bool CanWrite { get; }
Property Value
Filename
The wave file name or null if not applicable
public string Filename { get; }
Property Value
Length
Number of bytes of audio in the data chunk
public override long Length { get; }
Property Value
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
WaveFormat
WaveFormat of this wave file
public WaveFormat WaveFormat { get; }
Property Value
Methods
AddChunk(IWaveChunkWriter)
Adds a chunk via an IWaveChunkWriter implementation. The writer's Position decides placement.
public void AddChunk(IWaveChunkWriter chunk)
Parameters
chunkIWaveChunkWriter
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
chunkIdstringFour-character chunk identifier (e.g.
"bext").databyte[]Chunk payload. Word-alignment padding is handled by the writer.
positionChunkPositionWhere 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
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
positionintSample position of the cue point.
labelstringText label (stored as UTF-8).
lengthintLength 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
filenamestringThe filename to use
sourceIWaveProviderThe 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
filenamestringThe filename to write to
sourceISampleProviderThe source sample provider
Dispose(bool)
Actually performs the close, making sure the header contains the correct data
protected override void Dispose(bool disposing)
Parameters
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
bufferbyte[]An array of bytes. When this method returns, the buffer contains the specified byte array with the values between
offsetand (offset+count- 1) replaced by the bytes read from the current source.offsetintThe zero-based byte offset in
bufferat which to begin storing the data read from the current stream.countintThe 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
countis 0 or the end of the stream has been reached.
Exceptions
- ArgumentException
The sum of
offsetandcountis larger than the buffer length.- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorcountis 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
offsetlongA byte offset relative to the
originparameter.originSeekOriginA 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
valuelongThe 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
databyte[]the buffer containing the wave data
offsetintthe offset from which to start writing
countintthe 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
dataReadOnlySpan<byte>the span containing the wave data
WriteSample(float)
Writes a single sample to the Wave file
public void WriteSample(float sample)
Parameters
samplefloatthe 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
samplesshort[]The buffer containing the 16 bit samples
offsetintThe offset from which to start writing
countintThe 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
samplesfloat[]The buffer containing the floating point samples
offsetintThe offset from which to start writing
countintThe 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
outStreamStreamThe stream the method will output to
sourceIWaveProviderThe source audio