// created on 27/12/2002 at 20:20
using System;
using System.IO;
// ReSharper disable once CheckNamespace
namespace NAudio.Wave
{
///
/// Base class for all WaveStream classes. Derives from stream.
///
public abstract class WaveStream : Stream, IWaveProvider
{
///
/// Retrieves the WaveFormat for this stream
///
public abstract WaveFormat WaveFormat { get; }
// base class includes long Position get; set
// base class includes long Length get
// base class includes Read
// base class includes Dispose
///
/// We can read from this stream
///
public override bool CanRead => true;
///
/// We can seek within this stream
///
public override bool CanSeek => true;
///
/// We can't write to this stream
///
public override bool CanWrite => false;
///
/// Flush does not need to do anything
/// See
///
public override void Flush() { }
///
/// An alternative way of repositioning.
/// See
///
public override long Seek(long offset, SeekOrigin origin)
{
if (origin == SeekOrigin.Begin)
Position = offset;
else if (origin == SeekOrigin.Current)
Position += offset;
else
Position = Length + offset;
return Position;
}
///
/// Sets the length of the WaveStream. Not Supported.
///
///
public override void SetLength(long length)
{
throw new NotSupportedException("Can't set length of a WaveFormatString");
}
///
/// Writes to the WaveStream. Not Supported.
///
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException("Can't write to a WaveFormatString");
}
///
/// 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 => WaveFormat.BlockAlign;
///
/// Moves forward or backwards the specified number of seconds in the stream
///
/// Number of seconds to move, can be negative
public void Skip(int seconds)
{
long newPosition = Position + WaveFormat.AverageBytesPerSecond*seconds;
if (newPosition > Length)
Position = Length;
else if (newPosition < 0)
Position = 0;
else
Position = newPosition;
}
///
/// The current position in the stream in Time format
///
public virtual TimeSpan CurrentTime
{
get
{
return TimeSpan.FromSeconds((double)Position / WaveFormat.AverageBytesPerSecond);
}
set
{
Position = (long) (value.TotalSeconds * WaveFormat.AverageBytesPerSecond);
}
}
///
/// Total length in real-time of the stream (may be an estimate for compressed files)
///
public virtual TimeSpan TotalTime
{
get
{
return TimeSpan.FromSeconds((double) Length / WaveFormat.AverageBytesPerSecond);
}
}
///
/// Whether the WaveStream has non-zero sample data at the current position for the
/// specified count
///
/// Number of bytes to read
public virtual bool HasData(int count)
{
return Position < Length;
}
}
}