From 2cc5e6dc9c425af0291d7ea954f21ede53930cd6 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 18 Apr 2025 08:43:01 +0700 Subject: [PATCH] fix: partially fix music speed --- build-number.txt | 2 +- .../chomens_bot/commands/MusicCommand.java | 8 ++++---- .../plugins/MusicPlayerPlugin.java | 20 +++++++++---------- .../me/chayapak1/chomens_bot/song/Note.java | 8 ++++---- .../me/chayapak1/chomens_bot/song/Song.java | 20 +++++++++---------- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/build-number.txt b/build-number.txt index 59e055a4..633e3dc2 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2801 \ No newline at end of file +2833 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java index a9f1ceaa..55d23db7 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java @@ -404,7 +404,7 @@ public class MusicCommand extends Command { if (currentSong == null) throw new CommandException(Component.text("No song is currently playing")); - if (timestamp < 0 || timestamp > currentSong.length) + if (timestamp < 0 || timestamp > currentSong.length * bot.music.speed) throw new CommandException(Component.text("Invalid timestamp")); currentSong.setTime(timestamp); @@ -436,14 +436,14 @@ public class MusicCommand extends Command { final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; - final float speed = context.getFloat(true, false); + final double speed = context.getDouble(true, false); if (speed > 5) throw new CommandException(Component.text("Too fast!")); else if (speed < 0) throw new CommandException(Component.text("Invalid speed")); - long oldTime = -1; + double oldTime = -1; - if (currentSong != null) oldTime = currentSong.time; + if (currentSong != null) oldTime = currentSong.time / speed; bot.music.speed = speed; diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java index 198b1167..98fcaebb 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java @@ -39,6 +39,8 @@ public class MusicPlayerPlugin implements Listener { private static final String BOSS_BAR_NAME = "music"; + private static final DecimalFormat FORMATTER = new DecimalFormat("#,###"); + static { try { if (!Files.exists(SONG_DIR)) Files.createDirectory(SONG_DIR); @@ -55,8 +57,8 @@ public class MusicPlayerPlugin implements Listener { public Loop loop = Loop.OFF; // sus nightcore stuff,..,.,. - public float pitch = 0; - public float speed = 1; + public double pitch = 0; + public double speed = 1; public float volume = 0; public int amplify = 1; @@ -166,8 +168,8 @@ public class MusicPlayerPlugin implements Listener { if (bossBar != null && bot.options.useCore) { bossBar.setTitle(generateBossBar()); bossBar.setColor(bossBarColor); - bossBar.setValue((int) Math.floor(((double) (currentSong.time * speed) / 1000))); - bossBar.setMax((long) (currentSong.length * speed) / 1000); + bossBar.setValue((int) Math.floor(((currentSong.time / speed) / 1000))); + bossBar.setMax((long) (currentSong.length / speed) / 1000); } if (currentSong.paused || bot.core.isRateLimited()) return; @@ -322,20 +324,18 @@ public class MusicPlayerPlugin implements Listener { .append( Component .translatable("%s / %s", - formatTime((long) (currentSong.time * speed)).color(NamedTextColor.GRAY), - formatTime((long) (currentSong.length * speed)).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY) + formatTime((long) (currentSong.time / speed)).color(NamedTextColor.GRAY), + formatTime((long) (currentSong.length / speed)).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY) ); - final DecimalFormat formatter = new DecimalFormat("#,###"); - if (!bot.core.hasRateLimit()) { component = component .append(Component.text(" | ").color(NamedTextColor.DARK_GRAY)) .append( Component.translatable( "%s / %s", - Component.text(formatter.format(currentSong.position), NamedTextColor.GRAY), - Component.text(formatter.format(currentSong.size()), NamedTextColor.GRAY) + Component.text(FORMATTER.format(currentSong.position), NamedTextColor.GRAY), + Component.text(FORMATTER.format(currentSong.size()), NamedTextColor.GRAY) ).color(NamedTextColor.DARK_GRAY) ); diff --git a/src/main/java/me/chayapak1/chomens_bot/song/Note.java b/src/main/java/me/chayapak1/chomens_bot/song/Note.java index 69ca259a..c18f0a09 100644 --- a/src/main/java/me/chayapak1/chomens_bot/song/Note.java +++ b/src/main/java/me/chayapak1/chomens_bot/song/Note.java @@ -8,7 +8,7 @@ public class Note implements Comparable { public final double shiftedPitch; public final double originalPitch; public final float volume; - public final long time; + public final double time; public final int panning; public final int stereo; public final boolean isRainbowToggle; @@ -18,7 +18,7 @@ public class Note implements Comparable { final double pitch, final double originalPitch, final float volume, - final long time, + final double time, final int panning, final int stereo, final boolean isRainbowToggle @@ -42,7 +42,7 @@ public class Note implements Comparable { final double shiftedPitch, final double originalPitch, final float volume, - final long time, + final double time, final int panning, final int stereo ) { @@ -60,6 +60,6 @@ public class Note implements Comparable { @Override public int compareTo (final Note other) { - return Long.compare(time, other.time); + return Double.compare(time, other.time); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/song/Song.java b/src/main/java/me/chayapak1/chomens_bot/song/Song.java index f8ae460c..dce8946d 100644 --- a/src/main/java/me/chayapak1/chomens_bot/song/Song.java +++ b/src/main/java/me/chayapak1/chomens_bot/song/Song.java @@ -17,9 +17,9 @@ public class Song { public String requester = "Unknown"; public int position = 0; // Current note index 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 time = 0; // Time since start of song + public double startTime = 0; // Start time in millis since unix epoch + public double length = 0; // Milliseconds in the song + public double time = 0; // Time since start of song public long loopPosition = 0; // Milliseconds into the song to start looping public final Map lyrics = new HashMap<>(); @@ -100,26 +100,26 @@ public class Song { } } - public void setTime (final long t) { + public void setTime (final double t) { time = t; startTime = System.currentTimeMillis() - time; position = 0; - while (position < notes.size() && notes.get(position).time < t) { + while (position < notes.size() && notes.get(position).time / bot.music.speed <= t) { position++; } } public void advanceTime () { - time = (long) ((System.currentTimeMillis() - startTime) * bot.music.speed); + time = (System.currentTimeMillis() - startTime) * bot.music.speed; } public boolean reachedNextNote () { if (position < notes.size()) { - return notes.get(position).time <= time * bot.music.speed; + return notes.get(position).time / bot.music.speed <= time; } else { if (finished() && bot.music.loop != Loop.OFF) { if (position < notes.size()) { - return notes.get(position).time <= time * bot.music.speed; + return notes.get(position).time / bot.music.speed <= time; } else { return false; } @@ -133,7 +133,7 @@ public class Song { position = 0; startTime += length - loopPosition; time -= length - loopPosition; - while (position < notes.size() && notes.get(position).time < loopPosition) { + while (position < notes.size() && notes.get(position).time / bot.music.speed < loopPosition) { position++; } } @@ -144,7 +144,7 @@ public class Song { } public boolean finished () { - return (time * bot.music.speed) > length || position >= size(); + return time > length || position >= size(); } public int size () {