using System.Numerics;
namespace Sandbox.Audio;
///
/// Just a test - don't count on this sticking around
///
[Expose]
public sealed class LowPassProcessor : AudioProcessor
{
///
/// Cutoff frequency for the low-pass filter (normalized 0 to 1).
///
[Range( 0, 1 )]
public float Cutoff { get; set; } = 0.5f;
public class State : ListenerState
{
internal PerChannel PreviousSample;
}
///
/// Processes a single audio channel with a low-pass filter.
///
protected override unsafe void ProcessSingleChannel( AudioChannel channel, Span input )
{
float alpha = Cutoff; // Simple smoothing factor
float previous = CurrentState.PreviousSample.Get( channel );
int vectorSize = Vector.Count;
int i = 0;
if ( input.Length >= vectorSize )
{
var alphaVec = new Vector( alpha );
var prevVec = new Vector( previous );
for ( ; i <= input.Length - vectorSize; i += vectorSize )
{
var inputVec = new Vector( input.Slice( i, vectorSize ) );
prevVec = prevVec + alphaVec * (inputVec - prevVec);
prevVec.CopyTo( input.Slice( i, vectorSize ) );
}
previous = prevVec[vectorSize - 1]; // Store last processed value
}
// Process remaining elements
for ( ; i < input.Length; i++ )
{
previous = previous + alpha * (input[i] - previous);
input[i] = previous;
}
CurrentState.PreviousSample.Set( channel, previous );
}
}