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.looping = !SongHandler.getInstance().currentSong.looping;
|
||||||
|
SongHandler.getInstance().currentSong.loopCount = 0;
|
||||||
if (SongHandler.getInstance().currentSong.looping) {
|
if (SongHandler.getInstance().currentSong.looping) {
|
||||||
SongPlayer.addChatMessage("§6Enabled looping");
|
SongPlayer.addChatMessage("§6Enabled looping");
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,12 +244,16 @@ public class SongHandler {
|
||||||
long currentTime = Math.min(currentSong.time, currentSong.length);
|
long currentTime = Math.min(currentSong.time, currentSong.length);
|
||||||
long totalTime = currentSong.length;
|
long totalTime = currentSong.length;
|
||||||
MutableText text = Text.empty()
|
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(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));
|
.append(Text.literal(String.format("%s/%s", Util.formatTime(currentTime), Util.formatTime(totalTime))).formatted(Formatting.DARK_AQUA));
|
||||||
if (currentSong.looping) {
|
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);
|
ProgressDisplay.getInstance().setText(text);
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,6 @@ public class NBSConverter {
|
||||||
loopStartTick = buffer.getShort();
|
loopStartTick = buffer.getShort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ArrayList<NBSNote> nbsNotes = new ArrayList<>();
|
ArrayList<NBSNote> nbsNotes = new ArrayList<>();
|
||||||
short tick = -1;
|
short tick = -1;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -133,6 +132,7 @@ public class NBSConverter {
|
||||||
if (loop > 0) {
|
if (loop > 0) {
|
||||||
song.looping = true;
|
song.looping = true;
|
||||||
song.loopPosition = getMilliTime(loopStartTick, tempo);
|
song.loopPosition = getMilliTime(loopStartTick, tempo);
|
||||||
|
song.loopCount = maxLoopCount;
|
||||||
}
|
}
|
||||||
for (NBSNote note : nbsNotes) {
|
for (NBSNote note : nbsNotes) {
|
||||||
Instrument instrument;
|
Instrument instrument;
|
||||||
|
|
|
@ -12,8 +12,10 @@ public class Song {
|
||||||
public boolean paused = true;
|
public boolean paused = true;
|
||||||
public long startTime = 0; // Start time in millis since unix epoch
|
public long startTime = 0; // Start time in millis since unix epoch
|
||||||
public long length = 0; // Milliseconds in the song
|
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 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) {
|
public Song(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -48,6 +50,7 @@ public class Song {
|
||||||
public void pause() {
|
public void pause() {
|
||||||
if (!paused) {
|
if (!paused) {
|
||||||
paused = true;
|
paused = true;
|
||||||
|
// Recalculates time so that the song will continue playing after the exact point it was paused
|
||||||
advanceTime();
|
advanceTime();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,8 +59,8 @@ public class Song {
|
||||||
time = t;
|
time = t;
|
||||||
startTime = System.currentTimeMillis() - time;
|
startTime = System.currentTimeMillis() - time;
|
||||||
position = 0;
|
position = 0;
|
||||||
while (reachedNextNote()) {
|
while (position < notes.size() && notes.get(position).time < t) {
|
||||||
getNextNote();
|
position++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +70,15 @@ public class Song {
|
||||||
|
|
||||||
public boolean reachedNextNote() {
|
public boolean reachedNextNote() {
|
||||||
if (position < notes.size()) {
|
if (position < notes.size()) {
|
||||||
return notes.get(position).time <= this.time;
|
return notes.get(position).time <= time;
|
||||||
} else {
|
} else {
|
||||||
if (looping) {
|
if (time > length && shouldLoop()) {
|
||||||
return notes.get(0).time + length <= this.time;
|
loop();
|
||||||
|
if (position < notes.size()) {
|
||||||
|
return notes.get(position).time <= time;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -79,7 +87,7 @@ public class Song {
|
||||||
|
|
||||||
public Note getNextNote() {
|
public Note getNextNote() {
|
||||||
if (position >= notes.size()) {
|
if (position >= notes.size()) {
|
||||||
if (looping) {
|
if (shouldLoop()) {
|
||||||
loop();
|
loop();
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -89,13 +97,29 @@ public class Song {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean finished() {
|
public boolean finished() {
|
||||||
return time > length;
|
return time > length && !shouldLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loop() {
|
private void loop() {
|
||||||
position = 0;
|
position = 0;
|
||||||
startTime += length;
|
startTime += length - loopPosition;
|
||||||
time -= length;
|
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() {
|
public int size() {
|
||||||
|
|
Loading…
Reference in a new issue