Skip to content

Bitwig Extensions

Still working on the Open Woods Project. For now check out the bitwig library of extensions. https://github.com/bitwig/bitwig-extensions

My Super Bank Class!

Super Bank
public class SuperBank {


  private final HashMap<CursorTrack, ArrayList<BooleanValue>> cursor_track_equals_values;
  private final ArrayList<SuperBankDevices> track_devices;
  int NUM_TRACKS = 96;
  int NUM_SCENES = 64;

  TrackBank trackBank;


  private ControllerHost host;

  public SuperBank(ControllerHost host) {
    this.host = host;
    track_devices = new ArrayList<SuperBankDevices>();

    trackBank = host.createTrackBank(NUM_TRACKS, 0, NUM_SCENES, true);
    for (int i = 0; i < NUM_TRACKS; i++) {
      final int track_id = i;
      Track     track    = trackBank.getItemAt(i);
      track.arm().markInterested();
      track.name().markInterested();
      track.color().markInterested();
      track.isActivated().markInterested();
      track.isGroup().markInterested();
      track.isGroupExpanded().markInterested();

      track_devices.add(new SuperBankDevices(track));

      //Always Select track when selecting a slot.
      track.clipLauncherSlotBank().addIsSelectedObserver((slot_index, b) -> slotBankIsSelectedUpdate(track_id, slot_index, b));
    }

    cursor_track_equals_values = new HashMap<CursorTrack, ArrayList<BooleanValue>>();
    Signal fold = host.getDocumentState().getSignalSetting("Collapse", "Super Bank", "Collapse Super Bank");
    fold.addSignalObserver(() -> expandTracks(false));
    Signal unfold = host.getDocumentState().getSignalSetting("Expand", "Super Bank", "Expand Super Bank");
    unfold.addSignalObserver(() -> expandTracks(true));
  }


  private void slotBankIsSelectedUpdate(int track_id, int slot_index, boolean b) {
    //select a track when you select a slot.
    if (b) {
      Track            channel = trackBank.getItemAt(track_id);
      ClipLauncherSlot d       = channel.clipLauncherSlotBank().getItemAt(slot_index);
      channel.selectInEditor();
    }
  }

  private void isSelectedUpdate(int track_id, boolean b) {
    host.println("selected: " + track_id + " : " + b);
  }

  public void createEqualsValues(CursorTrack cursor_track) {
    ArrayList<BooleanValue> equals_values = new ArrayList<>();
    for (int i = 0; i < NUM_TRACKS; i++) {
      BooleanValue equals_value = trackBank.getItemAt(i).createEqualsValue(cursor_track);
      equals_value.markInterested();
      equals_values.add(equals_value);
    }
    cursor_track_equals_values.put(cursor_track, equals_values);
  }

  public TrackBank get() {
    return trackBank;
  }

  public List<String> getIndexedTrackNames() {
    List<String> trackNames = new ArrayList<>();
    boolean masterHit = false;
    for (int i = 0; i < NUM_TRACKS; i++) {
      Track  track = trackBank.getItemAt(i);
      String name  = track.name().get();
      if(name == "Master") {
        masterHit = true;
      }
      if(masterHit && name.isEmpty()) {
        break;
      }
      trackNames.add(name);
    }
    return trackNames;
  };

  public int getCursorIndex(CursorTrack cursor_track) {
    ArrayList<BooleanValue> equals_values = cursor_track_equals_values.get(cursor_track);
    int                     size          = equals_values.size();
    for (int i = 0; i < size; i++) {
      boolean is_equals = equals_values.get(i).get();
      if (is_equals) {
        return i;
      }
    }
    return -1;
  }

  public Track getItemAt(int i) {
    return trackBank.getItemAt(i);
  }

  public boolean cursorHasDrumPads(CursorTrack cursor_track) {
    int index = getCursorIndex(cursor_track);
    return track_devices.get(index).hasDrumPads();
  }

  public ArrayList<Integer> getCursorDrumPads(CursorTrack cursor_track) {
    int index = getCursorIndex(cursor_track);
    if (index != -1) {
      return track_devices.get(index).getDrumPadNotes();
    } else {
      return new ArrayList<>();
    }
  }

  public ArrayList<Device> getCursorTrackDevices(CursorTrack cursor_track) {
    int index = getCursorIndex(cursor_track);
    if (index != -1) {
      return track_devices.get(index).getDevices();
    } else {
      return new ArrayList<>();
    }
  }

  /**
   * Sets expansion of all groups in bank until they are all set to target.
   *
   * */
  public void expandTracks(boolean expanded) {
    List<Track> tracks = new ArrayList<>();
    for (int i = 0; i < NUM_TRACKS; i++) {
      Track track = trackBank.getItemAt(i);
      if (trackBank.getItemAt(i).isGroup().get()) {
        if(  track.isGroupExpanded().get() !=  expanded) {
          track.isGroupExpanded().set(expanded);
          host.scheduleTask(()->expandTracks(expanded), 50);
          return;
        }
      }
    }
  }
}