MVH/NAudio-2.2.1/NAudio.Extras/AutoDisposeFileReader.cs

38 lines
921 B
C#
Raw Normal View History

2024-06-07 00:47:07 +02:00
using NAudio.Wave;
using System;
namespace NAudio.Extras
{
/// <summary>
/// Used by AudioPlaybackEngine
/// </summary>
public class AutoDisposeFileReader : ISampleProvider
{
private readonly ISampleProvider reader;
private bool isDisposed;
public AutoDisposeFileReader(ISampleProvider reader)
{
this.reader = reader;
WaveFormat = reader.WaveFormat;
}
public int Read(float[] buffer, int offset, int count)
{
if (isDisposed)
return 0;
int read = reader.Read(buffer, offset, count);
if (read == 0)
{
if (reader is IDisposable d)
{
d.Dispose();
}
isDisposed = true;
}
return read;
}
public WaveFormat WaveFormat { get; }
}
}