More efficient beat progress calculation. (#214)

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2016-12-19 22:39:49 -05:00
parent 90a2f1d2f2
commit 7fa9355bbf

View File

@ -77,6 +77,12 @@ public class MusicController {
/** The track dim level, if dimmed. */ /** The track dim level, if dimmed. */
private static float dimLevel = 1f; private static float dimLevel = 1f;
/** Current timing point index in the track, advanced by {@link #getBeatProgress()}. */
private static int timingPointIndex;
/** Last non-inherited timing point. */
private static TimingPoint lastTimingPoint;
// This class should not be instantiated. // This class should not be instantiated.
private MusicController() {} private MusicController() {}
@ -135,8 +141,10 @@ public class MusicController {
player.addListener(new MusicListener() { player.addListener(new MusicListener() {
@Override @Override
public void musicEnded(Music music) { public void musicEnded(Music music) {
if (music == player) // don't fire if music swapped if (music == player) { // don't fire if music swapped
trackEnded = true; trackEnded = true;
resetTimingPoint();
}
} }
@Override @Override
@ -158,6 +166,7 @@ public class MusicController {
setVolume(Options.getMusicVolume() * Options.getMasterVolume()); setVolume(Options.getMusicVolume() * Options.getMasterVolume());
trackEnded = false; trackEnded = false;
pauseTime = 0f; pauseTime = 0f;
resetTimingPoint();
if (loop) if (loop)
player.loop(); player.loop();
else else
@ -192,28 +201,33 @@ public class MusicController {
*/ */
public static Float getBeatProgress() { public static Float getBeatProgress() {
Beatmap map = getBeatmap(); Beatmap map = getBeatmap();
if (!isPlaying() || map == null || map.timingPoints == null) { if (!isPlaying() || map == null || map.timingPoints == null || map.timingPoints.isEmpty())
return null; return null;
// initialization
if (timingPointIndex == 0 && lastTimingPoint == null && !map.timingPoints.isEmpty()) {
TimingPoint timingPoint = map.timingPoints.get(0);
if (!timingPoint.isInherited())
lastTimingPoint = timingPoint;
} }
// advance timing point index, record last non-inherited timing point
int trackPosition = getPosition(); int trackPosition = getPosition();
TimingPoint p = null; for (int i = timingPointIndex + 1; i < map.timingPoints.size(); i++) {
double beatlen = 0d; TimingPoint timingPoint = map.timingPoints.get(i);
int time = 0; if (trackPosition < timingPoint.getTime())
for (TimingPoint pts : map.timingPoints) {
if (pts.getTime() > getPosition()) {
break; break;
timingPointIndex = i;
if (!timingPoint.isInherited() && timingPoint.getBeatLength() > 0)
lastTimingPoint = timingPoint;
} }
p = pts; if (lastTimingPoint == null)
if (!p.isInherited() && p.getBeatLength() > 0) { return null; // no timing info
beatlen = p.getBeatLength();
time = p.getTime(); // calculate beat progress
} double beatLength = lastTimingPoint.getBeatLength() * 100.0;
} int beatTime = lastTimingPoint.getTime();
if (p == null) { return (float) ((((trackPosition - beatTime) * 100.0) % beatLength) / beatLength);
return null;
}
double beatLength = beatlen * 100d;
return (float) ((((trackPosition - time) * 100) % beatLength) / beatLength);
} }
/** /**
@ -257,8 +271,10 @@ public class MusicController {
public static void stop() { public static void stop() {
if (isPlaying()) if (isPlaying())
player.stop(); player.stop();
if (trackExists()) if (trackExists()) {
pauseTime = 0f; pauseTime = 0f;
resetTimingPoint();
}
} }
/** /**
@ -297,7 +313,11 @@ public class MusicController {
* @param position the new track position (in ms) * @param position the new track position (in ms)
*/ */
public static boolean setPosition(int position) { public static boolean setPosition(int position) {
return (trackExists() && position >= 0 && player.setPosition(position / 1000f)); if (!trackExists() || position < 0)
return false;
resetTimingPoint();
return (player.setPosition(position / 1000f));
} }
/** /**
@ -338,6 +358,7 @@ public class MusicController {
public static void play(boolean loop) { public static void play(boolean loop) {
if (trackExists()) { if (trackExists()) {
trackEnded = false; trackEnded = false;
resetTimingPoint();
if (loop) if (loop)
player.loop(); player.loop();
else else
@ -408,6 +429,14 @@ public class MusicController {
setVolume(volume); setVolume(volume);
} }
/**
* Resets timing point information.
*/
private static void resetTimingPoint() {
timingPointIndex = 0;
lastTimingPoint = null;
}
/** /**
* Completely resets MusicController state. * Completely resets MusicController state.
* <p> * <p>
@ -438,6 +467,7 @@ public class MusicController {
themePlaying = false; themePlaying = false;
pauseTime = 0f; pauseTime = 0f;
trackDimmed = false; trackDimmed = false;
resetTimingPoint();
// releases all sources from previous tracks // releases all sources from previous tracks
destroyOpenAL(); destroyOpenAL();