mirror of
https://github.com/osukey/osukey.git
synced 2025-07-02 08:49:59 +09:00
Use SortedList + BinarySearch to find control points at time values.
This commit is contained in:
@ -55,7 +55,7 @@ namespace osu.Desktop.VisualTests.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
var controlPointInfo = new ControlPointInfo();
|
var controlPointInfo = new ControlPointInfo();
|
||||||
controlPointInfo.ControlPoints.Add(new TimingControlPoint
|
controlPointInfo.TimingPoints.Add(new TimingControlPoint
|
||||||
{
|
{
|
||||||
BeatLength = 200
|
BeatLength = 200
|
||||||
});
|
});
|
||||||
|
@ -2,11 +2,13 @@
|
|||||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
using OpenTK.Input;
|
using OpenTK.Input;
|
||||||
using osu.Framework.Configuration;
|
using osu.Framework.Configuration;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Lists;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Beatmaps.ControlPoints;
|
using osu.Game.Beatmaps.ControlPoints;
|
||||||
using osu.Game.Beatmaps.Timing;
|
using osu.Game.Beatmaps.Timing;
|
||||||
@ -38,11 +40,14 @@ namespace osu.Game.Rulesets.Mania.UI
|
|||||||
double lastSpeedMultiplier = 1;
|
double lastSpeedMultiplier = 1;
|
||||||
double lastBeatLength = 500;
|
double lastBeatLength = 500;
|
||||||
|
|
||||||
// Generate the timing points, making non-timing changes use the previous timing change
|
// Merge timing + difficulty points
|
||||||
var timingChanges = Beatmap.ControlPointInfo.ControlPoints.Where(c => c is TimingControlPoint || c is DifficultyControlPoint).Select(c =>
|
var allPoints = new SortedList<ControlPoint>(Comparer<ControlPoint>.Default);
|
||||||
{
|
allPoints.AddRange(Beatmap.ControlPointInfo.TimingPoints);
|
||||||
var change = new TimingChange();
|
allPoints.AddRange(Beatmap.ControlPointInfo.DifficultyPoints);
|
||||||
|
|
||||||
|
// Generate the timing points, making non-timing changes use the previous timing change
|
||||||
|
var timingChanges = allPoints.Select(c =>
|
||||||
|
{
|
||||||
var timingPoint = c as TimingControlPoint;
|
var timingPoint = c as TimingControlPoint;
|
||||||
var difficultyPoint = c as DifficultyControlPoint;
|
var difficultyPoint = c as DifficultyControlPoint;
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ namespace osu.Game.Rulesets.Taiko.UI
|
|||||||
TaikoHitObject lastObject = Beatmap.HitObjects[Beatmap.HitObjects.Count - 1];
|
TaikoHitObject lastObject = Beatmap.HitObjects[Beatmap.HitObjects.Count - 1];
|
||||||
double lastHitTime = 1 + (lastObject as IHasEndTime)?.EndTime ?? lastObject.StartTime;
|
double lastHitTime = 1 + (lastObject as IHasEndTime)?.EndTime ?? lastObject.StartTime;
|
||||||
|
|
||||||
var timingPoints = Beatmap.ControlPointInfo.ControlPoints.OfType<TimingControlPoint>().ToList();
|
var timingPoints = Beatmap.ControlPointInfo.TimingPoints.ToList();
|
||||||
|
|
||||||
if (timingPoints.Count == 0)
|
if (timingPoints.Count == 0)
|
||||||
return;
|
return;
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace osu.Game.Beatmaps.ControlPoints
|
namespace osu.Game.Beatmaps.ControlPoints
|
||||||
{
|
{
|
||||||
public class ControlPoint
|
public class ControlPoint : IComparable<ControlPoint>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The time at which the control point takes effect.
|
/// The time at which the control point takes effect.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double Time;
|
public double Time;
|
||||||
|
|
||||||
|
public int CompareTo(ControlPoint other) => Time.CompareTo(other.Time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,63 +1,82 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using osu.Framework.Lists;
|
||||||
|
|
||||||
namespace osu.Game.Beatmaps.ControlPoints
|
namespace osu.Game.Beatmaps.ControlPoints
|
||||||
{
|
{
|
||||||
public class ControlPointInfo
|
public class ControlPointInfo
|
||||||
{
|
{
|
||||||
/// <summary>
|
public readonly SortedList<TimingControlPoint> TimingPoints = new SortedList<TimingControlPoint>(Comparer<TimingControlPoint>.Default);
|
||||||
/// All the control points.
|
public readonly SortedList<DifficultyControlPoint> DifficultyPoints = new SortedList<DifficultyControlPoint>(Comparer<DifficultyControlPoint>.Default);
|
||||||
/// </summary>
|
public readonly SortedList<SoundControlPoint> SoundPoints = new SortedList<SoundControlPoint>(Comparer<SoundControlPoint>.Default);
|
||||||
public readonly List<ControlPoint> ControlPoints = new List<ControlPoint>();
|
public readonly SortedList<EffectControlPoint> EffectPoints = new SortedList<EffectControlPoint>(Comparer<EffectControlPoint>.Default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the difficulty control point that is active at <paramref name="time"/>.
|
/// Finds the difficulty control point that is active at <paramref name="time"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time to find the difficulty control point at.</param>
|
/// <param name="time">The time to find the difficulty control point at.</param>
|
||||||
/// <returns>The difficulty control point.</returns>
|
/// <returns>The difficulty control point.</returns>
|
||||||
public DifficultyControlPoint DifficultyPointAt(double time) =>
|
public DifficultyControlPoint DifficultyPointAt(double time) => binarySearch(DifficultyPoints, time);
|
||||||
ControlPoints.OfType<DifficultyControlPoint>().LastOrDefault(t => t.Time <= time) ?? new DifficultyControlPoint();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the effect control point that is active at <paramref name="time"/>.
|
/// Finds the effect control point that is active at <paramref name="time"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time to find the effect control point at.</param>
|
/// <param name="time">The time to find the effect control point at.</param>
|
||||||
/// <returns>The effect control point.</returns>
|
/// <returns>The effect control point.</returns>
|
||||||
public EffectControlPoint EffectPointAt(double time) =>
|
public EffectControlPoint EffectPointAt(double time) => binarySearch(EffectPoints, time);
|
||||||
ControlPoints.OfType<EffectControlPoint>().LastOrDefault(t => t.Time <= time) ?? new EffectControlPoint();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the sound control point that is active at <paramref name="time"/>.
|
/// Finds the sound control point that is active at <paramref name="time"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time to find the sound control point at.</param>
|
/// <param name="time">The time to find the sound control point at.</param>
|
||||||
/// <returns>The sound control point.</returns>
|
/// <returns>The sound control point.</returns>
|
||||||
public SoundControlPoint SoundPointAt(double time) =>
|
public SoundControlPoint SoundPointAt(double time) => binarySearch(SoundPoints, time);
|
||||||
ControlPoints.OfType<SoundControlPoint>().LastOrDefault(t => t.Time <= time) ?? new SoundControlPoint();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the timing control point that is active at <paramref name="time"/>.
|
/// Finds the timing control point that is active at <paramref name="time"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time to find the timing control point at.</param>
|
/// <param name="time">The time to find the timing control point at.</param>
|
||||||
/// <returns>The timing control point.</returns>
|
/// <returns>The timing control point.</returns>
|
||||||
public TimingControlPoint TimingPointAt(double time) =>
|
public TimingControlPoint TimingPointAt(double time) => binarySearch(TimingPoints, time);
|
||||||
ControlPoints.OfType<TimingControlPoint>().LastOrDefault(t => t.Time <= time) ?? new TimingControlPoint();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the maximum BPM represented by any timing control point.
|
/// Finds the maximum BPM represented by any timing control point.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double BPMMaximum =>
|
public double BPMMaximum =>
|
||||||
60000 / (ControlPoints.OfType<TimingControlPoint>().OrderBy(c => c.BeatLength).FirstOrDefault() ?? new TimingControlPoint()).BeatLength;
|
60000 / (TimingPoints.OrderBy(c => c.BeatLength).FirstOrDefault() ?? new TimingControlPoint()).BeatLength;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the minimum BPM represented by any timing control point.
|
/// Finds the minimum BPM represented by any timing control point.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double BPMMinimum =>
|
public double BPMMinimum =>
|
||||||
60000 / (ControlPoints.OfType<TimingControlPoint>().OrderByDescending(c => c.BeatLength).FirstOrDefault() ?? new TimingControlPoint()).BeatLength;
|
60000 / (TimingPoints.OrderByDescending(c => c.BeatLength).FirstOrDefault() ?? new TimingControlPoint()).BeatLength;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the mode BPM (most common BPM) represented by the control points.
|
/// Finds the mode BPM (most common BPM) represented by the control points.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double BPMMode =>
|
public double BPMMode =>
|
||||||
60000 / (ControlPoints.OfType<TimingControlPoint>().GroupBy(c => c.BeatLength).OrderByDescending(grp => grp.Count()).FirstOrDefault()?.FirstOrDefault() ?? new TimingControlPoint()).BeatLength;
|
60000 / (TimingPoints.GroupBy(c => c.BeatLength).OrderByDescending(grp => grp.Count()).FirstOrDefault()?.FirstOrDefault() ?? new TimingControlPoint()).BeatLength;
|
||||||
|
|
||||||
|
private T binarySearch<T>(SortedList<T> list, double time)
|
||||||
|
where T : ControlPoint, new()
|
||||||
|
{
|
||||||
|
if (list.Count == 0)
|
||||||
|
return new T();
|
||||||
|
|
||||||
|
if (time < list[0].Time)
|
||||||
|
return new T();
|
||||||
|
|
||||||
|
int index = list.BinarySearch(new T() { Time = time });
|
||||||
|
|
||||||
|
// Check if we've found an exact match (t == time)
|
||||||
|
if (index >= 0)
|
||||||
|
return list[index];
|
||||||
|
|
||||||
|
index = ~index;
|
||||||
|
|
||||||
|
if (index == list.Count)
|
||||||
|
return list[list.Count - 1];
|
||||||
|
return list[index - 1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -285,7 +285,7 @@ namespace osu.Game.Beatmaps.Formats
|
|||||||
|
|
||||||
if (timingChange && (beatLength != timingPoint.BeatLength || timeSignature != timingPoint.TimeSignature))
|
if (timingChange && (beatLength != timingPoint.BeatLength || timeSignature != timingPoint.TimeSignature))
|
||||||
{
|
{
|
||||||
beatmap.ControlPointInfo.ControlPoints.Add(new TimingControlPoint
|
beatmap.ControlPointInfo.TimingPoints.Add(new TimingControlPoint
|
||||||
{
|
{
|
||||||
Time = time,
|
Time = time,
|
||||||
BeatLength = beatLength,
|
BeatLength = beatLength,
|
||||||
@ -295,7 +295,7 @@ namespace osu.Game.Beatmaps.Formats
|
|||||||
|
|
||||||
if (speedMultiplier != difficultyPoint.SpeedMultiplier)
|
if (speedMultiplier != difficultyPoint.SpeedMultiplier)
|
||||||
{
|
{
|
||||||
beatmap.ControlPointInfo.ControlPoints.Add(new DifficultyControlPoint
|
beatmap.ControlPointInfo.DifficultyPoints.Add(new DifficultyControlPoint
|
||||||
{
|
{
|
||||||
Time = time,
|
Time = time,
|
||||||
SpeedMultiplier = speedMultiplier
|
SpeedMultiplier = speedMultiplier
|
||||||
@ -304,7 +304,7 @@ namespace osu.Game.Beatmaps.Formats
|
|||||||
|
|
||||||
if (stringSampleSet != soundPoint.SampleBank || sampleVolume != soundPoint.SampleVolume)
|
if (stringSampleSet != soundPoint.SampleBank || sampleVolume != soundPoint.SampleVolume)
|
||||||
{
|
{
|
||||||
beatmap.ControlPointInfo.ControlPoints.Add(new SoundControlPoint
|
beatmap.ControlPointInfo.SoundPoints.Add(new SoundControlPoint
|
||||||
{
|
{
|
||||||
Time = time,
|
Time = time,
|
||||||
SampleBank = stringSampleSet,
|
SampleBank = stringSampleSet,
|
||||||
@ -314,7 +314,7 @@ namespace osu.Game.Beatmaps.Formats
|
|||||||
|
|
||||||
if (kiaiMode != effectPoint.KiaiMode || omitFirstBarSignature != effectPoint.OmitFirstBarLine)
|
if (kiaiMode != effectPoint.KiaiMode || omitFirstBarSignature != effectPoint.OmitFirstBarLine)
|
||||||
{
|
{
|
||||||
beatmap.ControlPointInfo.ControlPoints.Add(new EffectControlPoint
|
beatmap.ControlPointInfo.EffectPoints.Add(new EffectControlPoint
|
||||||
{
|
{
|
||||||
Time = time,
|
Time = time,
|
||||||
KiaiMode = kiaiMode,
|
KiaiMode = kiaiMode,
|
||||||
|
Reference in New Issue
Block a user