improve and add extra features to .txt file music converter + fix song name

This commit is contained in:
Chayapak 2023-09-26 16:45:23 +07:00
parent a9b616590d
commit 58815ee3ad
2 changed files with 38 additions and 19 deletions

View file

@ -9,7 +9,7 @@ import java.util.Collections;
public class Song { public class Song {
public final ArrayList<Note> notes = new ArrayList<>(); public final ArrayList<Note> notes = new ArrayList<>();
public final String originalName; public final String originalName;
public final String name; public String name;
public String requester = "Unknown"; public String requester = "Unknown";
public int position = 0; // Current note index public int position = 0; // Current note index
public boolean paused = true; public boolean paused = true;
@ -31,8 +31,18 @@ public class Song {
private final Bot bot; private final Bot bot;
public Song (String originalName, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) { public Song (String originalName, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) {
String name; this.originalName = originalName;
this.bot = bot;
this.songName = songName;
this.songAuthor = songAuthor;
this.songOriginalAuthor = songOriginalAuthor;
this.songDescription = songDescription;
this.nbs = nbs;
updateName();
}
public void updateName() {
// real ohio code // real ohio code
// TODO: clean this up // TODO: clean this up
if (isNotNullAndNotBlank(songOriginalAuthor) && !isNotNullAndNotBlank(songAuthor)) name = String.format( if (isNotNullAndNotBlank(songOriginalAuthor) && !isNotNullAndNotBlank(songAuthor)) name = String.format(
@ -52,15 +62,6 @@ public class Song {
isNotNullAndNotBlank(songName) ? songName : originalName isNotNullAndNotBlank(songName) ? songName : originalName
); );
else name = isNotNullAndNotBlank(songName) ? songName : originalName; else name = isNotNullAndNotBlank(songName) ? songName : originalName;
this.originalName = originalName;
this.name = name;
this.bot = bot;
this.songName = songName;
this.songAuthor = songAuthor;
this.songOriginalAuthor = songOriginalAuthor;
this.songDescription = songDescription;
this.nbs = nbs;
} }
// should this be in idk, util? // should this be in idk, util?

View file

@ -2,8 +2,6 @@ package land.chipmunk.chayapak.chomens_bot.song;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import java.util.Arrays;
public class TextFileConverter implements Converter { public class TextFileConverter implements Converter {
@Override @Override
public Song getSongFromBytes(byte[] bytes, String fileName, Bot bot) { public Song getSongFromBytes(byte[] bytes, String fileName, Bot bot) {
@ -16,7 +14,7 @@ public class TextFileConverter implements Converter {
final Song song = new Song(fileName, bot, null, null, null, null, false); final Song song = new Song(fileName, bot, null, null, null, null, false);
for (String line : data.split("\r\n|\r|\n")) { for (String line : data.split("\r\n|\r|\n")) {
if (line.isEmpty()) continue; if (line.isBlank()) continue;
// worst way to implement this but it works lol // worst way to implement this but it works lol
if (line.startsWith("title:")) { if (line.startsWith("title:")) {
@ -33,18 +31,38 @@ public class TextFileConverter implements Converter {
continue; continue;
} }
song.updateName();
final Integer[] mapped = Arrays.stream(line.split(":")).map(Integer::parseInt).toArray(Integer[]::new); final String[] split = line.split(":");
final int tick = mapped[0]; final int tick = Integer.parseInt(split[0]);
final int pitch = mapped[1]; final int pitch = (int) Float.parseFloat(split[1]);
final int instrument = mapped[2]; final String instrument = split[2];
int intInstrument = -1;
try {
intInstrument = Integer.parseInt(instrument);
} catch (NumberFormatException ignored) {}
float volume = 1;
if (split.length > 3) volume = Float.parseFloat(split[3]);
final int time = tick * 50; final int time = tick * 50;
length = Math.max(length, time); length = Math.max(length, time);
song.add(new Note(Instrument.fromId(instrument), pitch, 1, time, -1, 100)); song.add(
new Note(
intInstrument == -1 ?
Instrument.of(instrument) :
Instrument.fromId(intInstrument),
pitch,
volume,
time,
-1,
100
)
);
} }
song.length = song.get(song.size() - 1).time + 50; song.length = song.get(song.size() - 1).time + 50;