using System; namespace NAudio.Wave.SampleProviders { /// /// Simple class that raises an event on every sample /// public class NotifyingSampleProvider : ISampleProvider, ISampleNotifier { private readonly ISampleProvider source; // try not to give the garbage collector anything to deal with when playing live audio private readonly SampleEventArgs sampleArgs = new SampleEventArgs(0, 0); private readonly int channels; /// /// Initializes a new instance of NotifyingSampleProvider /// /// Source Sample Provider public NotifyingSampleProvider(ISampleProvider source) { this.source = source; channels = WaveFormat.Channels; } /// /// WaveFormat /// public WaveFormat WaveFormat => source.WaveFormat; /// /// Reads samples from this sample provider /// /// Sample buffer /// Offset into sample buffer /// Number of samples desired /// Number of samples read public int Read(float[] buffer, int offset, int sampleCount) { int samplesRead = source.Read(buffer, offset, sampleCount); if (Sample != null) { for (int n = 0; n < samplesRead; n += channels) { sampleArgs.Left = buffer[offset + n]; sampleArgs.Right = channels > 1 ? buffer[offset + n + 1] : sampleArgs.Left; Sample(this, sampleArgs); } } return samplesRead; } /// /// Sample notifier /// public event EventHandler Sample; } }