package korgnano;

import javax.sound.midi.*;

public class Kontrol2 implements Receiver {
  private MidiDevice device;
  public Kontrol2() {
    this.device = getNanoKontrol2Device();
  }

  ShortMessage lastMessage;
  long lastMessageTimestamp;

  private int[] sliders = new int[8];
  private int[] dials = new int[8];
  private boolean[] soloButtons = new boolean[8];
  private boolean[] muteButtons = new boolean[8];
  private boolean[] recordButtons = new boolean[8];

  public int slider(int index) {
    return sliders[index-1];
  }
  public int dial(int index) {
    return dials[index-1];
  }
  public boolean solo(int index) {
    return soloButtons[index-1];
  }
  public boolean mute(int index) {
    return muteButtons[index-1];
  }
  public boolean record(int index) {
    return recordButtons[index-1];
  }

  // Receiver methods:
  public void send(MidiMessage midiMessage, long timestamp) {  // send, aka receive
    if (midiMessage.getStatus() == ShortMessage.CONTROL_CHANGE) {  // nanoKONTROL only sends CONTROL messages.
      ShortMessage message = (ShortMessage)midiMessage;

      int data1 = message.getData1();
      int data2 = message.getData2();

      if (0 <= data1 && data1 <= 7) {
        sliders[data1] = data2;
      }
      else if (16 <= data1 && data1 <= 24) {
        dials[data1-16] = data2;
      }
      else if (32 <= data1 && data1 <= 39) {
        // only on when you're holding it:
        //soloButtons[data1-32] = data2 == 127;

        // toggle when you press down:
        if (data2 == 127) { soloButtons[data1-32] = !soloButtons[data1-32]; }

        // toggle when you release:
        //if (data2 == 0) { soloButtons[data1-32] = !soloButtons[data1-32]; }
      }
      else if (48 <= data1 && data1 <= 55) {
        if (data2 == 127) { muteButtons[data1-48] = !muteButtons[data1-48]; }
      }
      else if (64 <= data1 && data1 <= 71) {
        if (data2 == 127) { recordButtons[data1-64] = !recordButtons[data1-64]; }
      }

      lastMessage = message;
      lastMessageTimestamp = timestamp;
    }
  }
  public void close() {
  }

  // initialization methods
  private MidiDevice getNanoKontrol2Device() {
    MidiDevice.Info info = getNanoKontrol2DeviceInfo();
    if (info != null) {
      try {
        MidiDevice device = MidiSystem.getMidiDevice(info);
        if (!device.isOpen()) {
          device.open();
        }

        device.getTransmitter().setReceiver(this);
        //device.getTransmitter().setReceiver(device.getReceiver());

        return device;
      }
      catch(MidiUnavailableException x) {
        System.err.println("Tried to connect to the Midi device, but it looks like Midi is unavailable");
        return null;
      }
    }
    return null;
  }

  private MidiDevice.Info getNanoKontrol2DeviceInfo() {
    //println(MidiSystem.getMidiDeviceInfo());
    for (MidiDevice.Info info : MidiSystem.getMidiDeviceInfo()) {
      //println(info.getName());
      //println(info.getDescription());
      if (info.getName().indexOf("nanoKONTROL2") == 0) { // [hw:2,0,0]  <- the '2' is, like, the channel
        return info;
      }
    }
    System.err.println("Couldn't find the korg nanoKontrol2!");
    return null;
  }
}

@@ -1102,21 +1102,25 @@
     int 
-data1
+channelId
  = messa
@@ -1144,21 +1144,28 @@
     int 
-data2
+messageValue
  = messa
@@ -1191,38 +1191,46 @@
    if (0 %3C= 
-data1 && data1
+channelId && channelId
  %3C= 7) %7B%0A   
@@ -1246,22 +1246,33 @@
 ers%5B
-data1%5D = data2
+channelId%5D = messageValue
 ;%0A  
@@ -1294,38 +1294,46 @@
 e if (16 %3C= 
-data1 && data1
+channelId && channelId
  %3C= 24) %7B%0A  
@@ -1348,25 +1348,36 @@
 als%5B
-data1-16%5D = data2
+channelId-16%5D = messageValue
 ;%0A  
@@ -1399,38 +1399,46 @@
 e if (32 %3C= 
-data1 && data1
+channelId && channelId
  %3C= 39) %7B%0A  
@@ -1504,25 +1504,36 @@
 ons%5B
-data1-32%5D = data2
+channelId-32%5D = messageValue
  == 
@@ -1581,37 +1581,44 @@
 wn:%0A        if (
-data2
+messageValue
  == 127) %7B soloB
@@ -1616,37 +1616,41 @@
 ) %7B soloButtons%5B
-data1
+channelId
 -32%5D = !soloButt
@@ -1645,37 +1645,41 @@
  = !soloButtons%5B
-data1
+channelId
 -32%5D; %7D%0A%0A       
@@ -1717,29 +1717,36 @@
       //if (
-data2
+messageValue
  == 0) %7B sol
@@ -1750,29 +1750,33 @@
 soloButtons%5B
-data1
+channelId
 -32%5D = !solo
@@ -1783,21 +1783,25 @@
 Buttons%5B
-data1
+channelId
 -32%5D; %7D%0A
@@ -1825,38 +1825,46 @@
 e if (48 %3C= 
-data1 && data1
+channelId && channelId
  %3C= 55) %7B%0A  
@@ -1865,37 +1865,44 @@
 ) %7B%0A        if (
-data2
+messageValue
  == 127) %7B muteB
@@ -1904,29 +1904,33 @@
 muteButtons%5B
-data1
+channelId
 -48%5D = !mute
@@ -1937,21 +1937,25 @@
 Buttons%5B
-data1
+channelId
 -48%5D; %7D%0A
@@ -1987,22 +1987,30 @@
  %3C= 
-data1 && data1
+channelId && channelId
  %3C= 
@@ -2031,13 +2031,20 @@
 if (
-data2
+messageValue
  == 
@@ -2060,29 +2060,33 @@
 cordButtons%5B
-data1
+channelId
 -64%5D = !reco
@@ -2099,13 +2099,17 @@
 ons%5B
-data1
+channelId
 -64%5D

Rename method-local variables