/** A handler for track state changes coming from Ardour */
  private void handleChange(OSCMessage message, int whatChanged) {

    // Log.d(TAG, "Received Change: " + message.toString());

    if (state != OscService.READY) {

      Log.d(TAG, "In non Ready state. Returning.");
      return;
    }

    // Route remote Id
    Integer i = (Integer) message.getArg(0);
    Float f = null;

    for (org.ardour.Track track : routes) {

      if (track.remoteId == i.intValue()) {

        switch (whatChanged) {
          case REC_CHANGED:
            f = (Float) message.getArg(1);
            track.recEnabled = (f.floatValue() == 1);
            break;
          case SOLO_CHANGED:
            f = (Float) message.getArg(1);
            track.soloEnabled = (f.floatValue() == 1);
            break;
          case MUTE_CHANGED:
            f = (Float) message.getArg(1);
            track.muteEnabled = (f.floatValue() == 1);
            break;
          case NAME_CHANGED:
            track.name = (String) message.getArg(1);
            break;
          case GAIN_CHANGED:
            double gainval = (float) message.getArg(1) * 1000.0;
            if (gainval <= 0.0000000001) gainval = 0.0000000001;
            track.trackVolume = track.valueToSlider(gainval);
            System.out.println(
                "Gain as Int: " + track.valueToSlider(gainval) + ", fromArdour: " + gainval);
            if (track.getTrackVolumeOnSeekBar())
              return; // gein change from user on seekbar we return here

            break;
        }

        // send message to view
        Message msg = transportHandler.obtainMessage(2000, routes);
        transportHandler.sendMessage(msg);

        return;
      }
    }
  }
        @Override
        public void messageReceived(OSCMessage message, SocketAddress addr, long time) {

          // Log.d(TAG, "Received Reply: " + message.getName());

          switch (state) {
            case READY:
              int changeId = -1;

              if (message.getName().equals("/route/solo")) {
                changeId = SOLO_CHANGED;
                handleChange(message, changeId);
              } else if (message.getName().equals("/route/mute")) {
                changeId = MUTE_CHANGED;
                handleChange(message, changeId);
              } else if (message.getName().equals("/route/rec")) {
                changeId = REC_CHANGED;
                handleChange(message, changeId);
              } else if (message.getName().equals("/route/name")) {
                changeId = NAME_CHANGED;
                handleChange(message, changeId);
              } else if (message.getName().equals("/route/gain")) {
                changeId = GAIN_CHANGED;
                handleChange(message, changeId);
              } else if (message.getName().equals("/ardour/transport_frame")) {

                Long clock = (Long) message.getArg(0);

                Message msg = transportHandler.obtainMessage(3000, clock);
                transportHandler.sendMessage(msg);
              }

              break;

            case ROUTES_REQUESTED:
              updateTrackList(message);

              break;
          }
        }
  /**
   * Update the tracklist as received from Ardour
   *
   * @param message
   */
  private void updateTrackList(OSCMessage message) {

    Log.d("A", message.toString());

    /*
    for(int i = 0;i < message.getArgCount(); i++){
    	System.out.println("Message at " + i + " : " + message.getArg(i));
    }

    */
    // First arg
    String s = (String) message.getArg(0);

    if (s.equals("end_route_list")) {

      Long frameRate = (Long) message.getArg(1);

      Message msg = transportHandler.obtainMessage(500, frameRate);
      transportHandler.sendMessage(msg);

      Long maxFrame = (Long) message.getArg(2);

      Message msg1 = transportHandler.obtainMessage(400, maxFrame);
      transportHandler.sendMessage(msg1);

      Message msg2 = transportHandler.obtainMessage(1000, routes);
      transportHandler.sendMessage(msg2);

      state = OscService.READY;
    } else {

      org.ardour.Track t = new org.ardour.Track();

      // System.out.println("Track contains: " + s);

      if (s.equals("AT")) {
        t.type = org.ardour.Track.AUDIO;
      } else if (s.equals("MT")) {
        t.type = org.ardour.Track.MIDI;
      } else if (s.equals("B")) {
        t.type = org.ardour.Track.BUS;
      }

      // Set the name of the track
      s = (String) message.getArg(1);
      t.name = s;

      // Set mute state
      Integer i = (Integer) message.getArg(4);
      t.muteEnabled = (i.intValue() > 0);

      // Set solo state
      i = (Integer) message.getArg(5);
      t.soloEnabled = (i.intValue() > 0);

      // Set remote id
      i = (Integer) message.getArg(6);
      t.remoteId = i.intValue();

      // Set record state
      if (t.type == org.ardour.Track.AUDIO || t.type == org.ardour.Track.MIDI) {
        i = (Integer) message.getArg(7);
        t.recEnabled = (i.intValue() > 0);
      }

      routes.add(t);
    }
  }