using System;
using NAudio.Wave;
namespace NAudio.Extras
{
///
/// Loopable WaveStream
///
public class LoopStream : WaveStream
{
readonly WaveStream sourceStream;
///
/// Creates a new Loop stream
///
public LoopStream(WaveStream source)
{
sourceStream = source;
}
///
/// The WaveFormat of this stream
///
public override WaveFormat WaveFormat
{
get { return sourceStream.WaveFormat; }
}
///
/// Length in bytes of this stream (effectively infinite)
///
public override long Length
{
get { return long.MaxValue / 32; }
}
///
/// Position within this stream in bytes
///
public override long Position
{
get
{
return sourceStream.Position;
}
set
{
sourceStream.Position = value;
}
}
///
/// Always has data available
///
public override bool HasData(int count)
{
// infinite loop
return true;
}
///
/// Read data from this stream
///
public override int Read(byte[] buffer, int offset, int count)
{
int read = 0;
while (read < count)
{
int required = count - read;
int readThisTime = sourceStream.Read(buffer, offset + read, required);
if (readThisTime < required)
{
sourceStream.Position = 0;
}
if (sourceStream.Position >= sourceStream.Length)
{
sourceStream.Position = 0;
}
read += readThisTime;
}
return read;
}
///
/// Dispose this WaveStream (disposes the source)
///
protected override void Dispose(bool disposing)
{
sourceStream.Dispose();
base.Dispose(disposing);
}
}
}