Code/FX/Vibrato.cs
using System;
using Sandbox;
namespace SFXR;
[Title( "Vibrato" )]
[Category( "SFXR Effects" )]
[Icon( "waves" )]
public class SFXRVibrato : SFXREffect
{
/// <summary>
/// Amount by which the frequency is changed
/// (Default: 0, Range: 0 - 1)
/// </summary>
[Property, Range( 0f, 1f, 0.01f )] public float Depth { get; set; } = 0.3f;
/// <summary>
/// Frequency of the vibrato in Hz
/// (Default: 0, Range: 0 - 100)
/// </summary>
[Property, Range( 0f, 100f )]
public float Speed { get; set; } = 40;
public override short[] Apply( short[] samples, SFXRComponent sound )
{
if ( Depth == 0 || Speed == 0 )
{
return samples;
}
short[] outputSamples = new short[samples.Length];
double oscillatorPosition = 0;
double oscillatorIncrement = 2 * Math.PI * Speed / (double)sound.SampleRate;
for ( int i = 0; i < samples.Length; i++ )
{
oscillatorPosition += oscillatorIncrement;
if ( oscillatorPosition > 2 * Math.PI )
{
oscillatorPosition -= 2 * Math.PI;
}
double vibrato = Math.Sin( oscillatorPosition ) * Depth;
outputSamples[i] = (short)(samples[i] * (1 + (float)vibrato));
}
return outputSamples;
}
}