using System;
using NAudio.Utils;
namespace NAudio.Wave.SampleProviders
{
///
/// Converts a mono sample provider to stereo, with a customisable pan strategy
///
public class PanningSampleProvider : ISampleProvider
{
private readonly ISampleProvider source;
private float pan;
private float leftMultiplier;
private float rightMultiplier;
private readonly WaveFormat waveFormat;
private float[] sourceBuffer;
private IPanStrategy panStrategy;
///
/// Initialises a new instance of the PanningSampleProvider
///
/// Source sample provider, must be mono
public PanningSampleProvider(ISampleProvider source)
{
if (source.WaveFormat.Channels != 1)
{
throw new ArgumentException("Source sample provider must be mono");
}
this.source = source;
waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(source.WaveFormat.SampleRate, 2);
panStrategy = new SinPanStrategy();
}
///
/// Pan value, must be between -1 (left) and 1 (right)
///
public float Pan
{
get
{
return pan;
}
set
{
if (value < -1.0f || value > 1.0f)
{
throw new ArgumentOutOfRangeException(nameof(value), "Pan must be in the range -1 to 1");
}
pan = value;
UpdateMultipliers();
}
}
///
/// The pan strategy currently in use
///
public IPanStrategy PanStrategy
{
get
{
return panStrategy;
}
set
{
panStrategy = value;
UpdateMultipliers();
}
}
private void UpdateMultipliers()
{
var multipliers = panStrategy.GetMultipliers(Pan);
leftMultiplier = multipliers.Left;
rightMultiplier = multipliers.Right;
}
///
/// The WaveFormat of this sample provider
///
public WaveFormat WaveFormat => 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 count)
{
int sourceSamplesRequired = count / 2;
sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceSamplesRequired);
int sourceSamplesRead = source.Read(sourceBuffer, 0, sourceSamplesRequired);
int outIndex = offset;
for (int n = 0; n < sourceSamplesRead; n++)
{
buffer[outIndex++] = leftMultiplier * sourceBuffer[n];
buffer[outIndex++] = rightMultiplier * sourceBuffer[n];
}
return sourceSamplesRead * 2;
}
}
///
/// Pair of floating point values, representing samples or multipliers
///
public struct StereoSamplePair
{
///
/// Left value
///
public float Left { get; set; }
///
/// Right value
///
public float Right { get; set; }
}
///
/// Required Interface for a Panning Strategy
///
public interface IPanStrategy
{
///
/// Gets the left and right multipliers for a given pan value
///
/// Pan value from -1 to 1
/// Left and right multipliers in a stereo sample pair
StereoSamplePair GetMultipliers(float pan);
}
///
/// Simplistic "balance" control - treating the mono input as if it was stereo
/// In the centre, both channels full volume. Opposite channel decays linearly
/// as balance is turned to to one side
///
public class StereoBalanceStrategy : IPanStrategy
{
///
/// Gets the left and right channel multipliers for this pan value
///
/// Pan value, between -1 and 1
/// Left and right multipliers
public StereoSamplePair GetMultipliers(float pan)
{
float leftChannel = (pan <= 0) ? 1.0f : ((1 - pan) / 2.0f);
float rightChannel = (pan >= 0) ? 1.0f : ((pan + 1) / 2.0f);
// Console.WriteLine(pan + ": " + leftChannel + "," + rightChannel);
return new StereoSamplePair() { Left = leftChannel, Right = rightChannel };
}
}
///
/// Square Root Pan, thanks to Yuval Naveh
///
public class SquareRootPanStrategy : IPanStrategy
{
///
/// Gets the left and right channel multipliers for this pan value
///
/// Pan value, between -1 and 1
/// Left and right multipliers
public StereoSamplePair GetMultipliers(float pan)
{
// -1..+1 -> 1..0
float normPan = (-pan + 1) / 2;
float leftChannel = (float)Math.Sqrt(normPan);
float rightChannel = (float)Math.Sqrt(1 - normPan);
// Console.WriteLine(pan + ": " + leftChannel + "," + rightChannel);
return new StereoSamplePair() { Left = leftChannel, Right = rightChannel };
}
}
///
/// Sinus Pan, thanks to Yuval Naveh
///
public class SinPanStrategy : IPanStrategy
{
private const float HalfPi = (float)Math.PI / 2;
///
/// Gets the left and right channel multipliers for this pan value
///
/// Pan value, between -1 and 1
/// Left and right multipliers
public StereoSamplePair GetMultipliers(float pan)
{
// -1..+1 -> 1..0
float normPan = (-pan + 1) / 2;
float leftChannel = (float)Math.Sin(normPan * HalfPi);
float rightChannel = (float)Math.Cos(normPan * HalfPi);
// Console.WriteLine(pan + ": " + leftChannel + "," + rightChannel);
return new StereoSamplePair() { Left = leftChannel, Right = rightChannel };
}
}
///
/// Linear Pan
///
public class LinearPanStrategy : IPanStrategy
{
///
/// Gets the left and right channel multipliers for this pan value
///
/// Pan value, between -1 and 1
/// Left and right multipliers
public StereoSamplePair GetMultipliers(float pan)
{
// -1..+1 -> 1..0
float normPan = (-pan + 1) / 2;
float leftChannel = normPan;
float rightChannel = 1 - normPan;
return new StereoSamplePair() { Left = leftChannel, Right = rightChannel };
}
}
}