import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by Sam on 6/14/14.
 */

public class LemonCompressor
{
    private static Scanner in = null;
    private static PrintWriter out = null;

    public static void main(String[] args) {
        try {
            in = new Scanner(new File(args[0]));
            out = new PrintWriter(new File(args[1]));
        } catch (Exception e) {
            System.out.println("Usage: java -jar lemon-compressor.jar in.c out.c");
            return;
        }
        in.useDelimiter(",");

        ArrayList<String> lastNotes = new ArrayList<String>();
        String lastRest = "0xFF00";

        String word;
        boolean paused = false;
        boolean noteFirst = false;
        boolean pauseFirst = false;
        int pauseCount = 0;
        int voice = 0;
        int dataLength = 0;

        StringBuilder result = new StringBuilder();

        while(in.hasNext()) {
            word = in.next();
            word = word.replaceAll("\r\n","");

            if (!paused) {
                if (word.equals("0x9000")) {
                    pauseCount++;
                    paused = true;
                    if (!noteFirst) {
                        pauseFirst = true;
                    }
                } else {
                    lastNotes.add(word);
                    if (!pauseFirst) {
                        noteFirst = true;
                    }
                }
            } else {
                if (word.equals("0x9000")) {
                    pauseCount++;
                } else {
                    String hex = Integer.toHexString(pauseCount);
                    if (pauseCount < 16) {
                        hex = "0" + hex;
                    }
                    if (!(pauseFirst && lastRest.equals("0xFF00"))) {
                        result.append(lastRest + ",");
                        dataLength++;
                    }
                    for (String w : lastNotes) {
                        result.append(w + ",");
                        dataLength++;
                    }
                    result.append("\n");
                    lastNotes.clear();

                    //lastRest = "0xF" + Integer.toHexString(voice) + hex;
                    lastRest = "0xFF" + hex;
                    lastNotes.add(word);

                    pauseCount = 0;
                    paused = false;
                }
            }
            voice++;

            if (voice == 4) {
                voice = 0;
            }
        }
        String hex = Integer.toHexString(pauseCount);
        if (pauseCount < 16) {
            hex = "0" + hex;
        }
        result.append(lastRest + ",");
        dataLength++;
        for (String w : lastNotes) {
            result.append(w + ",");
            dataLength++;
        }
        result.append("\n");
        result.append("0xFF" + hex);
        dataLength++;
        result.insert(0, "0x00" + Integer.toHexString(dataLength) + ",\n");

        out.println(result.toString());
        out.close();
    }

    private static void readDataBlock() throws IOException {
        in.nextLine();
        in.nextLine();

        in.useDelimiter(",");

        String word;
        boolean paused = false;
        int pauseCount = 0;

        StringBuilder result = new StringBuilder();

        while(in.hasNext()) {
            word = in.next();
            if (!paused) {
                if (word.equals("0x9000")) {
                    paused = true;
                }
                else {
                    result.append(word + ",");
                }
            }
            else {
                if (word.equals("0x9000")) {
                    pauseCount++;
                }
                else {
                    result.append("0xFF" + Integer.toHexString(pauseCount) + ",");

                    pauseCount = 0;
                    paused = false;
                }
            }
        }

        in.nextLine();
        in.nextLine();
        in.nextLine();
        in.nextLine();
    }
}

@@ -18,36 +18,8 @@
 le;%0A
-import java.io.IOException;%0A
 impo
@@ -172,30 +172,29 @@
 %0A%7B%0A    p
-rivate
+ublic
  static 
 Scanner 
@@ -185,16 +185,51 @@
  static 
+void main(String%5B%5D args) %7B%0A        
 Scanner 
@@ -243,30 +243,19 @@
 ll;%0A    
-private static
+   
  PrintWr
@@ -276,53 +276,8 @@
 l;%0A%0A
-    public static void main(String%5B%5D args) %7B%0A
     
@@ -2186,83 +2186,8 @@
 );%0A%0A
-                    //lastRest = %220xF%22 + Integer.toHexString(voice) + hex;%0A
     
@@ -2394,17 +2394,16 @@
 oice++;%0A
-%0A
         
@@ -2467,32 +2467,53 @@
     %7D%0A        %7D%0A
+        in.close();%0A%0A
         String h
@@ -2542,32 +2542,33 @@
 ng(pauseCount);%0A
+%0A
         if (paus
@@ -2674,32 +2674,33 @@
   dataLength++;%0A
+%0A
         for (Str
@@ -3027,1005 +3027,7 @@
    %7D
-%0A%0A    private static void readDataBlock() throws IOException %7B%0A        in.nextLine();%0A        in.nextLine();%0A%0A        in.useDelimiter(%22,%22);%0A%0A        String word;%0A        boolean paused = false;%0A        int pauseCount = 0;%0A%0A        StringBuilder result = new StringBuilder();%0A%0A        while(in.hasNext()) %7B%0A            word = in.next();%0A            if (!paused) %7B%0A                if (word.equals(%220x9000%22)) %7B%0A                    paused = true;%0A                %7D%0A                else %7B%0A                    result.append(word + %22,%22);%0A                %7D%0A            %7D%0A            else %7B%0A                if (word.equals(%220x9000%22)) %7B%0A                    pauseCount++;%0A                %7D%0A                else %7B%0A                    result.append(%220xFF%22 + Integer.toHexString(pauseCount) + %22,%22);%0A%0A                    pauseCount = 0;%0A                    paused = false;%0A                %7D%0A            %7D%0A        %7D%0A%0A        in.nextLine();%0A        in.nextLine();%0A        in.nextLine();%0A        in.nextLine();%0A    %7D
 %0A%7D%0A

clean up/ format code