Stuck MIDI notes

I am having a problem with stuck MIDI notes which happens when a noteOff happens quickly after a noteOn. I am receiving MIDI through MIDIGuitar 2, which is an audio to midi converter. When converting guitar notes, very short bogus notes happens, so note durations can be very short. I filter out these events later in the code, but I would like to find a simple way to deal with the immediate MIDI playback of the note. The problem is that there is no way of knowing how long a note will be, ie. how long before a noteOff for the same note is sent, when the noteOn happens - can’t look into the future.

I can solve the problem with FreeSelf and DetectSilence but since I am using my synthdefs in different ways, including setting amp to 0 and back to non-0, this would mean I would have to keep several versions of the synthdefs which is not ideal but could be a last resort.

Any other ideas to how I can avoid dead synths piling up?

Here is a demonstration of the issue:

(
~notes = Array.newClear(128);
MIDIdef.noteOn(\noteOn, {|vel, nn, chan, srcID|
	~notes[nn] = Synth(\default, [freq: nn.midicps, amp: 0.1]);
	~notes[nn].nodeID.debug(\nodeID);
});

MIDIdef.noteOff(\noteOff, {|vel, nn|
	~notes[nn].set(\gate, 0)
});
// m = MIDIOut.new(MIDIClient.destinations.collect{|n|n.device}.indexOfEqual("IAC Driver"));
m = MIDIOut.new(1); // corresponds to my IAC 
)

( // run several time and watched the node tree. I get stuck notes pretty frequently
{
	10.do{
		var nn = rrand(50, 60);
		m.noteOn(nn);
		0.01.wait;
		m.noteOff(nn)
	}
}.fork
)

Can you add Impulse.kr(0) to the gate? No need for multiple SynthDefs, just habitually force one control block of the gate being open.

There are several other threads about this problem.

hjh

Yes weird I did not think of that, have used the trick elsewhere, thought the problem was cause by something else. Anyway, it works, thanks.