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;
|
import java.util.HashMap;
|
||||||
|
|
||||||
// Author: hhhzzzsss
|
// Author: hhhzzzsss
|
||||||
public class MidiConverter {
|
public class MidiConverter implements Converter {
|
||||||
public static final int SET_INSTRUMENT = 0xC0;
|
public static final int SET_INSTRUMENT = 0xC0;
|
||||||
public static final int SET_TEMPO = 0x51;
|
public static final int SET_TEMPO = 0x51;
|
||||||
public static final int NOTE_ON = 0x90;
|
public static final int NOTE_ON = 0x90;
|
||||||
public static final int NOTE_OFF = 0x80;
|
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));
|
Sequence sequence = MidiSystem.getSequence(new ByteArrayInputStream(bytes));
|
||||||
return getSong(sequence, name, bot);
|
return getSong(sequence, name, bot);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
// Author: hhhzzzsss
|
// Author: hhhzzzsss
|
||||||
public class NBSConverter {
|
public class NBSConverter implements Converter {
|
||||||
public static final Instrument[] instrumentIndex = new Instrument[] {
|
public static final Instrument[] instrumentIndex = new Instrument[] {
|
||||||
Instrument.HARP,
|
Instrument.HARP,
|
||||||
Instrument.BASS,
|
Instrument.BASS,
|
||||||
|
@ -53,7 +53,8 @@ public class NBSConverter {
|
||||||
public boolean key = false;
|
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);
|
ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
||||||
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,10 @@ public class Song {
|
||||||
public long time = 0; // Time since start of song
|
public long time = 0; // Time since start of song
|
||||||
public long loopPosition = 200; // Milliseconds into the song to start looping
|
public long loopPosition = 200; // Milliseconds into the song to start looping
|
||||||
|
|
||||||
public final String songName;
|
public String songName;
|
||||||
public final String songAuthor;
|
public String songAuthor;
|
||||||
public final String songOriginalAuthor;
|
public String songOriginalAuthor;
|
||||||
public final String songDescription;
|
public String songDescription;
|
||||||
|
|
||||||
public final boolean nbs;
|
public final boolean nbs;
|
||||||
|
|
||||||
|
|
|
@ -11,11 +11,22 @@ import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
// Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable
|
// Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable
|
||||||
// because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
// because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
public class SongLoaderRunnable implements Runnable {
|
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;
|
public final String fileName;
|
||||||
|
|
||||||
private Path songPath;
|
private Path songPath;
|
||||||
|
@ -83,18 +94,12 @@ public class SongLoaderRunnable implements Runnable {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
for (Converter converter : converters) {
|
||||||
song = MidiConverter.getSongFromBytes(bytes, name, bot);
|
if (song != null) break;
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (song == null) {
|
|
||||||
try {
|
try {
|
||||||
song = NBSConverter.getSongFromBytes(bytes, name, bot);
|
song = converter.getSongFromBytes(bytes, name, bot);
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {}
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (song == null) {
|
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