Эх сурвалжийг харах

changes in data structure
some refactoring

Klaas, Wilfried 6 жил өмнө
parent
commit
77a8343eb6

+ 108 - 88
SPSEmulator-service/src/main/java/de/mcs/tools/midicontroller/ConvertJsonData2Hex.java

@@ -26,6 +26,7 @@ import java.io.BufferedOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 
@@ -45,6 +46,10 @@ import de.mcs.utils.JacksonUtils;
  */
 public class ConvertJsonData2Hex {
 
+  private static final int NUMBER_OF_SWITCHES = 6;
+  private static final int NUMBER_OF_SEQUENZES = 16;
+  private static final int NUMBER_OF_MIDICOMMANDS = 16;
+
   /**
    * @param args
    * @throws Exception
@@ -68,95 +73,12 @@ public class ConvertJsonData2Hex {
         out.write((byte) programData.getExternalMidi());
 
         // bis hier sind es 15 Bytes
-        byte switchSettings = 0x00;
-        ButtonData[] buttons = programData.getButtons();
-        if ((buttons != null) && (buttons.length > 2)) {
-          for (int i = 0; i < 6; i++) {
-            byte[] buttonName = getEmptyByteArray(8);
-            ButtonData buttonData;
-            if (i < buttons.length) {
-              buttonData = buttons[i];
-            } else {
-              buttonData = new ButtonData();
-              buttonData.setName("");
-              buttonData.setColor(0);
-              buttonData.setType(TYPE.SWITCH);
-            }
-            if (i < 6) {
-              buttonName = copyInto(buttonName, getStringAsByte(buttonData.getName(), 8));
-              out.write(buttonName);
-              out.write((byte) buttonData.getColor());
-            }
-            if (ButtonData.TYPE.SWITCH.equals(buttonData.getType())) {
-              switchSettings = (byte) (switchSettings | (0x01 << i));
-            }
-          }
-        } else {
-          throw new Exception("buttons not correct configured.");
-        }
-        // 9 pro Button = 54 pos: 54+15 69
-        out.write(switchSettings);
-
-        // Events starten bei 70
-        for (int i = 0; i < 15; i++) {
-          byte[] data = getEmptyByteArray(49);
-          if ((programData.getSequences() != null) && (programData.getSequences().length > i)) {
-            int eventTyp = 0;
-            SequenceData eventData = programData.getSequences()[i];
-            switch (eventData.getType()) {
-            case INTERNAL:
-              eventTyp = 0;
-              break;
-            case EXPRESSION:
-              eventTyp = 0x70;
-              break;
-            case BUTTON:
-              eventTyp = 0x10 * (eventData.getValue());
-              break;
-            default:
-              break;
-            }
-            eventTyp = eventTyp | eventData.getEvent().ordinal();
-            data[0] = (byte) (eventTyp & 0xFF);
-            DataData[] datas = eventData.getDatas();
-            int pos = 1;
-            for (DataData dataData : datas) {
-              int dataType = dataData.getType().getByte();
-              if (DataData.CHANNEL.INTERNAL.equals(dataData.getChannel())) {
-                dataType = dataType + programData.getInternalMidi();
-              }
-              if (DataData.CHANNEL.EXTERNAL.equals(dataData.getChannel())) {
-                dataType = dataType + programData.getExternalMidi();
-              }
-              data[pos++] = (byte) dataType;
-              if ((pos + 1) < data.length) { // mindestens 2 noch
-                                             // platz
-                switch (dataData.getType()) {
-                case CC:
-                case NOTE_OFF:
-                case NOTE_ON:
-                  data[pos++] = (byte) dataData.getData1();
-                  data[pos++] = (byte) dataData.getData2();
-                  break;
-                case ALL_NOTE_OFF:
-                  data[pos++] = 0x78;
-                  data[pos++] = 0x00;
-                  break;
-                case PC:
-                  data[pos++] = (byte) dataData.getData1();
-                  data[pos++] = (byte) 0;
-                  break;
-                default:
-                  break;
-                }
-              } else {
-                throw new Exception("not enough memory.");
-              }
-            }
-          }
-          out.write(data);
-        }
+        buildSwitches(out, programData);
 
+        // Sequenzes starten bei 70
+        buildSequenzes(out, programData);
+
+        // end identifier
         out.write(0xFF);
         System.out.println(String.format("actual size: %d", out.size() - saveSize));
       }
@@ -197,6 +119,104 @@ public class ConvertJsonData2Hex {
 
   }
 
+  private static void buildSequenzes(ByteArrayOutputStream out, ProgramData programData) throws Exception, IOException {
+    for (int i = 0; i < NUMBER_OF_SEQUENZES; i++) {
+      byte[] data = getEmptyByteArray(1 + 3 * NUMBER_OF_MIDICOMMANDS);
+      if ((programData.getSequences() != null) && (programData.getSequences().length > i)) {
+        int eventTyp = 0;
+        SequenceData eventData = programData.getSequences()[i];
+        switch (eventData.getType()) {
+        case INTERNAL:
+          eventTyp = 0;
+          break;
+        case EXPRESSION:
+          eventTyp = 0x70;
+          break;
+        case BUTTON:
+          eventTyp = 0x10 * (eventData.getValue());
+          break;
+        default:
+          break;
+        }
+        eventTyp = eventTyp | eventData.getEvent().ordinal();
+        data[0] = (byte) (eventTyp & 0xFF);
+        buildSequenzData(programData, data, eventData);
+      }
+      out.write(data);
+    }
+  }
+
+  private static void buildSequenzData(ProgramData programData, byte[] data, SequenceData eventData) throws Exception {
+    DataData[] datas = eventData.getDatas();
+    int pos = 1;
+    if (datas.length > NUMBER_OF_MIDICOMMANDS) {
+      throw new Exception("not enough memory.");
+    }
+    for (DataData dataData : datas) {
+      int dataType = dataData.getType().getByte();
+      if (DataData.CHANNEL.INTERNAL.equals(dataData.getChannel())) {
+        dataType = dataType + programData.getInternalMidi();
+      }
+      if (DataData.CHANNEL.EXTERNAL.equals(dataData.getChannel())) {
+        dataType = dataType + programData.getExternalMidi();
+      }
+      data[pos++] = (byte) dataType;
+      if ((pos + 1) < data.length) { // mindestens 2 noch
+                                     // platz
+        switch (dataData.getType()) {
+        case CC:
+        case NOTE_OFF:
+        case NOTE_ON:
+          data[pos++] = (byte) dataData.getData1();
+          data[pos++] = (byte) dataData.getData2();
+          break;
+        case ALL_NOTE_OFF:
+          data[pos++] = 0x78;
+          data[pos++] = 0x00;
+          break;
+        case PC:
+          data[pos++] = (byte) dataData.getData1();
+          data[pos++] = (byte) 0;
+          break;
+        default:
+          break;
+        }
+      }
+    }
+  }
+
+  private static void buildSwitches(ByteArrayOutputStream out, ProgramData programData)
+      throws UnsupportedEncodingException, IOException, Exception {
+    byte switchSettings = 0x00;
+    ButtonData[] buttons = programData.getButtons();
+    if ((buttons != null) && (buttons.length > 2)) {
+      for (int i = 0; i < NUMBER_OF_SWITCHES; i++) {
+        byte[] buttonName = getEmptyByteArray(8);
+        ButtonData buttonData;
+        if (i < buttons.length) {
+          buttonData = buttons[i];
+        } else {
+          buttonData = new ButtonData();
+          buttonData.setName(String.format("btn%d_nn", i));
+          buttonData.setColor(0);
+          buttonData.setType(TYPE.SWITCH);
+        }
+
+        buttonName = copyInto(buttonName, getStringAsByte(buttonData.getName(), 8));
+
+        out.write(buttonName);
+        out.write((byte) buttonData.getColor());
+        if (ButtonData.TYPE.SWITCH.equals(buttonData.getType())) {
+          switchSettings = (byte) (switchSettings | (0x01 << i));
+        }
+      }
+    } else {
+      throw new Exception("buttons not correct configured.");
+    }
+    // 9 pro Button = 54 pos: 54+15 69
+    out.write(switchSettings);
+  }
+
   private static byte[] getStringAsByte(String value, int count) throws UnsupportedEncodingException {
     String newValue = value;
     if (newValue.length() > count) {