I’m currently trying to create a system where the sound of cutting cabbage is divided into four layers, and those layers are played sequentially.
I wrote the following script, and although Kontakt doesn’t show any errors, it doesn’t work as intended.
I would really appreciate any advice or feedback.
on init
{ Display initialization message }
message("--- Cabbage Cut Foley Engine Initialized ---")
{ Define constants for Group Indices 0 to 4 }
declare const $GRP_L1_ATTACK := 0
declare const $GRP_L2_TEAR := 1
declare const $GRP_L3_SPEED := 2
declare const $GRP_L4_BOARD := 3
declare const $GRP_L5_RELEASE := 4
{ Define the sequence of groups to be played }
declare %sequence[5] := ($GRP_L1_ATTACK, $GRP_L2_TEAR, $GRP_L3_SPEED, $GRP_L4_BOARD, $GRP_L5_RELEASE)
{ Polyphonic variables to store the unique Event ID and incoming Velocity }
declare polyphonic $played_id
declare polyphonic $velocity
declare $i { Loop counter variable }
$i := 0
declare $current_group_index
$current_group_index := %sequence[$i]
end on
on note
{ 1. Ignore the incoming Note On event to prevent default triggering }
ignore_event($EVENT_ID)
{ Capture the input velocity }
$velocity := $EVENT_VELOCITY
{ 3. Loop through the defined group sequence }
while ($i < num_elements(%sequence))
{ Disallow all groups initially so the script takes full control of playback }
disallow_group($ALL_GROUPS)
{ 4. Disable all groups, then enable only the current target group }
disallow_group($ALL_GROUPS)
allow_group($current_group_index)
{ 5. Generate a new note event (voice) and get its ID. Duration 0 plays the entire sample. }
$played_id := play_note($EVENT_NOTE, $velocity, 0, 0)
{ 6. Wait loop: monitor the event status until the voice is finished. }
while (event_status($played_id) = $EVENT_STATUS_NOTE_QUEUE)
wait(10000) { Wait for 10 milliseconds to prevent CPU overload }
end while
inc($i) { Increment counter to move to the next group }
end while
{ 7. Once sequence is complete, disallow all groups again }
disallow_group($ALL_GROUPS)
end on
on release
{ Stop the currently monitored event if the user releases the key early }
note_off($played_id)
{ Ignore the original MIDI Note Off event }
ignore_event($EVENT_ID)
end on