triggering multiple instances of a function and line~ amplitude envelope with midi keys

Daniel's icon

Hey everyone,

Attached is a function and line~ envelope that I am using as a part of a bigger patch. It is triggered by my midi keyboard to be used as an amplitude envelope. The goal is that each time I press a midi note, the envelope will play until the end of the envelope specified, or until I release the midi note, and this should be the case regaurdless of whether I press another note in the meantime. I have gotten the monophonic version to work, that is, if I only press one key at a time, and do not press any other key, but I want to use four instances of it, so that when I press four keys at once, each key will trigger its own envelope. The problem is when I try to do multiple instances of this envelope, each key interferes with triggering the other envelopes. Can multiple instances of this envelope be made and used to work with multiple midi keys at one time?

Could anyone help me rectify this problem? I'm lacking brain power! I have used a sine wave in this patch just as a placeholder for the sound that the envelope will be used for. ( can it be done without poly~?)

funcAmpEnvelope.maxpat
Max Patch