diff --git a/simplevgm.iml b/simplevgm.iml index bf6e2fc..a2e3b88 100644 --- a/simplevgm.iml +++ b/simplevgm.iml @@ -1,9 +1,12 @@ - + + + + diff --git a/src/uk/co/omgdrv/simplevgm/MusicEmu.java b/src/uk/co/omgdrv/simplevgm/MusicEmu.java index 9d9d6cb..b41f5a5 100644 --- a/src/uk/co/omgdrv/simplevgm/MusicEmu.java +++ b/src/uk/co/omgdrv/simplevgm/MusicEmu.java @@ -146,7 +146,8 @@ protected void logError() if (!trackEnded_) { trackEnded_ = true; - System.out.println("emulation error"); + System.out.println("Unexpected error"); + new Exception().printStackTrace(); } } @@ -195,4 +196,4 @@ private void applyFade(byte[] io, int count) scaleSamples(io, i, n, gain); } } -} +} \ No newline at end of file diff --git a/src/Runner.java b/src/uk/co/omgdrv/simplevgm/Runner.java similarity index 79% rename from src/Runner.java rename to src/uk/co/omgdrv/simplevgm/Runner.java index 64d4e3e..83bb198 100644 --- a/src/Runner.java +++ b/src/uk/co/omgdrv/simplevgm/Runner.java @@ -1,4 +1,5 @@ -import uk.co.omgdrv.simplevgm.VGMPlayer; +package uk.co.omgdrv.simplevgm; + import uk.co.omgdrv.simplevgm.model.PsgProvider; import uk.co.omgdrv.simplevgm.util.Util; @@ -19,19 +20,34 @@ public class Runner { private static boolean DISABLE_PSG = false; - private static String VGM_FOLDER = "/data/emu/vgm/"; //"vgm"; - private static String VGM_FILE = "ghostbuster.vgm"; + private static String VGM_FOLDER = "."; //"vgm"; + private static String VGM_FILE = "file.vgm"; private static Predicate vgmFilesPredicate = p -> p.toString().endsWith(".vgm") || p.toString().endsWith(".vgz"); public static void main(String[] args) throws Exception { + Path path = getPathToPlay(args); + boolean isFolder = path.toFile().isDirectory(); + System.out.println(String.format("Playing %s: %s", + (isFolder ? "folder" : "file"), path.toAbsolutePath().toString())); PsgProvider psgProvider = DISABLE_PSG ? PsgProvider.NO_SOUND : null; VGMPlayer v = VGMPlayer.createInstance(psgProvider, 44100); Runner r = new Runner(); -// r.playAll(v, VGM_FOLDER); - r.playRecursive(v, VGM_FOLDER); + if(isFolder){ + r.playRecursive(v, path); + } else { + r.playOne(v, path); + } + } + + private static Path getPathToPlay(String[] args){ + Path p = Paths.get("."); + if(args.length > 0){ + p = Paths.get(args[0]); + } + return p; } private void playAll(VGMPlayer v, String folderName) throws Exception { @@ -40,8 +56,7 @@ private void playAll(VGMPlayer v, String folderName) throws Exception { files.stream().forEach(f -> playOne(v, f)); } - private void playRecursive(VGMPlayer v, String folderName) throws Exception { - Path folder = Paths.get(folderName); + private void playRecursive(VGMPlayer v, Path folder) throws Exception { Set fileSet = new HashSet<>(); Files.walkFileTree(folder, createFileVisitor(fileSet)); List list = new ArrayList<>(fileSet); diff --git a/src/uk/co/omgdrv/simplevgm/VgmEmu.java b/src/uk/co/omgdrv/simplevgm/VgmEmu.java index 9074b62..2690b92 100644 --- a/src/uk/co/omgdrv/simplevgm/VgmEmu.java +++ b/src/uk/co/omgdrv/simplevgm/VgmEmu.java @@ -244,8 +244,7 @@ protected int runMsec(int msec) break; default: - switch (cmd & 0xF0) - { + switch (cmd & 0xF0) { case CMD_PCM_DELAY: write_pcm(time, data[pcm_pos++] & 0xFF); time += cmd & 0x0F; @@ -254,36 +253,8 @@ protected int runMsec(int msec) case CMD_SHORT_DELAY: time += (cmd & 0x0F) + 1; break; - - //unsupported two operands - case 0x30: - case 0x40: - pos += 1; - System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd)); - break; - - //unsupported two operands - case 0x50: - case 0xA0: - case 0xB0: - System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd)); - pos += 2; - break; - //unsupported three operands - case 0xC0: - case 0xD0: - System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd)); - pos += 3; - break; - //unsupported four operands - case 0xE0: - case 0xF0: - System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd)); - pos += 4; - break; default: - System.out.println(String.format("Unexpected command: %s, at position: %s", Integer.toHexString(cmd), Integer.toHexString(pos))); - logError(); + handleUnsupportedCommand(cmd); break; } } @@ -310,6 +281,45 @@ protected int runMsec(int msec) return endTime; } + private void handleUnsupportedCommand(int cmd) { + System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() + ", unsupported command: " + Integer.toHexString(cmd)); + switch (cmd & 0xF0) { + //unsupported one operand + case 0x30: + case 0x40: + pos += 1; + break; + //unsupported two operands + case 0x50: + case 0xA0: + case 0xB0: + pos += 2; + break; + //unsupported three operands + case 0xC0: + case 0xD0: + pos += 3; + break; + //unsupported four operands + case 0xE0: + case 0xF0: + pos += 4; + break; + case 0x90: //vgm 1.60, dac stream control 0x90 - 0x95 + int subCmd = cmd & 0x7; + int diff = subCmd < 2 || subCmd == 5 ? 4 : 5; + diff = subCmd == 3 ? 10 : diff; + diff = subCmd == 4 ? 1 : diff; + pos += diff; + break; + default: + System.out.println(String.format("Unexpected command: %s, at position: %s", Integer.toHexString(cmd), Integer.toHexString(pos))); + logError(); + break; + } + } + + protected void mixSamples(byte[] out, int out_off, int count) { if (fm == null) @@ -333,4 +343,4 @@ protected void mixSamples(byte[] out, int out_off, int count) fm_pos = in_off; } -} +} \ No newline at end of file