on init
set_script_title("Keyswitch Retuning")
declare $vksr_instrument_has_keyswitches_below :=0
declare $vksr_instrument_has_keyswitches_above :=128
declare $vksr_keyswitchv2_retuning_byte_position:=0
declare $vksr_keyswitch_instruction:=0
declare $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position := 0
declare $vksr_midi_tuning_enabled:=0
declare $vksr_show_diagnostics := 1
declare @vksr_current_instruction_string
@vksr_current_instruction_string := ""
declare %vksr_pitch_bend_for_output_note[128]
declare %vksr_output_note[128]
declare %vksr_output_note_at_note_on[128]
declare %vksr_is_active[128]
declare %vksr_note_id[128]
declare $vksr_EVENT_NOTE:=0
declare $vksr_EVENT_VELOCITY:=0
declare $vskr_CC_NUM:=0
declare $vksr_input_note := 0
declare $vksr_next_output_note := 0
declare $vksr_next_pitch_bend_coarse := 0
declare $vksr_next_pitch_bend_fine := 0
declare $vksr_next_pitch_bend_extra_fine := 0
declare $vksr_pitch_bend_combined := 0
declare $vksr_note_shift := 0
declare $vksr_note_on_id := 0
declare $vksr_input_pitch_bend_0_at := 8192
declare $vksr_input_pitch_steps_in_semitone := 16384
declare $vksr_cc119_was:=0
declare $vksr_result:= 0
declare $vksr_ready_to_retune := 0
declare $vksr_parsed :=0
declare $vksr_i := 0
declare $vskr_data_value:=0
declare ui_button $vksr_ResetTo12EQ
$vksr_ResetTo12EQ:=1
set_text ($vksr_ResetTo12EQ,"Reset to 12-equal")
make_persistent (%vksr_pitch_bend_for_output_note)
make_persistent (%vksr_output_note)
declare $vksr_normal_group
$vksr_i := 0
while ($vksr_i<128)
%vksr_is_active[$vksr_i] := 0
%vksr_note_id[$vksr_i] := 0
%vksr_output_note_at_note_on[$vksr_i] := $vksr_i
%vksr_pitch_bend_for_output_note[$vksr_i] := 0
%vksr_output_note[$vksr_i] := $vksr_i
$vksr_i := $vksr_i+1
end while
if($vksr_show_diagnostics#0)
message("on init")
end if
end on
function vksr_reset_to_12et
$vksr_i := 0
while ($vksr_i<128)
%vksr_is_active[$vksr_i] := 0
%vksr_note_id[$vksr_i] := 0
%vksr_pitch_bend_for_output_note[$vksr_i] := 0
%vksr_output_note[$vksr_i] := $vksr_i
%vksr_output_note_at_note_on[$vksr_i] := $vksr_i
$vksr_i := $vksr_i+1
end while
$vksr_next_pitch_bend_extra_fine:= 0
$vksr_keyswitch_instruction:=0
$vksr_keyswitchv2_retuning_byte_position:= 0
$vksr_ResetTo12EQ:=1
end function
on ui_control ($vksr_ResetTo12EQ)
$vksr_ResetTo12EQ := 1
call vksr_reset_to_12et
end on
function vksr_retune_currently_active_note
if (%vksr_is_active[$vksr_input_note] # 0)
if (%vksr_output_note_at_note_on[$vksr_input_note]=%vksr_output_note[$vksr_input_note])
change_tune(%vksr_note_id[$vksr_input_note],%vksr_pitch_bend_for_output_note[$vksr_input_note],0)
else
$vksr_note_shift := %vksr_output_note[$vksr_input_note]-%vksr_output_note_at_note_on[$vksr_input_note]
change_tune(%vksr_note_id[$vksr_input_note],%vksr_pitch_bend_for_output_note[$vksr_input_note]+($vksr_note_shift*100000),0)
end if
end if
end function
function vksr_diagnostics_message_for_current_instruction
if($vksr_show_diagnostics # 0)
message(@vksr_current_instruction_string)
end if
end function
function vksr_process_tuning_keyswitches
$vksr_ready_to_retune:=0
$vksr_parsed:=0
$vskr_data_value:=-1
if ($vksr_EVENT_NOTE=126)
$vskr_data_value:=$vksr_EVENT_VELOCITY
end if
if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=127)
$vskr_data_value:=0
end if
if ($vksr_midi_tuning_enabled = 119)
if ($vksr_EVENT_NOTE#127 and $vksr_EVENT_NOTE#126)
$vksr_keyswitch_instruction:=0
$vksr_keyswitchv2_retuning_byte_position:=0
end if
if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY#127)
$vksr_keyswitch_instruction:=$vksr_EVENT_VELOCITY
$vksr_keyswitchv2_retuning_byte_position:=0
if($vksr_keyswitch_instruction=11)
$vksr_keyswitchv2_extra_fine_pitch_bend_byte_position := 1
if($vksr_show_diagnostics#0)
@vksr_current_instruction_string := @vksr_current_instruction_string & " pitch bend extra fine"
end if
$vksr_parsed:=1
end if
if($vksr_keyswitch_instruction=126)
call vksr_reset_to_12et
$vksr_parsed:=1
end if
if($vksr_keyswitch_instruction = 4)
$vksr_keyswitchv2_retuning_byte_position:=4
if($vksr_show_diagnostics#0)
@vksr_current_instruction_string := "retune " & $vksr_keyswitchv2_retuning_byte_position & " bytes "
end if
$vksr_parsed:=1
end if
if($vksr_keyswitch_instruction = 3)
if($vksr_show_diagnostics#0)
@vksr_current_instruction_string := "retune " & $vksr_keyswitchv2_retuning_byte_position & " bytes "
end if
$vksr_keyswitchv2_retuning_byte_position:=3
$vksr_parsed:=1
end if
if($vksr_keyswitch_instruction = 2)
$vksr_keyswitchv2_retuning_byte_position:=2
@vksr_current_instruction_string := "retune " & $vksr_keyswitchv2_retuning_byte_position & " bytes "
$vksr_parsed:=1
end if
if($vksr_keyswitch_instruction = 1)
$vksr_keyswitchv2_retuning_byte_position:=1
if($vksr_show_diagnostics#0)
@vksr_current_instruction_string := "retune " & $vksr_keyswitchv2_retuning_byte_position & " bytes "
end if
$vksr_parsed:=1
end if
if($vksr_keyswitch_instruction = 127)
$vksr_parsed:=1
end if
if($vksr_parsed# 0)
$vksr_ResetTo12EQ:=0
end if
if($vksr_parsed = 0)
$vksr_keyswitchv2_retuning_byte_position:=0
$vksr_keyswitch_instruction:=0
if($vksr_show_diagnostics # 0 and $vksr_keyswitch_instruction #0)
@vksr_current_instruction_string := " " & @vksr_current_instruction_string & " End tuning (instr: " & $vksr_EVENT_VELOCITY & ")"
end if
end if
end if
if($vksr_keyswitch_instruction = 100)
if($vksr_show_diagnostics#0)
@vksr_current_instruction_string := @vksr_current_instruction_string & " - switch of midi retuning "
end if
$vksr_midi_tuning_enabled:=0
end if
if($vksr_keyswitchv2_extra_fine_pitch_bend_byte_position=1)
if ($vskr_data_value>=0)
$vksr_next_pitch_bend_extra_fine:=$vskr_data_value
$vksr_ready_to_retune:=1
end if
$vksr_keyswitchv2_extra_fine_pitch_bend_byte_position:=0
if($vksr_show_diagnostics # 0)
@vksr_current_instruction_string := @vksr_current_instruction_string & "extra fine pb: " & $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position
end if
end if
if($vksr_keyswitchv2_retuning_byte_position#0)
if ($vskr_data_value>=0)
if($vksr_keyswitchv2_retuning_byte_position= 4)
$vksr_input_note := $vskr_data_value
if($vksr_show_diagnostics # 0)
@vksr_current_instruction_string := @vksr_current_instruction_string & " input note: " & $vskr_data_value
end if
end if
if($vksr_keyswitchv2_retuning_byte_position= 3)
$vksr_next_output_note := $vskr_data_value
if($vksr_show_diagnostics # 0)
@vksr_current_instruction_string := @vksr_current_instruction_string & " output note: " & $vskr_data_value
end if
end if
if($vksr_keyswitchv2_retuning_byte_position= 2)
$vksr_next_pitch_bend_coarse := $vskr_data_value
$vksr_next_pitch_bend_extra_fine:=0
if($vksr_show_diagnostics # 0)
@vksr_current_instruction_string := @vksr_current_instruction_string & " pb coarse: " & $vskr_data_value
end if
end if
if($vksr_keyswitchv2_retuning_byte_position= 1)
$vksr_next_pitch_bend_fine := $vskr_data_value
$vksr_next_pitch_bend_extra_fine:=0
$vksr_next_pitch_bend_extra_fine:=0
if($vksr_show_diagnostics # 0)
@vksr_current_instruction_string := @vksr_current_instruction_string & " pb fine: " & $vskr_data_value
end if
end if
$vksr_keyswitchv2_retuning_byte_position:=$vksr_keyswitchv2_retuning_byte_position-1
if ($vksr_keyswitchv2_retuning_byte_position = 0)
$vksr_ready_to_retune:=1
$vksr_keyswitchv2_retuning_byte_position:=$vksr_keyswitch_instruction
end if
end if
end if
if($vksr_ready_to_retune#0)
if($vksr_show_diagnostics # 0)
@vksr_current_instruction_string:=@vksr_current_instruction_string & " RETUNED "
end if
%vksr_output_note[$vksr_input_note] := $vksr_next_output_note
$vksr_pitch_bend_combined := $vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine
%vksr_pitch_bend_for_output_note[$vksr_input_note] := 1000*100*($vksr_pitch_bend_combined-$vksr_input_pitch_bend_0_at)/($vksr_input_pitch_steps_in_semitone)
$vksr_pitch_bend_combined := $vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine
%vksr_pitch_bend_for_output_note[$vksr_input_note] := 1000*100*($vksr_pitch_bend_combined-$vksr_input_pitch_bend_0_at)/($vksr_input_pitch_steps_in_semitone)
%vksr_pitch_bend_for_output_note[$vksr_input_note] := %vksr_pitch_bend_for_output_note[$vksr_input_note] + (1000*100*$vksr_next_pitch_bend_extra_fine)/($vksr_input_pitch_steps_in_semitone*128)
$vksr_next_pitch_bend_extra_fine := 0
call vksr_retune_currently_active_note
end if
end if
if($vksr_show_diagnostics # 0)
call vksr_diagnostics_message_for_current_instruction
end if
end function
function vksr_retune_note_on
%vksr_is_active[$vksr_EVENT_NOTE] := 1
%vksr_output_note_at_note_on[$vksr_EVENT_NOTE] := %vksr_output_note[$vksr_EVENT_NOTE]
if($vksr_instrument_has_keyswitches_below<=%vksr_output_note[$vksr_EVENT_NOTE] and %vksr_output_note[$vksr_EVENT_NOTE] <=$vksr_instrument_has_keyswitches_above)
$vksr_note_on_id := play_note(%vksr_output_note[$vksr_EVENT_NOTE],$vksr_EVENT_VELOCITY,0,-1)
change_tune($vksr_note_on_id,%vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE],0)
%vksr_note_id[$vksr_EVENT_NOTE] := $vksr_note_on_id
if($vksr_show_diagnostics#0)
message("Note " & $vksr_EVENT_NOTE & " as " & %vksr_output_note[$vksr_EVENT_NOTE] ...
& ", bend: " & %vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE] & " millicents, " & " Velocity " & $vksr_EVENT_VELOCITY)
end if
change_tune($vksr_note_on_id,%vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE],0)
%vksr_note_id[$vksr_EVENT_NOTE] := $vksr_note_on_id
else
message("OUT OF RANGE note - " & $vksr_EVENT_NOTE & " -> " ...
& %vksr_output_note[$vksr_EVENT_NOTE] & " - IGNORED - INSTRUMENT RANGE " &$vksr_instrument_has_keyswitches_below ...
& " to " & $vksr_instrument_has_keyswitches_above...
)
end if
end function
function vksr_on_controller
if($vskr_CC_NUM = 119)
$vksr_midi_tuning_enabled:=%CC[119]
end if
end function
on note
$vksr_EVENT_NOTE:=$EVENT_NOTE
$vksr_EVENT_VELOCITY:=$EVENT_VELOCITY
if ($vksr_midi_tuning_enabled = 119 and $EVENT_NOTE >=126 )
ignore_event($EVENT_ID)
call vksr_process_tuning_keyswitches
else
if($vksr_instrument_has_keyswitches_below<=$vksr_EVENT_NOTE and $vksr_EVENT_NOTE <=$vksr_instrument_has_keyswitches_above)
ignore_event($EVENT_ID)
call vksr_retune_note_on
end if
end if
end on
on release
%vksr_is_active[$EVENT_NOTE] := 0
%vksr_note_id[$EVENT_NOTE] := 0
end on
on controller
$vskr_CC_NUM:=$CC_NUM
call vksr_on_controller
end on