Counting all midi notes over the whole range

Kim Morken's icon

Hi! I'm working with MIDI in Max and I have a situation where the same note (e.g. MIDI note 60) is played twice in quick succession — meaning two note-on events are triggered before any note-off.

I'm splitting note-on and note-off handling into separate parts of my patch, but I want to make sure that note-off is only sent after all note-ons have been released. That is:

  • If a note-on for note 60 comes in twice,

  • I need two corresponding note-offs before I actually send out the note-off message.

Basically, I want to keep track of how many times a note has been triggered, and only send the note-off when the count for that note reaches zero.

Context:
I'm building a playable arpeggiator, where every key press — regardless of pitch — triggers the next note in a pre-defined sequence. The sequence can include the same note multiple times, so a single note might be "turned on" multiple times before it's supposed to turn off.

Is there a clean way to implement this kind of reference counting for MIDI notes in Max?

Thanks!

TFL's icon

You could use a list (stored in [zl.reg]), [array] or [coll] to store this kind of data. Here is an example using an array.

Max Patch
Copy patch and select New From Clipboard in Max.
Roman Thilenius's icon

when the second note on 60 comes in, should a second event start or is the second identical note on ignored by your generator?

Source Audio's icon

you can use makenote with long note duration, check repeat modes,

and pick one that suits you best.

Kim Morken's icon

Thanks. I will try this

Roman: The second note on 60 should also get played.

I started making a sub patch with 128 counters, one for each number counting up and down for note on and off, and outputting a 0 when the counter was reset. I think this was probably i stupid way of fiksing this?

Would a solution like this be wery cpu heavy?

I will try your sugestions:) Thanks a lot

TFL's icon

Would a solution like this be wery cpu heavy?

No because there is very not much happening, but would still be far from the most simple/efficient solution.

My proposition is basically the same conceptually except that the number of played notes for each note number is stored in a single array, and I add/remove 1 for a given note just by querying the number at the index defined by the node number, pass it through a [+ 1] or [- 1] depending on if the velocity is > 0 or is 0, and store the new result back into the array.
You could add a [== 0] just after the [- 1] so you could know when the count for a given note reaches 0.

But it is still not very clear to me how you generate your note on/off messages in your arpegiator.


and only send the note-off when the count for that note reaches zero

Not sure [makenote] will work for this, as it will send a note off a the end of each note duration.

Kim Morken's icon

Thanks a lot TFL! Your sugesstion worked like a charm:) Its not completely finnished but here it is:)

Playable_arp.amxd
amxd

Source Audio's icon

makenote follows input,

means it clears if note off is received before set time elapsed.