Check .list Until PortName found

edited September 2014 in Library Questions

Hi All,

I'm using TheMIDIBus library for my project, although I believe this to be a generic .list question. I have this line that connects to the MIDI device:

MidiBus.list();

myBus = new MidiBus(this, "DEVICE NAME", 0);

This is great, however I want to be able to use my GUI whether the MIDI device is plugged in or not, but when it's connected to my PC, the GUI sends a "Device Found" message and enables some of its features. Is there a way I can continuously check the list for "DEVICE NAME" until it's found? Haven't been able to come up with anything on my own, so I'm hoping there's an easy solution out there. Thanks in advance!

Pat

Answers

  • edited September 2014

    You should create a parallel Thread in order to keep attempting instantiating a new MidiBus until success!
    For that, you can use the following Processing's undocumented function called thread(""). (*)
    It invokes a function w/ the same name as its String argument as a separate Thread.
    You just need to arrange a loop there which breaks itself once a MidiBus object is created.

    Check it out my example below: B-)

    P.S.: Since I've never used this library before, got no idea how to check whether a device is valid!
    It's up to you to modify this attempt in order to do your bid! :p

    // forum.processing.org/two/discussion/7393/
    // check-list-until-portname-found
    
    import themidibus.MidiBus;
    MidiBus device;
    
    static final String MIDI  = "Real Time Sequencer";
    static final int INTERVAL = 10 * 1000; // 10 seconds.
    static final int FPS = 1;
    
    void setup() {
      size(300, 200, JAVA2D);
      frameRate(FPS);
      thread("checkMIDI");
    }
    
    void draw() {
      background((color) random(#000000));
    
      frame.setTitle("Seconds: " + frameCount/FPS);
    
      if (device != null) {
        println("Finally found: ");
        println(device);
        noLoop();
      }
    }
    
    void checkMIDI() {
      for (; device == null; delay(INTERVAL))
        device = new MidiBus(this, MIDI);
    }
    
Sign In or Register to comment.