From 77c6dfd65aa9d177021a516d96d2fdaccf8ede93 Mon Sep 17 00:00:00 2001 From: yugecin Date: Sun, 18 Dec 2016 23:31:17 +0100 Subject: [PATCH] added MusicController#getBeatProgress --- .../opsu/audio/MusicController.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/itdelatrisu/opsu/audio/MusicController.java b/src/itdelatrisu/opsu/audio/MusicController.java index 5ecfa76d..636e94be 100644 --- a/src/itdelatrisu/opsu/audio/MusicController.java +++ b/src/itdelatrisu/opsu/audio/MusicController.java @@ -22,6 +22,7 @@ import itdelatrisu.opsu.ErrorHandler; import itdelatrisu.opsu.Options; import itdelatrisu.opsu.beatmap.Beatmap; import itdelatrisu.opsu.beatmap.BeatmapParser; +import itdelatrisu.opsu.beatmap.TimingPoint; import itdelatrisu.opsu.ui.UI; import java.io.File; @@ -183,6 +184,39 @@ public class MusicController { */ public static Beatmap getBeatmap() { return lastBeatmap; } + /** + * Gets the progress of the current beat. + * @return progress as a value in [0, 1], where 0 means a beat just happened and 1 means the next beat is coming right now. + */ + public static Float getBeatProgress() { + if (!isPlaying() || getBeatmap() == null) { + return null; + } + Beatmap map = getBeatmap(); + if (map.timingPoints == null) { + return null; + } + int trackposition = getPosition(); + TimingPoint p = null; + double beatlen = 0d; + int time = 0; + for (TimingPoint pts : map.timingPoints) { + if (pts.getTime() > getPosition()) { + break; + } + p = pts; + if (!p.isInherited() && p.getBeatLength() > 0) { + beatlen = p.getBeatLength(); + time = p.getTime(); + } + } + if (p == null) { + return null; + } + double beatLength = beatlen * 100d; + return (float) ((((trackposition - time) * 100) % beatLength) / beatLength); + } + /** * Returns true if the current track is playing. */