2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio.Track;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
{
|
|
|
|
public class BeatSyncedContainer : Container
|
|
|
|
{
|
2018-05-23 16:37:39 +08:00
|
|
|
protected readonly IBindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
private int lastBeat;
|
|
|
|
private TimingControlPoint lastTimingPoint;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-23 12:49:18 +08:00
|
|
|
/// The amount of time before a beat we should fire <see cref="OnNewBeat(int, TimingControlPoint, EffectControlPoint, ChannelAmplitudes)"/>.
|
2018-04-13 17:19:50 +08:00
|
|
|
/// This allows for adding easing to animations that may be synchronised to the beat.
|
|
|
|
/// </summary>
|
|
|
|
protected double EarlyActivationMilliseconds;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The time in milliseconds until the next beat.
|
|
|
|
/// </summary>
|
|
|
|
public double TimeUntilNextBeat { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The time in milliseconds since the last beat
|
|
|
|
/// </summary>
|
|
|
|
public double TimeSinceLastBeat { get; private set; }
|
|
|
|
|
2019-12-16 18:16:54 +08:00
|
|
|
/// <summary>
|
2019-12-18 16:58:29 +08:00
|
|
|
/// How many beats per beatlength to trigger. Defaults to 1.
|
2019-12-16 18:16:54 +08:00
|
|
|
/// </summary>
|
|
|
|
public int Divisor { get; set; } = 1;
|
|
|
|
|
2020-01-14 00:39:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// An optional minimum beat length. Any beat length below this will be multiplied by two until valid.
|
|
|
|
/// </summary>
|
|
|
|
public double MinimumBeatLength { get; set; }
|
|
|
|
|
2019-12-16 17:51:22 +08:00
|
|
|
protected bool IsBeatSyncedWithTrack { get; private set; }
|
|
|
|
|
2019-09-03 06:57:29 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
Track track = null;
|
|
|
|
IBeatmap beatmap = null;
|
|
|
|
|
2020-02-09 20:25:28 +08:00
|
|
|
double currentTrackTime = 0;
|
|
|
|
TimingControlPoint timingPoint = null;
|
|
|
|
EffectControlPoint effectPoint = null;
|
2019-09-03 06:57:29 +08:00
|
|
|
|
|
|
|
if (Beatmap.Value.TrackLoaded && Beatmap.Value.BeatmapLoaded)
|
|
|
|
{
|
|
|
|
track = Beatmap.Value.Track;
|
|
|
|
beatmap = Beatmap.Value.Beatmap;
|
|
|
|
}
|
|
|
|
|
2020-02-09 20:25:28 +08:00
|
|
|
if (track != null && beatmap != null && track.IsRunning && track.Length > 0)
|
2019-09-03 06:57:29 +08:00
|
|
|
{
|
2020-02-09 20:25:28 +08:00
|
|
|
currentTrackTime = track.CurrentTime + EarlyActivationMilliseconds;
|
2019-09-03 06:57:29 +08:00
|
|
|
|
|
|
|
timingPoint = beatmap.ControlPointInfo.TimingPointAt(currentTrackTime);
|
|
|
|
effectPoint = beatmap.ControlPointInfo.EffectPointAt(currentTrackTime);
|
2020-02-09 20:25:28 +08:00
|
|
|
}
|
2019-09-03 06:57:29 +08:00
|
|
|
|
2020-02-09 20:25:28 +08:00
|
|
|
IsBeatSyncedWithTrack = timingPoint?.BeatLength > 0;
|
2019-12-16 17:51:22 +08:00
|
|
|
|
2020-02-09 20:25:28 +08:00
|
|
|
if (timingPoint == null || !IsBeatSyncedWithTrack)
|
2019-09-03 06:57:29 +08:00
|
|
|
{
|
|
|
|
currentTrackTime = Clock.CurrentTime;
|
2020-07-18 10:53:04 +08:00
|
|
|
timingPoint = TimingControlPoint.DEFAULT;
|
|
|
|
effectPoint = EffectControlPoint.DEFAULT;
|
2019-09-03 06:57:29 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-12-16 18:16:54 +08:00
|
|
|
double beatLength = timingPoint.BeatLength / Divisor;
|
|
|
|
|
2020-01-14 00:39:45 +08:00
|
|
|
while (beatLength < MinimumBeatLength)
|
|
|
|
beatLength *= 2;
|
|
|
|
|
2019-12-17 11:06:09 +08:00
|
|
|
int beatIndex = (int)((currentTrackTime - timingPoint.Time) / beatLength) - (effectPoint.OmitFirstBarLine ? 1 : 0);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
// The beats before the start of the first control point are off by 1, this should do the trick
|
|
|
|
if (currentTrackTime < timingPoint.Time)
|
|
|
|
beatIndex--;
|
|
|
|
|
2019-12-16 18:16:54 +08:00
|
|
|
TimeUntilNextBeat = (timingPoint.Time - currentTrackTime) % beatLength;
|
2018-04-13 17:19:50 +08:00
|
|
|
if (TimeUntilNextBeat < 0)
|
2019-12-16 18:16:54 +08:00
|
|
|
TimeUntilNextBeat += beatLength;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-12-16 18:16:54 +08:00
|
|
|
TimeSinceLastBeat = beatLength - TimeUntilNextBeat;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2020-04-17 16:04:09 +08:00
|
|
|
if (timingPoint == lastTimingPoint && beatIndex == lastBeat)
|
2018-04-13 17:19:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
using (BeginDelayedSequence(-TimeSinceLastBeat, true))
|
2020-06-23 20:30:37 +08:00
|
|
|
OnNewBeat(beatIndex, timingPoint, effectPoint, track?.CurrentAmplitudes ?? ChannelAmplitudes.Empty);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
lastBeat = beatIndex;
|
|
|
|
lastTimingPoint = timingPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2019-02-01 14:42:15 +08:00
|
|
|
private void load(IBindable<WorkingBeatmap> beatmap)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2018-05-23 16:37:39 +08:00
|
|
|
Beatmap.BindTo(beatmap);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:49:18 +08:00
|
|
|
protected virtual void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|