using System;
using System.Collections.Generic;
using NAudio.Utils;
namespace NAudio.Wave.SampleProviders
{
///
/// Allows any number of inputs to be patched to outputs
/// Uses could include swapping left and right channels, turning mono into stereo,
/// feeding different input sources to different soundcard outputs etc
///
public class MultiplexingSampleProvider : ISampleProvider
{
private readonly IList inputs;
private readonly WaveFormat waveFormat;
private readonly int outputChannelCount;
private readonly int inputChannelCount;
private readonly List mappings;
///
/// Creates a multiplexing sample provider, allowing re-patching of input channels to different
/// output channels
///
/// Input sample providers. Must all be of the same sample rate, but can have any number of channels
/// Desired number of output channels.
public MultiplexingSampleProvider(IEnumerable inputs, int numberOfOutputChannels)
{
this.inputs = new List(inputs);
outputChannelCount = numberOfOutputChannels;
if (this.inputs.Count == 0)
{
throw new ArgumentException("You must provide at least one input");
}
if (numberOfOutputChannels < 1)
{
throw new ArgumentException("You must provide at least one output");
}
foreach (var input in this.inputs)
{
if (waveFormat == null)
{
if (input.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
{
throw new ArgumentException("Only 32 bit float is supported");
}
waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(input.WaveFormat.SampleRate, numberOfOutputChannels);
}
else
{
if (input.WaveFormat.BitsPerSample != waveFormat.BitsPerSample)
{
throw new ArgumentException("All inputs must have the same bit depth");
}
if (input.WaveFormat.SampleRate != waveFormat.SampleRate)
{
throw new ArgumentException("All inputs must have the same sample rate");
}
}
inputChannelCount += input.WaveFormat.Channels;
}
mappings = new List();
for (int n = 0; n < outputChannelCount; n++)
{
mappings.Add(n % inputChannelCount);
}
}
///
/// persistent temporary buffer to prevent creating work for garbage collector
///
private float[] inputBuffer;
///
/// Reads samples from this sample provider
///
/// Buffer to be filled with sample data
/// Offset into buffer to start writing to, usually 0
/// Number of samples required
/// Number of samples read
public int Read(float[] buffer, int offset, int count)
{
int sampleFramesRequested = count / outputChannelCount;
int inputOffset = 0;
int sampleFramesRead = 0;
// now we must read from all inputs, even if we don't need their data, so they stay in sync
foreach (var input in inputs)
{
int samplesRequired = sampleFramesRequested * input.WaveFormat.Channels;
inputBuffer = BufferHelpers.Ensure(inputBuffer, samplesRequired);
int samplesRead = input.Read(inputBuffer, 0, samplesRequired);
sampleFramesRead = Math.Max(sampleFramesRead, samplesRead / input.WaveFormat.Channels);
for (int n = 0; n < input.WaveFormat.Channels; n++)
{
int inputIndex = inputOffset + n;
for (int outputIndex = 0; outputIndex < outputChannelCount; outputIndex++)
{
if (mappings[outputIndex] == inputIndex)
{
int inputBufferOffset = n;
int outputBufferOffset = offset + outputIndex;
int sample = 0;
while (sample < sampleFramesRequested && inputBufferOffset < samplesRead)
{
buffer[outputBufferOffset] = inputBuffer[inputBufferOffset];
outputBufferOffset += outputChannelCount;
inputBufferOffset += input.WaveFormat.Channels;
sample++;
}
// clear the end
while (sample < sampleFramesRequested)
{
buffer[outputBufferOffset] = 0;
outputBufferOffset += outputChannelCount;
sample++;
}
}
}
}
inputOffset += input.WaveFormat.Channels;
}
return sampleFramesRead * outputChannelCount;
}
///
/// The output WaveFormat for this SampleProvider
///
public WaveFormat WaveFormat => waveFormat;
///
/// Connects a specified input channel to an output channel
///
/// Input Channel index (zero based). Must be less than InputChannelCount
/// Output Channel index (zero based). Must be less than OutputChannelCount
public void ConnectInputToOutput(int inputChannel, int outputChannel)
{
if (inputChannel < 0 || inputChannel >= InputChannelCount)
{
throw new ArgumentException("Invalid input channel");
}
if (outputChannel < 0 || outputChannel >= OutputChannelCount)
{
throw new ArgumentException("Invalid output channel");
}
mappings[outputChannel] = inputChannel;
}
///
/// The number of input channels. Note that this is not the same as the number of input wave providers. If you pass in
/// one stereo and one mono input provider, the number of input channels is three.
///
public int InputChannelCount => inputChannelCount;
///
/// The number of output channels, as specified in the constructor.
///
public int OutputChannelCount => outputChannelCount;
}
}