# Sustain pedal script



## hastoy (May 7, 2008)

Hi everyone,

I'm using a sustain pedal script written by Kotori (hi Nils if you're reading this !) and I'm having a problem with it. Whenever I play many notes, on the release of the sustain pedal I get an annoying click. I've got a pentium quad and 3gb of ram, a good sound card etc. so it's not my computer. I'm posting the script below and if some of you code gurus out there could help I would appreciate a lot. thanks :D 

Giom

here's the script:

{ "keep all" notes custom sustain pedal behaviour.

Sustain pedal script by Nils Liberg and based on ideas by Hans Adamson

Explanation:
Upon release of the sustain pedal the builtin Kontakt sustain pedal script will release all
notes which are not held pressed. If multiple instances of a single note was triggered the
last one will be kept and note-off messages for the other ones issued. This can cause
problems when you, while holding the pedal pressed, play for example a high-velocity C4 and 
then a low-velocity C4 and then release the pedal while holding the C4 note pressed.
Since the first event is released and the second is not there will be a sudden jump down
in volume which is quite unrealistic for pianos.

Unlike the builtin script this script will keep both of the two C4 samples sounding until the key is released. 
}

on init
declare const $NONE := 0 
declare $i 
declare %ids[512] { active event IDs in no particular order, NONE for unused elements }
declare %notes[512] { note number for each of the above events }

{ turn off Kontakt's builtin sustain pedal script }
SET_CONDITION(NO_SYS_SCRIPT_PEDAL) 
end on

on note
{ add this note event to the first empty array slot }
$i := search(%ids, 0) 
if ($i # -1)
%ids[$i] := $EVENT_ID
%notes[$i] := $EVENT_NOTE 
else
ignore_event($EVENT_ID) { just a safety precaution in case the polyphony would reach 512 }
end if
end on

on release
if (%CC[64] >= 64)
{ ignore note-off when sustain pedal is pressed }
ignore_event($EVENT_ID)
else
{ release all event IDs with the same note number as this event }
$i := 0
while ($i <= num_elements(%ids)-1)
if (%ids[$i] # $NONE and %notes[$i] = $EVENT_NOTE)
note_off(%ids[$i])
%ids[$i] := $NONE
end if
inc($i)
end while
end if
end on

on controller
{ if sustain pedal released }
if (%CC_TOUCHED[64] # 0 and %CC[64] < 64)
{ release all events for which the key is no longer held pressed }
$i := 0
while ($i <= num_elements(%ids)-1)
if (%ids[$i] # $NONE and %KEY_DOWN[%notes[$i]] = 0)
note_off(%ids[$i])
%ids[$i] := $NONE
end if
inc($i)
end while
end if
end on


----------

