1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 11:35:35 +08:00

Merge pull request #28547 from nekodex/audio-ducking-fx

Add a 'ducking' effect to the currently playing track when changing ruleset
This commit is contained in:
Salman Ahmed 2024-07-08 13:12:04 +03:00 committed by GitHub
commit 2f4e447cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 362 additions and 53 deletions

View File

@ -0,0 +1,180 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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());
}
}
}

View File

@ -1,17 +1,17 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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);

View File

@ -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<double> progress)
{
lowPassFilter.Cutoff = Math.Max(1, (int)(progress.NewValue * AudioFilter.MAX_LOWPASS_CUTOFF * 0.5));
if (progress.NewValue < progress.OldValue)
return;

View File

@ -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)

View File

@ -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<DuckParameters> duckOperations = new List<DuckParameters>();
/// <summary>
/// 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).
/// </summary>
/// <returns>A <see cref="IDisposable"/> which will restore the duck operation when disposed.</returns>
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);
});
}
/// <summary>
/// A convenience method that ducks the currently playing track, then after a delay, restores automatically.
/// </summary>
/// <param name="delayUntilRestore">A delay in milliseconds which defines how long to delay restoration after ducking completes.</param>
/// <param name="parameters">Parameters defining the ducking operation.</param>
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
{
/// <summary>
/// The duration of the ducking transition in milliseconds.
/// Defaults to 100 ms.
/// </summary>
public double DuckDuration = 100;
/// <summary>
/// The final volume which should be reached during ducking, when 0 is silent and 1 is original volume.
/// Defaults to 25%.
/// </summary>
public double DuckVolumeTo = 0.25;
/// <summary>
/// The low-pass cutoff frequency which should be reached during ducking. If not required, set to <see cref="AudioFilter.MAX_LOWPASS_CUTOFF"/>.
/// Defaults to 300 Hz.
/// </summary>
public int DuckCutoffTo = 300;
/// <summary>
/// The easing curve to be applied during ducking.
/// Defaults to <see cref="Easing.Out"/>.
/// </summary>
public Easing DuckEasing = Easing.Out;
/// <summary>
/// The duration of the restoration transition in milliseconds.
/// Defaults to 500 ms.
/// </summary>
public double RestoreDuration = 500;
/// <summary>
/// The easing curve to be applied during restoration.
/// Defaults to <see cref="Easing.In"/>.
/// </summary>
public Easing RestoreEasing = Easing.In;
}
public enum TrackChangeDirection
{
None,

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,13 @@ 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>();
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<RulesetInfo> 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;

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();

View File

@ -36,7 +36,7 @@
</PackageReference>
<PackageReference Include="Realm" Version="11.5.0" />
<PackageReference Include="ppy.osu.Framework" Version="2024.702.0" />
<PackageReference Include="ppy.osu.Game.Resources" Version="2024.622.0" />
<PackageReference Include="ppy.osu.Game.Resources" Version="2024.705.0" />
<PackageReference Include="Sentry" Version="4.3.0" />
<!-- Held back due to 0.34.0 failing AOT compilation on ZstdSharp.dll dependency. -->
<PackageReference Include="SharpCompress" Version="0.36.0" />