Skip to content

Commit

Permalink
Improve the filters by storing them persistently instead of constantl…
Browse files Browse the repository at this point in the history
…y creating new ones
  • Loading branch information
Nytra committed Nov 25, 2024
1 parent 9052f1c commit eeedcd6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
30 changes: 26 additions & 4 deletions ProjectObsidian/Components/Audio/BandPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class BandPassFilter : Component, IAudioSource, IWorldElement

private double lastTime;

private object lowFilter = null;
private object highFilter = null;

public bool IsActive
{
get => Source.Target != null && Source.Target.IsActive;
Expand All @@ -40,13 +43,22 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
tempBuffer = buffer;
Source.Target.Read(tempBuffer);

var LowPass = new ButterworthFilter.FilterButterworth<S>(HighFrequency, (int)(tempBuffer.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), ButterworthFilter.FilterButterworth<S>.PassType.Lowpass, Resonance);
var HighPass = new ButterworthFilter.FilterButterworth<S>(LowFrequency, (int)(tempBuffer.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), ButterworthFilter.FilterButterworth<S>.PassType.Highpass, Resonance);
if (lowFilter == null)
{
lowFilter = new ButterworthFilter.FilterButterworth<S>();
}
if (highFilter == null)
{
highFilter = new ButterworthFilter.FilterButterworth<S>();
}

((ButterworthFilter.FilterButterworth<S>)lowFilter).UpdateCoefficients(HighFrequency, (int)(tempBuffer.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), ButterworthFilter.FilterButterworth<S>.PassType.Lowpass, Resonance);
((ButterworthFilter.FilterButterworth<S>)highFilter).UpdateCoefficients(LowFrequency, (int)(tempBuffer.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), ButterworthFilter.FilterButterworth<S>.PassType.Highpass, Resonance);

for (int i = 0; i < tempBuffer.Length; i++)
{
LowPass.Update(ref tempBuffer[i]);
HighPass.Update(ref tempBuffer[i]);
((ButterworthFilter.FilterButterworth<S>)lowFilter).Update(ref tempBuffer[i]);
((ButterworthFilter.FilterButterworth<S>)highFilter).Update(ref tempBuffer[i]);
}

lastTime = Engine.Current.AudioSystem.DSPTime;
Expand All @@ -60,5 +72,15 @@ protected override void OnAwake()
HighFrequency.Value = 20000f;
lastTime = Engine.Current.AudioSystem.DSPTime;
}

protected override void OnChanges()
{
base.OnChanges();
if (Source.GetWasChangedAndClear())
{
lowFilter = null;
highFilter = null;
}
}
}
}
29 changes: 20 additions & 9 deletions ProjectObsidian/Components/Audio/ButterworthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ButterworthFilter : Component, IAudioSource, IWorldElement

private double lastTime;

private object filter = null;

public bool IsActive
{
get
Expand All @@ -35,6 +37,15 @@ protected override void OnAwake()
Resonance.Value = 1.41f;
}

protected override void OnChanges()
{
base.OnChanges();
if (Source.GetWasChangedAndClear())
{
filter = null;
}
}

public int ChannelCount => Source.Target?.ChannelCount ?? 0;

public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
Expand All @@ -50,11 +61,16 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>

Source.Target.Read(span);

var filter = new FilterButterworth<S>(Frequency, (int)(span.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), LowPass ? FilterButterworth<S>.PassType.Lowpass : FilterButterworth<S>.PassType.Highpass, Resonance);
if (filter == null)
{
filter = new FilterButterworth<S>();
}

((FilterButterworth<S>)filter).UpdateCoefficients(Frequency, (int)(span.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), LowPass ? FilterButterworth<S>.PassType.Lowpass : FilterButterworth<S>.PassType.Highpass, Resonance);

for (int i = 0; i < span.Length; i++)
{
filter.Update(ref span[i]);
((FilterButterworth<S>)filter).Update(ref span[i]);
}

lastTime = Engine.Current.AudioSystem.DSPTime;
Expand All @@ -71,7 +87,7 @@ public class FilterButterworth<S> where S: unmanaged, IAudioSample<S>
private readonly int sampleRate;
private readonly PassType passType;

private readonly float c, a1, a2, a3, b1, b2;
private float c, a1, a2, a3, b1, b2;

/// <summary>
/// Array of input values, latest are in front
Expand All @@ -83,13 +99,8 @@ public class FilterButterworth<S> where S: unmanaged, IAudioSample<S>
/// </summary>
private S[] outputHistory = new S[3];

public FilterButterworth(float frequency, int sampleRate, PassType passType, float resonance)
public void UpdateCoefficients(float frequency, int sampleRate, PassType passType, float resonance)
{
this.resonance = resonance;
this.frequency = frequency;
this.sampleRate = sampleRate;
this.passType = passType;

switch (passType)
{
case PassType.Lowpass:
Expand Down

0 comments on commit eeedcd6

Please sign in to comment.