Skip to content

Commit

Permalink
Phase-swappable allpass
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Feb 25, 2024
1 parent ef18189 commit e4c21c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Cavern/Filters/Allpass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Cavern.Filters {
/// <summary>
/// Simple first-order allpass filter.
/// </summary>
public class Allpass : BiquadFilter {
public class Allpass : PhaseSwappableBiquadFilter {
/// <summary>
/// Simple first-order allpass filter with maximum flatness and no additional gain.
/// </summary>
Expand Down Expand Up @@ -41,10 +41,17 @@ public Allpass(int sampleRate, double centerFreq, double q, double gain) : base(

/// <inheritdoc/>
protected override void Reset(float cosW0, float alpha, float divisor) {
a2 = (1 - alpha) * divisor;
b0 = (float)Math.Pow(10, gain * .05f) * a2;
if (phaseSwapped) {
divisor = 1 / (1 - alpha);
a2 = (1 + alpha) * divisor;
b0 = 1;
b2 = (float)Math.Pow(10, gain * .05f) * a2;
} else {
a2 = (1 - alpha) * divisor;
b0 = (float)Math.Pow(10, gain * .05f) * a2;
b2 = 1;
}
a1 = b1 = -2 * cosW0 * divisor;
b2 = 1;
}
}
}
32 changes: 32 additions & 0 deletions Cavern/Filters/PhaseSwappableBiquadFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Cavern.Filters {
/// <summary>
/// Simple first-order biquad filter with the option to invert the phase response.
/// </summary>
public abstract class PhaseSwappableBiquadFilter : BiquadFilter {
/// <summary>
/// Biquad filters usually achieve their effect by delaying lower frequencies. Phase swapping delays higher frequencies.
/// </summary>
public bool PhaseSwapped {
get => phaseSwapped;
set {
phaseSwapped = value;
Reset(CenterFreq, Q, Gain);
}
}

/// <summary>
/// State of phase swapping to be used when the filter is reset.
/// </summary>
protected bool phaseSwapped = false;

/// <inheritdoc/>
protected PhaseSwappableBiquadFilter(int sampleRate, double centerFreq) : base(sampleRate, centerFreq) { }

/// <inheritdoc/>
protected PhaseSwappableBiquadFilter(int sampleRate, double centerFreq, double q) : base(sampleRate, centerFreq, q) { }

/// <inheritdoc/>
protected PhaseSwappableBiquadFilter(int sampleRate, double centerFreq, double q, double gain) :
base(sampleRate, centerFreq, q, gain) { }
}
}

0 comments on commit e4c21c0

Please sign in to comment.