Functions outside draw? the midibus

Hello I have a very basic question but would love some clarification.

I have a project that will utilize Midi, I have installed theMidiBus and it is all working, I found a tutorial that helped me but I am confused about something.

In the examples he builds a function below draw to listen to midi input and print the values to the screen.

What I don’t understand is that he does not call this function from with in draw. And yet its live and active when running a very simple sketch. Is it due to theMidiBus library, is it something else? I sure feel like I have written a function and then not had it work because no one was actually calling it, via update or some such.

/void setup() {
size(800, 600);
background(255);
myBus = new MidiBus(this, 1, -1); //Create a new MidiBus with input1 and the default Java Sound Synthesizer as the output device.
}

void draw() {
fill(125);
rect(x, y, 30, 30);
}

void controllerChange(int channel, int number, int value) {
// Receive a controllerChange
println();
println(“Controller Change:”);
println("--------");
println(“Channel:”+channel);
println(“Number:”+number);
println(“Value:”+value);
}/

http://discourse.processing.org/faq#format-your-code

That’s known as a callback function.

Even setup(), draw(), etc. are also callback functions.

You need to read a library documentation in order to know whether it features any callbacks.

3 Likes

Thank you, that makes sense. I appreciate your help.

2 Likes

So then is

keyPressed() ;

a call back function as well?

Yes, keyPressed() is indeed a callback method from class PApplet: :keyboard:

However controllerChange() isn’t actually predefined within class PApplet. :no_mouth:

The class MidiBus needs to use Java reflective techniques in order to introspect whether or not we had defined a controllerChange() callback method within our PApplet subclass sketch: :mag:

1 Like

So good! THANK YOU

I will dig into this!