Make most common BPM more accurate

This commit is contained in:
smoogipoo
2021-01-15 14:28:49 +09:00
parent abfc5f24a3
commit c6e9a6cd5a
7 changed files with 49 additions and 9 deletions

View File

@ -48,6 +48,44 @@ namespace osu.Game.Beatmaps
public virtual IEnumerable<BeatmapStatistic> GetStatistics() => Enumerable.Empty<BeatmapStatistic>();
public double GetMostCommonBeatLength()
{
// The last playable time in the beatmap - the last timing point extends to this time.
// Note: This is more accurate and may present different results because osu-stable didn't have the ability to calculate slider durations in this context.
double lastTime = HitObjects.LastOrDefault()?.GetEndTime() ?? ControlPointInfo.TimingPoints.LastOrDefault()?.Time ?? 0;
var beatLengthsAndDurations =
// Construct a set of (beatLength, duration) tuples for each individual timing point.
ControlPointInfo.TimingPoints.Select((t, i) =>
{
if (t.Time > lastTime)
return (beatLength: t.BeatLength, 0);
var nextTime = i == ControlPointInfo.TimingPoints.Count - 1 ? lastTime : ControlPointInfo.TimingPoints[i + 1].Time;
return (beatLength: t.BeatLength, duration: nextTime - t.Time);
})
// Aggregate durations into a set of (beatLength, duration) tuples for each beat length
.GroupBy(t => t.beatLength)
.Select(g => (beatLength: g.Key, duration: g.Sum(t => t.duration)))
// And if there are no timing points, use a default.
.DefaultIfEmpty((TimingControlPoint.DEFAULT_BEAT_LENGTH, 0));
// Find the single beat length with the maximum aggregate duration.
double maxDurationBeatLength = double.NegativeInfinity;
double maxDuration = double.NegativeInfinity;
foreach (var (beatLength, duration) in beatLengthsAndDurations)
{
if (duration > maxDuration)
{
maxDuration = duration;
maxDurationBeatLength = beatLength;
}
}
return 60000 / maxDurationBeatLength;
}
IBeatmap IBeatmap.Clone() => Clone();
public Beatmap<T> Clone()