"on note" callback not executed if gap after release is too small

artmyb
artmyb Member Posts: 1 Newcomer
edited December 2023 in Scripting Workshop

Hi there!

I am trying to write a legato script in Kontakt 5 script processor. It has some functionalities like:

- if there is a note overlap, naturally a legato sample is played back.

-1 seconds after a legato transition, if the note is held, the legato sample blends into a "start" sample, in case legato samples are not long enough

-if there is a small gap (200ms by default, adjustable) between a note release and another note on signal:

1) if the notes are different, a legato sample is played back.

2) if the notes are the same, a repetition sample is played back.

The Problem is:

if the gap is too small (to be specific, smaller than 12ms that a found by experimenting with adjusting midi data on a DAW); "on note" callback is not executed at all.

I mean, it really is not executed at all, not just due to some boolean condition within the callback. I am sure of that because i've checked by printing a message just after "on note" line, which is not displayed of the gap is <12ms

What might be the reason for this and how to solve it?

the code (only the note on/off callbacks):

on note
$last_id := $EVENT_ID
ignore_event($ALL_EVENTS)
inc($note_counter)
$last_time_note := $ENGINE_UPTIME
$counter := num_elements(%downkeys)-1
while ($counter > 0)
	%downkeys[$counter] := %downkeys[$counter-1]
	dec($counter)
end while


$before_rep := 0


%downkeys[0] := $EVENT_NOTE



	if ($note_counter = 1)
		fade_out($ALL_EVENTS,$fade_time_release,1)
		disallow_group($ALL_GROUPS)
		if ($ENGINE_UPTIME < $last_time_release + $gap_time)
			message("WHY?")
			$before_rep := 1
			stop_wait($release_id,1)
			if ($EVENT_NOTE = $last_release )
				allow_group(find_group("0"))
			else
				allow_group(find_group(!intervals[$EVENT_NOTE-$last_release+12]))
			end if
		else
			allow_group(find_group("start"))
		end if
		play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0)
	else
		if ($EVENT_NOTE-%downkeys[1] < 13 and $EVENT_NOTE-%downkeys[1] > -13)
			fade_out($ALL_EVENTS,$fade_time,1)
			disallow_group($ALL_GROUPS)
			allow_group(find_group(!intervals[$EVENT_NOTE-%downkeys[1]+12]))
			fade_in(play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0),$fade_time)
		else
			fade_out($ALL_EVENTS,$fade_time_release,1)
			disallow_group($ALL_GROUPS)
			allow_group(find_group("release"))
			fade_in(play_note(%downkeys[1],$EVENT_VELOCITY,0,0),$fade_time_release)
			disallow_group($ALL_GROUPS)
			allow_group(find_group("start"))
			play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0)


		end if
	end if
	$before1_wait := $ENGINE_UPTIME
	wait(1000000)
	if ($ENGINE_UPTIME >= $before1_wait+1000)
		fade_out($ALL_EVENTS,500000,1)
		disallow_group($ALL_GROUPS)
		allow_group(find_group("start"))
		fade_in(play_note(%downkeys[0],$EVENT_VELOCITY,0,0),500000)
	end if


end on


{**********}


on release
ignore_event($ALL_EVENTS)
dec($note_counter)
$last_time_release := $ENGINE_UPTIME
$release_id := $NI_CALLBACK_ID


$before_rep := 0
$last_release := $EVENT_NOTE
	if ($note_counter = 0)
		wait(1000*$gap_time)
		if ($before_rep = 0)
			fade_out($ALL_EVENTS,$fade_time_release,1)
			disallow_group($ALL_GROUPS)
			allow_group(find_group("release"))
			fade_in(play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0),$fade_time_release)
		end if
		$counter := search(%downkeys,$EVENT_NOTE)
				while ($counter < num_elements(%downkeys))
					%downkeys[$counter] := %downkeys[$counter+1]
					inc($counter)
				end while
	else
		if ($EVENT_NOTE = %downkeys[0])
			if ($EVENT_NOTE-%downkeys[1] < 13 and $EVENT_NOTE-%downkeys[1] > -13)
				fade_out($ALL_EVENTS,$fade_time,1)
				disallow_group($ALL_GROUPS)
				allow_group(find_group(!intervals[%downkeys[1]-$EVENT_NOTE+12]))
				fade_in(play_note(%downkeys[1],$EVENT_VELOCITY,0,0),$fade_time)
	    	else
				fade_out($ALL_EVENTS,$fade_time_release,1)
				disallow_group($ALL_GROUPS)
				allow_group(find_group("release"))
				fade_in(play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0),$fade_time_release)
				disallow_group($ALL_GROUPS)
				allow_group(find_group("start"))
				play_note(%downkeys[1],$EVENT_VELOCITY,0,0)
			end if
			$counter := search(%downkeys,$EVENT_NOTE)
				while ($counter < num_elements(%downkeys))
					%downkeys[$counter] := %downkeys[$counter+1]
					inc($counter)
				end while
		else
			$counter := search(%downkeys,$EVENT_NOTE)
				while ($counter < num_elements(%downkeys))
					%downkeys[$counter] := %downkeys[$counter+1]
					inc($counter)
				end while
		end if


	end if

	$before2_wait := $ENGINE_UPTIME
	wait(1000000)
	if ($ENGINE_UPTIME >= $before2_wait+1000)
		fade_out($ALL_EVENTS,500000,1)
		disallow_group($ALL_GROUPS)
		allow_group(find_group("start"))
		fade_in(play_note(%downkeys[0],$EVENT_VELOCITY,0,0),500000)
	end if


end on


Back To Top