Drawing something with midi controller (TheMidiBus)

edited February 2016 in Library Questions

Hi,

I am very new to Processing and try to experiment with creating visuals with a midi track. As a starter I tried to simply draw a line with a midi controller. It does receive the controller data because if I println(value) it shows the value of my controller but it doesn't use it to draw something.

Any help appreciated.

Answers

  • Might be a threading issue where controllerChange() is called outside of the draw loop. Processing 3 got really strict on where you call graphic methods. Try storing value in a global variable and call line() in draw.

  • Thanks for your reply. I tried the following but it doesn't work. What I don't understand is, if I return a variable from inside the ControllerChange function, how can I grab this variable? Obviously, I am not calling the function in draw. Thanks!

    import themidibus.*; //Import the library
    
    MidiBus myBus; // The MidiBus
     
    void setup() {
    size(400, 400);
    background(100);
    MidiBus.list();
    myBus = new MidiBus(this, 2, 2);
    }
    
    int value; 
    
    void draw(){
    line(0, value, width, value);
    }
    
    int controllerChange(int channel, int number, int value) {
      // Receive a controllerChange
      println();
      println("Controller Change:");
      println("--------");
      println("Channel:"+channel);
      println("Number:"+number);
      println("Value:"+value);
      return value;
    }
    
  • Can you still receive midi messages in controllerChange()? Try changing it back to void and replace return value;with this.value = value;.

  • PDE comes w/ lotsa Processing examples, including those from contributed libraries.
    Hit CTRL+SHIFT+O to open it. Then click at "Contributed Libraries". And finally "The MidiBus".

  • @colouredmirrorball: You're a legend, thanks a lot, it works now. If you have a minute here a follow-up question: 1. If I want to access not only value but also the number and channel variables outside of ControlChange, how would that work?

  • Sure, just make them global variables as well.

    It might get complex if you make a new variable for each possible combination of channel and number, maybe you can create an array (2D if you want to include channel in addition to number) or a little class. It's really going to depend on what you want to do with them further on...

  • interesting topic. I am new to Processing, and tried to follow the code posted above, but if I replace return value; with this.value=value; I'll get an error message ("this method must return a result of type int"). anybody can help with this?

  • edited March 2016

    It's because you didn't change the return type of the method back to void.

    void controllerChange(int channel, int number, int value) {
      this.value = value;
    }
    
  • ok, understand now, thank you so much for your help!

Sign In or Register to comment.