using System;
using System.IO;
// ReSharper disable once CheckNamespace
namespace NAudio.Wave
{
///
/// WaveStream that simply passes on data from its source stream
/// (e.g. a MemoryStream)
///
public class RawSourceWaveStream : WaveStream
{
private readonly Stream sourceStream;
private readonly WaveFormat waveFormat;
///
/// Initialises a new instance of RawSourceWaveStream
///
/// The source stream containing raw audio
/// The waveformat of the audio in the source stream
public RawSourceWaveStream(Stream sourceStream, WaveFormat waveFormat)
{
this.sourceStream = sourceStream;
this.waveFormat = waveFormat;
}
///
/// Initialises a new instance of RawSourceWaveStream
///
/// The buffer containing raw audio
/// Offset in the source buffer to read from
/// Number of bytes to read in the buffer
/// The waveformat of the audio in the source stream
public RawSourceWaveStream(byte[] byteStream, int offset, int count, WaveFormat waveFormat)
{
sourceStream = new MemoryStream(byteStream, offset, count);
this.waveFormat = waveFormat;
}
///
/// The WaveFormat of this stream
///
public override WaveFormat WaveFormat => waveFormat;
///
/// The length in bytes of this stream (if supported)
///
public override long Length => sourceStream.Length;
///
/// The current position in this stream
///
public override long Position
{
get
{
return sourceStream.Position;
}
set
{
sourceStream.Position = value - (value % waveFormat.BlockAlign);
}
}
///
/// Reads data from the stream
///
public override int Read(byte[] buffer, int offset, int count)
{
try
{
return sourceStream.Read(buffer, offset, count);
}
catch (EndOfStreamException)
{
return 0;
}
}
}
}