From e2185e2950573ab2dda5c19259a817eac28d68f8 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:23:56 +0700 Subject: [PATCH] support txt songs and some "improvement" --- .../chayapak/chomens_bot/song/Converter.java | 7 +++ .../chomens_bot/song/MidiConverter.java | 5 +- .../chomens_bot/song/NBSConverter.java | 5 +- .../chayapak/chomens_bot/song/Song.java | 8 +-- .../chomens_bot/song/SongLoaderRunnable.java | 25 +++++---- .../chomens_bot/song/TextFileConverter.java | 54 +++++++++++++++++++ 6 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/song/Converter.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/song/TextFileConverter.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Converter.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Converter.java new file mode 100644 index 0000000..f760e60 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Converter.java @@ -0,0 +1,7 @@ +package land.chipmunk.chayapak.chomens_bot.song; + +import land.chipmunk.chayapak.chomens_bot.Bot; + +public interface Converter { + Song getSongFromBytes (byte[] bytes, String fileName, Bot bot) throws Exception; +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/MidiConverter.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/MidiConverter.java index 744ee7f..302dadb 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/MidiConverter.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/MidiConverter.java @@ -10,13 +10,14 @@ import java.util.Comparator; import java.util.HashMap; // Author: hhhzzzsss -public class MidiConverter { +public class MidiConverter implements Converter { public static final int SET_INSTRUMENT = 0xC0; public static final int SET_TEMPO = 0x51; public static final int NOTE_ON = 0x90; public static final int NOTE_OFF = 0x80; - public static Song getSongFromBytes(byte[] bytes, String name, Bot bot) throws InvalidMidiDataException, IOException { + @Override + public Song getSongFromBytes(byte[] bytes, String name, Bot bot) throws InvalidMidiDataException, IOException { Sequence sequence = MidiSystem.getSequence(new ByteArrayInputStream(bytes)); return getSong(sequence, name, bot); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/NBSConverter.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/NBSConverter.java index 18c4163..1fc60c8 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/NBSConverter.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/NBSConverter.java @@ -9,7 +9,7 @@ import java.nio.file.Path; import java.util.ArrayList; // Author: hhhzzzsss -public class NBSConverter { +public class NBSConverter implements Converter { public static final Instrument[] instrumentIndex = new Instrument[] { Instrument.HARP, Instrument.BASS, @@ -53,7 +53,8 @@ public class NBSConverter { public boolean key = false; } - public static Song getSongFromBytes(byte[] bytes, String fileName, Bot bot) throws IOException { + @Override + public Song getSongFromBytes(byte[] bytes, String fileName, Bot bot) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(ByteOrder.LITTLE_ENDIAN); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java index f9c38bf..659552d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java @@ -17,10 +17,10 @@ public class Song { public long time = 0; // Time since start of song public long loopPosition = 200; // Milliseconds into the song to start looping - public final String songName; - public final String songAuthor; - public final String songOriginalAuthor; - public final String songDescription; + public String songName; + public String songAuthor; + public String songOriginalAuthor; + public String songDescription; public final boolean nbs; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java index 445a5e5..d864758 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java @@ -11,11 +11,22 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; import java.util.stream.Stream; // Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable // because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! public class SongLoaderRunnable implements Runnable { + // should the converters be here? + public static final List converters = new ArrayList<>(); + + static { + converters.add(new MidiConverter()); + converters.add(new NBSConverter()); + converters.add(new TextFileConverter()); + } + public final String fileName; private Path songPath; @@ -83,18 +94,12 @@ public class SongLoaderRunnable implements Runnable { return; } - try { - song = MidiConverter.getSongFromBytes(bytes, name, bot); - } catch (Exception e) { - e.printStackTrace(); - } + for (Converter converter : converters) { + if (song != null) break; - if (song == null) { try { - song = NBSConverter.getSongFromBytes(bytes, name, bot); - } catch (Exception e) { - e.printStackTrace(); - } + song = converter.getSongFromBytes(bytes, name, bot); + } catch (Exception ignored) {} } if (song == null) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/TextFileConverter.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/TextFileConverter.java new file mode 100644 index 0000000..8c10ee7 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/TextFileConverter.java @@ -0,0 +1,54 @@ +package land.chipmunk.chayapak.chomens_bot.song; + +import land.chipmunk.chayapak.chomens_bot.Bot; + +import java.util.Arrays; + +public class TextFileConverter implements Converter { + @Override + public Song getSongFromBytes(byte[] bytes, String fileName, Bot bot) { + final String data = new String(bytes); + + if (!data.contains(":")) return null; + + int length = 0; + + final Song song = new Song(fileName, bot, null, null, null, null, false); + + for (String line : data.split("\r\n|\r|\n")) { + if (line.isEmpty()) continue; + + // worst way to implement this but it works lol + if (line.startsWith("title:")) { + song.songName = line.substring("title:".length()); + continue; + } else if (line.startsWith("author:")) { + song.songAuthor = line.substring("author:".length()); + continue; + } else if (line.startsWith("originalAuthor:")) { + song.songOriginalAuthor = line.substring("originalAuthor:".length()); + continue; + } else if (line.startsWith("description:")) { + song.songDescription = line.substring("description:".length()); + continue; + } + + + final Integer[] mapped = Arrays.stream(line.split(":")).map(Integer::parseInt).toArray(Integer[]::new); + + final int tick = mapped[0]; + final int pitch = mapped[1]; + final int instrument = mapped[2]; + + final int time = tick * 50; + + length = Math.max(length, time); + + song.add(new Note(Instrument.fromId(instrument), pitch, 1, time, -1, 100)); + } + + song.length = song.get(song.size() - 1).time + 50; + + return song; + } +}