TXT song converter.

This commit is contained in:
Chayapak 2023-09-29 11:38:28 +07:00
parent fe92fae94f
commit adf17c27bb
2 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,87 @@
package com.github.hhhzzzsss.songplayer.conversion;
import com.github.hhhzzzsss.songplayer.playing.SongHandler;
import com.github.hhhzzzsss.songplayer.song.Instrument;
import com.github.hhhzzzsss.songplayer.song.Note;
import com.github.hhhzzzsss.songplayer.song.Song;
public class TXTConverter {
public static Instrument[] instrumentIndex = new Instrument[] {
Instrument.HARP,
Instrument.BASS,
Instrument.BASEDRUM,
Instrument.SNARE,
Instrument.HAT,
Instrument.GUITAR,
Instrument.FLUTE,
Instrument.BELL,
Instrument.CHIME,
Instrument.XYLOPHONE,
Instrument.IRON_XYLOPHONE,
Instrument.COW_BELL,
Instrument.DIDGERIDOO,
Instrument.BIT,
Instrument.BANJO,
Instrument.PLING,
};
public static Song getSongFromBytes (byte[] bytes, String fileName) {
return getSong(new String(bytes), fileName);
}
public static Song getSong (String data, String fileName) {
if (!data.contains(":")) return null;
int length = 0;
final Song song = new Song(fileName);
for (String line : data.split("\r\n|\r|\n")) {
if (line.isBlank()) continue;
if (line.startsWith("title:")) {
song.name = line.substring("title:".length());
continue;
}
final String[] split = line.split(":");
final int tick = Integer.parseInt(split[0]);
int pitch = (int) Float.parseFloat(split[1]) + 33 + SongHandler.getInstance().pitch;
final String instrument = split[2];
int intInstrument = -1;
try {
intInstrument = Integer.parseInt(instrument);
} catch (NumberFormatException ignored) {}
if (intInstrument == -1) continue;
if (intInstrument > instrumentIndex.length - 1) continue;
final Instrument instrumentClass = instrumentIndex[intInstrument];
if (instrumentClass == null) continue;
final int time = tick * 50;
length = Math.max(length, time);
while (pitch < 33) pitch += 12;
while (pitch > 57) pitch -= 12;
pitch -= 33;
song.add(
new Note(
pitch + (instrumentClass.instrumentId * 25),
time
)
);
}
song.length = song.get(song.size() - 1).time + 50;
return song;
}
}

View file

@ -4,6 +4,7 @@ import com.github.hhhzzzsss.songplayer.SongPlayer;
import com.github.hhhzzzsss.songplayer.Util; import com.github.hhhzzzsss.songplayer.Util;
import com.github.hhhzzzsss.songplayer.conversion.MidiConverter; import com.github.hhhzzzsss.songplayer.conversion.MidiConverter;
import com.github.hhhzzzsss.songplayer.conversion.NBSConverter; import com.github.hhhzzzsss.songplayer.conversion.NBSConverter;
import com.github.hhhzzzsss.songplayer.conversion.TXTConverter;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
@ -75,6 +76,15 @@ public class SongLoaderThread extends Thread{
catch (Exception e) {} catch (Exception e) {}
} }
if (song == null) {
try {
song = TXTConverter.getSongFromBytes(bytes, filename);
}
catch (Exception e) {
e.printStackTrace();
}
}
if (song == null) { if (song == null) {
throw new IOException("Invalid song format"); throw new IOException("Invalid song format");
} }