Triggering a Counter With a MIDdef

/* I cannot get this simple counter to work in a MIDIdef. I need to count the number of notes that I play. Thank you in advance for help!*/

MIDIIn.connectAll;

~counting = 1;
~counting = ~counting + 1;

(
MIDIdef.noteOn(
\counting,

{arg a = 1;
	
	a = a + 1;
	a.postln;
};

);
)

Hello!
I think the number of notes “that you play” could be interpreted as follows:

  1. The chronological sum of the notes which have been played.
  2. The simultaneous notes which are pressed currently.

Assuming from your code template, I think the first one is what you think.
Is the following code appropriate? It includes both of what I mentioned above:

(
~numPressedCurrently = 0;
~numPressedChronologically = 0;
MIDIIn.connectAll;
~noteOn = MIDIdef.noteOn(\noteOn, {arg ...args;
	~numPressedChronologically = ~numPressedChronologically + 1;
	~numPressedCurrently = ~numPressedCurrently + 1;
	("played:" + ~numPressedChronologically ++ "; pressed: " + ~numPressedCurrently).postln;
});
~noteOff = MIDIdef.noteOff(\noteOff, {arg ...args;
	~numPressedCurrently = ~numPressedCurrently - 1;
	("played:" + ~numPressedChronologically ++ "; pressed: " + ~numPressedCurrently).postln;
})
)

Your MIDIdef runs every note you play. It starts with setting a = 1, adds 1 to it and posts the result.

In order to keep count you can’t re-set your counter every time you want to add to it. You already have defined a global variable, use that in your function instead of a local variable, like
{~counting = ~counting + 1},

1 Like

This is great, thank you!

1 Like