2022-08-17 14:39:28 +08:00
|
|
|
// 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;
|
2022-08-18 13:52:47 +08:00
|
|
|
using System.Diagnostics;
|
2022-08-17 14:39:28 +08:00
|
|
|
using osu.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
{
|
2022-08-18 14:01:47 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A clock intended to be the single source-of-truth for beatmap timing.
|
|
|
|
///
|
|
|
|
/// It provides some functionality:
|
2022-08-22 13:02:20 +08:00
|
|
|
/// - Optionally applies (and tracks changes of) beatmap, user, and platform offsets (see ctor argument applyOffsets).
|
|
|
|
/// - Adjusts <see cref="Seek"/> operations to account for any applied offsets, seeking in raw "beatmap" time values.
|
2022-08-18 14:01:47 +08:00
|
|
|
/// - Exposes track length.
|
|
|
|
/// - Allows changing the source to a new track (for cases like editor track updating).
|
|
|
|
/// </summary>
|
2022-08-17 14:39:28 +08:00
|
|
|
public partial class FramedBeatmapClock : Component, IFrameBasedClock, IAdjustableClock, ISourceChangeableClock
|
|
|
|
{
|
2022-08-18 13:52:47 +08:00
|
|
|
private readonly bool applyOffsets;
|
|
|
|
|
|
|
|
private readonly OffsetCorrectionClock? userGlobalOffsetClock;
|
|
|
|
private readonly OffsetCorrectionClock? platformOffsetClock;
|
2024-02-19 11:16:54 +08:00
|
|
|
private readonly FramedOffsetClock? userBeatmapOffsetClock;
|
2022-08-18 13:52:47 +08:00
|
|
|
|
|
|
|
private readonly IFrameBasedClock finalClockSource;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
2022-08-22 13:03:51 +08:00
|
|
|
private Bindable<double>? userAudioOffset;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
private IDisposable? beatmapOffsetSubscription;
|
|
|
|
|
2023-09-22 21:02:18 +08:00
|
|
|
private readonly DecouplingFramedClock decoupledTrack;
|
2024-02-29 23:03:27 +08:00
|
|
|
private readonly InterpolatingFramedClock interpolatedTrack;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuConfigManager config { get; set; } = null!;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private RealmAccess realm { get; set; } = null!;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
|
|
|
|
|
2023-07-06 13:02:20 +08:00
|
|
|
public bool IsRewinding { get; private set; }
|
|
|
|
|
2023-09-22 12:29:46 +08:00
|
|
|
public FramedBeatmapClock(bool applyOffsets, bool requireDecoupling, IClock? source = null)
|
2022-08-17 14:39:28 +08:00
|
|
|
{
|
2022-08-18 13:52:47 +08:00
|
|
|
this.applyOffsets = applyOffsets;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
2023-09-22 21:02:18 +08:00
|
|
|
decoupledTrack = new DecouplingFramedClock(source) { AllowDecoupling = requireDecoupling };
|
2022-08-17 14:39:28 +08:00
|
|
|
|
2023-09-21 17:49:44 +08:00
|
|
|
// An interpolating clock is used to ensure precise time values even when the host audio subsystem is not reporting
|
|
|
|
// high precision times (on windows there's generally only 5-10ms reporting intervals, as an example).
|
2024-02-29 23:03:27 +08:00
|
|
|
interpolatedTrack = new InterpolatingFramedClock(decoupledTrack);
|
2023-09-21 16:02:31 +08:00
|
|
|
|
2022-08-18 13:52:47 +08:00
|
|
|
if (applyOffsets)
|
|
|
|
{
|
|
|
|
// Audio timings in general with newer BASS versions don't match stable.
|
|
|
|
// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
|
2024-01-18 13:21:02 +08:00
|
|
|
platformOffsetClock = new OffsetCorrectionClock(interpolatedTrack) { Offset = RuntimeInfo.OS == RuntimeInfo.Platform.Windows ? 15 : 0 };
|
2022-08-18 13:52:47 +08:00
|
|
|
|
|
|
|
// User global offset (set in settings) should also be applied.
|
2024-01-18 13:21:02 +08:00
|
|
|
userGlobalOffsetClock = new OffsetCorrectionClock(platformOffsetClock);
|
2022-08-18 13:52:47 +08:00
|
|
|
|
|
|
|
// User per-beatmap offset will be applied to this final clock.
|
2024-02-19 11:16:54 +08:00
|
|
|
finalClockSource = userBeatmapOffsetClock = new FramedOffsetClock(userGlobalOffsetClock);
|
2022-08-18 13:52:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-04 17:04:07 +08:00
|
|
|
finalClockSource = interpolatedTrack;
|
2022-08-18 13:52:47 +08:00
|
|
|
}
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-08-18 13:52:47 +08:00
|
|
|
if (applyOffsets)
|
|
|
|
{
|
|
|
|
Debug.Assert(userBeatmapOffsetClock != null);
|
|
|
|
Debug.Assert(userGlobalOffsetClock != null);
|
|
|
|
|
|
|
|
userAudioOffset = config.GetBindable<double>(OsuSetting.AudioOffset);
|
|
|
|
userAudioOffset.BindValueChanged(offset => userGlobalOffsetClock.Offset = offset.NewValue, true);
|
|
|
|
|
2023-09-21 18:53:55 +08:00
|
|
|
// TODO: this doesn't update when using ChangeSource() to change beatmap.
|
2022-08-18 13:52:47 +08:00
|
|
|
beatmapOffsetSubscription = realm.SubscribeToPropertyChanged(
|
|
|
|
r => r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings,
|
|
|
|
settings => settings.Offset,
|
|
|
|
val =>
|
|
|
|
{
|
|
|
|
userBeatmapOffsetClock.Offset = val;
|
|
|
|
});
|
|
|
|
}
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
2022-09-09 13:01:52 +08:00
|
|
|
|
2023-09-21 18:10:14 +08:00
|
|
|
finalClockSource.ProcessFrame();
|
2023-07-06 13:02:20 +08:00
|
|
|
|
|
|
|
if (Clock.ElapsedFrameTime != 0)
|
|
|
|
IsRewinding = Clock.ElapsedFrameTime < 0;
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:42:50 +08:00
|
|
|
public double TotalAppliedOffset
|
2022-08-17 14:39:28 +08:00
|
|
|
{
|
2022-08-18 14:01:47 +08:00
|
|
|
get
|
|
|
|
{
|
|
|
|
if (!applyOffsets)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Debug.Assert(userGlobalOffsetClock != null);
|
|
|
|
Debug.Assert(userBeatmapOffsetClock != null);
|
|
|
|
Debug.Assert(platformOffsetClock != null);
|
|
|
|
|
2024-02-19 11:16:54 +08:00
|
|
|
return userGlobalOffsetClock.RateAdjustedOffset + userBeatmapOffsetClock.Offset + platformOffsetClock.RateAdjustedOffset;
|
2022-08-18 14:01:47 +08:00
|
|
|
}
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#region Delegation of IAdjustableClock / ISourceChangeableClock to decoupled clock.
|
|
|
|
|
2023-09-21 17:45:34 +08:00
|
|
|
public void ChangeSource(IClock? source) => decoupledTrack.ChangeSource(source);
|
2022-08-17 14:39:28 +08:00
|
|
|
|
2023-09-21 14:32:29 +08:00
|
|
|
public IClock Source => decoupledTrack.Source;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
2023-09-21 14:32:29 +08:00
|
|
|
decoupledTrack.Reset();
|
2022-08-18 13:52:47 +08:00
|
|
|
finalClockSource.ProcessFrame();
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
2023-09-21 14:32:29 +08:00
|
|
|
decoupledTrack.Start();
|
2022-08-18 13:52:47 +08:00
|
|
|
finalClockSource.ProcessFrame();
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
{
|
2023-09-21 14:32:29 +08:00
|
|
|
decoupledTrack.Stop();
|
2022-08-18 13:52:47 +08:00
|
|
|
finalClockSource.ProcessFrame();
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Seek(double position)
|
|
|
|
{
|
2023-09-21 14:32:29 +08:00
|
|
|
bool success = decoupledTrack.Seek(position - TotalAppliedOffset);
|
2022-08-18 13:52:47 +08:00
|
|
|
finalClockSource.ProcessFrame();
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:32:29 +08:00
|
|
|
public void ResetSpeedAdjustments() => decoupledTrack.ResetSpeedAdjustments();
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
public double Rate
|
|
|
|
{
|
2023-09-21 14:32:29 +08:00
|
|
|
get => decoupledTrack.Rate;
|
|
|
|
set => decoupledTrack.Rate = value;
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Delegation of IFrameBasedClock to clock with all offsets applied
|
|
|
|
|
2022-08-18 13:52:47 +08:00
|
|
|
public double CurrentTime => finalClockSource.CurrentTime;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
2022-08-18 13:52:47 +08:00
|
|
|
public bool IsRunning => finalClockSource.IsRunning;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
public void ProcessFrame()
|
|
|
|
{
|
|
|
|
// Noop to ensure an external consumer doesn't process the internal clock an extra time.
|
|
|
|
}
|
|
|
|
|
2022-08-18 13:52:47 +08:00
|
|
|
public double ElapsedFrameTime => finalClockSource.ElapsedFrameTime;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
2022-08-18 13:52:47 +08:00
|
|
|
public double FramesPerSecond => finalClockSource.FramesPerSecond;
|
2022-08-17 14:39:28 +08:00
|
|
|
|
|
|
|
#endregion
|
2022-08-18 14:01:47 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
beatmapOffsetSubscription?.Dispose();
|
|
|
|
}
|
2024-02-29 23:03:27 +08:00
|
|
|
|
|
|
|
public string GetSnapshot()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
$"originalSource: {output(Source)}\n" +
|
|
|
|
$"userGlobalOffsetClock: {output(userGlobalOffsetClock)}\n" +
|
|
|
|
$"platformOffsetClock: {output(platformOffsetClock)}\n" +
|
|
|
|
$"userBeatmapOffsetClock: {output(userBeatmapOffsetClock)}\n" +
|
|
|
|
$"interpolatedTrack: {output(interpolatedTrack)}\n" +
|
|
|
|
$"decoupledTrack: {output(decoupledTrack)}\n" +
|
|
|
|
$"finalClockSource: {output(finalClockSource)}\n";
|
|
|
|
|
|
|
|
string output(IClock? clock)
|
|
|
|
{
|
|
|
|
if (clock == null)
|
|
|
|
return "null";
|
|
|
|
|
|
|
|
if (clock is IFrameBasedClock framed)
|
|
|
|
return $"current: {clock.CurrentTime:N2} running: {clock.IsRunning} rate: {clock.Rate} elapsed: {framed.ElapsedFrameTime:N2}";
|
|
|
|
|
|
|
|
return $"current: {clock.CurrentTime:N2} running: {clock.IsRunning} rate: {clock.Rate}";
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 14:39:28 +08:00
|
|
|
}
|
|
|
|
}
|