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
|
|
|
|
2021-07-15 15:13:13 +08:00
|
|
|
using System;
|
2017-05-17 14:18:56 +08:00
|
|
|
using osu.Framework.Allocation;
|
2017-05-23 15:11:46 +08:00
|
|
|
using osu.Framework.Audio.Track;
|
2017-05-17 14:14:04 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Beatmaps;
|
2017-05-23 12:55:18 +08:00
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-05-17 14:14:04 +08:00
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
{
|
2021-07-15 13:30:38 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A container which fires a callback when a new beat is reached.
|
2022-08-02 16:54:42 +08:00
|
|
|
/// Consumes a parent <see cref="IBeatSyncProvider"/>.
|
2021-07-15 13:30:38 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This container does not set its own clock to the source used for beat matching.
|
|
|
|
/// This means that if the beat source clock is playing faster or slower, animations may unexpectedly overlap.
|
|
|
|
/// Make sure this container's Clock is also set to the expected source (or within a parent element which provides this).
|
2021-07-15 13:56:49 +08:00
|
|
|
///
|
|
|
|
/// This container will also trigger beat events when the beat matching clock is paused at <see cref="TimingControlPoint.DEFAULT"/>'s BPM.
|
2021-07-15 13:30:38 +08:00
|
|
|
/// </remarks>
|
2017-05-17 14:14:04 +08:00
|
|
|
public partial class BeatSyncedContainer : Container
|
|
|
|
{
|
2017-05-19 09:19:00 +08:00
|
|
|
private int lastBeat;
|
2022-08-02 16:54:42 +08:00
|
|
|
|
2022-08-02 17:18:40 +08:00
|
|
|
private TimingControlPoint? lastTimingPoint { get; set; }
|
|
|
|
|
2022-08-03 16:47:32 +08:00
|
|
|
protected bool IsKiaiTime { get; private set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-05-23 11:29:43 +08:00
|
|
|
/// <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)"/>.
|
2017-05-23 11:29:43 +08:00
|
|
|
/// This allows for adding easing to animations that may be synchronised to the beat.
|
|
|
|
/// </summary>
|
|
|
|
protected double EarlyActivationMilliseconds;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-07-15 15:13:13 +08:00
|
|
|
/// <summary>
|
|
|
|
/// While this container automatically applied an animation delay (meaning any animations inside a <see cref="OnNewBeat"/> implementation will
|
|
|
|
/// always be correctly timed), the event itself can potentially fire away from the related beat.
|
|
|
|
///
|
|
|
|
/// By setting this to false, cases where the event is to be fired more than <see cref="MISTIMED_ALLOWANCE"/> from the related beat will be skipped.
|
|
|
|
/// </summary>
|
|
|
|
protected bool AllowMistimedEventFiring = true;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The maximum deviance from the actual beat that an <see cref="OnNewBeat"/> can fire when <see cref="AllowMistimedEventFiring"/> is set to false.
|
|
|
|
/// </summary>
|
|
|
|
public const double MISTIMED_ALLOWANCE = 16;
|
|
|
|
|
2017-07-09 17:23:34 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The time in milliseconds until the next beat.
|
|
|
|
/// </summary>
|
|
|
|
public double TimeUntilNextBeat { get; private set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-07-10 17:07:38 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The time in milliseconds since the last beat
|
|
|
|
/// </summary>
|
|
|
|
public double TimeSinceLastBeat { get; private set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
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; }
|
|
|
|
|
2021-07-15 13:56:49 +08:00
|
|
|
/// <summary>
|
2022-08-02 16:54:42 +08:00
|
|
|
/// Whether this container is currently tracking a beat sync provider.
|
2021-07-15 13:56:49 +08:00
|
|
|
/// </summary>
|
2019-12-16 17:51:22 +08:00
|
|
|
protected bool IsBeatSyncedWithTrack { get; private set; }
|
|
|
|
|
2021-07-15 13:30:38 +08:00
|
|
|
[Resolved]
|
2022-08-02 16:54:42 +08:00
|
|
|
protected IBeatSyncProvider BeatSyncSource { get; private set; } = null!;
|
2022-05-20 22:23:51 +08:00
|
|
|
|
2022-05-22 21:15:53 +08:00
|
|
|
protected virtual void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
|
2021-07-15 13:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-03 06:57:29 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
2021-07-15 13:56:49 +08:00
|
|
|
TimingControlPoint timingPoint;
|
|
|
|
EffectControlPoint effectPoint;
|
2021-07-15 13:30:38 +08:00
|
|
|
|
2023-07-13 21:12:55 +08:00
|
|
|
IsBeatSyncedWithTrack = BeatSyncSource.Clock.IsRunning;
|
2021-07-15 13:30:38 +08:00
|
|
|
|
2022-05-22 21:15:53 +08:00
|
|
|
double currentTrackTime;
|
2021-07-15 13:56:49 +08:00
|
|
|
|
|
|
|
if (IsBeatSyncedWithTrack)
|
2019-09-03 06:57:29 +08:00
|
|
|
{
|
2022-05-22 21:15:53 +08:00
|
|
|
currentTrackTime = BeatSyncSource.Clock.CurrentTime + EarlyActivationMilliseconds;
|
|
|
|
|
2022-08-02 16:54:42 +08:00
|
|
|
timingPoint = BeatSyncSource.ControlPoints?.TimingPointAt(currentTrackTime) ?? TimingControlPoint.DEFAULT;
|
|
|
|
effectPoint = BeatSyncSource.ControlPoints?.EffectPointAt(currentTrackTime) ?? EffectControlPoint.DEFAULT;
|
2020-02-09 20:25:28 +08:00
|
|
|
}
|
2021-07-15 13:56:49 +08:00
|
|
|
else
|
2019-09-03 06:57:29 +08:00
|
|
|
{
|
2021-07-15 13:56:49 +08:00
|
|
|
// this may be the case where the beat syncing clock has been paused.
|
|
|
|
// we still want to show an idle animation, so use this container's time instead.
|
2021-12-05 12:31:55 +08:00
|
|
|
currentTrackTime = Clock.CurrentTime + EarlyActivationMilliseconds;
|
2022-05-22 21:15:53 +08:00
|
|
|
|
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;
|
|
|
|
|
2023-02-28 18:29:31 +08:00
|
|
|
int beatIndex = (int)((currentTrackTime - timingPoint.Time) / beatLength) - (timingPoint.OmitFirstBarLine ? 1 : 0);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-05-22 16:15:23 +08:00
|
|
|
// The beats before the start of the first control point are off by 1, this should do the trick
|
2017-05-23 12:55:18 +08:00
|
|
|
if (currentTrackTime < timingPoint.Time)
|
2017-05-23 15:11:46 +08:00
|
|
|
beatIndex--;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-12-16 18:16:54 +08:00
|
|
|
TimeUntilNextBeat = (timingPoint.Time - currentTrackTime) % beatLength;
|
2021-07-17 13:35:43 +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
|
|
|
|
2022-08-02 17:18:40 +08:00
|
|
|
if (ReferenceEquals(timingPoint, lastTimingPoint) && beatIndex == lastBeat)
|
2017-05-22 16:15:23 +08:00
|
|
|
return;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-07-15 15:13:13 +08:00
|
|
|
// as this event is sometimes used for sound triggers where `BeginDelayedSequence` has no effect, avoid firing it if too far away from the beat.
|
|
|
|
// this can happen after a seek operation.
|
|
|
|
if (AllowMistimedEventFiring || Math.Abs(TimeSinceLastBeat) < MISTIMED_ALLOWANCE)
|
|
|
|
{
|
|
|
|
using (BeginDelayedSequence(-TimeSinceLastBeat))
|
2022-08-02 16:54:42 +08:00
|
|
|
OnNewBeat(beatIndex, timingPoint, effectPoint, BeatSyncSource.CurrentAmplitudes);
|
2021-07-15 15:13:13 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-05-23 15:11:46 +08:00
|
|
|
lastBeat = beatIndex;
|
2022-08-02 17:18:40 +08:00
|
|
|
lastTimingPoint = timingPoint;
|
|
|
|
|
2022-08-03 19:07:54 +08:00
|
|
|
IsKiaiTime = effectPoint.KiaiMode;
|
2017-05-17 14:14:04 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 22:23:04 +08:00
|
|
|
}
|