// 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 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; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Configuration; using osu.Game.Database; namespace osu.Game.Screens.Play { /// /// A which uses a as a source. /// /// This is the most complete 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. /// /// /// /// This is intended to be used as a single controller for gameplay, or as a reference source for other s. /// public class MasterGameplayClockContainer : GameplayClockContainer, IBeatSyncProvider { /// /// Duration before gameplay start time required before skip button displays. /// public const double MINIMUM_SKIP_TIME = 1000; public readonly BindableNumber UserPlaybackRate = new BindableDouble(1) { Default = 1, MinValue = 0.5, MaxValue = 2, Precision = 0.1, }; private double totalAppliedOffset => userBeatmapOffsetClock.RateAdjustedOffset + userGlobalOffsetClock.RateAdjustedOffset + platformOffsetClock.RateAdjustedOffset; private readonly BindableDouble pauseFreqAdjust = new BindableDouble(); // Important that this starts at zero, matching the paused state of the clock. private readonly WorkingBeatmap beatmap; private OffsetCorrectionClock userGlobalOffsetClock = null!; private OffsetCorrectionClock userBeatmapOffsetClock = null!; private OffsetCorrectionClock platformOffsetClock = null!; private Bindable userAudioOffset = null!; private IDisposable? beatmapOffsetSubscription; private readonly double skipTargetTime; [Resolved] private RealmAccess realm { get; set; } = null!; [Resolved] private OsuConfigManager config { get; set; } = null!; private readonly List> nonGameplayAdjustments = new List>(); public override IEnumerable NonGameplayAdjustments => nonGameplayAdjustments.Select(b => b.Value); /// /// Create a new master gameplay clock container. /// /// The beatmap to be used for time and metadata references. /// The latest time which should be used when introducing gameplay. Will be used when skipping forward. public MasterGameplayClockContainer(WorkingBeatmap beatmap, double skipTargetTime) : base(beatmap.Track) { this.beatmap = beatmap; this.skipTargetTime = skipTargetTime; } protected override void LoadComplete() { base.LoadComplete(); userAudioOffset = config.GetBindable(OsuSetting.AudioOffset); userAudioOffset.BindValueChanged(offset => userGlobalOffsetClock.Offset = offset.NewValue, true); beatmapOffsetSubscription = realm.SubscribeToPropertyChanged( r => r.Find(beatmap.BeatmapInfo.ID)?.UserSettings, settings => settings.Offset, val => userBeatmapOffsetClock.Offset = val); // 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; // If a custom start time was not specified, calculate the best value to use. StartTime ??= findEarliestStartTime(); Reset(startClock: isStarted); } private double findEarliestStartTime() { // 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). double time = Math.Min(0, skipTargetTime); // 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; } protected override void OnIsPausedChanged(ValueChangedEvent isPaused) { if (IsLoaded) { // During normal operation, the source is stopped after performing a frequency ramp. if (isPaused.NewValue) { this.TransformBindableTo(pauseFreqAdjust, 0, 200, Easing.Out).OnComplete(_ => { if (IsPaused.Value == isPaused.NewValue) base.OnIsPausedChanged(isPaused); }); } else this.TransformBindableTo(pauseFreqAdjust, 1, 200, Easing.In); } else { if (isPaused.NewValue) base.OnIsPausedChanged(isPaused); // 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. FramedClock.ProcessFrame(); } } public override void Start() { addSourceClockAdjustments(); base.Start(); } /// /// Seek to a specific time in gameplay. /// /// /// Adjusts for any offsets which have been applied (so the seek may not be the expected point in time on the underlying audio track). /// /// The destination time to seek to. 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. base.Seek(time - totalAppliedOffset); } /// /// Skip forward to the next valid skip point. /// public void Skip() { if (FramedClock.CurrentTime > skipTargetTime - MINIMUM_SKIP_TIME) return; double skipTarget = skipTargetTime - MINIMUM_SKIP_TIME; if (FramedClock.CurrentTime < 0 && skipTarget > 6000) // double skip exception for storyboards with very long intros skipTarget = 0; Seek(skipTarget); } protected override IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) { // 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. platformOffsetClock = new OffsetCorrectionClock(source, pauseFreqAdjust) { Offset = RuntimeInfo.OS == RuntimeInfo.Platform.Windows ? 15 : 0 }; // the final usable gameplay clock with user-set offsets applied. userGlobalOffsetClock = new OffsetCorrectionClock(platformOffsetClock, pauseFreqAdjust); return userBeatmapOffsetClock = new OffsetCorrectionClock(userGlobalOffsetClock, pauseFreqAdjust); } /// /// Changes the backing clock to avoid using the originally provided track. /// public void StopUsingBeatmapClock() { removeSourceClockAdjustments(); ChangeSource(new TrackVirtual(beatmap.Track.Length)); addSourceClockAdjustments(); } private bool speedAdjustmentsApplied; private void addSourceClockAdjustments() { if (speedAdjustmentsApplied) return; if (SourceClock is not Track track) return; track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust); track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate); nonGameplayAdjustments.Add(pauseFreqAdjust); nonGameplayAdjustments.Add(UserPlaybackRate); speedAdjustmentsApplied = true; } private void removeSourceClockAdjustments() { if (!speedAdjustmentsApplied) return; if (SourceClock is not Track track) return; track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust); track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate); nonGameplayAdjustments.Remove(pauseFreqAdjust); nonGameplayAdjustments.Remove(UserPlaybackRate); speedAdjustmentsApplied = false; } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); beatmapOffsetSubscription?.Dispose(); removeSourceClockAdjustments(); } ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo; IClock IBeatSyncProvider.Clock => this; ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : ChannelAmplitudes.Empty; } }