2019-03-06 11:30:14 +00: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;
|
2019-04-08 09:32:05 +00:00
|
|
|
using System.Collections.Generic;
|
2019-03-06 11:30:14 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
2019-03-12 09:14:01 +00:00
|
|
|
using osu.Framework.Audio;
|
2019-11-01 05:11:18 +00:00
|
|
|
using osu.Framework.Audio.Track;
|
2019-03-06 11:30:14 +00:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-03-17 15:46:15 +00:00
|
|
|
/// Encapsulates gameplay timing logic and provides a <see cref="Play.GameplayClock"/> for children.
|
2019-03-06 11:30:14 +00:00
|
|
|
/// </summary>
|
|
|
|
public class GameplayClockContainer : Container
|
|
|
|
{
|
|
|
|
private readonly WorkingBeatmap beatmap;
|
2019-04-10 08:13:12 +00:00
|
|
|
private readonly IReadOnlyList<Mod> mods;
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2019-12-09 08:34:04 +00:00
|
|
|
/// The <see cref="WorkingBeatmap"/>'s track.
|
2019-03-06 11:30:14 +00:00
|
|
|
/// </summary>
|
2019-12-09 08:34:04 +00:00
|
|
|
private Track track;
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
public readonly BindableBool IsPaused = new BindableBool();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The decoupled clock used for gameplay. Should be used for seeks and clock control.
|
|
|
|
/// </summary>
|
|
|
|
private readonly DecoupleableInterpolatingFramedClock adjustableClock;
|
|
|
|
|
2019-04-03 07:58:20 +00:00
|
|
|
private readonly double gameplayStartTime;
|
|
|
|
|
2019-11-21 07:55:18 +00:00
|
|
|
private readonly double firstHitObjectTime;
|
|
|
|
|
2019-12-09 10:41:31 +00:00
|
|
|
public readonly BindableNumber<double> UserPlaybackRate = new BindableDouble(1)
|
2019-03-06 11:30:14 +00:00
|
|
|
{
|
|
|
|
Default = 1,
|
|
|
|
MinValue = 0.5,
|
|
|
|
MaxValue = 2,
|
|
|
|
Precision = 0.1,
|
|
|
|
};
|
|
|
|
|
2019-03-08 06:05:45 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The final clock which is exposed to underlying components.
|
|
|
|
/// </summary>
|
|
|
|
[Cached]
|
2019-03-17 15:46:15 +00:00
|
|
|
public readonly GameplayClock GameplayClock;
|
2019-03-08 06:05:45 +00:00
|
|
|
|
2019-03-06 11:30:14 +00:00
|
|
|
private Bindable<double> userAudioOffset;
|
|
|
|
|
2019-04-03 07:58:20 +00:00
|
|
|
private readonly FramedOffsetClock userOffsetClock;
|
|
|
|
|
|
|
|
private readonly FramedOffsetClock platformOffsetClock;
|
2019-03-06 11:30:14 +00:00
|
|
|
|
2019-11-22 05:14:18 +00:00
|
|
|
public GameplayClockContainer(WorkingBeatmap beatmap, IReadOnlyList<Mod> mods, double gameplayStartTime)
|
2019-03-06 11:30:14 +00:00
|
|
|
{
|
|
|
|
this.beatmap = beatmap;
|
2019-04-08 09:32:05 +00:00
|
|
|
this.mods = mods;
|
2019-04-03 07:58:20 +00:00
|
|
|
this.gameplayStartTime = gameplayStartTime;
|
2019-11-26 10:41:19 +00:00
|
|
|
firstHitObjectTime = beatmap.Beatmap.HitObjects.First().StartTime;
|
2019-03-06 11:30:14 +00:00
|
|
|
|
2019-03-08 06:05:45 +00:00
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
2019-12-09 08:34:04 +00:00
|
|
|
track = beatmap.Track;
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
adjustableClock = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
|
|
|
|
|
|
|
|
// 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.
|
2020-02-07 08:51:38 +00:00
|
|
|
platformOffsetClock = new FramedOffsetClock(adjustableClock) { Offset = RuntimeInfo.OS == RuntimeInfo.Platform.Windows ? 15 : 0 };
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
// the final usable gameplay clock with user-set offsets applied.
|
2019-04-03 07:58:20 +00:00
|
|
|
userOffsetClock = new FramedOffsetClock(platformOffsetClock);
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
// the clock to be exposed via DI to children.
|
2019-04-03 07:58:20 +00:00
|
|
|
GameplayClock = new GameplayClock(userOffsetClock);
|
2019-03-25 11:25:47 +00:00
|
|
|
|
|
|
|
GameplayClock.IsPaused.BindTo(IsPaused);
|
2019-03-06 11:30:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 07:58:20 +00:00
|
|
|
private double totalOffset => userOffsetClock.Offset + platformOffsetClock.Offset;
|
|
|
|
|
2019-11-21 06:19:06 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Duration before gameplay start time required before skip button displays.
|
|
|
|
/// </summary>
|
|
|
|
public const double MINIMUM_SKIP_TIME = 1000;
|
|
|
|
|
2019-06-09 15:56:35 +00:00
|
|
|
private readonly BindableDouble pauseFreqAdjust = new BindableDouble(1);
|
|
|
|
|
2019-03-06 11:30:14 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuConfigManager config)
|
|
|
|
{
|
|
|
|
userAudioOffset = config.GetBindable<double>(OsuSetting.AudioOffset);
|
2019-04-03 07:58:20 +00:00
|
|
|
userAudioOffset.BindValueChanged(offset => userOffsetClock.Offset = offset.NewValue, true);
|
2019-03-06 11:30:14 +00:00
|
|
|
|
2019-11-21 07:55:18 +00:00
|
|
|
// sane default provided by ruleset.
|
|
|
|
double startTime = Math.Min(0, gameplayStartTime);
|
2019-03-26 07:18:15 +00:00
|
|
|
|
2019-11-21 07:55:18 +00: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.
|
2019-03-26 07:18:15 +00:00
|
|
|
startTime = Math.Min(startTime, beatmap.Storyboard.FirstEventTime);
|
2019-11-21 07:55:18 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (beatmap.BeatmapInfo.AudioLeadIn > 0)
|
|
|
|
startTime = Math.Min(startTime, firstHitObjectTime - beatmap.BeatmapInfo.AudioLeadIn);
|
2019-03-26 07:18:15 +00:00
|
|
|
|
|
|
|
Seek(startTime);
|
2019-03-26 07:17:20 +00:00
|
|
|
|
2019-11-21 06:19:06 +00:00
|
|
|
adjustableClock.ProcessFrame();
|
2019-03-06 11:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Restart()
|
|
|
|
{
|
2020-03-03 04:38:10 +00:00
|
|
|
// The Reset() call below causes speed adjustments to be reset in an async context, leading to deadlocks.
|
|
|
|
// The deadlock can be prevented by resetting the track synchronously before entering the async context.
|
|
|
|
track.ResetSpeedAdjustments();
|
|
|
|
|
2019-03-06 11:30:14 +00:00
|
|
|
Task.Run(() =>
|
|
|
|
{
|
2019-12-09 08:34:04 +00:00
|
|
|
track.Reset();
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
2019-12-09 08:34:04 +00:00
|
|
|
adjustableClock.ChangeSource(track);
|
2019-03-06 11:30:14 +00:00
|
|
|
updateRate();
|
|
|
|
|
2019-04-22 08:06:40 +00:00
|
|
|
if (!IsPaused.Value)
|
|
|
|
Start();
|
2019-03-06 11:30:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
// Seeking the decoupled clock to its current time ensures that its source clock will be seeked to the same time
|
|
|
|
// This accounts for the audio clock source potentially taking time to enter a completely stopped state
|
2019-04-22 09:45:58 +00:00
|
|
|
Seek(GameplayClock.CurrentTime);
|
2019-03-06 11:30:14 +00:00
|
|
|
adjustableClock.Start();
|
2019-03-16 05:20:10 +00:00
|
|
|
IsPaused.Value = false;
|
2019-06-09 15:56:35 +00:00
|
|
|
|
|
|
|
this.TransformBindableTo(pauseFreqAdjust, 1, 200, Easing.In);
|
2019-03-06 11:30:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:19:06 +00:00
|
|
|
/// <summary>
|
2019-11-21 09:57:19 +00:00
|
|
|
/// Skip forward to the next valid skip point.
|
2019-11-21 06:19:06 +00:00
|
|
|
/// </summary>
|
|
|
|
public void Skip()
|
|
|
|
{
|
|
|
|
if (GameplayClock.CurrentTime > gameplayStartTime - MINIMUM_SKIP_TIME)
|
|
|
|
return;
|
|
|
|
|
|
|
|
double skipTarget = gameplayStartTime - MINIMUM_SKIP_TIME;
|
|
|
|
|
|
|
|
if (GameplayClock.CurrentTime < 0 && skipTarget > 6000)
|
|
|
|
// double skip exception for storyboards with very long intros
|
|
|
|
skipTarget = 0;
|
|
|
|
|
|
|
|
Seek(skipTarget);
|
|
|
|
}
|
|
|
|
|
2019-04-03 08:01:11 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Seek to a specific time in gameplay.
|
|
|
|
/// <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>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="time">The destination time to seek to.</param>
|
2019-04-03 07:58:20 +00:00
|
|
|
public 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.
|
|
|
|
adjustableClock.Seek(time - totalOffset);
|
2019-04-24 05:01:46 +00:00
|
|
|
|
|
|
|
// manually process frame to ensure GameplayClock is correctly updated after a seek.
|
|
|
|
userOffsetClock.ProcessFrame();
|
2019-04-03 07:58:20 +00:00
|
|
|
}
|
2019-03-06 11:30:14 +00:00
|
|
|
|
2019-03-16 05:20:10 +00:00
|
|
|
public void Stop()
|
|
|
|
{
|
2019-06-09 16:14:46 +00:00
|
|
|
this.TransformBindableTo(pauseFreqAdjust, 0, 200, Easing.Out).OnComplete(_ => adjustableClock.Stop());
|
2019-06-09 15:56:35 +00:00
|
|
|
|
2019-03-16 05:20:10 +00:00
|
|
|
IsPaused.Value = true;
|
|
|
|
}
|
2019-03-06 11:30:14 +00:00
|
|
|
|
2019-11-01 05:11:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Changes the backing clock to avoid using the originally provided beatmap's track.
|
|
|
|
/// </summary>
|
|
|
|
public void StopUsingBeatmapClock()
|
2019-03-06 11:30:14 +00:00
|
|
|
{
|
2019-12-09 08:34:04 +00:00
|
|
|
if (track != beatmap.Track)
|
2019-11-01 05:11:18 +00:00
|
|
|
return;
|
|
|
|
|
2019-11-06 01:38:04 +00:00
|
|
|
removeSourceClockAdjustments();
|
|
|
|
|
2019-12-09 08:34:04 +00:00
|
|
|
track = new TrackVirtual(beatmap.Track.Length);
|
|
|
|
adjustableClock.ChangeSource(track);
|
2019-03-06 11:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
if (!IsPaused.Value)
|
2019-04-03 07:58:20 +00:00
|
|
|
userOffsetClock.ProcessFrame();
|
2019-03-06 11:30:14 +00:00
|
|
|
|
|
|
|
base.Update();
|
|
|
|
}
|
|
|
|
|
2019-12-06 04:49:01 +00:00
|
|
|
private bool speedAdjustmentsApplied;
|
|
|
|
|
2019-03-06 11:30:14 +00:00
|
|
|
private void updateRate()
|
|
|
|
{
|
2019-12-09 08:34:04 +00:00
|
|
|
if (track == null) return;
|
2019-03-06 11:30:14 +00:00
|
|
|
|
2019-12-06 04:49:01 +00:00
|
|
|
speedAdjustmentsApplied = true;
|
2019-12-09 08:34:04 +00:00
|
|
|
track.ResetSpeedAdjustments();
|
2019-03-12 09:14:01 +00:00
|
|
|
|
2019-12-09 10:41:31 +00:00
|
|
|
track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
|
|
|
|
track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
|
2019-03-11 17:45:45 +00:00
|
|
|
|
2019-12-09 08:34:04 +00:00
|
|
|
foreach (var mod in mods.OfType<IApplicableToTrack>())
|
|
|
|
mod.ApplyToTrack(track);
|
2019-03-06 11:30:14 +00:00
|
|
|
}
|
2019-06-09 16:14:46 +00:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2019-11-06 01:38:04 +00:00
|
|
|
|
|
|
|
removeSourceClockAdjustments();
|
2019-12-09 08:34:04 +00:00
|
|
|
track = null;
|
2019-11-06 01:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void removeSourceClockAdjustments()
|
|
|
|
{
|
2019-12-06 04:49:01 +00:00
|
|
|
if (speedAdjustmentsApplied)
|
|
|
|
{
|
2019-12-09 08:34:04 +00:00
|
|
|
track.ResetSpeedAdjustments();
|
2019-12-06 04:49:01 +00:00
|
|
|
speedAdjustmentsApplied = false;
|
|
|
|
}
|
2019-06-09 16:14:46 +00:00
|
|
|
}
|
2019-03-06 11:30:14 +00:00
|
|
|
}
|
|
|
|
}
|