1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 05:02:56 +08:00

Apply a ducking effect to the currently playing track when switching ruleset

This commit is contained in:
Jamie Taylor 2024-06-22 01:36:30 +09:00
parent 8bbbedaec3
commit a56751511e
No known key found for this signature in database
GPG Key ID: 2ACFA8B6370B8C8C
3 changed files with 87 additions and 13 deletions

View File

@ -16,6 +16,7 @@ using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Game.Audio.Effects;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Rulesets.Mods;
@ -63,6 +64,16 @@ namespace osu.Game.Overlays
[Resolved]
private RealmAccess realm { get; set; }
private AudioFilter audioDuckFilter;
private readonly BindableDouble audioDuckVolume = new BindableDouble(1);
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
AddInternal(audioDuckFilter = new AudioFilter(audio.TrackMixer));
audio.Tracks.AddAdjustment(AdjustableProperty.Volume, audioDuckVolume);
}
protected override void LoadComplete()
{
base.LoadComplete();
@ -243,6 +254,52 @@ namespace osu.Game.Overlays
onSuccess?.Invoke();
});
/// <summary>
/// Attenuates the volume and/or filters the currently playing track.
/// </summary>
/// <param name="duration">Duration of the ducking transition, in ms.</param>
/// <param name="duckVolumeTo">Level to drop volume to (1.0 = 100%).</param>
/// <param name="duckCutoffTo">Cutoff frequency to drop `AudioFilter` to. Use `AudioFilter.MAX_LOWPASS_CUTOFF` to skip filter effect.</param>
/// <param name="easing">Easing for the ducking transition.</param>
public void Duck(int duration = 0, float duckVolumeTo = 0.25f, int duckCutoffTo = 300, Easing easing = Easing.InCubic)
{
Schedule(() =>
{
audioDuckFilter?.CutoffTo(duckCutoffTo, duration, easing);
this.TransformBindableTo(audioDuckVolume, duckVolumeTo, duration, easing);
});
}
/// <summary>
/// Restores the volume to full and stops filtering the currently playing track after having used <see cref="Duck"/>.
/// </summary>
/// <param name="duration">Duration of the unducking transition, in ms.</param>
/// <param name="easing">Easing for the unducking transition.</param>
public void Unduck(int duration = 500, Easing easing = Easing.InCubic)
{
Schedule(() =>
{
audioDuckFilter?.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF, duration, easing);
this.TransformBindableTo(audioDuckVolume, 1, duration, easing);
});
}
/// <summary>
/// A convenience method that ducks the currently playing track, then after a delay, unducks it.
/// </summary>
/// <param name="delay">Delay after audio is ducked before unducking begins, in ms.</param>
/// <param name="unduckDuration">Duration of the unducking transition, in ms.</param>
/// <param name="unduckEasing">Easing for the unducking transition.</param>
/// <param name="duckVolumeTo">Level to drop volume to (1.0 = 100%).</param>
/// <param name="duckCutoffTo">Cutoff frequency to drop `AudioFilter` to. Use `AudioFilter.MAX_LOWPASS_CUTOFF` to skip filter effect.</param>
/// <param name="duckDuration">Duration of the ducking transition, in ms.</param>
/// <param name="duckEasing">Easing for the ducking transition.</param>
public void TimedDuck(int delay, int unduckDuration = 500, Easing unduckEasing = Easing.InCubic, float duckVolumeTo = 0.25f, int duckCutoffTo = 300, int duckDuration = 0, Easing duckEasing = Easing.InCubic)
{
Duck(duckDuration, duckVolumeTo, duckCutoffTo, duckEasing);
Scheduler.AddDelayed(() => Unduck(unduckDuration, unduckEasing), delay);
}
private bool next()
{
if (beatmap.Disabled || !AllowTrackControl.Value)

View File

@ -3,8 +3,12 @@
#nullable disable
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
@ -21,6 +25,12 @@ namespace osu.Game.Overlays.Toolbar
{
protected Drawable ModeButtonLine { get; private set; }
[Resolved]
private MusicController musicController { get; set; }
private readonly Dictionary<RulesetInfo, Sample> rulesetSelectionSample = new Dictionary<RulesetInfo, Sample>();
private readonly Dictionary<RulesetInfo, SampleChannel> rulesetSelectionChannel = new Dictionary<RulesetInfo, SampleChannel>();
public ToolbarRulesetSelector()
{
RelativeSizeAxes = Axes.Y;
@ -28,7 +38,7 @@ namespace osu.Game.Overlays.Toolbar
}
[BackgroundDependencyLoader]
private void load()
private void load(AudioManager audio)
{
AddRangeInternal(new[]
{
@ -54,6 +64,11 @@ namespace osu.Game.Overlays.Toolbar
}
},
});
foreach (var r in Rulesets.AvailableRulesets)
rulesetSelectionSample[r] = audio.Samples.Get($@"UI/ruleset-select-{r.ShortName}");
Current.ValueChanged += playRulesetSelectionSample;
}
protected override void LoadComplete()
@ -84,6 +99,20 @@ namespace osu.Game.Overlays.Toolbar
}
}
private void playRulesetSelectionSample(ValueChangedEvent<RulesetInfo> r)
{
if (r.OldValue == null)
return;
if (rulesetSelectionChannel.TryGetValue(r.OldValue, out var sampleChannel))
sampleChannel?.Stop();
rulesetSelectionChannel[r.NewValue] = rulesetSelectionSample[r.NewValue].GetChannel();
rulesetSelectionChannel[r.NewValue]?.Play();
musicController?.TimedDuck(600);
}
public override bool HandleNonPositionalInput => !Current.Disabled && base.HandleNonPositionalInput;
public override bool HandlePositionalInput => !Current.Disabled && base.HandlePositionalInput;

View File

@ -2,8 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.UserInterface;
@ -19,8 +17,6 @@ namespace osu.Game.Overlays.Toolbar
{
private readonly RulesetButton ruleset;
private Sample? selectSample;
public ToolbarRulesetTabButton(RulesetInfo value)
: base(value)
{
@ -38,18 +34,10 @@ namespace osu.Game.Overlays.Toolbar
ruleset.SetIcon(rInstance.CreateIcon());
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
selectSample = audio.Samples.Get($@"UI/ruleset-select-{Value.ShortName}");
}
protected override void OnActivated() => ruleset.Active = true;
protected override void OnDeactivated() => ruleset.Active = false;
protected override void OnActivatedByUser() => selectSample?.Play();
private partial class RulesetButton : ToolbarButton
{
protected override HoverSounds CreateHoverSounds(HoverSampleSet sampleSet) => new HoverSounds();