# Repetition key multiscript? Is it even possible?



## ProfoundSilence (Jul 12, 2019)

Does anyone know if there is an assignable repetition multiscript? I.e. C0 repeats last note used, maintaining the velocity used when you press the repetition key(so it doesn't machine gun the last velocity, but rather repeats the note using the new velocity)

I find it's extremely useful in some libraries, but isn't used in the libraries I find useful. 

Would appreciate if something like this is even possible, and if it already exists... 

@EvilDragon Croatian King KSP, Mario, lord of lords - if anyone would know I'd guess you would first.


----------



## FriFlo (Jul 12, 2019)

ProfoundSilence said:


> Does anyone know if there is an assignable repetition multiscript? I.e. C0 repeats last note used, maintaining the velocity used when you press the repetition key(so it doesn't machine gun the last velocity, but rather repeats the note using the new velocity)
> 
> I find it's extremely useful in some libraries, but isn't used in the libraries I find useful.
> 
> ...


I don't know, if one exists, but it is pretty easy to write! I am currently not in the game of scripting and right now not even close to a computer with Kontakt to try it out ... But out of my head ... you need to store the last midi note received as a number (variable). An "if" condition decides, wether an incoming note is C0. If that is the case, the note number must be replaced with the variable (with note on and note off!!!). That's it! You can do it and after that you will feel powerful!


----------



## ProfoundSilence (Jul 12, 2019)

FriFlo said:


> I don't know, if one exists, but it is pretty easy to write! I am currently not in the game of scripting and right now not even close to a computer with Kontakt to try it out ... But out of my head ... you need to store the last midi note received as a number (variable). An "if" condition decides, wether an incoming note is C0. If that is the case, the note number must be replaced with the variable (with note on and note off!!!). That's it! You can do it and after that you will feel powerful!



im so far from the realm of writing any sort of script that it doesn't help that it's possible and easy. it just hurts that it's possibly easy and yet I can't find a link where someone has done it.


----------



## Lindon (Jul 15, 2019)

..so you could pay someone to do it..


----------



## aaronventure (Jul 21, 2019)

Here you go. With some extra features as well.


```
on init
  set_script_title("Re-trigger")
  set_ui_height(2)

  declare $last_note
  declare $last_velocity
  declare $is_playing := 0


  declare ui_label $title(4, 1)
  set_text($title,"Re-trigger by Aaron Venture")
  move_control($title,2,1)
  set_control_par(get_ui_id($title),$CONTROL_PAR_TEXT_ALIGNMENT,1)

  declare ui_value_edit $Key(0, 127, $VALUE_EDIT_MODE_NOTE_NAMES)
  $Key := 108
  move_control($Key,2,3)
  make_persistent($Key)

  declare ui_value_edit $Delay(0, 200, 1)
  $Delay := 15
  move_control($Delay,3,4)
  set_control_par_str(get_ui_id($Delay),$CONTROL_PAR_HELP,"Set the delay before the note is re-triggered.")
  make_persistent($Delay)

  declare ui_value_edit $dlyrandom(0, 50, 1)
  $dlyrandom := 50
  move_control($dlyrandom,4,4)
  set_control_par_str(get_ui_id($dlyrandom),$CONTROL_PAR_TEXT,"Randomize")
  set_control_par_str(get_ui_id($dlyrandom),$CONTROL_PAR_HELP,"Set the percentage for which the delay value can fluctuate up or down.")
  make_persistent($dlyrandom)

  declare ui_switch $rtnoff
  set_control_par_str(get_ui_id($rtnoff),$CONTROL_PAR_TEXT,"Note-off")
  set_control_par_str(get_ui_id($rtnoff),$CONTROL_PAR_HELP,"If this is enabled, releasing the re-trigger key will send a note-off and stop the re-triggered note.")
  move_control($rtnoff,5,3)
  make_persistent($rtnoff)
end on



on midi_in
  if ($MIDI_COMMAND = $MIDI_COMMAND_NOTE_ON and ($MIDI_BYTE_1 # $Key))
    $last_note := $MIDI_BYTE_1
    $last_velocity := $MIDI_BYTE_2
    $is_playing := 1
  else
    if ($MIDI_COMMAND = $MIDI_COMMAND_NOTE_OFF and ($MIDI_BYTE_1 = $last_note))
      $is_playing := 0
    else
      if ($MIDI_COMMAND = $MIDI_COMMAND_NOTE_ON and ($MIDI_BYTE_1 = $key) and ($is_playing = 1))
        set_midi ($MIDI_CHANNEL, $MIDI_COMMAND_NOTE_OFF, $last_note, $last_velocity)
        wait (($Delay + random(-$Delay * $dlyrandom / 100, $Delay * $dlyrandom / 100)) * 1000)
        set_midi ($MIDI_CHANNEL, $MIDI_COMMAND_NOTE_ON, $last_note, $last_velocity)
      else
        if ($MIDI_COMMAND = $MIDI_COMMAND_NOTE_OFF and ($MIDI_BYTE_1 = $key) and ($rtnoff = 1))
          set_midi($MIDI_CHANNEL, $MIDI_COMMAND_NOTE_OFF, $last_note, $last_velocity)
        end if
      end if
    end if
  end if
end on
```


----------



## ProfoundSilence (Jul 21, 2019)

aaronventure said:


> Here you go. With some extra features as well.
> 
> 
> ```
> ...


just in time, I have most of next week off. 

now everyone will blame me for slowing down infinite strings

oh well, you know I'll be picking it up when released, kontakt wizard


----------



## aaronventure (Jul 22, 2019)

Updated with a small bugfix, it wasn't working if you played legato (letting go the old note after the overlap would write $is_playing := 0, so in the second if statement that's now only written if the note_off corresponds to $last_note, which is always the last note_on received).


----------



## robgb (Jul 25, 2019)

I'm no coder, but this thread got me to thinking how nice it would be if I could make a note repeat simply on release giving me a kind of controllable pseudo-tremolo. So I wrote this very simple instrument level script:


on init
set_script_title("Repeat On Release")
end on

on release
play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
end on

Of course, since I have no idea what I'm doing, I found that the script works great on short notes, but it causes long notes to sustain infinitely and can only be stopped with the panic button.

Besides finding a fix for that problem, I'm wondering if there is a way to trigger this release method ONLY when a keyswitch is being held down. I hold down, say, C1 and the note repeats on release. But when I release C1, the release repeats don't happen.

Any ideas?


----------



## aaronventure (Jul 25, 2019)

robgb said:


> I'm no coder, but this thread got me to thinking how nice it would be if I could make a note repeat simply on release giving me a kind of controllable pseudo-tremolo. So I wrote this very simple instrument level script:
> 
> 
> on init
> ...



It's playing indefinitely because it doesn't receive an instruction to stop at any point. Your best bet would be to write the note id ($EVENT_ID) into a variable, so that in on_note you can check whether the note you're pressing is the same one you just released. It would then send a note_off, (preferably) wait a couple ms (you could do some randomization here otherwise the two samples will sound exactly as if you just took the audio files and set them right near each other), and play the note normally. Don't forget ignore_event. 

As for your keyswitch, just do an if check with %KEY_DOWN[$EVENT_NOTE]. Write your note number in there, or declare a value_edit control so you can drag to choose the keyswitch and then check against that. 

So your workflow would be, I guess, play a note, hit your enable_release_retrigger keyswitch, do your release stuff, and then let go of your keyswitch while a note is still playing so on_release doesn't trigger your play_note and actually releases your note. 

Unless you truly insist on doing it on an instrument level (which can cause all kinds of issues if you're using it with patches that have scripts of their own), the simpler solution would be to take my multiscript above and modify it to add another re-trigger that does exactly the same thing as the one currently in, and then just play these two re-triggers. I'd also make sure that the re-triggers don't overlap ever. This is how you'd do it:


```
on init
  set_script_title("Double Re-trigger by Aaron Venture")
  set_ui_height(2)
 
  declare $last_note
  declare $last_velocity
  declare $is_playing := 0
  declare ui_label $title(4, 1)
 
  set_text($title,"Double Re-trigger by Aaron Venture")
  move_control($title,2,1)
  set_control_par(get_ui_id($title),$CONTROL_PAR_TEXT_ALIGNMENT,1)
 
  declare ui_value_edit $Key(0, 127, $VALUE_EDIT_MODE_NOTE_NAMES)
  $Key := 108
  move_control($Key,2,3)
  make_persistent($Key)
 
  declare ui_value_edit $Key2(0, 127, $VALUE_EDIT_MODE_NOTE_NAMES)
  $Key2 := 107
  move_control($Key2,2,4)
  make_persistent($Key2)
 
  declare ui_value_edit $Delay(0, 200, 1)
  $Delay := 15
  move_control($Delay,3,4)
  set_control_par_str(get_ui_id($Delay),$CONTROL_PAR_HELP,"Set the delay before the note is re-triggered.")
  make_persistent($Delay)
 
  declare ui_value_edit $dlyrandom(0, 50, 1)
  $dlyrandom := 50
  move_control($dlyrandom,4,4)
  set_control_par_str(get_ui_id($dlyrandom),$CONTROL_PAR_TEXT,"Randomize")
  set_control_par_str(get_ui_id($dlyrandom),$CONTROL_PAR_HELP,"Set the percentage for which the delay value can fluctuate up or down.")
  make_persistent($dlyrandom)
 
  declare ui_switch $rtnoff
  set_control_par_str(get_ui_id($rtnoff),$CONTROL_PAR_TEXT,"Note-off")
  set_control_par_str(get_ui_id($rtnoff),$CONTROL_PAR_HELP,"If this is enabled, releasing the re-trigger key will send a note-off and stop the re-triggered note.")
  move_control($rtnoff,5,3)
  make_persistent($rtnoff)
end on

on ui_control($Key)
  if ($Key=$Key2 and ($Key2 # 0))
    $Key := $Key2-1
  else
    if ($Key=$Key2 and ($Key2=0))
      $Key := $Key2+1
    end if
  end if
end on

on ui_control($Key2)
  if ($Key2=$Key and ($Key # 0))
    $Key2 := $Key-1
  else
    if ($Key=$Key2 and ($Key2=0))
      $Key2 := $Key+1
    end if
  end if
end on

on midi_in
  if ($MIDI_COMMAND=$MIDI_COMMAND_NOTE_ON and ($MIDI_BYTE_1 # $Key) and ($MIDI_BYTE_1 # $Key2))
    $last_note := $MIDI_BYTE_1
    $last_velocity := $MIDI_BYTE_2
    $is_playing := 1
  else
    if ($MIDI_COMMAND=$MIDI_COMMAND_NOTE_OFF and ($MIDI_BYTE_1=$last_note))
      $is_playing := 0
    else
      if ($MIDI_COMMAND=$MIDI_COMMAND_NOTE_ON and ($MIDI_BYTE_1=$key or ($MIDI_BYTE_1=$Key2)) and ($is_playing=1))
        set_midi($MIDI_CHANNEL,$MIDI_COMMAND_NOTE_OFF,$last_note,$last_velocity)
        wait(($Delay+random(-$Delay*$dlyrandom/100,$Delay*$dlyrandom/100))*1000)
        set_midi($MIDI_CHANNEL,$MIDI_COMMAND_NOTE_ON,$last_note,$last_velocity)
      else
        if ($MIDI_COMMAND=$MIDI_COMMAND_NOTE_OFF and ($MIDI_BYTE_1=$key or ($MIDI_BYTE_1=$Key2)) and ($rtnoff=1))
          set_midi($MIDI_CHANNEL,$MIDI_COMMAND_NOTE_OFF,$last_note,$last_velocity)
        end if
      end if
    end if
  end if
end on
```


----------



## robgb (Jul 25, 2019)

aaronventure said:


> It's playing indefinitely because it doesn't receive an instruction to stop at any point. Your best bet would be to write the note id ($EVENT_ID) into a variable, so that in on_note you can check whether the note you're pressing is the same one you just released.



Thanks for all of this! Beyond the pale and much appreciated.


----------



## EvilDragon (Jul 25, 2019)

This would be a better way to do it:


```
on release
    play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, %NOTE_DURATION[$EVENT_NOTE])
end on
```

Takes the duration of the note you've just held and repeats the release retriggered note with the same duration. Can also add a little bit of velocity randomization to make it more interesting.


```
on init
    set_script_title("Release Retrigger")

    declare polyphonic $note
    declare polyphonic $vel
    declare polyphonic $vel_offset
end on

on note
    $note := $EVENT_NOTE
    $vel := $EVENT_VELOCITY
end on

on release
    $vel_offset := $vel + random(-16, 4)

    if ($vel_offset < 1)
        $vel_offset := 1
    end if

    if ($vel_offset > 127)
        $vel_offset := 127
    end if

    play_note($note, $vel, 0, %NOTE_DURATION[$EVENT_NOTE])
end on
```


----------



## aaronventure (Jul 26, 2019)

EvilDragon said:


> This would be a better way to do it:



Nice, I'd just add "if (%KEY_DOWN[$keyswitch] = 1)" to the whole thing.


----------



## robgb (Jul 26, 2019)

You guys are fucking brilliant. Thanks. I added Aaron's suggestion to ED's code and it works perfectly.


```
on init
    set_script_title("Release Retrigger")

    declare polyphonic $note
    declare polyphonic $vel
    declare polyphonic $vel_offset
    declare const $keyswitch := 36
    set_key_color($keyswitch, $KEY_COLOR_RED)

end on

on note
    $note := $EVENT_NOTE
    $vel := $EVENT_VELOCITY
end on

on release

 if (%KEY_DOWN[$keyswitch] = 1)

    $vel_offset := $vel + random(-16, 4)

    if ($vel_offset < 1)
        $vel_offset := 1
    end if

    if ($vel_offset > 127)
        $vel_offset := 127
    end if

    play_note($note, $vel, 0, %NOTE_DURATION[$EVENT_NOTE])

 end if
end on
```


----------



## jeorgia (Jan 14, 2020)

{Help}

on init 
declare $play_key 
set_key_color(36,$KEY_COLOR_RED)
declare $min := 37
declare $max := 86
make_persistent ($min)
make_persistent ($max) 
end on
on note
if (in_range($EVENT_NOTE, $min, $max))
ignore_event($EVENT_ID)
end if
play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, -1) 
end on
on release 
if(%KEY_DOWN[36] = 1 )
if ($EVENT_NOTE > 0)
play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, -1)
end if 
end if 
end on


----------



## clisma (Jan 14, 2020)

jeorgia said:


> Help Me


I'm guessing you're having trouble making it work? If so, there is a keyswitch note marked in red; you have to hold it for the script repetition to be triggered.


----------



## jeorgia (Jan 15, 2020)

clisma said:


> I'm guessing you're having trouble making it work? If so, there is a keyswitch note marked in red; you have to hold it for the script repetition to be triggered.



{This script is good. For one repetition.
I want to repeat 3 or more voice. FOR METAL GUITAR}

on init
declare $i
set_key_color(36,$KEY_COLOR_RED)
end on
on note
if (in_range($EVENT_NOTE,37,86))
$i := $EVENT_NOTE
end if
if ($EVENT_NOTE=36)
ignore_event($EVENT_ID)
play_note($i,$EVENT_VELOCITY,0,-1)
end if
end on


----------



## EvilDragon (Jan 15, 2020)

Use the factory arpeggiator script in Chord mode then.


----------

