diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java new file mode 100644 index 0000000..707ed22 --- /dev/null +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java @@ -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; + } +} diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java index 1294a40..7782445 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java @@ -4,6 +4,7 @@ import com.github.hhhzzzsss.songplayer.SongPlayer; import com.github.hhhzzzsss.songplayer.Util; import com.github.hhhzzzsss.songplayer.conversion.MidiConverter; import com.github.hhhzzzsss.songplayer.conversion.NBSConverter; +import com.github.hhhzzzsss.songplayer.conversion.TXTConverter; import java.io.IOException; import java.net.URL; @@ -75,6 +76,15 @@ public class SongLoaderThread extends Thread{ catch (Exception e) {} } + if (song == null) { + try { + song = TXTConverter.getSongFromBytes(bytes, filename); + } + catch (Exception e) { + e.printStackTrace(); + } + } + if (song == null) { throw new IOException("Invalid song format"); }