using System;
using System.Collections.Generic;
using System.Linq;
namespace NAudio.Wave.SampleProviders
{
///
/// Sample Provider to concatenate multiple sample providers together
///
public class ConcatenatingSampleProvider : ISampleProvider
{
private readonly ISampleProvider[] providers;
private int currentProviderIndex;
///
/// Creates a new ConcatenatingSampleProvider
///
/// The source providers to play one after the other. Must all share the same sample rate and channel count
public ConcatenatingSampleProvider(IEnumerable providers)
{
if (providers == null) throw new ArgumentNullException(nameof(providers));
this.providers = providers.ToArray();
if (this.providers.Length == 0) throw new ArgumentException("Must provide at least one input", nameof(providers));
if (this.providers.Any(p => p.WaveFormat.Channels != WaveFormat.Channels)) throw new ArgumentException("All inputs must have the same channel count", nameof(providers));
if (this.providers.Any(p => p.WaveFormat.SampleRate != WaveFormat.SampleRate)) throw new ArgumentException("All inputs must have the same sample rate", nameof(providers));
}
///
/// The WaveFormat of this Sample Provider
///
public WaveFormat WaveFormat => providers[0].WaveFormat;
///
/// Read Samples from this sample provider
///
public int Read(float[] buffer, int offset, int count)
{
var read = 0;
while (read < count && currentProviderIndex < providers.Length)
{
var needed = count - read;
var readThisTime = providers[currentProviderIndex].Read(buffer, read, needed);
read += readThisTime;
if (readThisTime == 0) currentProviderIndex++;
}
return read;
}
}
}