forked from ChomeNS/chomens-bot-java
support txt songs and some "improvement"
This commit is contained in:
parent
7d78df584e
commit
e2185e2950
6 changed files with 86 additions and 18 deletions
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<Converter> 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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue