diff --git a/osu.Game.Tests/Visual/Menus/TestSceneAudioDucking.cs b/osu.Game.Tests/Visual/Menus/TestSceneAudioDucking.cs new file mode 100644 index 0000000000..8d20d8e0d5 --- /dev/null +++ b/osu.Game.Tests/Visual/Menus/TestSceneAudioDucking.cs @@ -0,0 +1,180 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using NUnit.Framework; +using osu.Game.Audio.Effects; +using osu.Game.Overlays; + +namespace osu.Game.Tests.Visual.Menus +{ + public partial class TestSceneAudioDucking : OsuGameTestScene + { + [Test] + public void TestMomentaryDuck() + { + AddStep("duck momentarily", () => Game.MusicController.DuckMomentarily(1000)); + } + + [Test] + public void TestMultipleDucks() + { + IDisposable duckOp1 = null!; + IDisposable duckOp2 = null!; + + double normalVolume = 1; + + AddStep("get initial volume", () => + { + normalVolume = Game.Audio.Tracks.AggregateVolume.Value; + }); + + AddStep("duck one", () => + { + duckOp1 = Game.MusicController.Duck(new DuckParameters + { + DuckVolumeTo = 0.5, + }); + }); + + AddUntilStep("wait for duck to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.5f).Within(0.01)); + + AddStep("duck two", () => + { + duckOp2 = Game.MusicController.Duck(new DuckParameters + { + DuckVolumeTo = 0.2, + }); + }); + + AddUntilStep("wait for duck to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.2f).Within(0.01)); + + AddStep("restore two", () => duckOp2.Dispose()); + AddUntilStep("wait for restore to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.5f).Within(0.01)); + + AddStep("restore one", () => duckOp1.Dispose()); + AddUntilStep("wait for restore to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume).Within(0.01)); + } + + [Test] + public void TestMultipleDucksSameParameters() + { + var duckParameters = new DuckParameters + { + DuckVolumeTo = 0.5, + }; + + IDisposable duckOp1 = null!; + IDisposable duckOp2 = null!; + + double normalVolume = 1; + + AddStep("get initial volume", () => + { + normalVolume = Game.Audio.Tracks.AggregateVolume.Value; + }); + + AddStep("duck one", () => + { + duckOp1 = Game.MusicController.Duck(duckParameters); + }); + + AddUntilStep("wait for duck to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.5f).Within(0.01)); + + AddStep("duck two", () => + { + duckOp2 = Game.MusicController.Duck(duckParameters); + }); + + AddUntilStep("wait for duck to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.5f).Within(0.01)); + + AddStep("restore two", () => duckOp2.Dispose()); + AddUntilStep("wait for restore to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.5f).Within(0.01)); + + AddStep("restore one", () => duckOp1.Dispose()); + AddUntilStep("wait for restore to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume).Within(0.01)); + } + + [Test] + public void TestMultipleDucksReverseOrder() + { + IDisposable duckOp1 = null!; + IDisposable duckOp2 = null!; + + double normalVolume = 1; + + AddStep("get initial volume", () => + { + normalVolume = Game.Audio.Tracks.AggregateVolume.Value; + }); + + AddStep("duck one", () => + { + duckOp1 = Game.MusicController.Duck(new DuckParameters + { + DuckVolumeTo = 0.5, + }); + }); + + AddUntilStep("wait for duck to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.5f).Within(0.01)); + + AddStep("duck two", () => + { + duckOp2 = Game.MusicController.Duck(new DuckParameters + { + DuckVolumeTo = 0.2, + }); + }); + + AddUntilStep("wait for duck to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.2f).Within(0.01)); + + AddStep("restore one", () => duckOp1.Dispose()); + + // reverse order, less extreme duck removed so won't change + AddUntilStep("wait for restore to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume * 0.2f).Within(0.01)); + + AddStep("restore two", () => duckOp2.Dispose()); + AddUntilStep("wait for restore to complete", () => Game.Audio.Tracks.AggregateVolume.Value, () => Is.EqualTo(normalVolume).Within(0.01)); + } + + [Test] + public void TestMultipleDisposalIsNoop() + { + IDisposable duckOp1 = null!; + + AddStep("duck", () => duckOp1 = Game.MusicController.Duck()); + AddStep("restore", () => duckOp1.Dispose()); + AddStep("restore", () => duckOp1.Dispose()); + } + + [Test] + public void TestMultipleDucksDifferentPieces() + { + IDisposable duckOp1 = null!; + IDisposable duckOp2 = null!; + + AddStep("duck volume", () => + { + duckOp1 = Game.MusicController.Duck(new DuckParameters + { + DuckVolumeTo = 0.2, + DuckCutoffTo = AudioFilter.MAX_LOWPASS_CUTOFF, + DuckDuration = 500, + }); + }); + + AddStep("duck lowpass", () => + { + duckOp2 = Game.MusicController.Duck(new DuckParameters + { + DuckVolumeTo = 1, + DuckCutoffTo = 300, + DuckDuration = 500, + }); + }); + + AddStep("restore lowpass", () => duckOp2.Dispose()); + AddStep("restore volume", () => duckOp1.Dispose()); + } + } +} diff --git a/osu.Game/Collections/ManageCollectionsDialog.cs b/osu.Game/Collections/ManageCollectionsDialog.cs index ea663f45fe..9f8158af53 100644 --- a/osu.Game/Collections/ManageCollectionsDialog.cs +++ b/osu.Game/Collections/ManageCollectionsDialog.cs @@ -1,17 +1,17 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using osu.Framework.Allocation; -using osu.Framework.Audio; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Sprites; -using osu.Game.Audio.Effects; using osu.Game.Graphics; using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; +using osu.Game.Overlays; using osuTK; namespace osu.Game.Collections @@ -21,11 +21,14 @@ namespace osu.Game.Collections private const double enter_duration = 500; private const double exit_duration = 200; - private AudioFilter lowPassFilter = null!; - protected override string PopInSampleName => @"UI/overlay-big-pop-in"; protected override string PopOutSampleName => @"UI/overlay-big-pop-out"; + private IDisposable? duckOperation; + + [Resolved] + private MusicController? musicController { get; set; } + public ManageCollectionsDialog() { Anchor = Anchor.Centre; @@ -39,7 +42,7 @@ namespace osu.Game.Collections } [BackgroundDependencyLoader] - private void load(OsuColour colours, AudioManager audio) + private void load(OsuColour colours) { Children = new Drawable[] { @@ -110,19 +113,25 @@ namespace osu.Game.Collections }, } } - }, - lowPassFilter = new AudioFilter(audio.TrackMixer) + } }; } - public override bool IsPresent => base.IsPresent - // Safety for low pass filter potentially getting stuck in applied state due to - // transforms on `this` causing children to no longer be updated. - || lowPassFilter.IsAttached; + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + duckOperation?.Dispose(); + } protected override void PopIn() { - lowPassFilter.CutoffTo(300, 100, Easing.OutCubic); + duckOperation = musicController?.Duck(new DuckParameters + { + DuckVolumeTo = 1, + DuckDuration = 100, + RestoreDuration = 100, + }); + this.FadeIn(enter_duration, Easing.OutQuint); this.ScaleTo(0.9f).Then().ScaleTo(1f, enter_duration, Easing.OutQuint); } @@ -131,7 +140,7 @@ namespace osu.Game.Collections { base.PopOut(); - lowPassFilter.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF, 100, Easing.InCubic); + duckOperation?.Dispose(); this.FadeOut(exit_duration, Easing.OutQuint); this.ScaleTo(0.9f, exit_duration); diff --git a/osu.Game/Overlays/Dialog/PopupDialogDangerousButton.cs b/osu.Game/Overlays/Dialog/PopupDialogDangerousButton.cs index d16f49eab7..fec36fa7fa 100644 --- a/osu.Game/Overlays/Dialog/PopupDialogDangerousButton.cs +++ b/osu.Game/Overlays/Dialog/PopupDialogDangerousButton.cs @@ -3,7 +3,6 @@ #nullable disable -using System; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Sample; @@ -11,7 +10,6 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Shapes; using osu.Framework.Input.Events; -using osu.Game.Audio.Effects; using osu.Game.Graphics; using osu.Game.Graphics.Containers; @@ -57,7 +55,6 @@ namespace osu.Game.Overlays.Dialog private Sample tickSample; private Sample confirmSample; private double lastTickPlaybackTime; - private AudioFilter lowPassFilter = null!; private bool mouseDown; [BackgroundDependencyLoader] @@ -65,8 +62,6 @@ namespace osu.Game.Overlays.Dialog { tickSample = audio.Samples.Get(@"UI/dialog-dangerous-tick"); confirmSample = audio.Samples.Get(@"UI/dialog-dangerous-select"); - - AddInternal(lowPassFilter = new AudioFilter(audio.SampleMixer)); } protected override void LoadComplete() @@ -75,15 +70,8 @@ namespace osu.Game.Overlays.Dialog Progress.BindValueChanged(progressChanged, true); } - protected override void AbortConfirm() - { - lowPassFilter.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF); - base.AbortConfirm(); - } - protected override void Confirm() { - lowPassFilter.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF); confirmSample?.Play(); base.Confirm(); } @@ -123,8 +111,6 @@ namespace osu.Game.Overlays.Dialog private void progressChanged(ValueChangedEvent progress) { - lowPassFilter.Cutoff = Math.Max(1, (int)(progress.NewValue * AudioFilter.MAX_LOWPASS_CUTOFF * 0.5)); - if (progress.NewValue < progress.OldValue) return; diff --git a/osu.Game/Overlays/DialogOverlay.cs b/osu.Game/Overlays/DialogOverlay.cs index 9ad532ae50..4e7aff84bc 100644 --- a/osu.Game/Overlays/DialogOverlay.cs +++ b/osu.Game/Overlays/DialogOverlay.cs @@ -3,16 +3,16 @@ #nullable disable +using System; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Overlays.Dialog; using osu.Game.Graphics.Containers; using osu.Game.Input.Bindings; using System.Linq; +using JetBrains.Annotations; using osu.Framework.Allocation; -using osu.Framework.Audio; using osu.Framework.Input.Events; -using osu.Game.Audio.Effects; namespace osu.Game.Overlays { @@ -23,15 +23,16 @@ namespace osu.Game.Overlays protected override string PopInSampleName => "UI/dialog-pop-in"; protected override string PopOutSampleName => "UI/dialog-pop-out"; - private AudioFilter lowPassFilter; + [Resolved] + private MusicController musicController { get; set; } public PopupDialog CurrentDialog { get; private set; } public override bool IsPresent => Scheduler.HasPendingTasks - || dialogContainer.Children.Count > 0 - // Safety for low pass filter potentially getting stuck in applied state due to - // transforms on `this` causing children to no longer be updated. - || lowPassFilter.IsAttached; + || dialogContainer.Children.Count > 0; + + [CanBeNull] + private IDisposable duckOperation; public DialogOverlay() { @@ -49,10 +50,10 @@ namespace osu.Game.Overlays Origin = Anchor.Centre; } - [BackgroundDependencyLoader] - private void load(AudioManager audio) + protected override void Dispose(bool isDisposing) { - AddInternal(lowPassFilter = new AudioFilter(audio.TrackMixer)); + base.Dispose(isDisposing); + duckOperation?.Dispose(); } public void Push(PopupDialog dialog) @@ -105,13 +106,18 @@ namespace osu.Game.Overlays protected override void PopIn() { - lowPassFilter.CutoffTo(300, 100, Easing.OutCubic); + duckOperation = musicController?.Duck(new DuckParameters + { + DuckVolumeTo = 1, + DuckDuration = 100, + RestoreDuration = 100, + }); } protected override void PopOut() { base.PopOut(); - lowPassFilter.CutoffTo(AudioFilter.MAX_LOWPASS_CUTOFF, 100, Easing.InCubic); + duckOperation?.Dispose(); // PopOut gets called initially, but we only want to hide dialog when we have been loaded and are present. if (IsLoaded && CurrentDialog?.State.Value == Visibility.Visible) diff --git a/osu.Game/Overlays/MusicController.cs b/osu.Game/Overlays/MusicController.cs index ef12d1eba2..116e60a014 100644 --- a/osu.Game/Overlays/MusicController.cs +++ b/osu.Game/Overlays/MusicController.cs @@ -13,6 +13,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; @@ -59,6 +60,17 @@ namespace osu.Game.Overlays [Resolved] private RealmAccess realm { get; set; } = null!; + private readonly BindableDouble audioDuckVolume = new BindableDouble(1); + + private AudioFilter audioDuckFilter = null!; + + [BackgroundDependencyLoader] + private void load(AudioManager audio) + { + AddInternal(audioDuckFilter = new AudioFilter(audio.TrackMixer)); + audio.Tracks.AddAdjustment(AdjustableProperty.Volume, audioDuckVolume); + } + protected override void LoadComplete() { base.LoadComplete(); @@ -246,6 +258,54 @@ namespace osu.Game.Overlays onSuccess?.Invoke(); }); + private readonly List duckOperations = new List(); + + /// + /// Applies ducking, attenuating the volume and/or low-pass cutoff of the currently playing track to make headroom for effects (or just to apply an effect). + /// + /// A which will restore the duck operation when disposed. + public IDisposable Duck(DuckParameters? parameters = null) + { + parameters ??= new DuckParameters(); + + duckOperations.Add(parameters); + + DuckParameters volumeOperation = duckOperations.MinBy(p => p.DuckVolumeTo)!; + DuckParameters lowPassOperation = duckOperations.MinBy(p => p.DuckCutoffTo)!; + + audioDuckFilter.CutoffTo(lowPassOperation.DuckCutoffTo, lowPassOperation.DuckDuration, lowPassOperation.DuckEasing); + this.TransformBindableTo(audioDuckVolume, volumeOperation.DuckVolumeTo, volumeOperation.DuckDuration, volumeOperation.DuckEasing); + + return new InvokeOnDisposal(restoreDucking); + + void restoreDucking() => Schedule(() => + { + if (!duckOperations.Remove(parameters)) + return; + + DuckParameters? restoreVolumeOperation = duckOperations.MinBy(p => p.DuckVolumeTo); + DuckParameters? restoreLowPassOperation = duckOperations.MinBy(p => p.DuckCutoffTo); + + // If another duck operation is in the list, restore ducking to its level, else reset back to defaults. + audioDuckFilter.CutoffTo(restoreLowPassOperation?.DuckCutoffTo ?? AudioFilter.MAX_LOWPASS_CUTOFF, parameters.RestoreDuration, parameters.RestoreEasing); + this.TransformBindableTo(audioDuckVolume, restoreVolumeOperation?.DuckVolumeTo ?? 1, parameters.RestoreDuration, parameters.RestoreEasing); + }); + } + + /// + /// A convenience method that ducks the currently playing track, then after a delay, restores automatically. + /// + /// A delay in milliseconds which defines how long to delay restoration after ducking completes. + /// Parameters defining the ducking operation. + public void DuckMomentarily(double delayUntilRestore, DuckParameters? parameters = null) + { + parameters ??= new DuckParameters(); + + IDisposable duckOperation = Duck(parameters); + + Scheduler.AddDelayed(() => duckOperation.Dispose(), delayUntilRestore); + } + private bool next() { if (beatmap.Disabled || !AllowTrackControl.Value) @@ -419,6 +479,45 @@ namespace osu.Game.Overlays } } + public class DuckParameters + { + /// + /// The duration of the ducking transition in milliseconds. + /// Defaults to 100 ms. + /// + public double DuckDuration = 100; + + /// + /// The final volume which should be reached during ducking, when 0 is silent and 1 is original volume. + /// Defaults to 25%. + /// + public double DuckVolumeTo = 0.25; + + /// + /// The low-pass cutoff frequency which should be reached during ducking. If not required, set to . + /// Defaults to 300 Hz. + /// + public int DuckCutoffTo = 300; + + /// + /// The easing curve to be applied during ducking. + /// Defaults to . + /// + public Easing DuckEasing = Easing.Out; + + /// + /// The duration of the restoration transition in milliseconds. + /// Defaults to 500 ms. + /// + public double RestoreDuration = 500; + + /// + /// The easing curve to be applied during restoration. + /// Defaults to . + /// + public Easing RestoreEasing = Easing.In; + } + public enum TrackChangeDirection { None, diff --git a/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs b/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs index d49c340ed4..05ab505417 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs @@ -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,13 @@ namespace osu.Game.Overlays.Toolbar { protected Drawable ModeButtonLine { get; private set; } + [Resolved] + private MusicController musicController { get; set; } + + private readonly Dictionary rulesetSelectionSample = new Dictionary(); + private readonly Dictionary rulesetSelectionChannel = new Dictionary(); + private Sample defaultSelectSample; + public ToolbarRulesetSelector() { RelativeSizeAxes = Axes.Y; @@ -28,7 +39,7 @@ namespace osu.Game.Overlays.Toolbar } [BackgroundDependencyLoader] - private void load() + private void load(AudioManager audio) { AddRangeInternal(new[] { @@ -54,6 +65,13 @@ namespace osu.Game.Overlays.Toolbar } }, }); + + foreach (var r in Rulesets.AvailableRulesets) + rulesetSelectionSample[r] = audio.Samples.Get($@"UI/ruleset-select-{r.ShortName}"); + + defaultSelectSample = audio.Samples.Get(@"UI/default-select"); + + Current.ValueChanged += playRulesetSelectionSample; } protected override void LoadComplete() @@ -84,6 +102,29 @@ namespace osu.Game.Overlays.Toolbar } } + private void playRulesetSelectionSample(ValueChangedEvent r) + { + // Don't play sample on first setting of value + if (r.OldValue == null) + return; + + var channel = rulesetSelectionSample[r.NewValue]?.GetChannel(); + + // Skip sample choking and ducking for the default/fallback sample + if (channel == null) + { + defaultSelectSample.Play(); + return; + } + + foreach (var pair in rulesetSelectionChannel) + pair.Value?.Stop(); + + rulesetSelectionChannel[r.NewValue] = channel; + channel.Play(); + musicController?.DuckMomentarily(500, new DuckParameters { DuckDuration = 0 }); + } + public override bool HandleNonPositionalInput => !Current.Disabled && base.HandleNonPositionalInput; public override bool HandlePositionalInput => !Current.Disabled && base.HandlePositionalInput; diff --git a/osu.Game/Overlays/Toolbar/ToolbarRulesetTabButton.cs b/osu.Game/Overlays/Toolbar/ToolbarRulesetTabButton.cs index 0315bede64..3287ac6eaa 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarRulesetTabButton.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarRulesetTabButton.cs @@ -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(); diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 67c9f2e100..447c783b29 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -36,7 +36,7 @@ - +