1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-19 15:02:54 +08:00

Refactor Filter component to be more re-usable

This commit is contained in:
Jamie Taylor 2021-09-29 19:19:33 +09:00
parent 94e2dbd7e7
commit 2608d193a9
No known key found for this signature in database
GPG Key ID: 2ACFA8B6370B8C8C
2 changed files with 30 additions and 18 deletions

View File

@ -8,50 +8,62 @@ using osu.Framework.Graphics;
namespace osu.Game.Audio.Effects namespace osu.Game.Audio.Effects
{ {
public class LowPassFilter : Component public class Filter : Component
{ {
private const float filter_cutoff_start = 2000; public BQFType FilterType = BQFType.LowPass;
private const float filter_cutoff_end = 150; public float SweepCutoffStart = 2000;
private const float filter_sweep_duration = 100; public float SweepCutoffEnd = 150;
private readonly Bindable<float> filterFreq = new Bindable<float>(filter_cutoff_start); public float SweepDuration = 100;
public Easing SweepEasing = Easing.None;
public bool IsActive { get; private set; }
private readonly Bindable<float> filterFreq = new Bindable<float>();
private readonly AudioMixer mixer; private readonly AudioMixer mixer;
private readonly BQFParameters filter; private BQFParameters filter;
/// <summary> /// <summary>
/// A toggle-able low-pass filter with a subtle filter-sweep effect when toggled that can be attached to an <see cref="AudioMixer"/>. /// A BiQuad filter that performs a filter-sweep when toggled on or off.
/// </summary> /// </summary>
public LowPassFilter(AudioMixer mixer) /// <param name="mixer">The mixer this effect should be attached to.</param>
public Filter(AudioMixer mixer)
{ {
this.mixer = mixer; this.mixer = mixer;
filter = new BQFParameters
{
lFilter = BQFType.LowPass,
fCenter = filterFreq.Value
};
} }
public void Enable() public void Enable()
{ {
attachFilter(); attachFilter();
this.TransformBindableTo(filterFreq, filter_cutoff_end, filter_sweep_duration); this.TransformBindableTo(filterFreq, SweepCutoffEnd, SweepDuration, SweepEasing);
} }
public void Disable() public void Disable()
{ {
this.TransformBindableTo(filterFreq, filter_cutoff_start, filter_sweep_duration) this.TransformBindableTo(filterFreq, SweepCutoffStart, SweepDuration, SweepEasing).OnComplete(_ => detatchFilter());
.OnComplete(_ => detatchFilter());
} }
private void attachFilter() private void attachFilter()
{ {
if (IsActive) return;
filter = new BQFParameters
{
lFilter = FilterType,
fCenter = filterFreq.Value = SweepCutoffStart
};
mixer.Effects.Add(filter); mixer.Effects.Add(filter);
filterFreq.ValueChanged += updateFilter; filterFreq.ValueChanged += updateFilter;
IsActive = true;
} }
private void detatchFilter() private void detatchFilter()
{ {
if (!IsActive) return;
filterFreq.ValueChanged -= updateFilter; filterFreq.ValueChanged -= updateFilter;
mixer.Effects.Remove(filter); mixer.Effects.Remove(filter);
IsActive = false;
} }
private void updateFilter(ValueChangedEvent<float> cutoff) private void updateFilter(ValueChangedEvent<float> cutoff)

View File

@ -21,7 +21,7 @@ namespace osu.Game.Overlays
protected override string PopInSampleName => "UI/dialog-pop-in"; protected override string PopInSampleName => "UI/dialog-pop-in";
protected override string PopOutSampleName => "UI/dialog-pop-out"; protected override string PopOutSampleName => "UI/dialog-pop-out";
private LowPassFilter filter; private Filter filter;
public PopupDialog CurrentDialog { get; private set; } public PopupDialog CurrentDialog { get; private set; }
@ -42,7 +42,7 @@ namespace osu.Game.Overlays
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(AudioManager audio) private void load(AudioManager audio)
{ {
AddInternal(filter = new LowPassFilter(audio.TrackMixer)); AddInternal(filter = new Filter(audio.TrackMixer));
} }
public void Push(PopupDialog dialog) public void Push(PopupDialog dialog)