Fixed looping
This commit is contained in:
parent
4fbe707027
commit
334dde87e7
4 changed files with 43 additions and 14 deletions
|
@ -229,6 +229,7 @@ public class CommandProcessor {
|
|||
}
|
||||
|
||||
SongHandler.getInstance().currentSong.looping = !SongHandler.getInstance().currentSong.looping;
|
||||
SongHandler.getInstance().currentSong.loopCount = 0;
|
||||
if (SongHandler.getInstance().currentSong.looping) {
|
||||
SongPlayer.addChatMessage("§6Enabled looping");
|
||||
}
|
||||
|
|
|
@ -244,12 +244,16 @@ public class SongHandler {
|
|||
long currentTime = Math.min(currentSong.time, currentSong.length);
|
||||
long totalTime = currentSong.length;
|
||||
MutableText text = Text.empty()
|
||||
.append(Text.literal("Now playing " ).formatted(Formatting.GOLD))
|
||||
.append(Text.literal("Now playing ").formatted(Formatting.GOLD))
|
||||
.append(Text.literal(currentSong.name).formatted(Formatting.BLUE))
|
||||
.append(Text.literal(" | " ).formatted(Formatting.GOLD))
|
||||
.append(Text.literal(" | ").formatted(Formatting.GOLD))
|
||||
.append(Text.literal(String.format("%s/%s", Util.formatTime(currentTime), Util.formatTime(totalTime))).formatted(Formatting.DARK_AQUA));
|
||||
if (currentSong.looping) {
|
||||
text.append(Text.literal(" | Looping enabled" ).formatted(Formatting.GOLD));
|
||||
if (currentSong.loopCount > 0) {
|
||||
text.append(Text.literal(String.format(" | Loop (%d/%d)", currentSong.currentLoop, currentSong.loopCount)).formatted(Formatting.GOLD));
|
||||
} else {
|
||||
text.append(Text.literal(" | Looping enabled").formatted(Formatting.GOLD));
|
||||
}
|
||||
}
|
||||
ProgressDisplay.getInstance().setText(text);
|
||||
}
|
||||
|
|
|
@ -86,7 +86,6 @@ public class NBSConverter {
|
|||
loopStartTick = buffer.getShort();
|
||||
}
|
||||
|
||||
|
||||
ArrayList<NBSNote> nbsNotes = new ArrayList<>();
|
||||
short tick = -1;
|
||||
while (true) {
|
||||
|
@ -133,6 +132,7 @@ public class NBSConverter {
|
|||
if (loop > 0) {
|
||||
song.looping = true;
|
||||
song.loopPosition = getMilliTime(loopStartTick, tempo);
|
||||
song.loopCount = maxLoopCount;
|
||||
}
|
||||
for (NBSNote note : nbsNotes) {
|
||||
Instrument instrument;
|
||||
|
|
|
@ -12,8 +12,10 @@ public class Song {
|
|||
public boolean paused = true;
|
||||
public long startTime = 0; // Start time in millis since unix epoch
|
||||
public long length = 0; // Milliseconds in the song
|
||||
public long loopPosition = 0; // Milliseconds into the song to start looping
|
||||
public long time = 0; // Time since start of song
|
||||
public long loopPosition = 0; // Milliseconds into the song to start looping
|
||||
public int loopCount = 0; // Number of times to loop
|
||||
public int currentLoop = 0; // Number of loops so far
|
||||
|
||||
public Song(String name) {
|
||||
this.name = name;
|
||||
|
@ -48,6 +50,7 @@ public class Song {
|
|||
public void pause() {
|
||||
if (!paused) {
|
||||
paused = true;
|
||||
// Recalculates time so that the song will continue playing after the exact point it was paused
|
||||
advanceTime();
|
||||
}
|
||||
}
|
||||
|
@ -56,8 +59,8 @@ public class Song {
|
|||
time = t;
|
||||
startTime = System.currentTimeMillis() - time;
|
||||
position = 0;
|
||||
while (reachedNextNote()) {
|
||||
getNextNote();
|
||||
while (position < notes.size() && notes.get(position).time < t) {
|
||||
position++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,10 +70,15 @@ public class Song {
|
|||
|
||||
public boolean reachedNextNote() {
|
||||
if (position < notes.size()) {
|
||||
return notes.get(position).time <= this.time;
|
||||
return notes.get(position).time <= time;
|
||||
} else {
|
||||
if (looping) {
|
||||
return notes.get(0).time + length <= this.time;
|
||||
if (time > length && shouldLoop()) {
|
||||
loop();
|
||||
if (position < notes.size()) {
|
||||
return notes.get(position).time <= time;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -79,7 +87,7 @@ public class Song {
|
|||
|
||||
public Note getNextNote() {
|
||||
if (position >= notes.size()) {
|
||||
if (looping) {
|
||||
if (shouldLoop()) {
|
||||
loop();
|
||||
} else {
|
||||
return null;
|
||||
|
@ -89,13 +97,29 @@ public class Song {
|
|||
}
|
||||
|
||||
public boolean finished() {
|
||||
return time > length;
|
||||
return time > length && !shouldLoop();
|
||||
}
|
||||
|
||||
private void loop() {
|
||||
position = 0;
|
||||
startTime += length;
|
||||
time -= length;
|
||||
startTime += length - loopPosition;
|
||||
time -= length - loopPosition;
|
||||
while (position < notes.size() && notes.get(position).time < loopPosition) {
|
||||
position++;
|
||||
}
|
||||
currentLoop++;
|
||||
}
|
||||
|
||||
private boolean shouldLoop() {
|
||||
if (looping) {
|
||||
if (loopCount == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return currentLoop < loopCount;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
|
|
Loading…
Reference in a new issue