Merge pull request #16714 from frenzibyte/rewrite-hardware-correction-clock

Rewrite `HardwareCorrectionOffsetClock` to handle seeking on different gameplay rates
This commit is contained in:
Dan Balasescu 2022-01-31 17:32:22 +09:00 committed by GitHub
commit 62603e78fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 21 deletions

View File

@ -2,7 +2,13 @@
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Rulesets.Osu;
using osu.Game.Screens.Play;
using osu.Game.Tests.Visual;
@ -12,47 +18,93 @@ namespace osu.Game.Tests.Gameplay
[HeadlessTest]
public class TestSceneMasterGameplayClockContainer : OsuTestScene
{
private OsuConfigManager localConfig;
[BackgroundDependencyLoader]
private void load()
{
Dependencies.Cache(localConfig = new OsuConfigManager(LocalStorage));
}
[Test]
public void TestStartThenElapsedTime()
{
GameplayClockContainer gcc = null;
GameplayClockContainer gameplayClockContainer = null;
AddStep("create container", () =>
{
var working = CreateWorkingBeatmap(new OsuRuleset().RulesetInfo);
working.LoadTrack();
Add(gcc = new MasterGameplayClockContainer(working, 0));
Add(gameplayClockContainer = new MasterGameplayClockContainer(working, 0));
});
AddStep("start clock", () => gcc.Start());
AddUntilStep("elapsed greater than zero", () => gcc.GameplayClock.ElapsedFrameTime > 0);
AddStep("start clock", () => gameplayClockContainer.Start());
AddUntilStep("elapsed greater than zero", () => gameplayClockContainer.GameplayClock.ElapsedFrameTime > 0);
}
[Test]
public void TestElapseThenReset()
{
GameplayClockContainer gcc = null;
GameplayClockContainer gameplayClockContainer = null;
AddStep("create container", () =>
{
var working = CreateWorkingBeatmap(new OsuRuleset().RulesetInfo);
working.LoadTrack();
Add(gcc = new MasterGameplayClockContainer(working, 0));
Add(gameplayClockContainer = new MasterGameplayClockContainer(working, 0));
});
AddStep("start clock", () => gcc.Start());
AddUntilStep("current time greater 2000", () => gcc.GameplayClock.CurrentTime > 2000);
AddStep("start clock", () => gameplayClockContainer.Start());
AddUntilStep("current time greater 2000", () => gameplayClockContainer.GameplayClock.CurrentTime > 2000);
double timeAtReset = 0;
AddStep("reset clock", () =>
{
timeAtReset = gcc.GameplayClock.CurrentTime;
gcc.Reset();
timeAtReset = gameplayClockContainer.GameplayClock.CurrentTime;
gameplayClockContainer.Reset();
});
AddAssert("current time < time at reset", () => gcc.GameplayClock.CurrentTime < timeAtReset);
AddAssert("current time < time at reset", () => gameplayClockContainer.GameplayClock.CurrentTime < timeAtReset);
}
[Test]
public void TestSeekPerformsInGameplayTime(
[Values(1.0, 0.5, 2.0)] double clockRate,
[Values(0.0, 200.0, -200.0)] double userOffset,
[Values(false, true)] bool whileStopped)
{
ClockBackedTestWorkingBeatmap working = null;
GameplayClockContainer gameplayClockContainer = null;
AddStep("create container", () =>
{
working = new ClockBackedTestWorkingBeatmap(new OsuRuleset().RulesetInfo, new FramedClock(new ManualClock()), Audio);
working.LoadTrack();
Add(gameplayClockContainer = new MasterGameplayClockContainer(working, 0));
if (whileStopped)
gameplayClockContainer.Stop();
gameplayClockContainer.Reset();
});
AddStep($"set clock rate to {clockRate}", () => working.Track.AddAdjustment(AdjustableProperty.Frequency, new BindableDouble(clockRate)));
AddStep($"set audio offset to {userOffset}", () => localConfig.SetValue(OsuSetting.AudioOffset, userOffset));
AddStep("seek to 2500", () => gameplayClockContainer.Seek(2500));
AddAssert("gameplay clock time = 2500", () => Precision.AlmostEquals(gameplayClockContainer.CurrentTime, 2500, 10f));
AddStep("seek to 10000", () => gameplayClockContainer.Seek(10000));
AddAssert("gameplay clock time = 10000", () => Precision.AlmostEquals(gameplayClockContainer.CurrentTime, 10000, 10f));
}
protected override void Dispose(bool isDisposing)
{
localConfig?.Dispose();
base.Dispose(isDisposing);
}
}
}

View File

@ -43,7 +43,7 @@ namespace osu.Game.Screens.Play
Precision = 0.1,
};
private double totalOffset => userOffsetClock.Offset + platformOffsetClock.Offset;
private double totalAppliedOffset => userOffsetClock.RateAdjustedOffset + platformOffsetClock.RateAdjustedOffset;
private readonly BindableDouble pauseFreqAdjust = new BindableDouble(1);
@ -52,8 +52,8 @@ namespace osu.Game.Screens.Play
private readonly bool startAtGameplayStart;
private readonly double firstHitObjectTime;
private FramedOffsetClock userOffsetClock;
private FramedOffsetClock platformOffsetClock;
private HardwareCorrectionOffsetClock userOffsetClock;
private HardwareCorrectionOffsetClock platformOffsetClock;
private MasterGameplayClock masterGameplayClock;
private Bindable<double> userAudioOffset;
private double startOffset;
@ -128,7 +128,7 @@ namespace osu.Game.Screens.Play
{
// remove the offset component here because most of the time we want the seek to be aligned to gameplay, not the audio track.
// we may want to consider reversing the application of offsets in the future as it may feel more correct.
base.Seek(time - totalOffset);
base.Seek(time - totalAppliedOffset);
}
/// <summary>
@ -214,13 +214,25 @@ namespace osu.Game.Screens.Play
private class HardwareCorrectionOffsetClock : FramedOffsetClock
{
// we always want to apply the same real-time offset, so it should be adjusted by the difference in playback rate (from realtime) to achieve this.
// base implementation already adds offset at 1.0 rate, so we only add the difference from that here.
public override double CurrentTime => base.CurrentTime + offsetAdjust;
private readonly BindableDouble pauseRateAdjust;
private double offsetAdjust;
private double offset;
public new double Offset
{
get => offset;
set
{
if (value == offset)
return;
offset = value;
updateOffset();
}
}
public double RateAdjustedOffset => base.Offset;
public HardwareCorrectionOffsetClock(IClock source, BindableDouble pauseRateAdjust)
: base(source)
@ -231,10 +243,17 @@ namespace osu.Game.Screens.Play
public override void ProcessFrame()
{
base.ProcessFrame();
updateOffset();
}
private void updateOffset()
{
// changing this during the pause transform effect will cause a potentially large offset to be suddenly applied as we approach zero rate.
if (pauseRateAdjust.Value == 1)
offsetAdjust = Offset * (Rate - 1);
{
// we always want to apply the same real-time offset, so it should be adjusted by the difference in playback rate (from realtime) to achieve this.
base.Offset = Offset * Rate;
}
}
}