Improve IBeatSyncProvider interface and reduce beatmap track dependence

This commit is contained in:
Dean Herbert
2022-08-02 17:54:42 +09:00
parent 227906e30e
commit cc4cde2c79
6 changed files with 88 additions and 88 deletions

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio.Track; using osu.Framework.Audio;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
@ -14,12 +14,21 @@ namespace osu.Game.Beatmaps
/// Primarily intended for use with <see cref="BeatSyncedContainer"/>. /// Primarily intended for use with <see cref="BeatSyncedContainer"/>.
/// </summary> /// </summary>
[Cached] [Cached]
public interface IBeatSyncProvider public interface IBeatSyncProvider : IHasAmplitudes
{ {
/// <summary>
/// Check whether beat sync is currently available.
/// </summary>
public bool BeatSyncAvailable => Clock != null;
/// <summary>
/// Access any available control points from a beatmap providing beat sync. If <c>null</c>, no current provider is available.
/// </summary>
ControlPointInfo? ControlPoints { get; } ControlPointInfo? ControlPoints { get; }
/// <summary>
/// Access a clock currently responsible for providing beat sync. If <c>null</c>, no current provider is available.
/// </summary>
IClock? Clock { get; } IClock? Clock { get; }
ChannelAmplitudes? Amplitudes { get; }
} }
} }

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using osu.Framework.Allocation; using osu.Framework.Allocation;
@ -10,13 +8,12 @@ using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Screens.Play;
namespace osu.Game.Graphics.Containers namespace osu.Game.Graphics.Containers
{ {
/// <summary> /// <summary>
/// A container which fires a callback when a new beat is reached. /// A container which fires a callback when a new beat is reached.
/// Consumes a parent <see cref="GameplayClock"/> or <see cref="Beatmap"/> (whichever is first available). /// Consumes a parent <see cref="IBeatSyncProvider"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This container does not set its own clock to the source used for beat matching. /// This container does not set its own clock to the source used for beat matching.
@ -28,8 +25,9 @@ namespace osu.Game.Graphics.Containers
public class BeatSyncedContainer : Container public class BeatSyncedContainer : Container
{ {
private int lastBeat; private int lastBeat;
protected TimingControlPoint LastTimingPoint { get; private set; }
protected EffectControlPoint LastEffectPoint { get; private set; } protected TimingControlPoint? LastTimingPoint { get; private set; }
protected EffectControlPoint? LastEffectPoint { get; private set; }
/// <summary> /// <summary>
/// The amount of time before a beat we should fire <see cref="OnNewBeat(int, TimingControlPoint, EffectControlPoint, ChannelAmplitudes)"/>. /// The amount of time before a beat we should fire <see cref="OnNewBeat(int, TimingControlPoint, EffectControlPoint, ChannelAmplitudes)"/>.
@ -71,12 +69,12 @@ namespace osu.Game.Graphics.Containers
public double MinimumBeatLength { get; set; } public double MinimumBeatLength { get; set; }
/// <summary> /// <summary>
/// Whether this container is currently tracking a beatmap's timing data. /// Whether this container is currently tracking a beat sync provider.
/// </summary> /// </summary>
protected bool IsBeatSyncedWithTrack { get; private set; } protected bool IsBeatSyncedWithTrack { get; private set; }
[Resolved] [Resolved]
protected IBeatSyncProvider BeatSyncSource { get; private set; } protected IBeatSyncProvider BeatSyncSource { get; private set; } = null!;
protected virtual void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes) protected virtual void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
{ {
@ -87,19 +85,18 @@ namespace osu.Game.Graphics.Containers
TimingControlPoint timingPoint; TimingControlPoint timingPoint;
EffectControlPoint effectPoint; EffectControlPoint effectPoint;
IsBeatSyncedWithTrack = BeatSyncSource.Clock?.IsRunning == true && BeatSyncSource.ControlPoints != null; IsBeatSyncedWithTrack = BeatSyncSource.BeatSyncAvailable && BeatSyncSource.Clock?.IsRunning == true;
double currentTrackTime; double currentTrackTime;
if (IsBeatSyncedWithTrack) if (IsBeatSyncedWithTrack)
{ {
Debug.Assert(BeatSyncSource.ControlPoints != null);
Debug.Assert(BeatSyncSource.Clock != null); Debug.Assert(BeatSyncSource.Clock != null);
currentTrackTime = BeatSyncSource.Clock.CurrentTime + EarlyActivationMilliseconds; currentTrackTime = BeatSyncSource.Clock.CurrentTime + EarlyActivationMilliseconds;
timingPoint = BeatSyncSource.ControlPoints.TimingPointAt(currentTrackTime); timingPoint = BeatSyncSource.ControlPoints?.TimingPointAt(currentTrackTime) ?? TimingControlPoint.DEFAULT;
effectPoint = BeatSyncSource.ControlPoints.EffectPointAt(currentTrackTime); effectPoint = BeatSyncSource.ControlPoints?.EffectPointAt(currentTrackTime) ?? EffectControlPoint.DEFAULT;
} }
else else
{ {
@ -136,7 +133,7 @@ namespace osu.Game.Graphics.Containers
if (AllowMistimedEventFiring || Math.Abs(TimeSinceLastBeat) < MISTIMED_ALLOWANCE) if (AllowMistimedEventFiring || Math.Abs(TimeSinceLastBeat) < MISTIMED_ALLOWANCE)
{ {
using (BeginDelayedSequence(-TimeSinceLastBeat)) using (BeginDelayedSequence(-TimeSinceLastBeat))
OnNewBeat(beatIndex, timingPoint, effectPoint, BeatSyncSource.Amplitudes ?? ChannelAmplitudes.Empty); OnNewBeat(beatIndex, timingPoint, effectPoint, BeatSyncSource.CurrentAmplitudes);
} }
lastBeat = beatIndex; lastBeat = beatIndex;

View File

@ -588,6 +588,6 @@ namespace osu.Game
ControlPointInfo IBeatSyncProvider.ControlPoints => Beatmap.Value.BeatmapLoaded ? Beatmap.Value.Beatmap.ControlPointInfo : null; ControlPointInfo IBeatSyncProvider.ControlPoints => Beatmap.Value.BeatmapLoaded ? Beatmap.Value.Beatmap.ControlPointInfo : null;
IClock IBeatSyncProvider.Clock => Beatmap.Value.TrackLoaded ? Beatmap.Value.Track : (IClock)null; IClock IBeatSyncProvider.Clock => Beatmap.Value.TrackLoaded ? Beatmap.Value.Track : (IClock)null;
ChannelAmplitudes? IBeatSyncProvider.Amplitudes => Beatmap.Value.TrackLoaded ? Beatmap.Value.Track.CurrentAmplitudes : null; ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => Beatmap.Value.TrackLoaded ? Beatmap.Value.Track.CurrentAmplitudes : ChannelAmplitudes.Empty;
} }
} }

View File

@ -10,6 +10,7 @@ using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework; using osu.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -949,7 +950,7 @@ namespace osu.Game.Screens.Edit
ControlPointInfo IBeatSyncProvider.ControlPoints => editorBeatmap.ControlPointInfo; ControlPointInfo IBeatSyncProvider.ControlPoints => editorBeatmap.ControlPointInfo;
IClock IBeatSyncProvider.Clock => clock; IClock IBeatSyncProvider.Clock => clock;
ChannelAmplitudes? IBeatSyncProvider.Amplitudes => Beatmap.Value.TrackLoaded ? Beatmap.Value.Track.CurrentAmplitudes : null; ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => Beatmap.Value.TrackLoaded ? Beatmap.Value.Track.CurrentAmplitudes : ChannelAmplitudes.Empty;
private class BeatmapEditorToast : Toast private class BeatmapEditorToast : Toast
{ {

View File

@ -1,10 +1,12 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable using System;
using System.Collections.Generic;
using osuTK; using osu.Framework.Allocation;
using osuTK.Graphics; using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Batches; using osu.Framework.Graphics.Batches;
using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Colour;
@ -12,16 +14,10 @@ using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shaders; using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps;
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Framework.Extensions.Color4Extensions; using osu.Game.Beatmaps;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Menu namespace osu.Game.Screens.Menu
{ {
@ -30,8 +26,6 @@ namespace osu.Game.Screens.Menu
/// </summary> /// </summary>
public class LogoVisualisation : Drawable public class LogoVisualisation : Drawable
{ {
private readonly IBindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
/// <summary> /// <summary>
/// The number of bars to jump each update iteration. /// The number of bars to jump each update iteration.
/// </summary> /// </summary>
@ -76,7 +70,8 @@ namespace osu.Game.Screens.Menu
private readonly float[] frequencyAmplitudes = new float[256]; private readonly float[] frequencyAmplitudes = new float[256];
private IShader shader; private IShader shader = null!;
private readonly Texture texture; private readonly Texture texture;
public LogoVisualisation() public LogoVisualisation()
@ -93,32 +88,35 @@ namespace osu.Game.Screens.Menu
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(ShaderManager shaders, IBindable<WorkingBeatmap> beatmap) private void load(ShaderManager shaders)
{ {
this.beatmap.BindTo(beatmap);
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED); shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
} }
private readonly float[] temporalAmplitudes = new float[ChannelAmplitudes.AMPLITUDES_SIZE]; private readonly float[] temporalAmplitudes = new float[ChannelAmplitudes.AMPLITUDES_SIZE];
[Resolved]
private IBeatSyncProvider beatSyncProvider { get; set; } = null!;
private void updateAmplitudes() private void updateAmplitudes()
{ {
var effect = beatmap.Value.BeatmapLoaded && beatmap.Value.TrackLoaded bool isKiaiTime = false;
? beatmap.Value.Beatmap?.ControlPointInfo.EffectPointAt(beatmap.Value.Track.CurrentTime)
: null;
for (int i = 0; i < temporalAmplitudes.Length; i++) for (int i = 0; i < temporalAmplitudes.Length; i++)
temporalAmplitudes[i] = 0; temporalAmplitudes[i] = 0;
if (beatmap.Value.TrackLoaded) if (beatSyncProvider.Clock != null)
addAmplitudesFromSource(beatmap.Value.Track); {
isKiaiTime = beatSyncProvider.ControlPoints?.EffectPointAt(beatSyncProvider.Clock.CurrentTime).KiaiMode ?? false;
addAmplitudesFromSource(beatSyncProvider);
}
foreach (var source in amplitudeSources) foreach (var source in amplitudeSources)
addAmplitudesFromSource(source); addAmplitudesFromSource(source);
for (int i = 0; i < bars_per_visualiser; i++) for (int i = 0; i < bars_per_visualiser; i++)
{ {
float targetAmplitude = (temporalAmplitudes[(i + indexOffset) % bars_per_visualiser]) * (effect?.KiaiMode == true ? 1 : 0.5f); float targetAmplitude = (temporalAmplitudes[(i + indexOffset) % bars_per_visualiser]) * (isKiaiTime ? 1 : 0.5f);
if (targetAmplitude > frequencyAmplitudes[i]) if (targetAmplitude > frequencyAmplitudes[i])
frequencyAmplitudes[i] = targetAmplitude; frequencyAmplitudes[i] = targetAmplitude;
} }
@ -153,7 +151,7 @@ namespace osu.Game.Screens.Menu
protected override DrawNode CreateDrawNode() => new VisualisationDrawNode(this); protected override DrawNode CreateDrawNode() => new VisualisationDrawNode(this);
private void addAmplitudesFromSource([NotNull] IHasAmplitudes source) private void addAmplitudesFromSource(IHasAmplitudes source)
{ {
if (source == null) throw new ArgumentNullException(nameof(source)); if (source == null) throw new ArgumentNullException(nameof(source));
@ -170,8 +168,8 @@ namespace osu.Game.Screens.Menu
{ {
protected new LogoVisualisation Source => (LogoVisualisation)base.Source; protected new LogoVisualisation Source => (LogoVisualisation)base.Source;
private IShader shader; private IShader shader = null!;
private Texture texture; private Texture texture = null!;
// Assuming the logo is a circle, we don't need a second dimension. // Assuming the logo is a circle, we don't need a second dimension.
private float size; private float size;
@ -209,8 +207,6 @@ namespace osu.Game.Screens.Menu
ColourInfo colourInfo = DrawColourInfo.Colour; ColourInfo colourInfo = DrawColourInfo.Colour;
colourInfo.ApplyChild(transparent_white); colourInfo.ApplyChild(transparent_white);
if (audioData != null)
{
for (int j = 0; j < visualiser_rounds; j++) for (int j = 0; j < visualiser_rounds; j++)
{ {
for (int i = 0; i < bars_per_visualiser; i++) for (int i = 0; i < bars_per_visualiser; i++)
@ -247,7 +243,6 @@ namespace osu.Game.Screens.Menu
Vector2.Divide(inflation, barSize.Yx)); Vector2.Divide(inflation, barSize.Yx));
} }
} }
}
shader.Unbind(); shader.Unbind();
} }

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -53,21 +51,21 @@ namespace osu.Game.Screens.Play
private readonly WorkingBeatmap beatmap; private readonly WorkingBeatmap beatmap;
private HardwareCorrectionOffsetClock userGlobalOffsetClock; private HardwareCorrectionOffsetClock userGlobalOffsetClock = null!;
private HardwareCorrectionOffsetClock userBeatmapOffsetClock; private HardwareCorrectionOffsetClock userBeatmapOffsetClock = null!;
private HardwareCorrectionOffsetClock platformOffsetClock; private HardwareCorrectionOffsetClock platformOffsetClock = null!;
private MasterGameplayClock masterGameplayClock; private MasterGameplayClock masterGameplayClock = null!;
private Bindable<double> userAudioOffset; private Bindable<double> userAudioOffset = null!;
private IDisposable beatmapOffsetSubscription; private IDisposable? beatmapOffsetSubscription;
private readonly double skipTargetTime; private readonly double skipTargetTime;
[Resolved] [Resolved]
private RealmAccess realm { get; set; } private RealmAccess realm { get; set; } = null!;
[Resolved] [Resolved]
private OsuConfigManager config { get; set; } private OsuConfigManager config { get; set; } = null!;
/// <summary> /// <summary>
/// Create a new master gameplay clock container. /// Create a new master gameplay clock container.
@ -255,7 +253,7 @@ namespace osu.Game.Screens.Play
ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo; ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo;
IClock IBeatSyncProvider.Clock => GameplayClock; IClock IBeatSyncProvider.Clock => GameplayClock;
ChannelAmplitudes? IBeatSyncProvider.Amplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : null; ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : ChannelAmplitudes.Empty;
private class HardwareCorrectionOffsetClock : FramedOffsetClock private class HardwareCorrectionOffsetClock : FramedOffsetClock
{ {