2021-04-14 18:50:22 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-04-14 18:50:22 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Audio.Track;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Beatmaps;
|
2022-05-22 21:15:53 +08:00
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2021-04-14 18:50:22 +08:00
|
|
|
using osu.Game.Configuration;
|
2022-03-01 15:59:33 +08:00
|
|
|
using osu.Game.Database;
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
2021-04-16 19:47:09 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="GameplayClockContainer"/> which uses a <see cref="WorkingBeatmap"/> as a source.
|
|
|
|
/// <para>
|
|
|
|
/// This is the most complete <see cref="GameplayClockContainer"/> which takes into account all user and platform offsets,
|
|
|
|
/// and provides implementations for user actions such as skipping or adjusting playback rates that may occur during gameplay.
|
|
|
|
/// </para>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This is intended to be used as a single controller for gameplay, or as a reference source for other <see cref="GameplayClockContainer"/>s.
|
|
|
|
/// </remarks>
|
2022-05-22 21:15:53 +08:00
|
|
|
public class MasterGameplayClockContainer : GameplayClockContainer, IBeatSyncProvider
|
2021-04-14 18:50:22 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Duration before gameplay start time required before skip button displays.
|
|
|
|
/// </summary>
|
|
|
|
public const double MINIMUM_SKIP_TIME = 1000;
|
|
|
|
|
2021-04-19 18:55:59 +08:00
|
|
|
protected Track Track => (Track)SourceClock;
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
public readonly BindableNumber<double> UserPlaybackRate = new BindableDouble(1)
|
|
|
|
{
|
|
|
|
Default = 1,
|
|
|
|
MinValue = 0.5,
|
|
|
|
MaxValue = 2,
|
|
|
|
Precision = 0.1,
|
|
|
|
};
|
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
private double totalAppliedOffset => userBeatmapOffsetClock.RateAdjustedOffset + userGlobalOffsetClock.RateAdjustedOffset + platformOffsetClock.RateAdjustedOffset;
|
2021-04-14 18:50:22 +08:00
|
|
|
|
2022-03-24 13:43:33 +08:00
|
|
|
private readonly BindableDouble pauseFreqAdjust = new BindableDouble(); // Important that this starts at zero, matching the paused state of the clock.
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
private readonly WorkingBeatmap beatmap;
|
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
private HardwareCorrectionOffsetClock userGlobalOffsetClock;
|
|
|
|
private HardwareCorrectionOffsetClock userBeatmapOffsetClock;
|
2022-01-30 04:06:31 +08:00
|
|
|
private HardwareCorrectionOffsetClock platformOffsetClock;
|
2021-04-21 15:33:14 +08:00
|
|
|
private MasterGameplayClock masterGameplayClock;
|
2021-04-14 18:50:22 +08:00
|
|
|
private Bindable<double> userAudioOffset;
|
|
|
|
|
2022-03-01 18:57:09 +08:00
|
|
|
private IDisposable beatmapOffsetSubscription;
|
|
|
|
|
2022-04-13 13:03:52 +08:00
|
|
|
private readonly double skipTargetTime;
|
2022-03-17 22:39:45 +08:00
|
|
|
|
2022-03-01 18:57:09 +08:00
|
|
|
[Resolved]
|
|
|
|
private RealmAccess realm { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuConfigManager config { get; set; }
|
|
|
|
|
2022-03-17 22:39:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Create a new master gameplay clock container.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="beatmap">The beatmap to be used for time and metadata references.</param>
|
2022-04-13 13:03:52 +08:00
|
|
|
/// <param name="skipTargetTime">The latest time which should be used when introducing gameplay. Will be used when skipping forward.</param>
|
2022-04-13 13:09:49 +08:00
|
|
|
public MasterGameplayClockContainer(WorkingBeatmap beatmap, double skipTargetTime)
|
2021-04-14 18:50:22 +08:00
|
|
|
: base(beatmap.Track)
|
|
|
|
{
|
|
|
|
this.beatmap = beatmap;
|
2022-04-13 13:03:52 +08:00
|
|
|
this.skipTargetTime = skipTargetTime;
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
|
2022-03-01 17:29:04 +08:00
|
|
|
protected override void LoadComplete()
|
2021-04-14 18:50:22 +08:00
|
|
|
{
|
2022-03-01 17:29:04 +08:00
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-04-14 18:50:22 +08:00
|
|
|
userAudioOffset = config.GetBindable<double>(OsuSetting.AudioOffset);
|
2022-03-01 15:59:33 +08:00
|
|
|
userAudioOffset.BindValueChanged(offset => userGlobalOffsetClock.Offset = offset.NewValue, true);
|
|
|
|
|
2022-03-03 16:42:50 +08:00
|
|
|
beatmapOffsetSubscription = realm.SubscribeToPropertyChanged(
|
|
|
|
r => r.Find<BeatmapInfo>(beatmap.BeatmapInfo.ID)?.UserSettings,
|
|
|
|
settings => settings.Offset,
|
|
|
|
val => userBeatmapOffsetClock.Offset = val);
|
2021-04-14 18:50:22 +08:00
|
|
|
|
2022-03-17 22:39:45 +08:00
|
|
|
// Reset may have been called externally before LoadComplete.
|
|
|
|
// If it was, and the clock is in a playing state, we want to ensure that it isn't stopped here.
|
|
|
|
bool isStarted = !IsPaused.Value;
|
2021-04-20 17:25:46 +08:00
|
|
|
|
2022-03-17 22:39:45 +08:00
|
|
|
// If a custom start time was not specified, calculate the best value to use.
|
2022-04-13 12:49:58 +08:00
|
|
|
StartTime ??= findEarliestStartTime();
|
2022-03-17 19:54:42 +08:00
|
|
|
|
2022-04-13 12:49:58 +08:00
|
|
|
Reset(startClock: isStarted);
|
2022-03-17 22:39:45 +08:00
|
|
|
}
|
2022-03-17 19:54:42 +08:00
|
|
|
|
2022-03-18 00:15:17 +08:00
|
|
|
private double findEarliestStartTime()
|
2022-03-17 22:39:45 +08:00
|
|
|
{
|
2022-03-18 00:15:17 +08:00
|
|
|
// here we are trying to find the time to start playback from the "zero" point.
|
|
|
|
// generally this is either zero, or some point earlier than zero in the case of storyboards, lead-ins etc.
|
|
|
|
|
|
|
|
// start with the originally provided latest time (if before zero).
|
2022-04-13 13:03:52 +08:00
|
|
|
double time = Math.Min(0, skipTargetTime);
|
2022-03-17 22:39:45 +08:00
|
|
|
|
|
|
|
// if a storyboard is present, it may dictate the appropriate start time by having events in negative time space.
|
|
|
|
// this is commonly used to display an intro before the audio track start.
|
|
|
|
double? firstStoryboardEvent = beatmap.Storyboard.EarliestEventTime;
|
|
|
|
if (firstStoryboardEvent != null)
|
|
|
|
time = Math.Min(time, firstStoryboardEvent.Value);
|
|
|
|
|
|
|
|
// some beatmaps specify a current lead-in time which should be used instead of the ruleset-provided value when available.
|
|
|
|
// this is not available as an option in the live editor but can still be applied via .osu editing.
|
|
|
|
double firstHitObjectTime = beatmap.Beatmap.HitObjects.First().StartTime;
|
|
|
|
if (beatmap.BeatmapInfo.AudioLeadIn > 0)
|
|
|
|
time = Math.Min(time, firstHitObjectTime - beatmap.BeatmapInfo.AudioLeadIn);
|
|
|
|
|
|
|
|
return time;
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnIsPausedChanged(ValueChangedEvent<bool> isPaused)
|
|
|
|
{
|
2022-04-11 13:11:23 +08:00
|
|
|
if (IsLoaded)
|
2021-06-04 20:58:07 +08:00
|
|
|
{
|
2022-04-11 13:11:23 +08:00
|
|
|
// During normal operation, the source is stopped after performing a frequency ramp.
|
|
|
|
if (isPaused.NewValue)
|
2021-06-04 20:58:07 +08:00
|
|
|
{
|
2022-04-11 13:11:23 +08:00
|
|
|
this.TransformBindableTo(pauseFreqAdjust, 0, 200, Easing.Out).OnComplete(_ =>
|
|
|
|
{
|
|
|
|
if (IsPaused.Value == isPaused.NewValue)
|
|
|
|
AdjustableSource.Stop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this.TransformBindableTo(pauseFreqAdjust, 1, 200, Easing.In);
|
2021-06-04 20:58:07 +08:00
|
|
|
}
|
2021-04-14 18:50:22 +08:00
|
|
|
else
|
2022-04-11 13:11:23 +08:00
|
|
|
{
|
|
|
|
if (isPaused.NewValue)
|
|
|
|
AdjustableSource.Stop();
|
|
|
|
|
|
|
|
// If not yet loaded, we still want to ensure relevant state is correct, as it is used for offset calculations.
|
|
|
|
pauseFreqAdjust.Value = isPaused.NewValue ? 0 : 1;
|
|
|
|
|
|
|
|
// We must also process underlying gameplay clocks to update rate-adjusted offsets with the new frequency adjustment.
|
|
|
|
// Without doing this, an initial seek may be performed with the wrong offset.
|
|
|
|
GameplayClock.UnderlyingClock.ProcessFrame();
|
|
|
|
}
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:09:49 +08:00
|
|
|
public override void Start()
|
|
|
|
{
|
|
|
|
addSourceClockAdjustments();
|
|
|
|
base.Start();
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:33:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Seek to a specific time in gameplay.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Adjusts for any offsets which have been applied (so the seek may not be the expected point in time on the underlying audio track).
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="time">The destination time to seek to.</param>
|
2021-04-14 18:50:22 +08:00
|
|
|
public override void Seek(double time)
|
|
|
|
{
|
|
|
|
// 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.
|
2022-01-30 04:06:31 +08:00
|
|
|
base.Seek(time - totalAppliedOffset);
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Skip forward to the next valid skip point.
|
|
|
|
/// </summary>
|
|
|
|
public void Skip()
|
|
|
|
{
|
2022-04-13 13:03:52 +08:00
|
|
|
if (GameplayClock.CurrentTime > skipTargetTime - MINIMUM_SKIP_TIME)
|
2021-04-14 18:50:22 +08:00
|
|
|
return;
|
|
|
|
|
2022-04-13 13:03:52 +08:00
|
|
|
double skipTarget = skipTargetTime - MINIMUM_SKIP_TIME;
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
if (GameplayClock.CurrentTime < 0 && skipTarget > 6000)
|
|
|
|
// double skip exception for storyboards with very long intros
|
|
|
|
skipTarget = 0;
|
|
|
|
|
|
|
|
Seek(skipTarget);
|
|
|
|
}
|
|
|
|
|
2021-04-14 20:15:14 +08:00
|
|
|
protected override GameplayClock CreateGameplayClock(IFrameBasedClock source)
|
2021-04-14 18:50:22 +08:00
|
|
|
{
|
|
|
|
// Lazer's audio timings in general doesn't match stable. This is the result of user testing, albeit limited.
|
|
|
|
// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
|
2021-09-17 14:39:03 +08:00
|
|
|
platformOffsetClock = new HardwareCorrectionOffsetClock(source, pauseFreqAdjust) { Offset = RuntimeInfo.OS == RuntimeInfo.Platform.Windows ? 15 : 0 };
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
// the final usable gameplay clock with user-set offsets applied.
|
2022-03-01 15:59:33 +08:00
|
|
|
userGlobalOffsetClock = new HardwareCorrectionOffsetClock(platformOffsetClock, pauseFreqAdjust);
|
|
|
|
userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust);
|
2021-04-14 18:50:22 +08:00
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
return masterGameplayClock = new MasterGameplayClock(userBeatmapOffsetClock);
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Changes the backing clock to avoid using the originally provided track.
|
|
|
|
/// </summary>
|
|
|
|
public void StopUsingBeatmapClock()
|
|
|
|
{
|
|
|
|
removeSourceClockAdjustments();
|
2021-04-19 18:55:59 +08:00
|
|
|
ChangeSource(new TrackVirtual(beatmap.Track.Length));
|
2021-04-20 12:09:49 +08:00
|
|
|
addSourceClockAdjustments();
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool speedAdjustmentsApplied;
|
|
|
|
|
2021-04-20 12:09:49 +08:00
|
|
|
private void addSourceClockAdjustments()
|
2021-04-14 18:50:22 +08:00
|
|
|
{
|
|
|
|
if (speedAdjustmentsApplied)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
|
|
|
|
Track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
|
|
|
|
|
2021-04-21 15:33:14 +08:00
|
|
|
masterGameplayClock.MutableNonGameplayAdjustments.Add(pauseFreqAdjust);
|
|
|
|
masterGameplayClock.MutableNonGameplayAdjustments.Add(UserPlaybackRate);
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
speedAdjustmentsApplied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeSourceClockAdjustments()
|
|
|
|
{
|
|
|
|
if (!speedAdjustmentsApplied)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
|
|
|
|
Track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
|
|
|
|
|
2021-04-21 15:33:14 +08:00
|
|
|
masterGameplayClock.MutableNonGameplayAdjustments.Remove(pauseFreqAdjust);
|
|
|
|
masterGameplayClock.MutableNonGameplayAdjustments.Remove(UserPlaybackRate);
|
2021-04-14 18:50:22 +08:00
|
|
|
|
|
|
|
speedAdjustmentsApplied = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2022-03-01 17:29:04 +08:00
|
|
|
beatmapOffsetSubscription?.Dispose();
|
2021-04-14 18:50:22 +08:00
|
|
|
removeSourceClockAdjustments();
|
|
|
|
}
|
|
|
|
|
2022-05-22 21:15:53 +08:00
|
|
|
ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo;
|
|
|
|
IClock IBeatSyncProvider.Clock => GameplayClock;
|
2022-06-24 13:48:43 +08:00
|
|
|
ChannelAmplitudes? IBeatSyncProvider.Amplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : null;
|
2022-05-22 21:15:53 +08:00
|
|
|
|
2021-04-14 18:50:22 +08:00
|
|
|
private class HardwareCorrectionOffsetClock : FramedOffsetClock
|
|
|
|
{
|
2021-09-17 14:39:03 +08:00
|
|
|
private readonly BindableDouble pauseRateAdjust;
|
|
|
|
|
2022-01-30 04:06:31 +08:00
|
|
|
private double offset;
|
|
|
|
|
|
|
|
public new double Offset
|
|
|
|
{
|
|
|
|
get => offset;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == offset)
|
|
|
|
return;
|
|
|
|
|
|
|
|
offset = value;
|
|
|
|
|
|
|
|
updateOffset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public double RateAdjustedOffset => base.Offset;
|
2021-09-17 14:39:03 +08:00
|
|
|
|
|
|
|
public HardwareCorrectionOffsetClock(IClock source, BindableDouble pauseRateAdjust)
|
|
|
|
: base(source)
|
|
|
|
{
|
|
|
|
this.pauseRateAdjust = pauseRateAdjust;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ProcessFrame()
|
2021-04-14 18:50:22 +08:00
|
|
|
{
|
2021-09-17 14:39:03 +08:00
|
|
|
base.ProcessFrame();
|
2022-01-30 04:06:31 +08:00
|
|
|
updateOffset();
|
|
|
|
}
|
2021-09-17 14:39:03 +08:00
|
|
|
|
2022-01-30 04:06:31 +08:00
|
|
|
private void updateOffset()
|
|
|
|
{
|
2021-09-17 14:39:03 +08:00
|
|
|
// 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)
|
2022-01-30 04:06:31 +08:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2021-04-14 18:50:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 15:33:14 +08:00
|
|
|
private class MasterGameplayClock : GameplayClock
|
2021-04-14 18:50:22 +08:00
|
|
|
{
|
|
|
|
public readonly List<Bindable<double>> MutableNonGameplayAdjustments = new List<Bindable<double>>();
|
|
|
|
public override IEnumerable<Bindable<double>> NonGameplayAdjustments => MutableNonGameplayAdjustments;
|
|
|
|
|
2021-04-21 15:33:14 +08:00
|
|
|
public MasterGameplayClock(FramedOffsetClock underlyingClock)
|
2021-04-14 18:50:22 +08:00
|
|
|
: base(underlyingClock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|