Class WaveStream
Base class for all WaveStream classes. Derives from stream.
public abstract class WaveStream : Stream, IAsyncDisposable, IDisposable, IWaveProvider
- Inheritance
-
WaveStream
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
Implementing a WaveStream — which Read method should I override?
Read(byte[], int, int) is abstract on Stream and must be overridden. Read(Span<byte>) is virtual — its default implementation rents a buffer from Shared, calls the byte[] overload, and copies into the span. This is functionally correct but incurs one pool rent and one extra copy per read.
For best performance, implement your read logic in the Read(Span<byte>) overload and
make the byte[] overload forward to it:
public override int Read(Span<byte> buffer) { /* real read logic */ }
public override int Read(byte[] array, int offset, int count)
=> Read(array.AsSpan(offset, count));
All of NAudio's built-in readers follow this pattern. Legacy subclasses that only override the byte[] overload continue to work correctly, but pay the pooled-bridge cost on span-based reads.
Properties
BlockAlign
The block alignment for this wavestream. Do not modify the Position to anything that is not a whole multiple of this value
public virtual int BlockAlign { get; }
Property Value
CanRead
We can read from this stream
public override bool CanRead { get; }
Property Value
CanSeek
We can seek within this stream
public override bool CanSeek { get; }
Property Value
CanWrite
We can't write to this stream
public override bool CanWrite { get; }
Property Value
CurrentTime
The current position in the stream in Time format. On set, the resulting byte position is rounded down to a multiple of BlockAlign so the stream stays on a valid block boundary.
public virtual TimeSpan CurrentTime { get; set; }
Property Value
TotalTime
Total length in real-time of the stream (may be an estimate for compressed files)
public virtual TimeSpan TotalTime { get; }
Property Value
WaveFormat
Retrieves the WaveFormat for this stream
public abstract WaveFormat WaveFormat { get; }
Property Value
Methods
Flush()
Flush does not need to do anything See Flush()
public override void Flush()
HasData(int)
Whether the WaveStream has non-zero sample data at the current position for the specified count
public virtual bool HasData(int count)
Parameters
countintNumber of bytes to read
Returns
Seek(long, SeekOrigin)
An alternative way of repositioning. See Seek(long, SeekOrigin)
public override long Seek(long offset, SeekOrigin origin)
Parameters
offsetlongoriginSeekOrigin
Returns
SetLength(long)
Sets the length of the WaveStream. Not Supported.
public override void SetLength(long length)
Parameters
lengthlong
Skip(int)
Moves forward or backwards the specified number of seconds in the stream
public void Skip(int seconds)
Parameters
secondsintNumber of seconds to move, can be negative
Write(byte[], int, int)
Writes to the WaveStream. Not Supported.
public override void Write(byte[] buffer, int offset, int count)