using NAudio.Wave; namespace testexetrisathlon.SoundManagement { public class LoopStream : WaveStream { private readonly WaveStream _sourceStream; public LoopStream(WaveStream sourceStream) { _sourceStream = sourceStream; EnableLooping = true; } private bool EnableLooping { get; } public override WaveFormat WaveFormat => _sourceStream.WaveFormat; public override long Length => _sourceStream.Length; public override long Position { get => _sourceStream.Position; set => _sourceStream.Position = value; } public override int Read(byte[] buffer, int offset, int count) { int totalBytesRead = 0; while (totalBytesRead < count) { int bytesRead = _sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead); if (bytesRead == 0) { if (_sourceStream.Position == 0 || !EnableLooping) break; _sourceStream.Position = 0; } totalBytesRead += bytesRead; } return totalBytesRead; } public new void Dispose() { base.Dispose(); _sourceStream.Dispose(); } } }