{ Contact: support@robertinventor.com
Web page: http://robertinventor.com/ftswiki/High_numbered_keyswitches_retuning
Copyright (c) 2013 Robert Walker
This software is provided as-is, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not    claim that you wrote the original software. If you use this software    in a product, an acknowledgment in the product documentation would be
appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.    
This is the ZLib license suitable for free software and also for open source software and commmercial use
https://en.wikipedia.org/wiki/Zlib_License
}

{This code is based on a specification drawn up in discussion online mainly in the Xenharmonic Alliance facebook group.
 The main contributors were myself (Robert Walker), Kite Giedraitis, Rob Fielding
, and Graham Breed, and Ozan Yarman came up with the idea of using high numbered keyswitches.
 For details see the web page http://robertinventor.com/ftswiki/High_numbered_keyswitches_retuning
}

{YOU MAY NEED TO CONFIGURE THIS SCRIPT
* if instrument has keyswitches
* to select default user interface 
* to retune release samples
TO CONFIGURE, see start of the "on init" handler

IF YOU WANT TO MERTE IT WITH AN EXISTING SCRIPT

1. Add the code from the "on init", "on note", "on release" and "on controller"
handlers to your own handlers
2. Copy all the other routines into your own script
3. Configure it if necessary (see start of "on init")
3. If you need to reposition the controls, or hide some of them, see
vksr_move_controls, which is also duplicated at end of "on init"

All variables and routines start vksr_ or kspML_ so chance of variable collision is low
If you do get a variable collision, simply change the prefixes with search and replace of "vksr_"
in this code before you do the merge (kspML_ is for code inlined from KSPMathV600, see http://www.bigbobsmusicworld.com/kontakt-scripts/math-library
}

on init    
 
 set_script_title("Keyswitch Retuning")  
 make_perfview  { Uncomment this to make this into the performance view, the one you see on the front of the instrument  }
 
 { INSTRUMENT RANGE - usually best left at default of 
   $vksr_value_edit_instrument_range_min:=0
   $vksr_value_edit_instrument_range_max:=127
 - Here you can set $vksr_value_edit_instrument_range_min to lowest playable note
 - and $vksr_value_edit_instrument_range_max to highest playable note on the instrument
 - this stops the script from playing notes above or below that range as output notes 
 - they will just be skipped (silence)
 - you may need to set this if there are scripts after this one that respond to keyswitches.
 - if the keyswitches occur before this script in order in which the scripts are executed then there
 - should be no need to do anything as they are already processed by the time it gets to this script.
 }
 declare ui_value_edit $vksr_value_edit_instrument_range_min (0,127,1)
 $vksr_value_edit_instrument_range_min:=35   {' For keyswitches below instrument range. set this to lowest playable note, defaults to 0 }
 declare ui_value_edit $vksr_value_edit_instrument_range_max (0,127,1)
 $vksr_value_edit_instrument_range_max:=127 {' For keyswitches above insturment range, set this to highest playable note, defaults to 128 }
 declare $vksr_hide_user_interface:=0 {' When set to 1, hides everything except reset to 12et button}
 
 {SHIFT ENTIRE UI UP / DOWN}
 declare $vksr_ui_extra_vertical_shift:=0 {May be useful if integrating with another script}
 
 {SKIN MENU}

 {
 - One way to make a skin in Kontakt: put images with those file names, extension jpg, png etc
 - into a sub folder called "resources\Pictures" and then go to Instrument Options and choose "Create resource" and save
 - into the parent folder of the resources folder
 
 - Then this menu can be used to switch the skin on / off
 }

 declare ui_menu $vksr_menu_apply_skin
 add_menu_item($vksr_menu_apply_skin,"No skin buttons",0)
 add_menu_item($vksr_menu_apply_skin,"Skin buttons, menus etc if available (see script for details)",1)
 add_menu_item($vksr_menu_apply_skin,"Skin - with transparent label backgrounds",2)
 $vksr_menu_apply_skin:=2
 
 {This script uses as images: "button", "menu", and "transparent_background" - you can change those in
 @vksr_skin_button, @vksr_skin_menu, @vksr_skin_transparent_background}
 declare @vksr_skin_button
 @vksr_skin_button:="button"
 declare @vksr_skin_transparent_background
 @vksr_skin_transparent_background:="transparent"
 declare @vksr_skin_menu
 @vksr_skin_menu:="menu"
 
 {default to no skin background}
 {set_control_par_str($INST_WALLPAPER_ID, $CONTROL_PAR_PICTURE,"")}

 {MORE LESS MENU}
 {
 - This lets you select different versions of the user interface
 }
 declare ui_menu $vksr_menu_show_more
  declare $vksr_menu_item_pos:=0
 add_menu_item($vksr_menu_show_more,"Show less",0)
 add_menu_item($vksr_menu_show_more,"Less, & scale",1)
 add_menu_item($vksr_menu_show_more,"Show more",2)
 add_menu_item($vksr_menu_show_more,"More, & scale",3)
 add_menu_item($vksr_menu_show_more,"Tuning only",4)
 add_menu_item($vksr_menu_show_more,"Tuning & scale",5)
$vksr_menu_show_more:=0 {when set to 0, this hides the ui knobs - can be changed by the user}
 {These are constants - never changed but it is easier to read the script if you rename them as variables
 - so I do that throughout (in C would use #defines)
 - as a convention if the variable is all upper case, then it is a constant, in this script
 - except of course for the $vksr_sm_ prefix
 - so first declare the numbers for the menu items added already
 }
 declare $vksr_sm_LESS:=0
 declare $vksr_sm_LESS_WITH_SCALE:=1
 declare $vksr_sm_MORE:=2
 declare $vksr_sm_MORE_WITH_SCALE:=3
 declare $vksr_sm_TUNING_MENU_ONLY:=4
 declare $vksr_sm_TUNING_MENU_WITH_SCALE:=5
 
 $vksr_menu_item_pos:=6 {This is set to current menu pos and incremented as new items are added to the menu}
 
 add_menu_item($vksr_menu_show_more,"Tweaks",$vksr_menu_item_pos)
 declare $vksr_sm_TWEAKS
 $vksr_sm_TWEAKS:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1 
 
 add_menu_item($vksr_menu_show_more,"Documentation",$vksr_menu_item_pos)
 declare $vksr_sm_DOCUMENTATION
 $vksr_sm_DOCUMENTATION:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1 
 
 {INSTRUMENT RANGE - USER CONTROLS}
 {
  - Though you can hard code it into the script, you can also let user set it in the user interface
  - this is where the controls are added
 }
 set_text($vksr_value_edit_instrument_range_min,"From")
 set_text($vksr_value_edit_instrument_range_max,"To")
 declare ui_label $vksr_label_instrument_range2(3,2)
 
 declare ui_label $vksr_label_instrument_range(1,1)
 set_text($vksr_label_instrument_range,"Instrument range")
 set_text($vksr_label_instrument_range2,"Use instrument range to skip retuning for keyswitches")
 add_text_line($vksr_label_instrument_range2,"Notes that go outside this range won't be retuned")
 
 {RATIO RECOGNITION SENSITIVITY}
 {
 - sets how accurately a note needs to be tuned for the script to display it as a ratio
 - this makes no difference to the tuning of the note.
 }
 declare ui_menu $vksr_menu_detect_ratios
 add_menu_item($vksr_menu_detect_ratios,"Don't detect ratios",0)
 add_menu_item($vksr_menu_detect_ratios,"Auto detect ratios",1)
 $vksr_menu_detect_ratios:=0
 make_persistent($vksr_menu_detect_ratios)

 declare ui_label $vksr_label_ratio_recognition_sensitivity(2,1)
 declare ui_label $vksr_label_ratio_recognition_sensitivity2(1,1)
 declare ui_value_edit $vksr_value_edit_ratio_recognition_sensitivity(0,100000,1000)
 $vksr_value_edit_ratio_recognition_sensitivity:=2
 make_persistent($vksr_value_edit_ratio_recognition_sensitivity)
 
 set_text($vksr_label_ratio_recognition_sensitivity,"Ratio recognition sensitivity")
 set_text($vksr_label_ratio_recognition_sensitivity2,"(cents)")
 set_text($vksr_value_edit_ratio_recognition_sensitivity,"")
 
 
 {RELEASE RETUNING - NO LONGER NEEDED SO BEST SWITCHED OFF}
 {
  - I've left the code in place here just in case at some point someone needs it for
  - some especially tricky to retune instrument 
  - but so far none of the instruments need this any more and it has potentially
  - buggy behaviour - so switched off and commented out
 }
 declare $vksr_retune_release:=0
 
 {***UNCONMMENT THIS TO SWITCH ON RELEASE SAMPLE RETUNING***
 
 WARNING - THIS CAN CAUSE DOUBLE NOTES ON SOME INSTRUMENTS - LEAVE THIS SECTION IN COMMENTS IF UNSURE ***
 
 set_script_title("Keysw. + Release retuning")   
 $vksr_retune_release:=1
 SET_CONDITION(NO_SYS_SCRIPT_RLS_TRIG)
 
 {***END OF SECTION TO SWITCH ON RELEASE RETUNING***}
 
 
 {DECLARATIONS OF VARIABLES USED BY THIS SCRIPT}
 
 {
  - Note, all the variables used throughout this script have to be declared here in "on init"
  - the Kontakt scripting language doesn't have local variables
  - and all global variables have to be declared here.
  - and they have to be declared as constant values, can't for instance use
  - $vksr_INPUT_PITCH_STEPS_IN_SEMITONE:=$vksr_14_BYTES_MAX_DATA
  - in to declare $vksr_INPUT_PITCH_STEPS_IN_SEMITONE because the value on the right is a variable not a constant
 }
 
 {VARIABLES USED FOR PARSING THE INCOMING VELOCITY KEYSWITCHES}
 
 declare $vksr_keyswitchv2_byte_pos:=0 { gets set to e.g. 4 for 4 byte retuning
 -  gets decremented as data is received, retunes when reaches 0
 }
 
 declare $vksr_keyswitch_instruction:=0 { used for "running status" }
 declare $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position:=0
 
 declare %vksr_pitch_bend_for_output_note[128]
 declare %vksr_denum_for_output_note[128]
 declare %vksr_denom_for_output_note[128]
 declare %vksr_output_note[128]
 declare %vksr_input_note_volume[128]
 declare %vksr_input_note_pan[128]
 declare %vksr_pitch_bend_for_output_note_at_note_on[128]
 declare %vksr_output_note_at_note_on[128]
 declare %vksr_total_millicents_on_set_denum[128]
 declare %vksr_is_active[128]
 declare %vksr_note_id[128]


 {
  - used to pass these values to subroutines as the original values are only available
  - in the original handler such as "on note", not in subroutines called by it.
 }
 declare $vksr_EVENT_NOTE:=0 {records $EVENT_NOTE}
 declare $vksr_EVENT_VELOCITY:=0 {etc}
 declare $vksr_EVENT_ID:=0
 declare $vksr_CC_NUM:=0
 
 declare $vksr_abscents:=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
 
 {This script is hard coded to use Use full pitch bend range to adjust from -50 to 50 cents 
 - decided that it only adds to confusion and is of no benefit to user to permit different instruments
 - to use different pitch bend ranges for each note. Setting range to -50 to 50
 - gives maximum sensitivity for a double 7 bit variable (and there's an optional fine pitch byte as well
 - if you need more accuracy
 - this is just used for passing data via the keyswitches and does not limit the length of a pitch glide
 - which can be unlimited through the entire range of midi notes from 0 to 128
 - all the way through the glide the keyswitches retune the input note by specify the nearest 
 - midi output note to retune it to and then the 
 - pitch offset from that note in this format where 8192 = 0, 0 = -50 cents and 16384 = + 50 cents.
 }
 declare $vksr_14_BYTES_MAX_DATA:=128*128
 declare $vksr_INPUT_PITCH_BEND_0_AT:=0 
  {Have to declare it as a constant on init, then assign to $vksr_14_BYTES_MAX_DATA as separate instruction
  - or could just declare it as 128*128 of course }
 declare $vksr_INPUT_PITCH_STEPS_IN_SEMITONE:=0 
 $vksr_INPUT_PITCH_STEPS_IN_SEMITONE:=$vksr_14_BYTES_MAX_DATA {16384}
 $vksr_INPUT_PITCH_BEND_0_AT:=$vksr_14_BYTES_MAX_DATA/2 {8192}
 
 declare $CC119_was:=0 {CC119 is used to switch on the velocity keyswitches retuning}
 
 declare $vksr_scale_notes:=0
 declare $vksr_scale_notes_black:=0
 declare $vksr_scale_notes_for_i:=0
 
 declare $vksr_SEMITONE_IN_MILLICENTS:=100*1000
 declare $vksr_VOLUME_MAX_IN_MILLIDECIBELS:=100*1000
  declare $vksr_3o1_IN_MILLICENTS:=1901955
 declare $vksr_3o2_IN_MILLICENTS:=701955
 declare $vksr_OCTAVE_IN_MILLICENTS:=1200*1000
 
 {TRANSPOSING CONTROL}
 declare ui_value_edit $vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60 (0,127,1)
 {This is used to transpose the 1/1 in user defined tuning} 
 declare ui_value_edit $vksr_value_edit_1o1_transpose (-12700000,12700000,1000) 
 set_text($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,"1/1 at")
 
 declare %vksr_ok_denum[128]
 
 set_text($vksr_value_edit_1o1_transpose,"tr.")
 make_persistent($vksr_value_edit_1o1_transpose)
 make_persistent($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60)
 $vksr_value_edit_1o1_transpose:=0
 $vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60:=60 {The scales in the tuning drop down all use this as the 1/1}
 _read_persistent_var($vksr_value_edit_1o1_transpose) 
 _read_persistent_var($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60) 
 declare $vksr_1o1_pos
 $vksr_1o1_pos:=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60
 make_persistent($vksr_1o1_pos)
 _read_persistent_var($vksr_1o1_pos) 
 declare $vksr_1o1_millicents
 $vksr_1o1_millicents:=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60*$vksr_SEMITONE_IN_MILLICENTS
 make_persistent($vksr_1o1_millicents)
 _read_persistent_var($vksr_1o1_millicents) 
 declare $vksr_midi_tuning_enabled:=0
 
 make_persistent($vksr_midi_tuning_enabled)
 _read_persistent_var($vksr_midi_tuning_enabled) 
 declare @vksr_tuning_method
 @vksr_tuning_method:=""
 { Kontakt functions don't have arguments or return values
 - So just using a global variables like $vksr_result for now to return the values. 
 - and $vksr_arg to pass arguments to the functions
 - Okay so long as not multithreaded - should be okay for parsing midi.
 }
 declare $vksr_arg
 $vksr_arg:=0 
 declare $vksr_result
 $vksr_result:=0 
 declare $vksr_byte_arg:=0
 declare @vksr_return_char
 @vksr_return_char:=""
 
 { these next are local temporary variables except get syntax error messages in Kontakt 5 if I try to declare them
 within a function so declaring them here as globals, should be okay since midi is serial
 }
 declare $vksr_ready_to_retune:=0 
 declare $vksr_parsed:=0
 declare $vksr_adjust_volume_send:=0
 declare $vksr_adjust_pan_send:=0
 declare %vksr_do_adjust_pan_send_next_note[128]
 declare %vksr_do_adjust_volume_send_next_note[128]
 declare %vksr_adjust_pan_send_next_note[128]
 declare %vksr_adjust_volume_send_next_note[128]
 declare $vksr_adjust_volume:=127*128
 declare $vksr_adjust_pan:=0
 declare $vksr_j:=0
 declare $vksr_i:=0
 declare $vksr_jj:=0 {for use in functions that might be called in $vksr_i and $vksr_j loops}
 declare $vksr_ii:=0
 declare $vksr_offset_mod_nnotes:=0
 declare $vksr_is_black_note_for_scale_on_black_keys:=0
 declare $vksr_istart:=0
 declare $vksr_data_value:=0
 declare $vksr_next_denom:=0
 declare $vksr_next_denom_pos:=1
 declare $vksr_next_denum:=0
 declare $vksr_next_denum_pos:=1
 declare $vksr_scale_degree:=0
 declare $vksr_scale_degree_was:=0
 declare $vksr_1o1_pos_black:=-1
 declare $vksr_1o1_pos_white:=-1
 declare $vksr_1o1_pos_make_scale:=60  
 declare $vksr_temp:=0
 declare $vksr_temp2:=0
 declare $vksr_t:=0
 declare $vksr_u:=0
 declare $vksr_v:=0
 declare $vksr_gcd_common_factors_of_2:=1 
 declare @vksr_temp
 @vksr_temp:=""
 declare $vksr_offset_in_millicents:=0
 declare $vksr_denum:=0
 declare $vksr_denom:=0 
 declare $vksr_ok_check_knob_Note_for_find_cents_or_ratios_string:=1
 declare @vksr_cents_or_ratio_for_offset_in_milliseconds
 @vksr_cents_or_ratio_for_offset_in_milliseconds:=""
 declare $vksr_Note_increment:=0
 declare $vksr_pitch_increment:=0
 
 {Used to optimize calculation so you only find the millicents values once}
 declare %vksr_calculated_millicents_for_ratio[128*128]
 declare %vksr_millicents_for_ratio[128*128]
 


 { http://www.vi-control.net/forum/viewtopic.php?p=178475&highlight= 
 - used for the release retuning option if you have that enabled
 - This is used to make sure the release samplss are tuned in the same way as the
 - note itself
 }
 declare $vksr_normal_group { normal groups } 
 declare $vksr_release_group { release groups } 
 declare $vksr_OFF_ID { release sample ID }  
 $vksr_OFF_ID:=0
 
 {DEBUGGING STRINGS}
 {
 - these are displayed as messages on the status bar in 
 - vksr_diagnostics_message_for_current_instruction
 
 - if user switches on $vksr_menu_show_diagnostics # 0 
 - to decide which one to show
 - set $vksr_show_messages_events_string:=1
 - or  edit the routine vksr_diagnostics_message_for_current_instruction
 }
 declare @vksr_pan_and_expression_diagnostics_string
 @vksr_pan_and_expression_diagnostics_string:=""
 declare  @vksr_pitch_glide_diagnostics_string 
 @vksr_pitch_glide_diagnostics_string:=""
 declare  @vksr_current_instruction_string 
 @vksr_current_instruction_string:=""
 declare $vksr_show_messages_events_string:=0 {default to 1}
 declare @vksr_messages_events_string
 @vksr_messages_events_string:="" 
 
 {UI knobs and controls to adjust the pitch of individual notes
 - User selects the note to adjust with $vksr_knob_Note
 - Then select the note to retune it to with $vksr_knob_To
 - then adjusts the pitch bend with
 - $vksr_knob_Cents, $vksr_knob_CentiCents and $vksr_knob_LastDigit 
 }
 declare ui_knob $vksr_knob_Note(0, 127, 1)
 set_text ($vksr_knob_Note,"Note")
 
 declare ui_knob $vksr_knob_To(0, 127, 1) 
 set_text ($vksr_knob_To,"To")
 declare ui_knob $vksr_knob_Cents(-50000, 50000, 1000)
 set_text ($vksr_knob_Cents,"Cents")
 declare ui_knob $vksr_knob_CentiCents(0, 99, 1)
 declare ui_knob $vksr_knob_LastDigit(0, 9, 1)
 declare ui_button $vksr_button_ResetTo12EQ
 declare ui_value_edit $vksr_value_edit_Cents (-50000, 50000, 1000) 
 declare ui_value_edit $vksr_value_edit_denum (0,100000000,1) 
 declare ui_value_edit $vksr_value_edit_denom (0,100000000,1) 
 make_persistent($vksr_value_edit_denum)
 make_persistent($vksr_value_edit_denom)
 $vksr_value_edit_denum:=0
 $vksr_value_edit_denom:=0
 declare ui_value_edit $vksr_value_edit_Note (0,127,1) 
 set_text ($vksr_value_edit_Note,"Note")
 declare ui_value_edit $vksr_value_edit_To (0,127,1) 
 declare ui_value_edit $vksr_value_edit_digits_precision (0,3,1)
 declare ui_label $vksr_label_tuning_method (3,1) 
 set_text ($vksr_label_tuning_method,"Send Ctrlr 119, val. 119 to switch on velocity tuning keyswitches")
 
 {DOCUMENTATION UI PAGE TEXT}
 declare ui_label $vksr_label_documentation(5,6) 
 add_text_line($vksr_label_documentation,"To update tuning table via midi : FIRST SEND cc 119, val. 119 to switch on velocity tuning keyswitches.")
 add_text_line($vksr_label_documentation,"TO RETUNE: send note 127 vel. 4 (= retune) then data: input note, output note, pitch bend coarse, fine ")
 add_text_line($vksr_label_documentation,"DATA AS note 126 with velocity = value, or 127, vel. 127 for zero. Pitch bend range -50 to 50 cents. ")
 add_text_line($vksr_label_documentation,"WHEN FINISHED use note 127 vel. 100 to switch off retuning. You can now play all 128 notes in the table.")
 add_text_line($vksr_label_documentation,"")
 add_text_line($vksr_label_documentation,"For more documentation and to update script: http://robertinvenetor.com/velocity.htm")
 add_text_line($vksr_label_documentation,"Spec for velocity tuning by members of Xenharmonic Alliance")
 add_text_line($vksr_label_documentation,"Script by Robert Walker, free for any use including commercial (zlib license)")
 
 {USER MENU TO SHOW / HIDE DIAGNOSTICS}
 declare ui_menu $vksr_menu_show_diagnostics
 add_menu_item($vksr_menu_show_diagnostics,"Hide diagnostic messages for debugging",0)
 add_menu_item($vksr_menu_show_diagnostics,"Show diagnostic messages for debugging",1)
 make_persistent($vksr_menu_show_diagnostics) 
 { $vksr_menu_show_diagnostics:=0}
 _read_persistent_var($vksr_menu_show_diagnostics)
 
{configured in vksr_diagnostics_message_for_current_instruction}
declare  $vksr_menu_show_diagnostics_message_for_retuned_note:=0 
{if set to 1, shows message every time you do a  pitch glide, for tuning position during the glide - just a temporary message, gets overridden by other messages}

 {message("on init" & $vksr_menu_show_diagnostics)}
 if($vksr_menu_show_diagnostics # 0)
  message("on init")
 end if
 
 {MENU TO SWITCH VELOCITY KEYSWITCH RETUNING ON OR OFF}
 {It's not too likely that user will edit the tuning of notes via midi in manually using velocity keyswitches
 - so this is mainly a visual indication of whether another program is currently tuning it
 - and also - shows if the velocity keyswitch retuning has been switched off at the end of the tuning table
 - if not and if you want to play notes 127 and 126 - can use this to do so manually
 }
 
 declare ui_menu $vksr_menu_midi_tuning_method
 add_menu_item($vksr_menu_midi_tuning_method,"Tuning locked: notes 126 and 127 played from Midi In will have no effect on the tuning",0)
 add_menu_item($vksr_menu_midi_tuning_method,"Activated: Velocity keyswitch retuning via Midi In - with notes 126 for data and 127 for instructions - you can also select this by sending Controller 119, 119",1)
 
 {SCROLL SCALE MENU}
 {
  - This is for the visual display of the scale in the Tuning & Scale page
  - since there usually isn't enough room to fit the entire scale into the screen
  - then what do you do if user edits a note and retunes it to the displayed scale?
  - Default is to scroll the scale whenever you edit or play a note
  - (the played or edited note is highlighted with asterisk)
 }
 declare ui_menu $vksr_menu_scroll_scale
 add_menu_item($vksr_menu_scroll_scale,"Scroll scale on ed.",0)
 add_menu_item($vksr_menu_scroll_scale,"Scroll scale on play",1)
 add_menu_item($vksr_menu_scroll_scale,"Scroll for both",2)
 add_menu_item($vksr_menu_scroll_scale,"Scroll to selected note",3)
 
 declare $vksr_SCROLL_SCALE_ON_EDIT:=0
 declare $vksr_SCROLL_SCALE_ON_PLAY:=1
 declare $vksr_SCROLL_SCALE_ON_BOTH:=2
 declare $vksr_SCROLL_SCALE_TO_SELECTED_NOTE:=3
 declare $vksr_Note_user_edited:=-1 {not yet edited}
 declare $vksr_Note_played:=-1 {not yet pleyd}
 $vksr_menu_scroll_scale:=$vksr_SCROLL_SCALE_ON_BOTH
 
 {OFFSET} 
 {This edit lets you manually scroll the scale relative to the automatic selection
 - e.g. to put the played or edited note in the middle instead of to the left
 - of the list of notes displayed
 }
 declare ui_value_edit $vksr_value_edit_scroll_scale_midi_start (0,127,1) 
 set_text ($vksr_value_edit_scroll_scale_midi_start,"")
 $vksr_value_edit_scroll_scale_midi_start:=60
 declare ui_value_edit $vksr_value_edit_scroll_scale_show_before (-127,127,1) 
 set_text ($vksr_value_edit_scroll_scale_show_before,"extra scroll")
 $vksr_value_edit_scroll_scale_show_before:=3
 
 {TUNING DESCRIPTION}
 { - This can't be set directly as Kontakt only lets you type in numerical values - but can be set via Midi In using
 - the velocity tuning keyswitches 
 - also if you select a scale from the droplist of scales, its description is placed here
 }
 declare ui_label $vksr_label_tuning_description (3,1) 
 declare @vksr_tuning_description_string
 declare $vksr_added_edited_by_user_to_tuning_string:=0
 make_persistent($vksr_added_edited_by_user_to_tuning_string)
 
 @vksr_tuning_description_string:="Tuning: 12 equal"
 _read_persistent_var(@vksr_tuning_description_string) 
 set_text ($vksr_label_tuning_description,@vksr_tuning_description_string)
 declare ui_label $vksr_label_tuning_definition (6,1) 
 
 declare $vksr_tuning_definition_string_needs_update:=0
 declare $vksr_tuning_string_USER_EDITED:=1
 declare $vksr_tuning_string_REMAKE_NOT_USER_EDITED:=2
 declare @vksr_tuning_definition_string
 @vksr_tuning_definition_string:="1/1, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1100.0, 2/1"
 declare @vksr_highlight_black_keys_in_scale_before
 @vksr_highlight_black_keys_in_scale_before:="["
 declare @vksr_highlight_black_keys_in_scale_after
 @vksr_highlight_black_keys_in_scale_after:="]"
 _read_persistent_var(@vksr_tuning_definition_string) 
 set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
 
 $vksr_value_edit_Cents:=0 
 $vksr_value_edit_digits_precision:=2
 
 {*MAKE SCALES DROPLIST MENU AND ADD SCALES TO IT*}
 declare ui_value_edit $vksr_value_edit_n_equal (1,1000000,1) 
 declare ui_value_edit $vksr_value_edit_first_harmonic (1,1000000,1) 
 $vksr_value_edit_first_harmonic:=8
 set_text($vksr_value_edit_first_harmonic,"starts")
 declare ui_value_edit $vksr_value_edit_first_harmonic_black (1,1000000,1) 
 $vksr_value_edit_first_harmonic_black:=5
 set_text($vksr_value_edit_first_harmonic_black,"starts")
 
 declare ui_label $vksr_label_note_played (1,1)
 set_text ($vksr_label_note_played,"(no notes played)") 
 declare ui_menu $vksr_menu_scale_tuning {the scale presets}
 $vksr_menu_item_pos:=0
 declare $vksr_scale_tuning_12_EQUAL:=0
 add_menu_item($vksr_menu_scale_tuning,"Twelve Equal",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 add_menu_item($vksr_menu_scale_tuning,"n-Equal",$vksr_menu_item_pos)
 declare $vksr_scale_tuning_N_EQUAL
 $vksr_scale_tuning_N_EQUAL:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 add_menu_item($vksr_menu_scale_tuning,"Equal div. of 3/1",$vksr_menu_item_pos)
 declare $vksr_scale_tuning_N_EQUAL_of_3o1
 $vksr_scale_tuning_N_EQUAL_of_3o1:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 add_menu_item($vksr_menu_scale_tuning,"Equal div. of 3/2",$vksr_menu_item_pos)
 declare $vksr_scale_tuning_N_EQUAL_of_3o2
 $vksr_scale_tuning_N_EQUAL_of_3o2:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 add_menu_item($vksr_menu_scale_tuning,"Harmonics & subharmonics",$vksr_menu_item_pos)
 declare $vksr_scale_tuning_HARMONIC_AND_SUBHARMONICS
 $vksr_scale_tuning_HARMONIC_AND_SUBHARMONICS:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 declare $vksr_scale_tuning_Just_intonation_with_I__II__V_pure
 $vksr_scale_tuning_Just_intonation_with_I__II__V_pure:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"J.I. with I, II, V pure",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Just_intonation_with_I__II__V_pure[12]:=(...
                                                               0 {1/1}, 92179 {135/128}, 203910 {9/8}, 315641 {6/5}, 386314 {5/4}, 519551 {27/20}, ...
                                                               590224 {45/32}, 701955 {3/2}, 813686 {8/5}, 905865 {27/16}, 1017596 {9/5}, 1088269 {15/8}  )
 declare %vksr_scale_Just_intonation_with_I__II__V_pure_denums[12]:=(...
                                                                      1,  135,  9,  6,  5,  27,  45,  3,  8,  27,  9,  15  )
 declare %vksr_scale_Just_intonation_with_I__II__V_pure_denoms[12]:=(...
                                                                      1,  128,  8,  5,  4,  20,  32,  2,  5,  16,  5,  8  ) 
 
 declare $vksr_scale_tuning_Just_intonation_with_I__IV__V_pure
 $vksr_scale_tuning_Just_intonation_with_I__IV__V_pure:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"J.I. with I, IV, V pure",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Just_intonation_with_I__IV__V_pure[12]:=(...
                                                               0 {1/1}, 111731 {16/15}, 203910 {9/8}, 315641 {6/5}, 386314 {5/4}, 498045 {4/3}, ...
                                                               590224 {45/32}, 701955 {3/2}, 813686 {8/5}, 884359 {5/3}, 1017596 {9/5}, 1088269 {15/8}  )
 declare %vksr_scale_Just_intonation_with_I__IV__V_pure_denums[12]:=(...
                                                                      1,  16,  9,  6,  5,  4,  45,  3,  8,  5,  9,  15  )
 declare %vksr_scale_Just_intonation_with_I__IV__V_pure_denoms[12]:=(...
                                                                      1,  15,  8,  5,  4,  3,  32,  2,  5,  3,  5,  8  ) 
 
 declare $vksr_scale_tuning_Modern_Indian_Gamut___ragas
 $vksr_scale_tuning_Modern_Indian_Gamut___ragas:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Modern Indian Gamut - ragas",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Modern_Indian_Gamut___ragas[12]:=(...
                                                        0 {1/1}, 111731 {16/15}, 203910 {9/8}, 315641 {6/5}, 386314 {5/4}, 498045 {4/3}, ...
                                                        590224 {45/32}, 701955 {3/2}, 813686 {8/5}, 905865 {27/16}, 1017596 {9/5}, 1088269 {15/8}  )
 declare %vksr_scale_Modern_Indian_Gamut___ragas_denums[12]:=(...
                                                               1,  16,  9,  6,  5,  4,  45,  3,  8,  27,  9,  15  )
 declare %vksr_scale_Modern_Indian_Gamut___ragas_denoms[12]:=(...
                                                               1,  15,  8,  5,  4,  3,  32,  2,  5,  16,  5,  8  )
 
 declare $vksr_scale_tuning_Pythagorean__Middle_ages
 $vksr_scale_tuning_Pythagorean__Middle_ages:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Pythagorean (Middle ages)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 declare %vksr_scale_Pythagorean__Middle_ages[12]:=(...
                                                     0 {1/1}, 90225 {256/243}, 203910 {9/8}, 294135 {32/27}, 407820 {81/64}, 498045 {4/3}, ...
                                                     611730 {729/512}, 701955 {3/2}, 792180 {128/81}, 905865 {27/16}, 996090 {16/9}, 1109775 {243/128}  )
 declare %vksr_scale_Pythagorean__Middle_ages_denums[12]:=(...
                                                            1,  256,  9,  32,  81,  4,  729,  3,  128,  27,  16,  243  )
 declare %vksr_scale_Pythagorean__Middle_ages_denoms[12]:=(...
                                                            1,  243,  8,  27,  64,  3,  512,  2,  81,  16,  9,  128  )
 
 declare $vksr_scale_tuning_Quarter_comma_mean_tone__renaissance_o_baroque
 $vksr_scale_tuning_Quarter_comma_mean_tone__renaissance_o_baroque:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Quarter comma mean-tone (renaissance / baroque)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Quarter_comma_mean_tone__renaissance_o_baroque[12]:=(...
                                                                           0 {1/1}, 76049 {76.049}, 193157 {193.157}, 310265 {310.265}, 386314 {5/4}, 503422 {503.422}, ...
                                                                           579471 {579.471}, 696578 {696.578}, 772627 {25/16}, 889735 {889.735}, 1006843 {1006.843}, 1082892 {1082.892}  )
 
 declare %vksr_scale_Quarter_comma_mean_tone__renaissance_o_baroque_denums[12]:=(...
                                                                                  1,  0,  0,  0,  5,  0,  0,  0,  25,  0,  0,  0  )
 declare %vksr_scale_Quarter_comma_mean_tone__renaissance_o_baroque_denoms[12]:=(...
                                                                                  1,  0,  0,  0,  4,  0,  0,  0,  16,  0,  0,  0  )
 
 
 declare $vksr_scale_tuning_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical
 $vksr_scale_tuning_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Werckmeister III (1691, Bach's time, late baroque / early classical)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical[12]:=(...
                                                                                                0 {1/1}, 90225 {256/243}, 192180 {192.180}, 294135 {32/27}, 390225 {390.225}, 498045 {4/3}, ...
                                                                                                588270 {1024/729}, 696090 {696.090}, 792180 {128/81}, 888270 {888.270}, 996090 {16/9}, 1092180 {1092.180}  )
 
 declare %vksr_scale_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical_denums[12]:=(...
                                                                                                       1,  256,  0,  32,  0,  4,  1024,  0,  128,  0,  16,  0  )
 declare %vksr_scale_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical_denoms[12]:=(...
                                                                                                       1,  243,  0,  27,  0,  3,  729,  0,  81,  0,  9,  0  )
 
 
 declare $vksr_scale_tuning_Lambert_Chaumont_organ_temperament__1695
 $vksr_scale_tuning_Lambert_Chaumont_organ_temperament__1695:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Lambert Chaumont organ temperament (1695)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Lambert_Chaumont_organ_temperament__1695[12]:=(...
                                                                     0 {1/1}, 76049 {76.049}, 193157 {193.157}, 290909 {290.909}, 386314 {5/4}, 503422 {503.422}, ...
                                                                     579471 {579.471}, 696578 {696.578}, 772627 {25/16}, 889735 {889.735}, 997165 {997.165}, 1082892 {1082.892}  )
 
 declare %vksr_scale_Lambert_Chaumont_organ_temperament__1695_denums[12]:=(...
                                                                            1,  0,  0,  0,  5,  0,  0,  0,  25,  0,  0,  0  )
 declare %vksr_scale_Lambert_Chaumont_organ_temperament__1695_denoms[12]:=(...
                                                                            1,  0,  0,  0,  4,  0,  0,  0,  16,  0,  0,  0  )
 
 declare $vksr_scale_tuning_Rameau_Temperament__in_Nouveau_Systeme__1726
 $vksr_scale_tuning_Rameau_Temperament__in_Nouveau_Systeme__1726:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Rameau Temperament  in Nouveau Systeme (1726)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Rameau_Temperament__in_Nouveau_Systeme__1726[12]:=(...
                                                                         0 {1/1}, 92472 {92.472}, 193157 {193.157}, 302053 {302.053}, 386314 {5/4}, 503422 {503.422}, ...
                                                                         587682 {587.682}, 696578 {696.578}, 797263 {797.263}, 889735 {889.735}, 1006843 {1006.843}, 1082892 {1082.892}  )
 
 declare %vksr_scale_Rameau_Temperament__in_Nouveau_Systeme__1726_denums[12]:=(...
                                                                                1,  0,  0,  0,  5,  0,  0,  0,  0,  0,  0,  0  )
 declare %vksr_scale_Rameau_Temperament__in_Nouveau_Systeme__1726_denoms[12]:=(...
                                                                                1,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0  )
 
 declare $vksr_scale_tuning_Sixth_comma_mean_tone__Mozart_s_time__classical
 $vksr_scale_tuning_Sixth_comma_mean_tone__Mozart_s_time__classical:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Sixth comma mean-tone (Mozart's time, classical)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Sixth_comma_mean_tone__Mozart_s_time__classical[12]:=(...
                                                                            0 {1/1}, 88594 {88.594}, 196741 {196.741}, 304888 {304.888}, 393482 {393.482}, 501629 {501.629}, ...
                                                                            590224 {45/32}, 698371 {698.371}, 786965 {786.965}, 895112 {895.112}, 1003260 {1003.260}, 1091850 {1091.850}  )
 
 declare %vksr_scale_Sixth_comma_mean_tone__Mozart_s_time__classical_denums[12]:=(...
                                                                                   1,  0,  0,  0,  0,  0,  45,  0,  0,  0,  0,  0  )
 declare %vksr_scale_Sixth_comma_mean_tone__Mozart_s_time__classical_denoms[12]:=(...
                                                                                   1,  0,  0,  0,  0,  0,  32,  0,  0,  0,  0,  0  )
 
 declare $vksr_scale_tuning_Vallotti_and_Young__1800__late_classical
 $vksr_scale_tuning_Vallotti_and_Young__1800__late_classical:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Vallotti and Young (1800, late classical)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Vallotti_and_Young__1800__late_classical[12]:=(...
                                                                     0 {1/1}, 94135 {94.135}, 196090 {196.090}, 298045 {298.045}, 392180 {392.180}, 501955 {501.955}, ...
                                                                     592180 {592.180}, 698045 {698.045}, 796090 {796.090}, 894135 {894.135}, 1000000 {1000.000}, 1090220 {1090.220}  )
 
 declare %vksr_scale_Vallotti_and_Young__1800__late_classical_denums[12]:=(...
                                                                            1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  )
 declare %vksr_scale_Vallotti_and_Young__1800__late_classical_denoms[12]:=(...
                                                                            1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  ) 
 declare $vksr_scale_tuning_Kirnberger__1779
 $vksr_scale_tuning_Kirnberger__1779:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Kirnberger (1779)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Kirnberger__1779[12]:=(...
                                             0 {1/1}, 90225 {256/243}, 193849 {193.849}, 294135 {32/27}, 386314 {386.314}, 498045 {4/3}, ...
                                             590224 {45/32}, 696663 {696.663}, 792180 {128/81}, 889650 {889.650}, 996090 {16/9}, 1088269 {15/8}  )
 
 declare %vksr_scale_Kirnberger__1779_denums[12]:=(...
                                                    1,  256,  0,  32,  0,  4,  45,  0,  128,  0,  16,  15  )
 declare %vksr_scale_Kirnberger__1779_denoms[12]:=(...
                                                    1,  243,  0,  27,  0,  3,  32,  0,  81,  0,  9,  8  )
 
 declare $vksr_scale_tuning_Kirnberger_3__1779
 $vksr_scale_tuning_Kirnberger_3__1779:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Kirnberger 3 (1779)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Kirnberger_3__1779[12]:=(...
                                               0 {1/1}, 92179 {135/128}, 193157 {193.157}, 294135 {32/27}, 386314 {5/4}, 498045 {4/3}, ...
                                               590224 {45/32}, 696578 {696.578}, 794134 {405/256}, 889735 {889.735}, 996090 {16/9}, 1088269 {15/8}  )
 
 declare %vksr_scale_Kirnberger_3__1779_denums[12]:=(...
                                                      1,  135,  0,  32,  5,  4,  45,  0,  405,  0,  16,  15  )
 declare %vksr_scale_Kirnberger_3__1779_denoms[12]:=(...
                                                      1,  128,  0,  27,  4,  3,  32,  0,  256,  0,  9,  8  )  
 
 declare $vksr_scale_tuning_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885
 $vksr_scale_tuning_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Broadwood's Best (Ellis tuner number 4), Victorian (1885)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885[12]:=(...
                                                                                     0 {1/1}, 95965 {95.965}, 197990 {197.990}, 297955 {297.955}, 392980 {392.980}, 498945 {498.945}, ...
                                                                                     594970 {594.970}, 699995 {699.995}, 796960 {796.960}, 894985 {894.985}, 998950 {998.950}, 1093975 {1093.975}  )
 
 declare %vksr_scale_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885_denums[12]:=(...
                                                                                            1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  )
 declare %vksr_scale_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885_denoms[12]:=(...
                                                                                            1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  )
 
 declare $vksr_scale_tuning_Moore__representative_Victorian_well_temperament___1885
 $vksr_scale_tuning_Moore__representative_Victorian_well_temperament___1885:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Moore (representative Victorian well-temperament) (1885)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Moore__representative_Victorian_well_temperament___1885[12]:=(...
                                                                                    0 {1/1}, 98604 {98.604}, 203910 {9/8}, 302514 {302.514}, 399441 {399.441}, 498045 {4/3}, ...
                                                                                    603351 {603.351}, 701955 {3/2}, 800559 {800.559}, 897486 {897.486}, 1004470 {1004.470}, 1101400 {1101.400}  )
 
 declare %vksr_scale_Moore__representative_Victorian_well_temperament___1885_denums[12]:=(...
                                                                                           1,  0,  9,  0,  0,  4,  0,  3,  0,  0,  0,  0  )
 declare %vksr_scale_Moore__representative_Victorian_well_temperament___1885_denoms[12]:=(...
                                                                                           1,  0,  8,  0,  0,  3,  0,  2,  0,  0,  0,  0  )
 
 declare $vksr_scale_tuning_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat
 $vksr_scale_tuning_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Well-temperament Op de Coul, 1998. Fifths 5/14, 4/14 and 5/14 Pyth comma flat",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 declare %vksr_scale_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat[12]:=(...
                                                                                                          0 {1/1}, 98604 {98.604}, 203910 {9/8}, 302514 {302.514}, 399441 {399.441}, 498045 {4/3}, ...
                                                                                                          603351 {603.351}, 701955 {3/2}, 800559 {800.559}, 897486 {897.486}, 1004470 {1004.470}, 1101400 {1101.400}  )
 
 declare %vksr_scale_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat_denums[12]:=(...
                                                                                                                 1,  0,  9,  0,  0,  4,  0,  3,  0,  0,  0,  0  )
 declare %vksr_scale_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat_denoms[12]:=(...
                                                                                                                 1,  0,  8,  0,  0,  3,  0,  2,  0,  0,  0,  0  )  
 
 declare $vksr_scale_tuning_David_Canright_s_7_limit_twelve_tone
 $vksr_scale_tuning_David_Canright_s_7_limit_twelve_tone:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"David Canright's 7 limit twelve tone",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_David_Canright_s_7_limit_twelve_tone[12]:=(...
                                                                 0 {1/1}, 62961 {28/27}, 203910 {9/8}, 266871 {7/6}, 386314 {5/4}, 498045 {4/3}, ...
                                                                 590224 {45/32}, 701955 {3/2}, 764916 {14/9}, 884359 {5/3}, 968826 {7/4}, 1088269 {15/8}  )
 
 declare %vksr_scale_David_Canright_s_7_limit_twelve_tone_denums[12]:=(...
                                                                        1,  28,  9,  7,  5,  4,  45,  3,  14,  5,  7,  15  )
 declare %vksr_scale_David_Canright_s_7_limit_twelve_tone_denoms[12]:=(...
                                                                        1,  27,  8,  6,  4,  3,  32,  2,  9,  3,  4,  8  )
 
 declare $vksr_scale_tuning_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black
 $vksr_scale_tuning_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"American Gamelan Si Betty as Pelog on white, Slendro on Black",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black[12]:=(...
                                                                                          0 {1/1}, 67367 {67.367}, 184672 {184.672}, 298754 {298.754}, 267713 {267.713}, 603902 {603.902}, ...
                                                                                          566475 {566.475}, 703919 {703.919}, 795496 {795.496}, 796613 {796.613}, 971688 {971.688}, 1063085 {1063.085}  )
 
 declare %vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black_denums[12]:=(...
                                                                                                 1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  )
 declare %vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black_denoms[12]:=(...
                                                                                                 1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0  )
 
 
 declare $vksr_scale_tuning_Modern_Pelog_scale__Dan_Schmidt
 $vksr_scale_tuning_Modern_Pelog_scale__Dan_Schmidt:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Modern Pelog scale (Dan Schmidt) (7 note scale on white keys)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Modern_Pelog_scale__Dan_Schmidt[7]:=(...
                                                           0 {1/1}, 165004 {11/10}, 315641 {6/5}, 582512 {7/5}, 701955 {3/2}, 813686 {8/5}, ...
                                                           1017596 {9/5}  )
 
 declare %vksr_scale_Modern_Pelog_scale__Dan_Schmidt_denums[7]:=(...
                                                                  1,  11,  6,  7,  3,  8,  9  )
 declare %vksr_scale_Modern_Pelog_scale__Dan_Schmidt_denoms[7]:=(...
                                                                  1,  10,  5,  5,  2,  5,  5  )
 
 declare $vksr_scale_tuning_Xylophone_from_West_Africa
 $vksr_scale_tuning_Xylophone_from_West_Africa:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Xylophone from West Africa (7 note scale on white keys)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Xylophone_from_West_Africa[7]:=(...
                                                      0 {1/1}, 152000 {152.000}, 287000 {287.000}, 533000 {533.000}, 724000 {724.000}, 890000 {890.000}, ...
                                                      1039000 {1039.000}  )
 
 declare %vksr_scale_Xylophone_from_West_Africa_denums[7]:=(...
                                                             1,  0,  0,  0,  0,  0,  0  )
 declare %vksr_scale_Xylophone_from_West_Africa_denoms[7]:=(...
                                                             1,  0,  0,  0,  0,  0,  0  )
 
 declare $vksr_scale_tuning_Seven_tone_tuning_from_Thailand
 $vksr_scale_tuning_Seven_tone_tuning_from_Thailand:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Seven tone tuning from Thailand (on white keys)",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Seven_tone_tuning_from_Thailand[14]:=(...
                                                            0 {1/1}, 129096 {129.096}, 262077 {262.077}, 415113 {415.113}, 703196 {703.196}, 804353 {804.353}, ...
                                                            984182 {984.182}, 1200000 {2/1}, 1329100 {1329.100}, 1462080 {1462.080}, 1616580 {1616.580}, 1904440 {1904.440}, ...
                                                            2002010 {2002.010}, 2186290 {2186.290}  )
 
 declare %vksr_scale_Seven_tone_tuning_from_Thailand_denums[14]:=(...
                                                                   1,  0,  0,  0,  0,  0,  0,  2,  0,  0,  0,  0,  0,  0  )
 declare %vksr_scale_Seven_tone_tuning_from_Thailand_denoms[14]:=(...
                                                                   1,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0  ) 
 
  declare $vksr_scale_tuning_Golden_horogram_9__7
  $vksr_scale_tuning_Golden_horogram_9__7:=$vksr_menu_item_pos
  add_menu_item($vksr_menu_scale_tuning,"Golden horogram 9, 7 (on white keys)",$vksr_menu_item_pos)
  $vksr_menu_item_pos:=$vksr_menu_item_pos+1
  declare %vksr_scale_Golden_horogram_9__7[7]:=(...
  0 {1/1}, 81320 {81.320}, 362550 {362.550}, 543970 {543.970}, 725290 {725.290}, 906610 {906.610}, ...
  1087940 {1087.940}  )

  declare %vksr_scale_Golden_horogram_9__7_denums[7]:=(...
   1,  0,  0,  0,  0,  0,  0  )
  declare %vksr_scale_Golden_horogram_9__7_denoms[7]:=(...
   1,  0,  0,  0,  0,  0,  0  )


 declare $vksr_scale_tuning_EDITED
 $vksr_scale_tuning_EDITED:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_tuning,"Edited", $vksr_menu_item_pos)
 
 
 declare ui_menu $vksr_menu_scale_Black
 declare ui_value_edit $vksr_value_edit_n_equal_black (1,1000000,1) 
 set_text ($vksr_value_edit_n_equal_black,"n (black)")
 
 $vksr_value_edit_n_equal_black:=5
 $vksr_menu_item_pos:=0
 add_menu_item($vksr_menu_scale_Black,"Black: continues white",$vksr_menu_item_pos)
 declare $vksr_scale_black_SAME_AS_WHITE
 $vksr_scale_black_SAME_AS_WHITE:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 add_menu_item($vksr_menu_scale_Black,"Black: n-Equal",$vksr_menu_item_pos)
 declare $vksr_scale_black_N_EQUAL
 $vksr_scale_black_N_EQUAL:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 add_menu_item($vksr_menu_scale_Black,"Black: 3/1 equal divisions",$vksr_menu_item_pos)
 declare $vksr_scale_black_N_EQUAL_of_3o1
 $vksr_scale_black_N_EQUAL_of_3o1:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 add_menu_item($vksr_menu_scale_Black,"Black: 3/2 equal divisions",$vksr_menu_item_pos)
 declare $vksr_scale_black_N_EQUAL_of_3o2
 $vksr_scale_black_N_EQUAL_of_3o2:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 
 declare $vksr_scale_black_Pygmie_scale
 $vksr_scale_black_Pygmie_scale:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_Black,"Pygmie scale",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Pygmie_scale[5]:=(...
                                        0 {1/1}, 231174 {8/7}, 470781 {21/16}, 701955 {3/2}, 968826 {7/4}  )
 
 declare %vksr_scale_Pygmie_scale_denums[5]:=(...
                                               1,  8,  21,  3,  7  )
 declare %vksr_scale_Pygmie_scale_denoms[5]:=(...
                                               1,  7,  16,  2,  4  )
 
 declare $vksr_scale_black_Japanese_Koto_scale
 $vksr_scale_black_Japanese_Koto_scale:=$vksr_menu_item_pos
 add_menu_item($vksr_menu_scale_Black,"Japanese Koto scale",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare %vksr_scale_Japanese_Koto_scale[5]:=(...
                                               0 {1/1}, 203910 {9/8}, 315641 {6/5}, 701955 {3/2}, 813686 {8/5}  )
 
 declare %vksr_scale_Japanese_Koto_scale_denums[5]:=(...
                                                      1,  9,  6,  3,  8  )
 declare %vksr_scale_Japanese_Koto_scale_denoms[5]:=(...
                                                      1,  8,  5,  2,  5  ) 

  declare $vksr_scale_black_Golden_horogram_3__5
  $vksr_scale_black_Golden_horogram_3__5:=$vksr_menu_item_pos
  add_menu_item($vksr_menu_scale_Black,"Golden horogram 3, 5",$vksr_menu_item_pos)
  $vksr_menu_item_pos:=$vksr_menu_item_pos+1
  declare %vksr_scale_Golden_horogram_3__5[5]:=(...
  0 {1/1}, 259860 {259.860}, 519700 {519.700}, 779550 {779.550}, 1039400 {1039.400}  )

  declare %vksr_scale_Golden_horogram_3__5_denums[5]:=(...
   1,  0,  0,  0,  0  )
  declare %vksr_scale_Golden_horogram_3__5_denoms[5]:=(...
   1,  0,  0,  0,  0  )

 add_menu_item($vksr_menu_scale_Black,"Black: Harmonics & subharmonics",$vksr_menu_item_pos)
 declare $vksr_scale_black_HARMONIC_AND_SUBHARMONICS
 $vksr_scale_black_HARMONIC_AND_SUBHARMONICS:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 {*END OF SCALES DROPLIST SECTION}
 
 
 {Find the black and white keys}
 declare %vksr_white_black_pos[128]
 $vksr_i:=0
 $vksr_temp2:=1
 $vksr_temp:=0
 while ($vksr_i<128)
  if($vksr_i mod 12 = 0 or $vksr_i mod 12 = 2 or $vksr_i mod 12 = 4 or $vksr_i mod 12 = 5...
     or $vksr_i mod 12 = 7 or $vksr_i mod 12 = 9 or $vksr_i mod 12 = 11)
   {white keys}
   %vksr_white_black_pos[$vksr_i]:=$vksr_temp
   $vksr_temp:=$vksr_temp+1
  else
   %vksr_white_black_pos[$vksr_i]:=-$vksr_temp2
   $vksr_temp2:=$vksr_temp2+1
  end if
  $vksr_i:=$vksr_i+1
 end while
 
 declare ui_menu $vksr_menu_1o1_pos_note_name
 add_menu_item($vksr_menu_1o1_pos_note_name,"C", 0)
 add_menu_item($vksr_menu_1o1_pos_note_name,"Db",1)
 add_menu_item($vksr_menu_1o1_pos_note_name,"D", 2)
 add_menu_item($vksr_menu_1o1_pos_note_name,"Eb",3)
 add_menu_item($vksr_menu_1o1_pos_note_name,"E",4)
 add_menu_item($vksr_menu_1o1_pos_note_name,"F",5)
 add_menu_item($vksr_menu_1o1_pos_note_name,"Gb",6)
 add_menu_item($vksr_menu_1o1_pos_note_name,"G",7)
 add_menu_item($vksr_menu_1o1_pos_note_name,"Ab",8)
 add_menu_item($vksr_menu_1o1_pos_note_name,"A",9)
 add_menu_item($vksr_menu_1o1_pos_note_name,"Bb",10)
 add_menu_item($vksr_menu_1o1_pos_note_name,"B",11)
 make_persistent($vksr_menu_1o1_pos_note_name)
 declare !vksr_note_names[12]
 !vksr_note_names[0]:="C" 
 !vksr_note_names[1]:="Db" 
 !vksr_note_names[2]:="D" 
 !vksr_note_names[3]:="Eb" 
 !vksr_note_names[4]:="E" 
 !vksr_note_names[5]:="F" 
 !vksr_note_names[6]:="Gb" 
 !vksr_note_names[7]:="G" 
 !vksr_note_names[8]:="Ab" 
 !vksr_note_names[9]:="A" 
 !vksr_note_names[10]:="Bb" 
 !vksr_note_names[11]:="B" 
 {code inlined from KSPMathV600, see http://www.bigbobsmusicworld.com/kontakt-scripts/math-library}
 declare %kspML_M_Logs[21]:=(1000000000, 584962501, 321928095, 169925001, 87462841, 44394119, 22367813, 11227256, 5624549, 2815016, 1408195, 704269, 352178, 176099, 88052, 44028, 22014, 11006, 5504, 2751, 1376)
 declare $kspML_M_In
 declare $kspML_M_Out
 declare $kspML_M_Out2
 declare $kspML__n2
 declare $kspML__nx
 declare $kspML__sx
 {*END OF code inlined from KSPMathV600}
 declare $vksr_max_log2_n:=1024
 declare $vksr_max_primes:=160
 declare %vksr_primes[160]:=(...
                             2,3,5,7, 11, 13, 17, 19, 23, 29... 
                             , 31, 37, 41, 43, 47, 53, 59, 61, 67, 71... 
                             , 73, 79, 83, 89, 97,101,103,107,109,113 ...
                             ,127,131,137,139,149,151,157,163,167,173 ...
                             ,179,181,191,193,197,199,211,223,227,229... 
                             ... 
                             ,233,239,241,251,257,263,269,271,277,281 ...
                             ,283,293,307,311,313,317,331,337,347,349 ...
                             ,353,359,367,373,379,383,389,397,401,409 ...
                             ,419,421,431,433,439,443,449,457,461,463 ...
                             ,467,479,487,491,499,503,509,521,523,541 ...
                             ... 
                             ,547,557,563,569,571,577,587,593,599,601 ...
                             ,607,613,617,619,631,641,643,647,653,659 ...
                             ,661,673,677,683,691,701,709,719,727,733 ...
                             ,739,743,751,757,761,769,773,787,797,809 ...
                             ,811,821,823,827,829,839,853,857,859,863 ...
                             ... 
                             ,877,881,883,887,907,911,919,929,937,941)
 
 declare %vksr_ok_for_n_limit[128]
 declare $vksr_ok_for_n_limit_up_to_date:=0
 declare ui_label $vksr_label_ratios_prime_limit (2,2)
 declare ui_value_edit $vksr_value_edit_ratios_prime_limit (0,100000,1)
 $vksr_value_edit_ratios_prime_limit:=13
 set_text($vksr_label_ratios_prime_limit,"")
 set_text($vksr_value_edit_ratios_prime_limit,"Prime lim.")
 make_persistent($vksr_value_edit_ratios_prime_limit)
 declare %vksr_log2_n[1024]:=(...
                               -1022000000, 0, 1000000, 1584963, 2000000, 2321928, 2584963, 2807355, 3000000, 3169925, 3321928, 3459432, 3584963, 3700440, 3807355, 3906891, 4000000, 4087463, 4169925, 4247928, 4321928, 4392317, 4459432, 4523562, 4584963, 4643856, 4700440, 4754888, 4807355, 4857981, 4906891, 4954196, ...
                               5000000, 5044394, 5087463, 5129283, 5169925, 5209453, 5247928, 5285402, 5321928, 5357552, 5392317, 5426265, 5459432, 5491853, 5523562, 5554589, 5584963, 5614710, 5643856, 5672425, 5700440, 5727920, 5754888, 5781360, 5807355, 5832890, 5857981, 5882643, 5906891, 5930737, 5954196, 5977280, ...
                               6000000, 6022368, 6044394, 6066089, 6087463, 6108524, 6129283, 6149747, 6169925, 6189825, 6209453, 6228819, 6247928, 6266787, 6285402, 6303781, 6321928, 6339850, 6357552, 6375039, 6392317, 6409391, 6426265, 6442943, 6459432, 6475733, 6491853, 6507795, 6523562, 6539159, 6554589, 6569856, ...
                               6584963, 6599913, 6614710, 6629357, 6643856, 6658211, 6672425, 6686501, 6700440, 6714246, 6727920, 6741467, 6754888, 6768184, 6781360, 6794416, 6807355, 6820179, 6832890, 6845490, 6857981, 6870365, 6882643, 6894818, 6906891, 6918863, 6930737, 6942515, 6954196, 6965784, 6977280, 6988685, ...
                               7000000, 7011227, 7022368, 7033423, 7044394, 7055282, 7066089, 7076816, 7087463, 7098032, 7108524, 7118941, 7129283, 7139551, 7149747, 7159871, 7169925, 7179909, 7189825, 7199672, 7209453, 7219169, 7228819, 7238405, 7247928, 7257388, 7266787, 7276124, 7285402, 7294621, 7303781, 7312883, ...
                               7321928, 7330917, 7339850, 7348728, 7357552, 7366322, 7375039, 7383704, 7392317, 7400879, 7409391, 7417853, 7426265, 7434628, 7442943, 7451211, 7459432, 7467606, 7475733, 7483816, 7491853, 7499846, 7507795, 7515700, 7523562, 7531381, 7539159, 7546894, 7554589, 7562242, 7569856, 7577429, ...
                               7584963, 7592457, 7599913, 7607330, 7614710, 7622052, 7629357, 7636625, 7643856, 7651052, 7658211, 7665336, 7672425, 7679480, 7686501, 7693487, 7700440, 7707359, 7714246, 7721099, 7727920, 7734710, 7741467, 7748193, 7754888, 7761551, 7768184, 7774787, 7781360, 7787903, 7794416, 7800900, ...
                               7807355, 7813781, 7820179, 7826548, 7832890, 7839204, 7845490, 7851749, 7857981, 7864186, 7870365, 7876517, 7882643, 7888743, 7894818, 7900867, 7906891, 7912889, 7918863, 7924813, 7930737, 7936638, 7942515, 7948367, 7954196, 7960002, 7965784, 7971544, 7977280, 7982994, 7988685, 7994353, ...
                               8000000, 8005625, 8011227, 8016808, 8022368, 8027906, 8033423, 8038919, 8044394, 8049849, 8055282, 8060696, 8066089, 8071462, 8076816, 8082149, 8087463, 8092757, 8098032, 8103288, 8108524, 8113742, 8118941, 8124121, 8129283, 8134426, 8139551, 8144658, 8149747, 8154818, 8159871, 8164907, ...
                               8169925, 8174926, 8179909, 8184875, 8189825, 8194757, 8199672, 8204571, 8209453, 8214319, 8219169, 8224002, 8228819, 8233620, 8238405, 8243174, 8247928, 8252665, 8257388, 8262095, 8266787, 8271463, 8276124, 8280771, 8285402, 8290019, 8294621, 8299208, 8303781, 8308339, 8312883, 8317413, ...
                               8321928, 8326429, 8330917, 8335390, 8339850, 8344296, 8348728, 8353147, 8357552, 8361944, 8366322, 8370687, 8375039, 8379378, 8383704, 8388017, 8392317, 8396605, 8400879, 8405141, 8409391, 8413628, 8417853, 8422065, 8426265, 8430453, 8434628, 8438792, 8442943, 8447083, 8451211, 8455327, ...
                               8459432, 8463524, 8467606, 8471675, 8475733, 8479780, 8483816, 8487840, 8491853, 8495855, 8499846, 8503826, 8507795, 8511753, 8515700, 8519636, 8523562, 8527477, 8531381, 8535275, 8539159, 8543032, 8546894, 8550747, 8554589, 8558421, 8562242, 8566054, 8569856, 8573647, 8577429, 8581201, ...
                               8584963, 8588715, 8592457, 8596190, 8599913, 8603626, 8607330, 8611025, 8614710, 8618386, 8622052, 8625709, 8629357, 8632995, 8636625, 8640245, 8643856, 8647458, 8651052, 8654636, 8658211, 8661778, 8665336, 8668885, 8672425, 8675957, 8679480, 8682995, 8686501, 8689998, 8693487, 8696968, ...
                               8700440, 8703904, 8707359, 8710806, 8714246, 8717676, 8721099, 8724514, 8727920, 8731319, 8734710, 8738092, 8741467, 8744834, 8748193, 8751544, 8754888, 8758223, 8761551, 8764872, 8768184, 8771489, 8774787, 8778077, 8781360, 8784635, 8787903, 8791163, 8794416, 8797662, 8800900, 8804131, ...
                               8807355, 8810572, 8813781, 8816984, 8820179, 8823367, 8826548, 8829723, 8832890, 8836050, 8839204, 8842350, 8845490, 8848623, 8851749, 8854868, 8857981, 8861087, 8864186, 8867279, 8870365, 8873444, 8876517, 8879583, 8882643, 8885696, 8888743, 8891784, 8894818, 8897845, 8900867, 8903882, ...
                               8906891, 8909893, 8912889, 8915879, 8918863, 8921841, 8924813, 8927778, 8930737, 8933691, 8936638, 8939579, 8942515, 8945444, 8948367, 8951285, 8954196, 8957102, 8960002, 8962896, 8965784, 8968667, 8971544, 8974415, 8977280, 8980140, 8982994, 8985842, 8988685, 8991522, 8994353, 8997179, ...
                               9000000, 9002815, 9005625, 9008429, 9011227, 9014020, 9016808, 9019591, 9022368, 9025140, 9027906, 9030667, 9033423, 9036174, 9038919, 9041659, 9044394, 9047124, 9049849, 9052568, 9055282, 9057992, 9060696, 9063395, 9066089, 9068778, 9071462, 9074141, 9076816, 9079485, 9082149, 9084808, ...
                               9087463, 9090112, 9092757, 9095397, 9098032, 9100662, 9103288, 9105909, 9108524, 9111136, 9113742, 9116344, 9118941, 9121534, 9124121, 9126704, 9129283, 9131857, 9134426, 9136991, 9139551, 9142107, 9144658, 9147205, 9149747, 9152285, 9154818, 9157347, 9159871, 9162391, 9164907, 9167418, ...
                               9169925, 9172428, 9174926, 9177420, 9179909, 9182394, 9184875, 9187352, 9189825, 9192293, 9194757, 9197217, 9199672, 9202124, 9204571, 9207014, 9209453, 9211888, 9214319, 9216746, 9219169, 9221587, 9224002, 9226412, 9228819, 9231221, 9233620, 9236014, 9238405, 9240791, 9243174, 9245553, ...
                               9247928, 9250298, 9252665, 9255029, 9257388, 9259743, 9262095, 9264443, 9266787, 9269127, 9271463, 9273796, 9276124, 9278449, 9280771, 9283088, 9285402, 9287712, 9290019, 9292322, 9294621, 9296916, 9299208, 9301496, 9303781, 9306062, 9308339, 9310613, 9312883, 9315150, 9317413, 9319672, ...
                               9321928, 9324181, 9326429, 9328675, 9330917, 9333155, 9335390, 9337622, 9339850, 9342075, 9344296, 9346514, 9348728, 9350939, 9353147, 9355351, 9357552, 9359750, 9361944, 9364135, 9366322, 9368506, 9370687, 9372865, 9375039, 9377211, 9379378, 9381543, 9383704, 9385862, 9388017, 9390169, ...
                               9392317, 9394463, 9396605, 9398744, 9400879, 9403012, 9405141, 9407268, 9409391, 9411511, 9413628, 9415742, 9417853, 9419960, 9422065, 9424166, 9426265, 9428360, 9430453, 9432542, 9434628, 9436712, 9438792, 9440869, 9442943, 9445015, 9447083, 9449149, 9451211, 9453271, 9455327, 9457381, ...
                               9459432, 9461479, 9463524, 9465566, 9467606, 9469642, 9471675, 9473706, 9475733, 9477758, 9479780, 9481799, 9483816, 9485829, 9487840, 9489848, 9491853, 9493855, 9495855, 9497852, 9499846, 9501837, 9503826, 9505812, 9507795, 9509775, 9511753, 9513728, 9515700, 9517669, 9519636, 9521600, ...
                               9523562, 9525521, 9527477, 9529431, 9531381, 9533330, 9535275, 9537218, 9539159, 9541097, 9543032, 9544964, 9546894, 9548822, 9550747, 9552669, 9554589, 9556506, 9558421, 9560333, 9562242, 9564149, 9566054, 9567956, 9569856, 9571753, 9573647, 9575539, 9577429, 9579316, 9581201, 9583083, ...
                               9584963, 9586840, 9588715, 9590587, 9592457, 9594325, 9596190, 9598053, 9599913, 9601771, 9603626, 9605480, 9607330, 9609179, 9611025, 9612868, 9614710, 9616549, 9618386, 9620220, 9622052, 9623881, 9625709, 9627534, 9629357, 9631177, 9632995, 9634811, 9636625, 9638436, 9640245, 9642052, ...
                               9643856, 9645658, 9647458, 9649256, 9651052, 9652845, 9654636, 9656425, 9658211, 9659996, 9661778, 9663558, 9665336, 9667112, 9668885, 9670656, 9672425, 9674192, 9675957, 9677720, 9679480, 9681238, 9682995, 9684749, 9686501, 9688250, 9689998, 9691744, 9693487, 9695228, 9696968, 9698705, ...
                               9700440, 9702173, 9703904, 9705632, 9707359, 9709084, 9710806, 9712527, 9714246, 9715962, 9717676, 9719389, 9721099, 9722808, 9724514, 9726218, 9727920, 9729621, 9731319, 9733015, 9734710, 9736402, 9738092, 9739781, 9741467, 9743151, 9744834, 9746514, 9748193, 9749869, 9751544, 9753217, ...
                               9754888, 9756556, 9758223, 9759888, 9761551, 9763212, 9764872, 9766529, 9768184, 9769838, 9771489, 9773139, 9774787, 9776433, 9778077, 9779719, 9781360, 9782998, 9784635, 9786270, 9787903, 9789534, 9791163, 9792790, 9794416, 9796040, 9797662, 9799282, 9800900, 9802516, 9804131, 9805744, ...
                               9807355, 9808964, 9810572, 9812177, 9813781, 9815383, 9816984, 9818582, 9820179, 9821774, 9823367, 9824959, 9826548, 9828136, 9829723, 9831307, 9832890, 9834471, 9836050, 9837628, 9839204, 9840778, 9842350, 9843921, 9845490, 9847057, 9848623, 9850187, 9851749, 9853310, 9854868, 9856426, ...
                               9857981, 9859535, 9861087, 9862637, 9864186, 9865733, 9867279, 9868823, 9870365, 9871905, 9873444, 9874981, 9876517, 9878051, 9879583, 9881114, 9882643, 9884171, 9885696, 9887221, 9888743, 9890264, 9891784, 9893302, 9894818, 9896332, 9897845, 9899357, 9900867, 9902375, 9903882, 9905387, ...
                               9906891, 9908393, 9909893, 9911392, 9912889, 9914385, 9915879, 9917372, 9918863, 9920353, 9921841, 9923327, 9924813, 9926296, 9927778, 9929258, 9930737, 9932215, 9933691, 9935165, 9936638, 9938109, 9939579, 9941048, 9942515, 9943980, 9945444, 9946906, 9948367, 9949827, 9951285, 9952741, ...
                               9954196, 9955650, 9957102, 9958553, 9960002, 9961450, 9962896, 9964341, 9965784, 9967226, 9968667, 9970106, 9971544, 9972980, 9974415, 9975848, 9977280, 9978710, 9980140, 9981567, 9982994, 9984418, 9985842, 9987264, 9988685, 9990104, 9991522, 9992938, 9994353, 9995767, 9997179, 9998590...
                               )
 make_persistent ($vksr_1o1_millicents)   
 make_persistent ($vksr_menu_show_more)
 make_persistent ($vksr_knob_Note)
 make_persistent ($vksr_knob_To)
 make_persistent ($vksr_knob_Cents)
 make_persistent ($vksr_knob_CentiCents)
 make_persistent ($vksr_knob_LastDigit)
 make_persistent ($vksr_button_ResetTo12EQ)
 make_persistent ($vksr_value_edit_n_equal)
 make_persistent ($vksr_menu_scale_Black)
 make_persistent ($vksr_value_edit_n_equal_black)
 make_persistent ($vksr_menu_scale_tuning)
 _read_persistent_var($vksr_menu_scale_Black)
 
 make_persistent ($vksr_label_tuning_description)
 make_persistent ($vksr_label_tuning_definition)
 make_persistent (@vksr_tuning_description_string)
 make_persistent (@vksr_tuning_definition_string)
 make_persistent (%vksr_pitch_bend_for_output_note)
 make_persistent (%vksr_total_millicents_on_set_denum)
 make_persistent (%vksr_denum_for_output_note)
 make_persistent (%vksr_denom_for_output_note)
 make_persistent (%vksr_output_note)
 make_persistent ($vksr_menu_scroll_scale)
 make_persistent ($vksr_Note_user_edited)
 make_persistent ($vksr_Note_played)
 make_persistent ($vksr_value_edit_scroll_scale_midi_start)
 make_persistent ($vksr_value_edit_scroll_scale_show_before)
 make_persistent($vksr_value_edit_Note) 
 make_persistent($vksr_value_edit_To) 
 make_persistent($vksr_value_edit_Cents) 
 make_persistent($vksr_value_edit_digits_precision)  
 set_text ($vksr_knob_CentiCents,"Centicents")
 set_text ($vksr_knob_LastDigit,"Last digit")
 set_text ($vksr_button_ResetTo12EQ,"Reset to 12-equal")
 
 declare $vksr_move_ui_vertically:=0
 
 set_text ($vksr_value_edit_To,"To")
 set_text ($vksr_value_edit_Cents,"Cents")
 set_text ($vksr_value_edit_digits_precision,"Dec. pt")
 set_text ($vksr_value_edit_n_equal,"n")
 $vksr_value_edit_n_equal:=24
 set_text ($vksr_value_edit_n_equal,"n")
 set_text ($vksr_value_edit_denum,"Ratio")
 set_text ($vksr_value_edit_denom,"over")
 
 _read_persistent_var($vksr_menu_show_more) 
 _read_persistent_var($vksr_value_edit_n_equal_black)
 _read_persistent_var($vksr_menu_scale_tuning) 
 _read_persistent_var($vksr_menu_scroll_scale) 
 
 
 { This is work in progress - may add it later}
 declare $vksr_ok_process_volume_and_pan_keyswitches:=1
 declare $vksr_restore_expression_and_pan:=1
 { 
 - idea for special UI to adjust pan and volume for each note
 add_menu_item($vksr_menu_show_more,"Per note stereo pan & volume",$vksr_menu_item_pos)
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 declare $vksr_sm_PER_NOTE_PAN_AND_VOLUME
 $vksr_sm_PER_NOTE_PAN_AND_VOLUME:=$vksr_menu_item_pos
 $vksr_menu_item_pos:=$vksr_menu_item_pos+1
 }
 {
 declare ui_menu $vksr_menu_adjust_volume_and_pan
 add_menu_item($vksr_menu_adjust_volume_and_pan,"Don't adjust volume or pan",0)
 add_menu_item($vksr_menu_adjust_volume_and_pan,"Adjust volume and pan",1)
 make_persistent($vksr_menu_adjust_volume_and_pan) 
 }
 
  
 _read_persistent_var($vksr_menu_detect_ratios) 
 
 $vksr_restore_expression_and_pan:=1
 
 declare ui_value_edit $vksr_value_edit_Pan (-1000,1000,10) 
 set_text ($vksr_value_edit_Pan,"Pan")
 declare ui_value_edit $vksr_value_edit_Volume  (0,100000,1000) 
 set_text ($vksr_value_edit_Volume,"Vol.")
 $vksr_value_edit_Volume:=127*128
 $vksr_value_edit_Pan:=0
 {
 declare ui_knob $vksr_knob_Volume(0, 100000, 1000)
 set_text ($vksr_knob_Volume,"Volume")
 $vksr_knob_Volume:=500000
 declare ui_knob $vksr_knob_Pan(-500000,500000, 1000)
 $vksr_knob_Pan:=0
 set_text ($vksr_knob_Pan,"Pan")
 }
  
  
 {techy programming note: make sure to use read_persistent_var(..) for any variables used here in on init, otherwise they don't get read until
 after the on init completes processing - if you find that controls get shown only if the "More" menu entry is
 reselected after the instrument loads - that usualy means that the script is missing an instruction to
 read one of the persistent variables before this section of the coe
 }
 
 
 {HIDE EVERYTHING}
 { This is what you get if you set $vksr_hide_user_interface=1 (i.e. non 0)
  
 {DUPLICATES ******call hide_user_interface except here it shows $vksr_button_ResetTo12EQ ********}
 {so for minimal interface that's all you see}
 {Here if a control is moved to 0,0 then it is hidden from view so doesn't appear in the user interface}

 move_control($vksr_button_ResetTo12EQ,1,1)  {just show the reset to 12et button}
 move_control($vksr_label_tuning_method,0,0) 
 move_control($vksr_label_documentation,0,0)
 move_control($vksr_value_edit_1o1_transpose,0,0)
 move_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,0,0)
 move_control($vksr_label_tuning_method,0,0) 
 move_control($vksr_value_edit_n_equal,0,0) 
 move_control($vksr_value_edit_denum,0,0) 
 move_control($vksr_value_edit_denom,0,0) 
 move_control($vksr_label_note_played,0,0)  
 move_control($vksr_value_edit_digits_precision,0,0) 
 move_control($vksr_menu_scale_tuning,0,0)  
 move_control($vksr_menu_show_more,0,0) 
 move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
 move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
 move_control($vksr_menu_scroll_scale,0,0)  
 move_control($vksr_menu_show_more,0,0) 
 move_control($vksr_knob_Note,0,0) 
 move_control($vksr_menu_midi_tuning_method,0,0) 
 move_control($vksr_knob_To,0,0) 
 move_control($vksr_knob_Cents,0,0) 
 move_control($vksr_value_edit_Note,0,0) 
 move_control($vksr_value_edit_To,0,0) 
 move_control($vksr_value_edit_Cents,0,0) 
 move_control($vksr_value_edit_digits_precision,0,0) 
 move_control($vksr_knob_CentiCents,0,0) 
 move_control($vksr_knob_LastDigit,0,0) 
 move_control($vksr_label_tuning_description,0,0) 
 move_control($vksr_label_tuning_definition,0,0)
 move_control($vksr_menu_1o1_pos_note_name,0,0)
 move_control($vksr_value_edit_n_equal_black,0,0)
 move_control($vksr_menu_show_diagnostics,0,0)
 move_control($vksr_menu_apply_skin,0,0)
 move_control($vksr_label_instrument_range,0,2)
 move_control($vksr_value_edit_instrument_range_min,0,0)
 move_control($vksr_value_edit_instrument_range_max,0,0)
 move_control($vksr_label_instrument_range2,0,0)
 move_control($vksr_value_edit_first_harmonic,0,0)  
 move_control($vksr_value_edit_first_harmonic_black,0,0)  
 move_control($vksr_menu_scale_Black,0,0)  
 move_control($vksr_value_edit_Pan,0,0)  
 move_control($vksr_value_edit_Volume,0,0)  
 
 move_control($vksr_value_edit_ratios_prime_limit,0,0)  
 move_control($vksr_label_ratios_prime_limit,0,0)  
 move_control($vksr_value_edit_ratio_recognition_sensitivity,0,0)  
 
 move_control($vksr_menu_detect_ratios,0,0)  
 move_control($vksr_label_ratio_recognition_sensitivity,0,0)  
 move_control($vksr_label_ratio_recognition_sensitivity2,0,0)  
 {END OF DUPLICATES ******call hide_user_interface********}
 
 if($vksr_hide_user_interface=0)
 
 {SHOW OR HIDE CONTROLS AND MOVE THEM DEPENDING ON USER'S CHOICE IN THE DROP MENU OF UIS}
   
 
 {Since these are persistent values (will show same display if user closes the instrument and re-opens it)
 - then have to do this in on init and can't just call the routine here
 }
 
   {DUPLICATES:

  ***********call vksr_move_controls*************
  - call of function not permitted in on init
  }   


 $vksr_temp2:=1
 $vksr_temp:=6 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 if($vksr_menu_show_more=$vksr_sm_LESS)
  $vksr_temp:=4 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
  $vksr_temp2:=1
  $vksr_temp:=5 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_MORE)
  
  $vksr_temp:=6 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
  $vksr_temp:=3 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
  $vksr_temp:=4 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if  
 
 if($vksr_menu_show_more=$vksr_sm_DOCUMENTATION)
  move_control($vksr_label_documentation,1,1)
  move_control($vksr_menu_show_more,6,1)
  move_control($vksr_button_ResetTo12EQ,6,2) 
 else {$vksr_menu_show_more#$vksr_sm_DOCUMENTATION}
  {
  if($vksr_menu_show_more=$vksr_sm_PER_NOTE_PAN_AND_VOLUME)
   move_control($vksr_menu_show_more,6,1)
  else
  end if
  }
  if($vksr_menu_show_more=$vksr_sm_TWEAKS)
   move_control($vksr_button_ResetTo12EQ,0,0)
   move_control($vksr_label_instrument_range,1,1)
   move_control($vksr_value_edit_instrument_range_min,2,2)
   move_control($vksr_value_edit_instrument_range_max,3,1)
   move_control($vksr_label_instrument_range2,1,2)
   move_control($vksr_menu_show_diagnostics,5,1)
   move_control($vksr_menu_detect_ratios,1,4)  
   if($vksr_menu_detect_ratios#0)
    move_control($vksr_label_ratio_recognition_sensitivity,2,4)
    move_control($vksr_value_edit_ratio_recognition_sensitivity,4,4)
    move_control($vksr_label_ratio_recognition_sensitivity2,5,4) 
    move_control($vksr_value_edit_ratios_prime_limit,6,4) 
    move_control($vksr_label_ratios_prime_limit,5,5)  
   else
    move_control($vksr_label_ratio_recognition_sensitivity,0,0)
    move_control($vksr_value_edit_ratio_recognition_sensitivity,0,0)
    move_control($vksr_label_ratio_recognition_sensitivity2,0,0) 
    move_control($vksr_value_edit_ratios_prime_limit,0,0) 
    move_control($vksr_label_ratios_prime_limit,0,0)  
   end if
   
   move_control($vksr_label_note_played,1,5) 
   move_control($vksr_value_edit_denum,2,5) 
   move_control($vksr_value_edit_denom,3,5) 
   move_control($vksr_menu_apply_skin,4,1)
   
   move_control($vksr_menu_show_more,6,1)
   move_control($vksr_value_edit_digits_precision,6,2) 
  else {$vksr_menu_show_more#$vksr_sm_TWEAKS and $vksr_menu_show_more#$vksr_sm_DOCUMENTATION}
   move_control($vksr_menu_scale_Black,$vksr_temp2,$vksr_temp+$vksr_ui_extra_vertical_shift)
   move_control($vksr_value_edit_n_equal_black,0,0)
   if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL...
      or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o1...
      or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o2...
      )
    move_control($vksr_value_edit_n_equal_black,$vksr_temp2+1,$vksr_temp+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_scale_black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS)
    move_control($vksr_value_edit_first_harmonic_black,$vksr_temp2+1,$vksr_temp+$vksr_ui_extra_vertical_shift) 
   end if
   
   {(1,$vksr_temp2+6+$vksr_ui_extra_vertical_shift)}
   
   $vksr_temp:=0 { for $vksr_value_edit_1o1_transpose etc extra shift}
   $vksr_temp2:=0 { for $vksr_menu_scale_Black etc extra shift}
   if($vksr_menu_show_more=$vksr_sm_LESS)
    $vksr_temp:=-2
    $vksr_temp2:=-2
   end if    
   if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
    $vksr_temp:=-1
    $vksr_temp2:=0
   end if    
   if($vksr_menu_show_more=$vksr_sm_MORE)
    $vksr_temp:=0
    $vksr_temp2:=0
   end if    
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
    $vksr_temp:=-3
    $vksr_temp2:=-3
   end if    
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
    $vksr_temp:=-2
    $vksr_temp2:=-2
   end if   
   
    
   move_control($vksr_value_edit_1o1_transpose,0,0) 
    {Not really needed, got Tune control anyway in main kontakt instrument display
    {was: move_control($vksr_value_edit_1o1_transpose,4,$vksr_temp+6+$vksr_ui_extra_vertical_shift)}
    
   move_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,5,$vksr_temp+6+$vksr_ui_extra_vertical_shift)
   move_control($vksr_menu_1o1_pos_note_name,6,$vksr_temp+6+$vksr_ui_extra_vertical_shift)

   if($vksr_menu_show_more=$vksr_sm_MORE_WITH_SCALE)
    {isn't enough space in ui for these}
    move_control($vksr_value_edit_n_equal_black,0,0)
    move_control($vksr_menu_scale_Black,0,0)
    move_control($vksr_value_edit_1o1_transpose,0,0)
    move_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,0,0 )   
    move_control($vksr_menu_1o1_pos_note_name,0,0)
    move_control($vksr_value_edit_Pan,0,0)  
    move_control($vksr_value_edit_Volume,0,0)  
  end if
   
   if($vksr_menu_show_more=$vksr_sm_LESS or $vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE ...
      or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
    {hide knobs}
    $vksr_move_ui_vertically:=-2
    move_control($vksr_knob_Note,0,0) 
    move_control($vksr_knob_To,0,0) 
    move_control($vksr_knob_Cents,0,0) 
    move_control($vksr_knob_CentiCents,0,0) 
    move_control($vksr_knob_LastDigit,0,0) 
   else
    $vksr_move_ui_vertically:=0
    move_control($vksr_knob_Note,1,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_To,2,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_Cents,3,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_CentiCents,4,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_LastDigit,5,1+$vksr_ui_extra_vertical_shift) 
   end if
   
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY ...
      or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
    {tuning only}
    $vksr_move_ui_vertically:=-3
    move_control($vksr_button_ResetTo12EQ,0,0) 
    move_control($vksr_value_edit_Note,0,0) 
    move_control($vksr_value_edit_To,0,0) 
    move_control($vksr_value_edit_Cents,0,0) 
    move_control($vksr_value_edit_denum,0,0) 
    move_control($vksr_value_edit_denom,0,0) 
    if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
     move_control($vksr_menu_show_more,6,2+$vksr_ui_extra_vertical_shift) 
    end if
    if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
     
     move_control($vksr_menu_scroll_scale,1,4+$vksr_ui_extra_vertical_shift) 
     if($vksr_menu_scroll_scale=$vksr_SCROLL_SCALE_TO_SELECTED_NOTE+$vksr_ui_extra_vertical_shift)
      move_control($vksr_value_edit_scroll_scale_midi_start,2,4+$vksr_ui_extra_vertical_shift) 
      move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
     else
      move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
      move_control($vksr_value_edit_scroll_scale_show_before,2,4+$vksr_ui_extra_vertical_shift) 
     end if
     move_control($vksr_menu_show_more,6,1+$vksr_ui_extra_vertical_shift) 
    end if
   else {#($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE+$vksr_ui_extra_vertical_shift) }
    {tuning + user note edit controls}
    move_control($vksr_button_ResetTo12EQ,6,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_Note,1,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_To,2,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_Cents,3,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_denum,4,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_denom,5,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    if($vksr_menu_show_more=$vksr_sm_MORE_WITH_SCALE)
     {Only available space to far right below more drop menu}
     
     move_control($vksr_button_ResetTo12EQ,6,2)
     move_control($vksr_menu_scale_Black,6,3)
     if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL...
        or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o1...
        or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o2...
        )
      move_control($vksr_value_edit_n_equal_black,6,4) 
     end if 
     if($vksr_menu_scale_black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS)
      move_control($vksr_value_edit_first_harmonic_black,6,4) 
     end if
    end if
    if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
     {has 12-et reset button at far right so  move to positions 4, 5 }
     if($vksr_menu_scroll_scale=$vksr_SCROLL_SCALE_TO_SELECTED_NOTE)
      move_control($vksr_value_edit_scroll_scale_midi_start,4,5+$vksr_ui_extra_vertical_shift) 
      move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
     else
      move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
      move_control($vksr_value_edit_scroll_scale_show_before,4,5+$vksr_ui_extra_vertical_shift) 
     end if
     move_control($vksr_menu_scroll_scale,3,5+$vksr_ui_extra_vertical_shift) 
    end if
    {move_control($vksr_menu_show_more,6,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) }
    move_control($vksr_menu_show_more,6,1+$vksr_ui_extra_vertical_shift) 
   end if
   
   move_control($vksr_label_tuning_method,3,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_menu_midi_tuning_method,6,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_label_tuning_definition,3,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_label_tuning_description,3,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_menu_scale_tuning,1,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_label_note_played,1,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_value_edit_digits_precision,2,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   
   move_control($vksr_value_edit_n_equal,0,0)  
   move_control($vksr_value_edit_first_harmonic,0,0)  
   
   if($vksr_menu_show_more=$vksr_sm_LESS or $vksr_menu_show_more=$vksr_sm_MORE ...
      or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
    {no tuning definition}
    move_control($vksr_label_tuning_definition,0,0) 
    move_control($vksr_menu_scroll_scale,0,0) 
    move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
    move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
   end if 
   
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL...
      or $vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o1...
      or $vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o2...
      )
    move_control($vksr_value_edit_n_equal,2,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if
   
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_HARMONIC_AND_SUBHARMONICS)
    move_control($vksr_value_edit_first_harmonic,2,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if
   
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE+$vksr_ui_extra_vertical_shift)
    move_control($vksr_menu_show_more,6,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_label_tuning_method,3,2+$vksr_ui_extra_vertical_shift) 
    {hide everything except tuning drop down and related controls}
    move_control($vksr_menu_midi_tuning_method,6,2+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_label_note_played,1,2+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_digits_precision,0,0){(2,2+$vksr_ui_extra_vertical_shift) }
   end if
   
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE+$vksr_ui_extra_vertical_shift)
    move_control($vksr_label_tuning_method,3,2+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_label_tuning_definition,1,3+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_show_more=$vksr_sm_LESS or $vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
    {Could hide it?}
    move_control($vksr_value_edit_digits_precision,0,0)   
    {}
   end if  
   {end if }
   if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
    move_control($vksr_value_edit_Pan,3,8+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift)  
    move_control($vksr_value_edit_Volume,4,8+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_show_more=$vksr_sm_LESS)
    move_control($vksr_value_edit_Pan,3,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift)  
    move_control($vksr_value_edit_Volume,4,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_show_more=$vksr_sm_MORE)
    move_control($vksr_value_edit_Pan,3,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift)  
    move_control($vksr_value_edit_Volume,4,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if 
  end if  {$vksr_menu_show_more#$vksr_sm_TWEAKS}
 end if {$vksr_menu_show_more#$vksr_sm_DOCUMENTATION}

  {END OF: DUPLICATES:
  ************call vksr_move_controls***********
  - call of function not permitted in on init
  }  
 end if {$vksr_hide_user_interface=0}
 
 declare $vksr_MilliCents:=0
 $vksr_button_ResetTo12EQ:=1 {press the "reset to 12 et" button}
 
 
 _read_persistent_var($vksr_button_ResetTo12EQ) 
 
 if($vksr_button_ResetTo12EQ=1)
  {DUPLICATES:
  ************call vksr_reset_to_12et**************
  - call of function not permitted in on init
  }  
  $vksr_knob_Note:=0
  $vksr_knob_To:=0
  $vksr_knob_Cents:=0
  $vksr_knob_CentiCents:=0
  $vksr_knob_LastDigit:=0
  $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_at_note_on[$vksr_i]:=0
   { This has no effect if these are set to make_persistent }
   %vksr_pitch_bend_for_output_note[$vksr_i]:=0
   %vksr_total_millicents_on_set_denum[$vksr_i]:=0
   %vksr_denum_for_output_note[$vksr_i]:=0
   %vksr_denom_for_output_note[$vksr_i]:=0
   %vksr_output_note[$vksr_i]:=$vksr_i
   
   $vksr_i:=$vksr_i+1
   
  end while
 end if 
 {END OF DUPLICATES *********call vksr_reset_to_12et**********}
 
 make_persistent($vksr_menu_apply_skin)
 _read_persistent_var($vksr_menu_apply_skin) 
 
 if($vksr_menu_apply_skin#0)
  { DUPLICATES cal set_skin_or_remove}
  if($vksr_button_ResetTo12EQ=0)
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,@vksr_skin_button)
  else
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"")
  end if
  {Note, can't seem to do this for value_edits - get kontakt error message  "expression expected"}
  {}
  if($vksr_button_ResetTo12EQ=0)
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,@vksr_skin_button)
  else
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"")
  end if
  {Note, can't seem to do this for value_edits - get kontakt error message  "expression expected"}
  {}
  if($vksr_menu_apply_skin=2){transparent label backgrounds}
   { 
   With transparent background, can't see the scroll bar to right when too much text to fit in page  
   set_control_par_str(get_ui_id($vksr_label_ratios_prime_limit), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   }
   set_control_par_str(get_ui_id($vksr_label_instrument_range), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_instrument_range2), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_note_played), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_tuning_method), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_tuning_description), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_tuning_definition), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_menu_detect_ratios), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity2), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
  else
   {set_control_par_str(get_ui_id($vksr_label_ratios_prime_limit), $CONTROL_PAR_PICTURE,"")}
   set_control_par_str(get_ui_id($vksr_label_instrument_range), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_instrument_range2), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_note_played), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_tuning_method), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_tuning_description), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_tuning_definition), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_menu_detect_ratios), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity2), $CONTROL_PAR_PICTURE,"")
  end if
  set_control_par_str(get_ui_id($vksr_menu_apply_skin), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_1o1_pos_note_name), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_scroll_scale), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_midi_tuning_method), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_scale_tuning), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_scale_Black), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_show_more), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_show_diagnostics), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  
 end if
 { END OF DUPLICATES cal set_skin_or_remove}
 

 
end on { on init }

function set_skin_or_remove
 
 if($vksr_menu_apply_skin#0)
  { This displays warning about not found - is easy enough to set it using "instrument options"
  set_control_par_str($INST_WALLPAPER_ID, $CONTROL_PAR_PICTURE,"background")
  }
  if($vksr_button_ResetTo12EQ=0)
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,@vksr_skin_button)
  else
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"")
  end if
  {Note, can't seem to do this for value_edits - get kontakt error message  "expression expected"}
  {}
  if($vksr_button_ResetTo12EQ=0)
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,@vksr_skin_button)
  else
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"")
  end if
  {Note, can't seem to do this for value_edits - get kontakt error message  "expression expected"}
  {}
  if($vksr_menu_apply_skin=2){transparent label backgrounds}
   { 
   With transparent background, can't see the scroll bar to right when too much text to fit in page  
   set_control_par_str(get_ui_id($vksr_label_ratios_prime_limit), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   }
   set_control_par_str(get_ui_id($vksr_label_instrument_range), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_instrument_range2), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_note_played), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_tuning_method), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_tuning_description), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_tuning_definition), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_menu_detect_ratios), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
  set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity2), $CONTROL_PAR_PICTURE,@vksr_skin_transparent_background)
  else
   {set_control_par_str(get_ui_id($vksr_label_ratios_prime_limit), $CONTROL_PAR_PICTURE,"")}
   set_control_par_str(get_ui_id($vksr_label_instrument_range), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_instrument_range2), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_note_played), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_tuning_method), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_tuning_description), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_tuning_definition), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_menu_detect_ratios), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity), $CONTROL_PAR_PICTURE,"")
   set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity2), $CONTROL_PAR_PICTURE,"")
  end if
  set_control_par_str(get_ui_id($vksr_menu_apply_skin), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_1o1_pos_note_name), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_scroll_scale), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_midi_tuning_method), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_scale_tuning), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_scale_Black), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_show_more), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
  set_control_par_str(get_ui_id($vksr_menu_show_diagnostics), $CONTROL_PAR_PICTURE,@vksr_skin_menu)
 else {$vksr_menu_apply_skin=0}
  set_control_par_str(get_ui_id($vksr_menu_1o1_pos_note_name), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_apply_skin), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_scale_Black), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_note_played), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_tuning_method), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_tuning_description), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_tuning_definition), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_scroll_scale), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_midi_tuning_method), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_scale_tuning), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_show_more), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_show_diagnostics), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_instrument_range), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_instrument_range2), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_menu_detect_ratios), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity), $CONTROL_PAR_PICTURE,"")
  set_control_par_str(get_ui_id($vksr_label_ratio_recognition_sensitivity2), $CONTROL_PAR_PICTURE,"")
 end if
 
end function

on ui_control ($vksr_menu_apply_skin)
 call set_skin_or_remove
end on

function hide_user_interface
 
 move_control($vksr_button_ResetTo12EQ,0,0) 
 move_control($vksr_value_edit_1o1_transpose,0,0)
 move_control($vksr_menu_1o1_pos_note_name,0,0)
 move_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,0,0)
 move_control($vksr_label_tuning_method,0,0) 
 move_control($vksr_label_documentation,0,0) 
 move_control($vksr_value_edit_n_equal,0,0) 
 move_control($vksr_value_edit_denum,0,0) 
 move_control($vksr_value_edit_denom,0,0) 
 move_control($vksr_label_note_played,0,0)  
 move_control($vksr_value_edit_digits_precision,0,0) 
 move_control($vksr_menu_scale_tuning,0,0)  
 move_control($vksr_menu_show_more,0,0) 
 move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
 move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
 move_control($vksr_menu_scroll_scale,0,0)  
 move_control($vksr_menu_show_more,0,0) 
 move_control($vksr_knob_Note,0,0) 
 move_control($vksr_menu_midi_tuning_method,0,0) 
 move_control($vksr_knob_To,0,0) 
 move_control($vksr_knob_Cents,0,0) 
 move_control($vksr_value_edit_Note,0,0) 
 move_control($vksr_value_edit_To,0,0) 
 move_control($vksr_value_edit_Cents,0,0) 
 move_control($vksr_value_edit_digits_precision,0,0) 
 move_control($vksr_knob_CentiCents,0,0) 
 move_control($vksr_knob_LastDigit,0,0) 
 move_control($vksr_label_tuning_description,0,0) 
 move_control($vksr_label_tuning_definition,0,0) 
 move_control($vksr_menu_1o1_pos_note_name,0,0) 
 move_control($vksr_menu_scale_Black,0,0)
 move_control($vksr_value_edit_Pan,0,0)  
 move_control($vksr_value_edit_Volume,0,0)  

 move_control($vksr_value_edit_n_equal_black,0,0)
 move_control($vksr_menu_show_diagnostics,0,0)
 move_control($vksr_label_instrument_range,0,2)
 move_control($vksr_value_edit_instrument_range_min,0,0)
 move_control($vksr_value_edit_instrument_range_max,0,0)
 move_control($vksr_label_instrument_range2,0,0)
 move_control($vksr_value_edit_first_harmonic,0,0)  
 move_control($vksr_value_edit_first_harmonic_black,0,0)  
 move_control($vksr_menu_scale_Black,0,0)  
 move_control($vksr_menu_apply_skin,0,0)  
 move_control($vksr_value_edit_ratio_recognition_sensitivity,0,0)  
 move_control($vksr_value_edit_ratios_prime_limit,0,0)  
 move_control($vksr_label_ratios_prime_limit,0,0)  
 move_control($vksr_menu_detect_ratios,0,0)  
 move_control($vksr_label_ratio_recognition_sensitivity,0,0)  
 move_control($vksr_label_ratio_recognition_sensitivity2,0,0) 
 
end function

function vksr_find_cents_string_for_offset_in_millicents  
 {Converts number into a decimal string
 argument here is a number: $vksr_offset_in_millicents set in calling pro
 
 returns value as @vksr_cents_or_ratio_for_offset_in_milliseconds
 which is a string showing $vksr_offset_in_millicents as a decimal.
 
 Example if $vksr_offset_in_millicents=50055 
 then want to set 
 @vksr_cents_or_ratio_for_offset_in_milliseconds="50.055"
 (there may be an easier way to do this in Kontakt but I couldn't find it.
 }
 $vksr_temp:=$vksr_offset_in_millicents
 if($vksr_offset_in_millicents<0)
  $vksr_temp:=-$vksr_offset_in_millicents
 end if
 $vksr_temp:=$vksr_temp mod 1000
 $vksr_temp2:=100
 select($vksr_value_edit_digits_precision)
  case 2
   {e.g. $vksr_temp = 15 truncate upwards to 20 } 
   $vksr_temp:=($vksr_temp+5)/10
   $vksr_temp2:=10
  case 1
   {e.g. $vksr_temp = 150 truncate upwards to 200 } 
   $vksr_temp:=($vksr_temp+50)/100
   $vksr_temp2:=1
  case 0
   $vksr_temp:=($vksr_temp+500)/1000
 end select
 
 @vksr_temp:="" 
 {This is a string (prefix @ for strings in kontakt) used to add leading 0s after decimal point
  e.g. if the offset in millicents is 50055, then we want to return the string 50.055 
  then the calculated $vksr_temp will be 55, so the string @vksr_temp needs to be "0" 
  }
 if($vksr_temp<$vksr_temp2)
  @vksr_temp:="0"
 end if
 if($vksr_temp<$vksr_temp2/10)
  @vksr_temp:="00"
 end if
 if($vksr_temp=0)
  @vksr_temp:=""
 end if
 
 { now deal with case e.g. offset%vksr_1000 is 500 want to show only the single digit 5 after decimal point
 i.e. need to remove trailing 0s
 }
 if($vksr_temp mod 100 = 0)
  $vksr_temp:=$vksr_temp/100
 end if
 if($vksr_temp mod 10 = 0)
  $vksr_temp:=$vksr_temp/10
 end if
 @vksr_cents_or_ratio_for_offset_in_milliseconds:=$vksr_offset_in_millicents/1000 & "."& @vksr_temp  & $vksr_temp
 if($vksr_value_edit_digits_precision=0)
  $vksr_temp:=$vksr_offset_in_millicents/1000 + $vksr_temp
  @vksr_cents_or_ratio_for_offset_in_milliseconds:=$vksr_temp & ""
 end if
 
end function

function find_1o1
 
 {idea here, user can set the 1/1 by setting denum and denom to 1 - and then if they change it later
 to a scale without a 1/1, doesn't matter, remembers this value
 }
 
 $vksr_i:=0
 while ($vksr_i<128)
  if(%vksr_denum_for_output_note[$vksr_i]=1 and %vksr_denom_for_output_note[$vksr_i]=1)
   $vksr_1o1_millicents:=%vksr_output_note[$vksr_i]*$vksr_SEMITONE_IN_MILLICENTS...
                       +%vksr_pitch_bend_for_output_note[$vksr_i]   
  end if 
  $vksr_i:=$vksr_i+1
 end while 
 
end function

function find_vksr_istart_for_tuning_definition_string
 
 $vksr_istart:=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60
 
 if( $vksr_menu_scroll_scale= $vksr_SCROLL_SCALE_TO_SELECTED_NOTE)
  $vksr_istart:=$vksr_value_edit_scroll_scale_midi_start
 end if
 
 if( $vksr_menu_scroll_scale= $vksr_SCROLL_SCALE_ON_EDIT and $vksr_Note_user_edited>=0)
  $vksr_istart:=$vksr_Note_user_edited-$vksr_value_edit_scroll_scale_show_before
 end if
 if( $vksr_menu_scroll_scale=$vksr_SCROLL_SCALE_ON_PLAY and $vksr_Note_played>=0)
  $vksr_istart:=$vksr_Note_played-$vksr_value_edit_scroll_scale_show_before
 end if
 if( $vksr_menu_scroll_scale= $vksr_SCROLL_SCALE_ON_BOTH)
  $vksr_istart:=$vksr_knob_Note-$vksr_value_edit_scroll_scale_show_before
 end if  
 
 if($vksr_istart<0)
  $vksr_istart:=0
 end if
 
 if($vksr_istart>120)
  $vksr_istart:=120
 end if 
 
end function


{code inlined from KSPMathV600, see http://www.bigbobsmusicworld.com/kontakt-scripts/math-library}
function kspML__NLZ
 $kspML_M_Out:=1
 select ($kspML_M_In)
  case 1 to 127
   $kspML_M_Out:=25
  case 128 to 4095
   $kspML_M_Out:=19
  case 4096 to 262143
   $kspML_M_Out:=13
  case 262144 to 16777215
   $kspML_M_Out:=7
 end select
 select (sh_left($kspML_M_In,$kspML_M_Out-1))
  case 16777216 to 33554431
   $kspML_M_Out:=$kspML_M_Out+6
  case 33554432 to 67108863
   $kspML_M_Out:=$kspML_M_Out+5
  case 67108864 to 134217727
   $kspML_M_Out:=$kspML_M_Out+4
  case 134217728 to 268435455
   $kspML_M_Out:=$kspML_M_Out+3
  case 268435456 to 536870911
   $kspML_M_Out:=$kspML_M_Out+2
  case 536870912 to 1073741823
   $kspML_M_Out:=$kspML_M_Out+1
 end select
end function

function kspML__Lg
 if ($kspML_M_In<=0)
  $kspML_M_Out:=080000000h
 else
  call kspML__NLZ
  dec($kspML_M_Out)
  $kspML__nx:=sh_left($kspML_M_In,$kspML_M_Out)
  $kspML_M_Out2:=30-$kspML_M_Out
  if ($kspML__nx=1073741824)
   $kspML_M_Out:=0
  else
   $kspML_M_Out:=999999312
   $kspML__n2:=1
   while ($kspML__n2<=20 and ($kspML__nx>=0))
    $kspML__sx:=sh_right($kspML__nx,$kspML__n2)
    if (-$kspML__nx-$kspML__sx<0)
     $kspML__nx:=$kspML__nx+$kspML__sx
     $kspML_M_Out:=$kspML_M_Out-%kspML_M_Logs[$kspML__n2]
    end if
    inc($kspML__n2)
   end while
   $kspML_M_Out:=($kspML_M_Out+500)/1000
  end if
 end if
end function

function kspML__Log2
 call kspML__Lg
 if ($kspML_M_Out>=0)
  $kspML_M_Out:=$kspML_M_Out+($kspML_M_Out2*1000000)
 end if
end function

{end of code inlined from KSPMathV600, see http://www.bigbobsmusicworld.com/kontakt-scripts/math-library}

function vksr_Log2_arg
 
 if($vksr_arg<$vksr_max_log2_n)
  $vksr_result:=%vksr_log2_n[$vksr_arg]
 else
  { calculates Log2($vksr_arg)*1000000 }
  $kspML_M_In:=$vksr_arg
  call kspML__Log2
  $vksr_result:=$kspML_M_Out 
 end if
 
end function

function vksr_convert_ratio_to_millicents
 {set $vksr_denum and $vksr_denom before calling this }
 {returns value as $vksr_offset_in_millicents}
 if($vksr_denum<128 and $vksr_denom<128 and %vksr_calculated_millicents_for_ratio[$vksr_denum*128+$vksr_denom]#0)
  {speed up optimization}
  $vksr_result:=%vksr_millicents_for_ratio[$vksr_denum*128+$vksr_denom]
 else  
  { Calculate: 1200 *1000 * ( Log2($vksr_value_edit_denum) - log2($vksr_value_edit_denom)) }
  $vksr_result:=0
  $vksr_arg:=$vksr_denum
  call vksr_Log2_arg
  $vksr_temp:=$vksr_result
  
  $vksr_arg:=$vksr_denom
  call vksr_Log2_arg
  $vksr_temp2:=$vksr_result
  if($vksr_temp2>=0 and $vksr_temp>=0)
   
   { $vksr_temp:=12*($vksr_temp-$vksr_temp2)}
   { $vksr_offset_in_millicents:=$vksr_temp}
   $vksr_temp:=12*($vksr_temp-$vksr_temp2) 
   $vksr_result:=$vksr_temp/10 {i.e. * $vksr_SEMITONE_IN_MILLICENTS/1000000}
  end if
  if($vksr_denum<128 and $vksr_denom<128)
   %vksr_calculated_millicents_for_ratio[$vksr_denum*128+$vksr_denom]:=1
   %vksr_millicents_for_ratio[$vksr_denum*128+$vksr_denom]:=$vksr_result
  end if
 end if 
end function

function vksr_find_gcd_u_v
 {call with $vksr_u and vksr_v
 returns gcd$vksr_result
 }
 $vksr_result:=0
 
 {GCD(0,v) == v; GCD(u,0) == u, GCD(0,0) == 0 }
 if ($vksr_u= 0)
  $vksr_result:=$vksr_v
 end if
 if ($vksr_v= 0)
  $vksr_result:=$vksr_u
 end if
 
 if($vksr_u#0 and $vksr_v#0)
  $vksr_gcd_common_factors_of_2:=1
  
  while ($vksr_u mod 2 = 0 and $vksr_v mod 2 = 0)
   $vksr_u:=$vksr_u/2
   $vksr_v:=$vksr_v/2
   $vksr_gcd_common_factors_of_2:=$vksr_gcd_common_factors_of_2*2
  end while
  
  while ($vksr_u mod 2 = 0) {remove non common factors of 2 in $vksr_u}
   $vksr_u:=$vksr_u/2
  end while
  
  {From here on, $vksr_u is always odd.}
  while ($vksr_v#0)
   
   while ($vksr_v mod 2 = 0) {remove non common factors of 2 in v}
    $vksr_v:=$vksr_v/2
   end while   {v#0 here so while loop will terminate}
   
   {Now $vksr_u and v are both odd. Swap if necessary so $vksr_u <= v}
   if ($vksr_u > $vksr_v) 
    $vksr_t:=$vksr_v
    $vksr_v:=$vksr_u 
    $vksr_u:=$vksr_t
   end if 
   {If $vksr_u and v are both odd, and v = $vksr_u, then gcd(v, $vksr_u) = gcd((v - $vksr_u)/2, $vksr_u)}
   $vksr_v:=$vksr_v - $vksr_u {must be even}
  end while
  {restore common factors of 2}
  $vksr_result:=$vksr_u * $vksr_gcd_common_factors_of_2  
 end if {$vksr_u#0 and vksr_v#0}
 { message("u " & $vksr_u & " v " & $vksr_v & " 2f " & $vksr_gcd_common_factors_of_2 & " result " & $vksr_result)}
end function


function vksr_remove_common_multiples_denum_denom
 
 if($vksr_denum>0 and $vksr_denom > 0)
  $vksr_u:=$vksr_denum
  $vksr_v:=$vksr_denom
  call vksr_find_gcd_u_v
  if($vksr_result#0 and $vksr_denum mod $vksr_result = 0 and $vksr_denom mod $vksr_result = 0)
   $vksr_denum:=$vksr_denum/$vksr_result
   $vksr_denom:=$vksr_denom/$vksr_result
  end if
 end if 
 
end function

function vksr_convert_millicents_to_ratio
 
 {set $vksr_offset_in_millicents before calling this }
 {returns value as $vksr_denum and $vksr_denom}
 {Not optimized so best called in response to user action rather than in big while loop}
 if($vksr_menu_detect_ratios=0)
  $vksr_denum:=0
  $vksr_denom:=0
 else {$vksr_convert_millicents_to_ratio#0}
     $vksr_ii:=1
     
     {FIRST BUILD TABLE OF PRIME LIMIT NUMBERS IF NECESSARY - ONLY NEED TO DO ONCE OR IF USER CHANGES PRIME LIMIT}
     if($vksr_ok_for_n_limit_up_to_date=0)
      {check to find out which numbers are okay to the prime limit i.e. only divisible by 
       primes less than $vksr_value_edit_ratios_prime_limit
      }
      $vksr_ok_for_n_limit_up_to_date:=1
      @vksr_temp:=""
      if($vksr_value_edit_ratios_prime_limit=0)
       while ($vksr_ii<128)
        %vksr_ok_for_n_limit[$vksr_ii]:=1  
        @vksr_temp:=@vksr_temp & $vksr_ii & ", "
        $vksr_ii:=$vksr_ii+1
       end while
      else
       while ($vksr_ii<128)
        %vksr_ok_for_n_limit[$vksr_ii]:=0
        $vksr_temp:=$vksr_ii
        $vksr_jj:=0
        while ($vksr_jj<=$vksr_max_primes and %vksr_primes[$vksr_jj]<128 and %vksr_primes[$vksr_jj]<=$vksr_value_edit_ratios_prime_limit)
         if($vksr_temp mod %vksr_primes[$vksr_jj] = 0)
          $vksr_temp:=$vksr_temp/%vksr_primes[$vksr_jj]
         else
          $vksr_jj:=$vksr_jj+1
         end if
        end while
        if($vksr_temp=1)
         @vksr_temp:=@vksr_temp & $vksr_ii & ", "
         %vksr_ok_for_n_limit[$vksr_ii]:=1
        end if
        $vksr_ii:=$vksr_ii+1
       end while
      end if
      set_text($vksr_label_ratios_prime_limit,@vksr_temp)
     end if {$vksr_ok_for_n_limit_up_to_date=0 for  TABLE OF PRIME LIMIT NUMBERS}
     
     $vksr_temp:=0
     $vksr_ii:=1
     $vksr_jj:=1
     $vksr_denum:=0
     $vksr_denom:=0
     {try all ratios from 1/1 to 128/128 - this is 16384 iterations so well withiun the kontakt limit of
     49999 iterations
     }
     while ($vksr_ii<128)
      if(%vksr_ok_for_n_limit[$vksr_ii]#0)
       $vksr_jj:=1 
       while ($vksr_jj<128)
        if(%vksr_ok_for_n_limit[$vksr_jj]#0)
         
         if($vksr_denum=0 or $vksr_denom=0)
          $vksr_denum:=$vksr_ii
          $vksr_denom:=$vksr_jj
          call vksr_convert_ratio_to_millicents
          
          if($vksr_menu_show_diagnostics#0)
           {
           if($vksr_denum=3)
            if($vksr_denom=2)
             message(" test: offset " & $vksr_offset_in_millicents ...
                     & " ratio " & $vksr_denum...
                     & "/" & $vksr_denom & " is " & $vksr_result )
            end if
           end if
           }
          end if
          
          if($vksr_result>($vksr_offset_in_millicents+$vksr_value_edit_ratio_recognition_sensitivity)...
             or $vksr_result<($vksr_offset_in_millicents-$vksr_value_edit_ratio_recognition_sensitivity)...
                              )
           $vksr_denum:=0
           $vksr_denom:=0
          else
           if($vksr_menu_show_diagnostics#0)
            {
            if($vksr_denum=3)
             if($vksr_denom=2)
              message(" test: offset " & $vksr_offset_in_millicents ...
                      & " ratio " & $vksr_denum...
                      & "/" & $vksr_denom & " is " & $vksr_result )
             end if
            end if
            }
           end if
          end if
         end if {$vksr_denum=0 or $vksr_denom=0}
        end if {vksr_ok_for_n_limit[$vksr_jj]#0}
        $vksr_jj:=$vksr_jj+1
       end while
      end if {%vksr_ok_for_n_limit[$vksr_ii]#0}
      $vksr_ii:=$vksr_ii+1
     end while    
     call vksr_remove_common_multiples_denum_denom
 end if  {$vksr_convert_millicents_to_ratio#0}
end function


function vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
 
 if($vksr_arg>=0 and $vksr_arg< 128)
  
  if($vksr_ok_check_knob_Note_for_find_cents_or_ratios_string#0 and $vksr_arg=$vksr_knob_Note)
   $vksr_value_edit_denum:=0
   $vksr_value_edit_denom:=0
  end if
  
  $vksr_offset_in_millicents:=%vksr_output_note[$vksr_arg]*$vksr_SEMITONE_IN_MILLICENTS+ %vksr_pitch_bend_for_output_note[$vksr_arg]
  
  if($vksr_menu_show_diagnostics#0)
   {
   if($vksr_arg=$vksr_knob_Note)
    message("note (same as user edited) " & $vksr_arg & " offset " & $vksr_offset_in_millicents & " from " & $vksr_1o1_millicents...
            & " millicents from midi 0 = " & %vksr_total_millicents_on_set_denum[$vksr_arg] ...
            & " ratio " & %vksr_denum_for_output_note[$vksr_arg]...
            & "/" & %vksr_denom_for_output_note[$vksr_arg]...
            & " user interface ratio " & $vksr_value_edit_denum & " / " &  $vksr_value_edit_denom)
   end if
   }
  end if
  
  $vksr_offset_in_millicents:=$vksr_offset_in_millicents-$vksr_1o1_millicents
  
  call vksr_find_cents_string_for_offset_in_millicents
  
  if(%vksr_denum_for_output_note[$vksr_arg]#0 and %vksr_denom_for_output_note[$vksr_arg]#0)
   if(%vksr_total_millicents_on_set_denum[$vksr_arg]=...
      $vksr_SEMITONE_IN_MILLICENTS*%vksr_output_note[$vksr_arg]+%vksr_pitch_bend_for_output_note[$vksr_arg])
    {same cents value in milliseconds as when scale was made
    so can use the denum and denom ratio value as set for the scale if non 0
    so can use the denum and denom ratio value as set for the scale if non 0
    }
    if($vksr_menu_show_diagnostics#0)
     {
     if($vksr_arg=$vksr_knob_Note)
      message("note (same as user edited) " & $vksr_arg & "millicents from midi 0 = " & %vksr_total_millicents_on_set_denum[$vksr_arg]...
              & " = 1000*"  & %vksr_output_note[$vksr_arg] & " + " & %vksr_pitch_bend_for_output_note[$vksr_arg]...
              & " ratio " & %vksr_denum_for_output_note[$vksr_arg]...
              & "/" & %vksr_denom_for_output_note[$vksr_arg])
     end if
     }
    end if
    @vksr_cents_or_ratio_for_offset_in_milliseconds:=%vksr_denum_for_output_note[$vksr_arg]...
                                                    & "/" & %vksr_denom_for_output_note[$vksr_arg]
    if($vksr_arg=$vksr_knob_Note)
     if($vksr_menu_show_diagnostics#0)
      {
      message("User edited note " & $vksr_arg & "millicents from midi 0 = " & %vksr_total_millicents_on_set_denum[$vksr_arg]...
              & " = 1000*"  & %vksr_output_note[$vksr_arg] & " + " & %vksr_pitch_bend_for_output_note[$vksr_arg]...
              & " ratio " &  %vksr_denum_for_output_note[$vksr_arg]...
              & "/" & %vksr_denom_for_output_note[$vksr_arg])
      }
     end if     
     $vksr_value_edit_denum:=%vksr_denum_for_output_note[$vksr_arg]
     $vksr_value_edit_denom:=%vksr_denom_for_output_note[$vksr_arg]
    end if
   else
    if($vksr_menu_show_diagnostics#0)
     {
     message("Note: " & $vksr_arg & "millicents from midi 0 = " & %vksr_total_millicents_on_set_denum[$vksr_arg]...
             & " # " & $vksr_SEMITONE_IN_MILLICENTS & "*" & %vksr_output_note[$vksr_arg] & " + " & %vksr_pitch_bend_for_output_note[$vksr_arg])
     }
    end if
    if($vksr_ok_check_knob_Note_for_find_cents_or_ratios_string#0 and $vksr_arg=$vksr_knob_Note)
     $vksr_value_edit_denum:=0
     $vksr_value_edit_denom:=0
    end if
    
   end if
  end if
  
  if($vksr_ok_check_knob_Note_for_find_cents_or_ratios_string#0 and $vksr_arg=$vksr_knob_Note and ($vksr_value_edit_denum=0 or $vksr_value_edit_denom=0))
   $vksr_value_edit_denum:=0
   $vksr_value_edit_denom:=0
 
  $vksr_denum:=0 {set to 0 as default - i.e. not recognized as a ratio}
  $vksr_denom:=0
  
   {Is this a recgnized ratio anyway?}
  call vksr_convert_millicents_to_ratio {- this slowed it down a lot under logic on Mac}
   
   if($vksr_menu_show_diagnostics#0)
    {
    message("Try convert user edited note to ratio: " & $vksr_knob_Note & " offset " & $vksr_offset_in_millicents...
            & " millicents from midi note 0 "...
            & %vksr_total_millicents_on_set_denum[$vksr_arg]...
            & " # " & $vksr_SEMITONE_IN_MILLICENTS & "*" & %vksr_output_note[$vksr_knob_Note] & " + " & %vksr_pitch_bend_for_output_note[$vksr_knob_Note] ...
            & " found " & $vksr_denum & "/" & $vksr_denom)
    }
   end if
   if($vksr_denum#$vksr_value_edit_denum or $vksr_denom#$vksr_value_edit_denom)
    $vksr_value_edit_denum:=$vksr_denum
    $vksr_value_edit_denom:=$vksr_denom 
   end if
  end if
 end if { $vksr_arg>=0 and $vksr_arg< 128}
end function


function vksr_find_cents_or_ratios_string_for_arg
 
 $vksr_ok_check_knob_Note_for_find_cents_or_ratios_string:=0
 call vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
 $vksr_ok_check_knob_Note_for_find_cents_or_ratios_string:=1
 
end function

function update_user_denum_denom_for_knob_Note
 
 $vksr_arg:=$vksr_knob_Note
 if($vksr_menu_show_diagnostics#0)
  {}
  message("arg " & $vksr_arg & " for check for millicents same as ratio " )
  {}
 end if
 call vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
 
end function

function vksr_update_tuning_definition_string_if_nec
 
 if($vksr_menu_show_diagnostics#0)
  {message("$vksr_tuning_definition_string_needs_update " & $vksr_tuning_definition_string_needs_update)}
 end if
 if($vksr_tuning_definition_string_needs_update#0)
  @vksr_tuning_definition_string:="..."
  
  call find_vksr_istart_for_tuning_definition_string
  call find_1o1
  
  $vksr_i:=$vksr_istart
 
  while ($vksr_i<$vksr_istart+20 and $vksr_i<128)
   
   $vksr_arg:=$vksr_i
   call vksr_find_cents_or_ratios_string_for_arg
   
   if($vksr_i=$vksr_knob_Note)
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & ",  " 
    if(%vksr_white_black_pos[$vksr_i]<0)
     
     @vksr_tuning_definition_string:=@vksr_tuning_definition_string & @vksr_highlight_black_keys_in_scale_before
    end if  
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & "*" & @vksr_cents_or_ratio_for_offset_in_milliseconds 
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & "*"
    if(%vksr_white_black_pos[$vksr_i]<0)
     @vksr_tuning_definition_string:=@vksr_tuning_definition_string & @vksr_highlight_black_keys_in_scale_after
    end if
   else
    if($vksr_i=$vksr_knob_Note+1)
     {extra space after the "*, "}
     @vksr_tuning_definition_string:=@vksr_tuning_definition_string & ",  "
    else
     @vksr_tuning_definition_string:=@vksr_tuning_definition_string & ", "
    end if
    if(%vksr_white_black_pos[$vksr_i]<0)
     @vksr_tuning_definition_string:=@vksr_tuning_definition_string & @vksr_highlight_black_keys_in_scale_before
    end if
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & @vksr_cents_or_ratio_for_offset_in_milliseconds
    if(%vksr_white_black_pos[$vksr_i]<0)
     @vksr_tuning_definition_string:=@vksr_tuning_definition_string & @vksr_highlight_black_keys_in_scale_after
    end if
   end if
   $vksr_i:=$vksr_i+1
   
  end while
  
  @vksr_tuning_definition_string:=@vksr_tuning_definition_string & "..."
  call vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
  
 end if {$vksr_tuning_definition_string_needs_update#0}
 
end function

function vksr_set_tuning_text
 
 set_text ($vksr_label_tuning_description,@vksr_tuning_description_string)
 set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
 
end function

function vksr_set_button_ResetTo12EQ_skin
 if($vksr_menu_apply_skin#0)
  if($vksr_button_ResetTo12EQ=0)
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"button")
  else
   set_control_par_str(get_ui_id($vksr_button_ResetTo12EQ), $CONTROL_PAR_PICTURE,"")
  end if
 end if
 
end function

function vksr_tuning_edited
 
 $vksr_menu_scale_tuning:=$vksr_scale_tuning_EDITED
 call vksr_set_tuning_text
 $vksr_button_ResetTo12EQ:=0 {unpress the "reset to 12 et" button}
 call vksr_set_button_ResetTo12EQ_skin
 move_control($vksr_value_edit_n_equal,0,0) 
end function

function vksr_remake_tuning_definition_user_edited
 
 @vksr_tuning_definition_string:="(user edited)"
 $vksr_tuning_definition_string_needs_update:=$vksr_tuning_string_USER_EDITED
 call vksr_update_tuning_definition_string_if_nec
 call vksr_tuning_edited
 
end function

function vksr_remake_tuning_definition_not_user_edited
 
 $vksr_tuning_definition_string_needs_update:=$vksr_tuning_string_REMAKE_NOT_USER_EDITED
 call vksr_update_tuning_definition_string_if_nec 
 call vksr_set_tuning_text 
 
end function

function vksr_set_the_ui_knobs_etc
 { calling proc should set $vksr_knob_Note, then reest is set here}
 
 $vksr_knob_To:=%vksr_output_note[$vksr_knob_Note] 
 $vksr_knob_Cents:=%vksr_pitch_bend_for_output_note[$vksr_knob_Note]
 $vksr_value_edit_Cents:=$vksr_knob_Cents
 $vksr_value_edit_Note:=$vksr_knob_Note
 $vksr_value_edit_To:=$vksr_knob_To
 {
 $vksr_value_edit_denum:=%vksr_denum_for_output_note[$vksr_knob_Note]
 $vksr_value_edit_denom:=%vksr_denom_for_output_note[$vksr_knob_Note]
 }
 if($vksr_value_edit_To<$vksr_value_edit_instrument_range_min or $vksr_value_edit_To>$vksr_value_edit_instrument_range_max)  
  message("OUT OF RANGE note set -  " & $vksr_value_edit_Note & " -> " ...
          & $vksr_value_edit_To & " - WILL BE IGNORED - INSTRUMENT RANGE " &$vksr_value_edit_instrument_range_min ...
          & " to "&  $vksr_value_edit_instrument_range_max...
          )  
  $vksr_knob_To:=0
  $vksr_knob_Cents:=0
  $vksr_value_edit_denum:=0
  $vksr_value_edit_denom:=0
 end if
 
 { Calculate the other knob values from the $vksr_knob_Cents value
 Note the $vksr_knob_CentiCents value has been declared as a ui variable range 0 to 100, so cant accept
 values greater than 100, thats why its necessary to introduce a new variable $vksr_Millicents to do
 the calculation here
 }
 if ($vksr_knob_Cents<0)
  $vksr_abscents:=-$vksr_knob_Cents
  $vksr_MilliCents:=$vksr_abscents-1000*($vksr_abscents/1000)
 else
  $vksr_abscents:=$vksr_knob_Cents
  $vksr_MilliCents:=($vksr_knob_Cents-1000*($vksr_knob_Cents/1000))
 end if
 $vksr_knob_CentiCents:=$vksr_MilliCents/10
 $vksr_knob_LastDigit:=$vksr_MilliCents-10*$vksr_knob_CentiCents
 
end function

function vksr_reset_to_12et
 if($vksr_menu_show_diagnostics#0)
  message("Reset to 12-et")
 end if  
 $vksr_i:=0  
 $vksr_temp:=32
 $vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60:=60
 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_pitch_bend_for_output_note_at_note_on[$vksr_i]:=0
  %vksr_total_millicents_on_set_denum[$vksr_i]:=0
  %vksr_denum_for_output_note[$vksr_i]:=0
  %vksr_denom_for_output_note[$vksr_i]:=0
  %vksr_output_note[$vksr_i]:=$vksr_i
  %vksr_output_note_at_note_on[$vksr_i]:=$vksr_i
  if($vksr_i mod 12 = 0)
   %vksr_denum_for_output_note[$vksr_i]:=1
   %vksr_denom_for_output_note[$vksr_i]:=$vksr_temp
   if($vksr_i>$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60)    
    %vksr_denum_for_output_note[$vksr_i]:=$vksr_temp
    %vksr_denom_for_output_note[$vksr_i]:=1
    $vksr_temp:=$vksr_temp*2
   else
    $vksr_temp:=$vksr_temp/2
   end if
   %vksr_total_millicents_on_set_denum[$vksr_i]:=$vksr_i*$vksr_SEMITONE_IN_MILLICENTS
   
   if($vksr_menu_show_diagnostics#0 and $vksr_i=0)
    {
    message("at " & $vksr_i  ...
            & " millicents from midi 0 = " & %vksr_total_millicents_on_set_denum[$vksr_i] ...
            & " ratio " & %vksr_denum_for_output_note[$vksr_i]...
            & "/" & %vksr_denom_for_output_note[$vksr_i] )
    }
   end if
   
  end if
  
  $vksr_i:=$vksr_i+1
 end while
 $vksr_button_ResetTo12EQ:=1 {press the "reset to 12 et" button}
 $vksr_menu_scale_Black:=0
 call vksr_set_button_ResetTo12EQ_skin
 @vksr_tuning_description_string:="Tuining: 12 equal"
 @vksr_tuning_definition_string:="1/1, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1100.0, 2/1"
 $vksr_menu_scale_tuning:=0
 set_text ($vksr_label_tuning_description,@vksr_tuning_description_string)
 set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
 $vksr_next_pitch_bend_extra_fine:=0
 $vksr_keyswitch_instruction:=0
 $vksr_keyswitchv2_byte_pos:=0
 $vksr_1o1_millicents:=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60*$vksr_SEMITONE_IN_MILLICENTS
 $vksr_value_edit_1o1_transpose:=0
 $vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60:=60
 $vksr_menu_1o1_pos_note_name:=0
 
 call vksr_set_the_ui_knobs_etc
 
 
end function

function vksr_set_ui_tuning_method_label
 
 if ($vksr_midi_tuning_enabled = 121)
  @vksr_tuning_method:="RPN retuning"
 end if
 if ($vksr_midi_tuning_enabled = 119) {enable_high_note_keyswitches_retuning_v2 }
  @vksr_tuning_method:="Velocity keyswitch retuning (126, 127)"
  $vksr_menu_midi_tuning_method:=1
 end if
 if ($vksr_midi_tuning_enabled = 118) {enable_high_note_keyswitches_retuning }
  @vksr_tuning_method:="Velocity keyswitch retuning (122 to 127)"
 end if
 if  ($vksr_midi_tuning_enabled = 120) { enable_controller_retuning }
  @vksr_tuning_method:="Undefined controllers retuning"
 end if
 if($vksr_midi_tuning_enabled<118 or $vksr_midi_tuning_enabled > 121)
  if($CC119_was >=118 and  $CC119_was<= 121)
   @vksr_tuning_method:="WAS: " & @vksr_tuning_method
  end if
  $vksr_menu_midi_tuning_method:=0
 end if
 $CC119_was:=$vksr_midi_tuning_enabled
 set_text ($vksr_label_tuning_method,@vksr_tuning_method)
 
end function

function vksr_retune_currently_active_note
 
 { If $vksr_input_note is currently active, retune it immediately }
 if($vksr_menu_show_diagnostics # 0) 
  {message("Retune active note - In: " & $vksr_input_note & "  not active ")} 
 end if
 if (%vksr_is_active[$vksr_input_note] # 0)
  if($vksr_menu_show_diagnostics # 0)
  {}
   if($vksr_menu_show_diagnostics_message_for_retuned_note#0)
   message("Retune active note - In: " & $vksr_input_note & "  Out on Note ON " & %vksr_output_note_at_note_on[$vksr_input_note] &...
    "  Out Now " & %vksr_output_note[$vksr_input_note] & " Bend " & %vksr_pitch_bend_for_output_note[$vksr_input_note]/1000...
      & "." & abs ( %vksr_pitch_bend_for_output_note[$vksr_input_note] mod 1000 )   )
   end if
  {}
  end if
  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 { %vksr_is_active[$vksr_input_note]#0}
 
end function

function vksr_set_input_note_and_retune_currently_active_note
 
 $vksr_input_note:=$vksr_knob_Note
 %vksr_pitch_bend_for_output_note[$vksr_input_note]:=$vksr_knob_Cents
 
 call vksr_retune_currently_active_note
 
end function

function vksr_diagnostics_message_for_current_instruction
 if($vksr_menu_show_diagnostics # 0) 
  if($vksr_show_messages_events_string#0)
   message(@vksr_messages_events_string)
  else
   {other diagnostics - put the one you want to see last or comment out the others}
   message(@vksr_current_instruction_string) 
   message(@vksr_pitch_glide_diagnostics_string)
   message(@vksr_pan_and_expression_diagnostics_string)
  end if
 end if
end function

function vksr_get_return_char_for_byte_arg
 {CONVERTS $vksr_byte_arg to the correspoonding ascii character
 as @vksr_return_char
 This is used for parsing data received as velocity keyswitches that needs to be displayed as a string in the Kontakt user interface
 - such as the scale description or scale definition strings
 }
 @vksr_return_char:=""
 if($vksr_byte_arg=32)
  @vksr_return_char:=" "
 end if
 if($vksr_byte_arg=33)
  @vksr_return_char:="!"
 end if
 if($vksr_byte_arg=34)
  @vksr_return_char:="\"" {"\""}
 end if
 if($vksr_byte_arg=35)
  @vksr_return_char:="#"
 end if
 if($vksr_byte_arg=36)
  @vksr_return_char:="$"
 end if
 if($vksr_byte_arg=37)
  @vksr_return_char:="%"
 end if
 if($vksr_byte_arg=38)
  @vksr_return_char:="&"
 end if
 if($vksr_byte_arg=39)
  @vksr_return_char:="'"
 end if
 if($vksr_byte_arg=40)
  @vksr_return_char:="("
 end if
 if($vksr_byte_arg=41)
  @vksr_return_char:=")"
 end if
 if($vksr_byte_arg=42)
  @vksr_return_char:="*"
 end if
 if($vksr_byte_arg=43)
  @vksr_return_char:="+"
 end if
 if($vksr_byte_arg=44)
  @vksr_return_char:=","
 end if
 if($vksr_byte_arg=45)
  @vksr_return_char:="-"
 end if
 if($vksr_byte_arg=46)
  @vksr_return_char:="."
 end if
 if($vksr_byte_arg=47)
  @vksr_return_char:="/"
 end if
 if($vksr_byte_arg=48)
  @vksr_return_char:="0"
 end if
 if($vksr_byte_arg=49)
  @vksr_return_char:="1"
 end if
 if($vksr_byte_arg=50)
  @vksr_return_char:="2"
 end if
 if($vksr_byte_arg=51)
  @vksr_return_char:="3"
 end if
 if($vksr_byte_arg=52)
  @vksr_return_char:="4"
 end if
 if($vksr_byte_arg=53)
  @vksr_return_char:="5"
 end if
 if($vksr_byte_arg=54)
  @vksr_return_char:="6"
 end if
 if($vksr_byte_arg=55)
  @vksr_return_char:="7"
 end if
 if($vksr_byte_arg=56)
  @vksr_return_char:="8"
 end if
 if($vksr_byte_arg=57)
  @vksr_return_char:="9"
 end if
 if($vksr_byte_arg=58)
  @vksr_return_char:=":"
 end if
 if($vksr_byte_arg=59)
  @vksr_return_char:=";"
 end if
 if($vksr_byte_arg=60)
  @vksr_return_char:="<"
 end if
 if($vksr_byte_arg=61)
  @vksr_return_char:=":="
 end if
 if($vksr_byte_arg=62)
  @vksr_return_char:=">"
 end if
 if($vksr_byte_arg=63)
  @vksr_return_char:="?"
 end if
 if($vksr_byte_arg=64)
  @vksr_return_char:="@"
 end if
 if($vksr_byte_arg=65)
  @vksr_return_char:="A"
 end if
 if($vksr_byte_arg=66)
  @vksr_return_char:="B"
 end if
 if($vksr_byte_arg=67)
  @vksr_return_char:="C"
 end if
 if($vksr_byte_arg=68)
  @vksr_return_char:="D"
 end if
 if($vksr_byte_arg=69)
  @vksr_return_char:="E"
 end if
 if($vksr_byte_arg=70)
  @vksr_return_char:="F"
 end if
 if($vksr_byte_arg=71)
  @vksr_return_char:="G"
 end if
 if($vksr_byte_arg=72)
  @vksr_return_char:="H"
 end if
 if($vksr_byte_arg=73)
  @vksr_return_char:="I"
 end if
 if($vksr_byte_arg=74)
  @vksr_return_char:="J"
 end if
 if($vksr_byte_arg=75)
  @vksr_return_char:="K"
 end if
 if($vksr_byte_arg=76)
  @vksr_return_char:="L"
 end if
 if($vksr_byte_arg=77)
  @vksr_return_char:="M"
 end if
 if($vksr_byte_arg=78)
  @vksr_return_char:="N"
 end if
 if($vksr_byte_arg=79)
  @vksr_return_char:="O"
 end if
 if($vksr_byte_arg=80)
  @vksr_return_char:="P"
 end if
 if($vksr_byte_arg=81)
  @vksr_return_char:="Q"
 end if
 if($vksr_byte_arg=82)
  @vksr_return_char:="R"
 end if
 if($vksr_byte_arg=83)
  @vksr_return_char:="S"
 end if
 if($vksr_byte_arg=84)
  @vksr_return_char:="T"
 end if
 if($vksr_byte_arg=85)
  @vksr_return_char:="U"
 end if
 if($vksr_byte_arg=86)
  @vksr_return_char:="V"
 end if
 if($vksr_byte_arg=87)
  @vksr_return_char:="W"
 end if
 if($vksr_byte_arg=88)
  @vksr_return_char:="X"
 end if
 if($vksr_byte_arg=89)
  @vksr_return_char:="Y"
 end if
 if($vksr_byte_arg=90)
  @vksr_return_char:="Z"
 end if
 if($vksr_byte_arg=91)
  @vksr_return_char:="["
 end if
 { 
  This next gives a compile error for the "\" also for "\\" which I tried - why? 
  How do you put a \ into a string in Kontakt?
  For now, just displaying the \ as a / instead
 }
 if($vksr_byte_arg=92)
  @vksr_return_char:="/"
 end if
 if($vksr_byte_arg=93)
  @vksr_return_char:="]"
 end if
 if($vksr_byte_arg=94)
  @vksr_return_char:="^"
 end if
 if($vksr_byte_arg=95)
  @vksr_return_char:="_"
 end if
 if($vksr_byte_arg=96)
  @vksr_return_char:=" "
 end if
 if($vksr_byte_arg=97)
  @vksr_return_char:="a"
 end if
 if($vksr_byte_arg=98)
  @vksr_return_char:="b"
 end if
 if($vksr_byte_arg=99)
  @vksr_return_char:="c"
 end if
 if($vksr_byte_arg=100)
  @vksr_return_char:="d"
 end if
 if($vksr_byte_arg=101)
  @vksr_return_char:="e"
 end if
 if($vksr_byte_arg=102)
  @vksr_return_char:="f"
 end if
 if($vksr_byte_arg=103)
  @vksr_return_char:="g"
 end if
 if($vksr_byte_arg=104)
  @vksr_return_char:="h"
 end if
 if($vksr_byte_arg=105)
  @vksr_return_char:="i"
 end if
 if($vksr_byte_arg=106)
  @vksr_return_char:="j"
 end if
 if($vksr_byte_arg=107)
  @vksr_return_char:="k"
 end if
 if($vksr_byte_arg=108)
  @vksr_return_char:="l"
 end if
 if($vksr_byte_arg=109)
  @vksr_return_char:="m"
 end if
 if($vksr_byte_arg=110)
  @vksr_return_char:="n"
 end if
 if($vksr_byte_arg=111)
  @vksr_return_char:="o"
 end if
 if($vksr_byte_arg=112)
  @vksr_return_char:="p"
 end if
 if($vksr_byte_arg=113)
  @vksr_return_char:="q"
 end if
 if($vksr_byte_arg=114)
  @vksr_return_char:="r"
 end if
 if($vksr_byte_arg=115)
  @vksr_return_char:="s"
 end if
 if($vksr_byte_arg=116)
  @vksr_return_char:="t"
 end if
 if($vksr_byte_arg=117)
  @vksr_return_char:="u"
 end if
 if($vksr_byte_arg=118)
  @vksr_return_char:="v"
 end if
 if($vksr_byte_arg=119)
  @vksr_return_char:="w"
 end if
 if($vksr_byte_arg=120)
  @vksr_return_char:="x"
 end if
 if($vksr_byte_arg=121)
  @vksr_return_char:="y"
 end if
 if($vksr_byte_arg=122)
  @vksr_return_char:="z"
 end if
 if($vksr_byte_arg=123)
  @vksr_return_char:="{"
 end if
 if($vksr_byte_arg=124)
  @vksr_return_char:="|"
 end if
 if($vksr_byte_arg=125)
  @vksr_return_char:="}"
 end if
 if($vksr_byte_arg=126)
  @vksr_return_char:="~"
 end if
 if($vksr_byte_arg=127)
  @vksr_return_char:=""
 end if
 
end function

function on_receive_retune_keyswitch
 
 @vksr_tuning_description_string:="Tuning: (Updated via Midi In, no description yet)"
 @vksr_tuning_definition_string:="(Updated via Midi In)"
 $vksr_Note_user_edited:=-1 {not yet edited}
 $vksr_Note_played:=-1 {not yet pleyd}
 $vksr_tuning_definition_string_needs_update:=$vksr_tuning_string_REMAKE_NOT_USER_EDITED
 
end function

function vksr_reset_denum_denom_for_input_note
 
 %vksr_total_millicents_on_set_denum[$vksr_input_note]:=0    
 %vksr_denum_for_output_note[$vksr_input_note]:=0
 %vksr_denom_for_output_note[$vksr_input_note]:=0
 if($vksr_input_note=$vksr_knob_Note)
  $vksr_value_edit_denum:=0
  $vksr_value_edit_denom:=0
 end if
 
end function

function vksr_process_tuning_keyswitches
 {WHERE THE VELOCITY KEYSWITCHES ARE DECODED}
 
 {
 - Instructions are received with $vksr_EVENT_NOTE = 127 and $vksr_EVENT_VELOCITY =  instruction (e.g. 4 for retuning)
 - When received, first records the instruction as $vksr_keyswitch_instruction
 - then in a giant switch statement, sets $vksr_keyswitchv2_byte_pos 
 - to the number of bytes of data expected for the instruction (e.g. 4 for the retuning instruction)
 
 - Data is received as $vksr_EVENT_NOTE = 126 and $vksr_EVENT_VELOCITY = data
 - or as $vksr_EVENT_NOTE = 127 and $vksr_EVENT_VELOCITY = 127 for data value 0.
 
 - Each time new data is received - and the data is recorded, then the relevant information is recorded
 - depending on the instruction and current position in $vksr_keyswitchv2_byte_pos
 - for instance if the currently active instruction is $vksr_keyswitch_instruction=4 (retuning)
 - and  $vksr_keyswitchv2_byte_pos = 4 then 
 - it's time to record the input note for current instruction: $vksr_input_note:=$vksr_data_value
 - while when $vksr_keyswitchv2_byte_pos = 3 then it's time to record
 - the output note for the current retune instruction $vksr_output_note:=$vksr_data_value
 - and so on
 
 - then $vksr_keyswitchv2_byte_pos is decremented
 
 - Finally, when $vksr_keyswitchv2_byte_pos = 0 then the instruction is executed.
 - and $vksr_keyswitchv2_byte_pos is reset to the appropriate initial value for that instruction 
 - to permit running status - so then more data can be received until instruction changes or retuning is stopped
 }
 
 
 $vksr_data_value:=-1 {initialize to -1 - gets set to non negative value if data is received}
 
 {
 if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=6)
    if($vksr_menu_show_diagnostics#0)
       @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string & " expression instruction: " ...
                                            & $vksr_EVENT_NOTE & " vel " ...
                                            & $vksr_EVENT_VELOCITY
           message(@vksr_pan_and_expression_diagnostics_string)
      end if
  end if
 if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=5)
    if($vksr_menu_show_diagnostics#0)
       @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string & " pan instruction: " ...
                                            & $vksr_EVENT_NOTE & " vel " ...
                                            & $vksr_EVENT_VELOCITY
           message(@vksr_pan_and_expression_diagnostics_string)
      end if
  end if
 }
 
{CHECK TO SEE IF THIS NOTE IS VELOCITY RETUNING DATA - IF SO SET $vksr_data_value}
if ($vksr_EVENT_NOTE=126)
  $vksr_data_value:=$vksr_EVENT_VELOCITY
 end if
 if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=127)
  $vksr_data_value:=0
 end if
 
 if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
  @vksr_messages_events_string:=@vksr_messages_events_string & " (N " & $vksr_EVENT_NOTE & ", " & $vksr_EVENT_VELOCITY & ")"
  message(@vksr_messages_events_string)
 end if
 if($vksr_menu_show_diagnostics # 0) 
  {}message($vksr_midi_tuning_enabled & " (N " & $vksr_EVENT_NOTE & ", " & $vksr_EVENT_VELOCITY & ")" )
 end if
 
 $vksr_ready_to_retune:=0
 $vksr_parsed:=0
 
 {FIRST CHECK TO MAKE SURE TUNING IS ENABLED WITH CC 119}
 
 if ($vksr_midi_tuning_enabled = 119) {enable_high_note_keyswitches_retuning_v2 }
  call vksr_set_ui_tuning_method_label
  if($vksr_menu_show_diagnostics # 0) { and $vksr_%CC_119_was # 119)}
   {' message("enable_high_note_keyswitches_retuning_v2")}
  end if
  
  { CHECK FOR INSTRUCTIONS FOR 126, 127 METHOD - E.G. 127, 4 FOR COMPLETE RETUNING MESSAGE FOR A SINGLE NOTE }
  
  { 127, 126 = Reset to 12-et
  127,4 = Retune instruction (I chose 4 because it reminds you how many data bytes follow)  
  126, input note
  126, output note
  126, coarse pitch bend
  126, fine pitch bend
  127, 127 = data value 0
  }
  if ($vksr_EVENT_NOTE#127 and $vksr_EVENT_NOTE#126)
   { not recognized as instruction or data - so stop processing any instruction in progress }
   if($vksr_menu_show_diagnostics # 0 and $vksr_keyswitch_instruction #0) { and $vksr_%CC_119_was # 119)}
    message("ignoring rest of this instruction" & $vksr_keyswitch_instruction &  " note " &  $vksr_EVENT_NOTE&  " not an instruction (127) or data (126)")
   end if
   $vksr_keyswitch_instruction:=0      
   $vksr_keyswitchv2_byte_pos:=0 
  else   { $vksr_EVENT_NOTE#127 and $vksr_EVENT_NOTE#126 }  
   if($vksr_menu_show_diagnostics # 0) 
    {message(" instr " & $vksr_keyswitch_instruction & " byte pos " & $vksr_keyswitchv2_byte_pos &" note " &  $vksr_EVENT_NOTE &  " velocity " & $vksr_EVENT_VELOCITY)}
   end if
  end if { $vksr_EVENT_NOTE#127 and $vksr_EVENT_NOTE#126 }
  
  if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY#127)
      
   { "Instructions - except for 127, 127 for data 0 case, done separately }
   
   $vksr_keyswitch_instruction:=$vksr_EVENT_VELOCITY  {Record currently active instruction}
   $vksr_keyswitchv2_byte_pos:=0
   select($vksr_keyswitch_instruction)
    case 121 {tuning description}
     @vksr_tuning_description_string:="Tuning:"
     if($vksr_menu_show_diagnostics # 0) 
      set_text ($vksr_label_tuning_method,"Start tuning description (midi instruction received): " & @vksr_tuning_description_string)
     end if
     $vksr_parsed:=1
    case 120{tuning definition}
     @vksr_tuning_definition_string:=""
     $vksr_tuning_definition_string_needs_update:=0
     if($vksr_menu_show_diagnostics # 0) 
      set_text ($vksr_label_tuning_method,"Start tuning definition (midi instruction received): " & @vksr_tuning_definition_string)
     end if
     $vksr_parsed:=1
    case 112 { ratio denominator }
     $vksr_next_denom:=0
     $vksr_next_denom_pos:=1
     $vksr_parsed:=1
    case 113 { end ratio denominator }
     %vksr_denom_for_output_note[$vksr_input_note]:=$vksr_next_denom
     %vksr_total_millicents_on_set_denum[$vksr_input_note]...
       :=$vksr_SEMITONE_IN_MILLICENTS*%vksr_output_note[$vksr_input_note]+%vksr_pitch_bend_for_output_note[$vksr_input_note]
     $vksr_parsed:=1
    case 110 { ratio numerator }
     $vksr_next_denum:=0
     $vksr_next_denum_pos:=1
     $vksr_parsed:=1 
    case 111 {end ratio numerator }
     %vksr_denum_for_output_note[$vksr_input_note]:=$vksr_next_denum
     %vksr_total_millicents_on_set_denum[$vksr_input_note]...
       :=$vksr_SEMITONE_IN_MILLICENTS*%vksr_output_note[$vksr_input_note]+%vksr_pitch_bend_for_output_note[$vksr_input_note]
     
     $vksr_parsed:=1
    case 126 { note 127, vel 126 = Reset to 12-et}
     call vksr_reset_to_12et
     $vksr_parsed:=1
    case 51
     { 127,51 = set extra fine pitch adjustment for next note to be retuned
     126, e
     }
     $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position:=1
     $vksr_parsed:=1
     if($vksr_menu_show_diagnostics#0)
      @vksr_current_instruction_string:=@vksr_current_instruction_string & " pitch bend extra fine"
     end if
    case 11  {change pan of upcoming note }
     if($vksr_ok_process_volume_and_pan_keyswitches#0)
      $vksr_keyswitchv2_byte_pos:=3 {data for change_pan(...)  as note, then coarse then fine data }
      $vksr_parsed:=1
      if($vksr_menu_show_diagnostics#0)
     {@vksr_pan_and_expression_diagnostics_string & }
     {}
        @vksr_pan_and_expression_diagnostics_string:="PAN next note " ...
            & $vksr_keyswitchv2_byte_pos & " bytes "
          message(@vksr_pan_and_expression_diagnostics_string)
       {}
      end if
     end if
    case 10  {per note pan instruction }
      if($vksr_menu_show_diagnostics#0)
      {
       @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string & " pan " ...
                                            & $vksr_keyswitchv2_byte_pos & " bytes"
                                            
           message(@vksr_pan_and_expression_diagnostics_string)
      }{" - process " & $vksr_ok_process_volume_and_pan_keyswitches}
      end if
     if($vksr_ok_process_volume_and_pan_keyswitches#0)
      $vksr_keyswitchv2_byte_pos:=3 {data for change_pan(...)  as note, then coarse then fine data }
      $vksr_parsed:=1
     end if
    case 8  {change_volume_of_next_note}
     if($vksr_ok_process_volume_and_pan_keyswitches#0)
      $vksr_keyswitchv2_byte_pos:=3 {data for change_vol(...) as note then coarse then fine data }
      if($vksr_menu_show_diagnostics#0)
      {}
       @vksr_pitch_glide_diagnostics_string:= "volume " ...
                                            & $vksr_keyswitchv2_byte_pos & " bytes "
      {}
      end if
     end if
     $vksr_parsed:=1    
    case 7  {per note volume instruction }
     if($vksr_ok_process_volume_and_pan_keyswitches#0)
      $vksr_keyswitchv2_byte_pos:=3 {data for change_vol(...) as note, then coarse then fine data }
      if($vksr_menu_show_diagnostics#0)
      {
       @vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & "volume " ...
                                            & $vksr_keyswitchv2_byte_pos & " bytes "
      }
      end if
     end if
     $vksr_parsed:=1    
    case 4  {4 byte Retune instruction }
     call on_receive_retune_keyswitch
     $vksr_keyswitchv2_byte_pos:=4
     if($vksr_menu_show_diagnostics#0)
      @vksr_tuning_description_string:="(Updated via Midi In, no description yet - 4 byte message)"
      @vksr_current_instruction_string:="retune " & $vksr_keyswitchv2_byte_pos & " bytes "
      @vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & "retune " & $vksr_keyswitchv2_byte_pos & " bytes "
     end if
     $vksr_parsed:=1
     {DECIDED NOT TO DO THIS - these were extra instructions that let you set
     - just the output note, coarse and fine pitch bend
     - or just the coarse and fine, or just the fine pitch bend
     - for the same input note as before. However this complicates the specification
     - and only reduces the amount of data significantly in rare situations such as
     - long pitch glides on a single note (as soon as you have two or more simultaneous pitch glides
     - it makes no difference - but that's exactly when you would most need to reduce amount of data)
     - retaining the code here for now commented out just in case someone needs it later on for something
    case 3
     call on_receive_retune_keyswitch
     $vksr_keyswitchv2_byte_pos:=3
     if($vksr_menu_show_diagnostics#0)
      @vksr_tuning_description_string:="(Updated via Midi In, no description yet - 3 byte message)"
      @vksr_current_instruction_string:="retune " & $vksr_keyswitchv2_byte_pos & " bytes "
      @vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & "retune " & $vksr_keyswitchv2_byte_pos & " bytes "
     end if
     $vksr_parsed:=1
    case 2
     call on_receive_retune_keyswitch
     $vksr_keyswitchv2_byte_pos:=2
     if($vksr_menu_show_diagnostics#0)
      @vksr_tuning_description_string:="(Updated via Midi In, no description yet - 2 byte message)"
      @vksr_current_instruction_string:="retune " & $vksr_keyswitchv2_byte_pos & " bytes "
      @vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & "retune " & $vksr_keyswitchv2_byte_pos & " bytes "
     end if
     $vksr_parsed:=1
    case 1
     call on_receive_retune_keyswitch
     $vksr_keyswitchv2_byte_pos:=1
     if($vksr_menu_show_diagnostics#0)
      @vksr_tuning_description_string:="(Updated via Midi In, no description yet - 1 byte message)"
      @vksr_current_instruction_string:="retune " & $vksr_keyswitchv2_byte_pos & " byte "
      @vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & "retune " & $vksr_keyswitchv2_byte_pos & " bytes "
     end if
     $vksr_parsed:=1
     }
   end select
   
   if($vksr_parsed#0)
    {
    set_text ($vksr_label_tuning_description,"Tuning: " & @vksr_tuning_description_string)
    set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
    }
    if($vksr_menu_show_diagnostics # 0) 
     {' message(" Parsed instruction: note " &  $vksr_EVENT_NOTE &  " velocity " & $vksr_keyswitch_instruction)}
    end if
   end if { $vksr_parsed#0 }
   
   if($vksr_parsed = 0)
    { not recognized as instruction or data - so stop processing any instruction in progress }
    if($vksr_menu_show_diagnostics # 0 and $vksr_keyswitch_instruction #0) { and $vksr_%CC_119_was # 119)}
     { message("End tuning with unrecognized instruction at: byte position: " & $vksr_keyswitch_instruction & " note " & $vksr_EVENT_NOTE & " velocity "  & $vksr_EVENT_VELOCITY & " unsupported or unrecognized instruction")}
     @vksr_current_instruction_string:=@vksr_current_instruction_string & " End tuning (instr: "  & $vksr_EVENT_VELOCITY  & ")"
     if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
      @vksr_messages_events_string:=@vksr_messages_events_string & "  & $vksr_EVENT_NOTE & ", " & $vksr_EVENT_VELOCITY  & ">"
     end if
     call vksr_diagnostics_message_for_current_instruction
     call vksr_update_tuning_definition_string_if_nec 
     call vksr_set_tuning_text
    end if
    call vksr_update_tuning_definition_string_if_nec 
    call vksr_set_tuning_text
    
    if($vksr_keyswitch_instruction = 100) {' end tuning and switch off tuning method}
     $vksr_midi_tuning_enabled:=0
     @vksr_current_instruction_string:=@vksr_current_instruction_string & " - switch of midi retuning "
     if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
      @vksr_messages_events_string:=@vksr_messages_events_string & " (T " & $vksr_midi_tuning_enabled & ") "
      call vksr_diagnostics_message_for_current_instruction
     end if
     call vksr_set_ui_tuning_method_label
    end if
    $vksr_keyswitchv2_byte_pos:=0
    $vksr_keyswitch_instruction:=0
    { @vksr_current_instruction_string:=""}
   end if { $vksr_parsed = 0 }
  end if { ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY#127) }
  
  { CHECK FOR EXTRA FINE PITCH BEND FOR 126, 127 METHOD }
  
  if($vksr_keyswitchv2_extra_fine_pitch_bend_byte_position=1)
   if ($vksr_data_value>=0)
    $vksr_next_pitch_bend_extra_fine:=$vksr_data_value
    $vksr_ready_to_retune:=1
   end if
   if($vksr_menu_show_diagnostics # 0)
    @vksr_current_instruction_string:=@vksr_current_instruction_string & "extra fine pb: " & $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position
    call vksr_diagnostics_message_for_current_instruction
   end if
   $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position:=0
  end if { $vksr_keyswitchv2_extra_fine_pitch_bend_byte_position=1 }
  
  if($vksr_menu_show_diagnostics # 0 and $vksr_keyswitchv2_byte_pos#0) 
   message(" $vksr_keyswitchv2_byte_pos " &  $vksr_keyswitchv2_byte_pos)
  end if
  
  { CHECK FOR DATA FOR 126, 127 METHOD - THIS WILL BE EITHER 126, N OR 127, 127 FOR DATA VALUE 0 }
  
  if($vksr_keyswitch_instruction = 112 ){ratio denominator }
   {arbitrary precision, received least significant byte first 
   - we keep track of position using $vksr_next_denom_pos
   - which starts at 1.
   - so - here first we add in the new data multiplied by $vksr_next_denom_pos
   - and then multiply $vksr_next_denom_pos by 128
   - ready for next time around
   }
   if($vksr_data_value>=0)
    $vksr_next_denom:=$vksr_next_denom+$vksr_next_denom_pos*$vksr_data_value
    $vksr_next_denom_pos:=$vksr_next_denom_pos*128
   end if
  end if  
  if($vksr_keyswitch_instruction = 110 ){ratio numerator }
   if($vksr_data_value>=0)
    
    if($vksr_menu_show_diagnostics#0)
     {@vksr_current_instruction_string:=@vksr_current_instruction_string & " calc : "...
                                      &  $vksr_next_denum  & " + "  & $vksr_next_denum_pos &  "*"  & $vksr_data_value}
    end if
    $vksr_next_denum:=$vksr_next_denum+$vksr_next_denum_pos*$vksr_data_value
    
    if($vksr_menu_show_diagnostics#0)
     {@vksr_current_instruction_string:=@vksr_current_instruction_string & " (= " & $vksr_next_denum & ")"}
    end if
    
    $vksr_next_denum_pos:=$vksr_next_denum_pos*128
   end if
  end if
  
  if($vksr_keyswitch_instruction = 121 ){tuning description}
   {receives all the bytes of string as ascii characters, end with zero}
   
   { so have to convert $vksr_data_value to a character first to add to the sring
   - done with vksr_get_return_char_for_byte_arg
   }
   
   if($vksr_data_value>0)
    
    if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
     {
     @vksr_messages_events_string:=@vksr_messages_events_string & " <<" & $vksr_EVENT_VELOCITY & ">>"
     }
    end if
    $vksr_byte_arg:=$vksr_data_value
    call vksr_get_return_char_for_byte_arg
    @vksr_tuning_description_string:=@vksr_tuning_description_string & @vksr_return_char
    set_text ($vksr_label_tuning_description,@vksr_tuning_description_string)
    
   end if
   
   if ($vksr_data_value=0)
    {the tuning description is zero terminated}
    
    set_text ($vksr_label_tuning_description,@vksr_tuning_description_string)
    $vksr_keyswitch_instruction:=0 {End of description}
    
   end if
   
  end if {$vksr_keyswitch_instruction = 121}
  
  if($vksr_keyswitch_instruction = 120 ){tuning definition string as received from midi in to show to user}
   
   if ($vksr_data_value>0)
    
    if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
     {
     @vksr_messages_events_string:=@vksr_messages_events_string & " <<" & $vksr_EVENT_VELOCITY & ">>"
     }
    end if
    $vksr_byte_arg:=$vksr_EVENT_VELOCITY
    call vksr_get_return_char_for_byte_arg
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & @vksr_return_char
    set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
    
   end if
   
   if ($vksr_data_value=0)
    
    set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
    $vksr_keyswitch_instruction:=0 {End of definition}
    
   end if
   
  end if {$vksr_keyswitch_instruction = 120}
  
  if($vksr_keyswitchv2_byte_pos#0)
   
   { Data byte for input note, output note, coarse or fine pitch bend
   depending on  $vksr_keyswitchv2_byte_pos 
   }
   if ($vksr_data_value>=0)
    select($vksr_keyswitch_instruction)
     case 11  {psn for next note }
      if($vksr_ok_process_volume_and_pan_keyswitches#0)
        if($vksr_menu_show_diagnostics#0)
     {@vksr_pan_and_expression_diagnostics_string & }
     {}
        @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string & " pos " ...
            & $vksr_keyswitchv2_byte_pos & " data " & $vksr_data_value
          message(@vksr_pan_and_expression_diagnostics_string)
       {}
      end if
     select($vksr_keyswitchv2_byte_pos)
         case 3 {input note}
         $vksr_input_note:=$vksr_data_value
       case 2 { pan, coarse}
         $vksr_adjust_pan:=$vksr_data_value*128
        case 1 {pan, fine}
         $vksr_adjust_pan:=$vksr_adjust_pan+$vksr_data_value
       end select
      end if
     case 10  {per note pan instruction }
      if($vksr_ok_process_volume_and_pan_keyswitches#0)
       select($vksr_keyswitchv2_byte_pos)
        case 3 {input note}
         $vksr_input_note:=$vksr_data_value
        case 2 { pan, coarse}
         $vksr_adjust_pan:=$vksr_data_value*128
        case 1 {pan, fine}
         $vksr_adjust_pan:=$vksr_adjust_pan+$vksr_data_value
       end select
      end if
      case 8  {volume of next note }
      if($vksr_ok_process_volume_and_pan_keyswitches#0)
       select($vksr_keyswitchv2_byte_pos)
        case 3 {input note}
         $vksr_input_note:=$vksr_data_value
       case 2 { vol, coarse}
         $vksr_adjust_volume:=$vksr_data_value*128
        case 1 {vol, fine}
         $vksr_adjust_volume:=$vksr_adjust_volume+$vksr_data_value
       end select
      end if
     case 7  {per note volume instruction }
      if($vksr_ok_process_volume_and_pan_keyswitches#0)
       select($vksr_keyswitchv2_byte_pos)
        case 3 {input note}
         $vksr_input_note:=$vksr_data_value
        case 2 { vol, coarse}
         $vksr_adjust_volume:=$vksr_data_value*128
        case 1 {vol, fine}
         $vksr_adjust_volume:=$vksr_adjust_volume+$vksr_data_value
       end select
      end if
     case 4  {4 byte retune instruction }
      select($vksr_keyswitchv2_byte_pos)
       case 4 {input note}
        $vksr_input_note:=$vksr_data_value
        call vksr_reset_denum_denom_for_input_note
        if($vksr_menu_show_diagnostics # 0)
         @vksr_current_instruction_string:=@vksr_current_instruction_string & " input note: " & $vksr_data_value & " "
         {@vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & " i. note: " & $vksr_data_value & " "}
        end if
       case 3 {output note}
        $vksr_next_output_note:=$vksr_data_value
        call vksr_reset_denum_denom_for_input_note
        if($vksr_menu_show_diagnostics # 0)
         @vksr_current_instruction_string:=@vksr_current_instruction_string & " output note: " & $vksr_data_value & " "
         {@vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & " o. note: " & $vksr_data_value & " "}
        end if
       case  2
        $vksr_next_pitch_bend_coarse:=$vksr_data_value
        $vksr_next_pitch_bend_extra_fine:=0
        call vksr_reset_denum_denom_for_input_note
        if($vksr_menu_show_diagnostics # 0)
         @vksr_current_instruction_string:=@vksr_current_instruction_string & " pb coarse: " & $vksr_data_value & " "
         {@vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & " pbc: " & $vksr_data_value & " "}
        end if
       case 1 
        $vksr_next_pitch_bend_fine:=$vksr_data_value
        $vksr_next_pitch_bend_extra_fine:=0
        call vksr_reset_denum_denom_for_input_note
        if($vksr_menu_show_diagnostics # 0)
         @vksr_current_instruction_string:=@vksr_current_instruction_string & " pb fine: " & $vksr_data_value & " "
         {@vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & " pbf: " & $vksr_data_value & " "}
        end if
      end select
    end select
    { After data recieved - Decrement byte position}
    $vksr_keyswitchv2_byte_pos:=$vksr_keyswitchv2_byte_pos-1
    if($vksr_menu_show_diagnostics # 0) 
     message(" $vksr_keyswitchv2_byte_pos " &  $vksr_keyswitchv2_byte_pos)
    end if
    { If tuning position is zero, then ready to retune }
    if ($vksr_keyswitchv2_byte_pos = 0)
     select($vksr_keyswitch_instruction)
      case 11  {pan for next note }
         if($vksr_ok_process_volume_and_pan_keyswitches#0)
             $vksr_keyswitchv2_byte_pos:=3  {for running status}
             %vksr_adjust_pan_send_next_note[$vksr_input_note]:=(2000*$vksr_adjust_pan/$vksr_14_BYTES_MAX_DATA)-1000
             %vksr_do_adjust_pan_send_next_note[$vksr_input_note]:=1
             {@vksr_pan_and_expression_diagnostics_string  &}{} 
               if($vksr_menu_show_diagnostics#0)
                       @vksr_pan_and_expression_diagnostics_string:= ...
                                                                  "change_pan for next  " & $vksr_input_note & " to " & $vksr_adjust_pan/128 & "." & ($vksr_adjust_pan mod 128) & " as " & %vksr_adjust_pan_send_next_note[$vksr_input_note] & ") "
                     message(@vksr_pan_and_expression_diagnostics_string)
                 end if
             {}
          end if
     case 10  {per note pan instruction }
         if($vksr_ok_process_volume_and_pan_keyswitches#0)
             $vksr_keyswitchv2_byte_pos:=3  {for running status}
             if (%vksr_is_active[$vksr_input_note] # 0)
                 $vksr_adjust_pan_send:=(2000*$vksr_adjust_pan/$vksr_14_BYTES_MAX_DATA)-1000
                 %vksr_input_note_pan[$vksr_input_note]:=$vksr_adjust_pan_send
                 $vksr_value_edit_Pan:=$vksr_adjust_pan_send
                 change_pan(%vksr_note_id[$vksr_input_note],$vksr_adjust_pan_send,0) 
                 set_event_par(%vksr_note_id[$vksr_input_note],$EVENT_PAR_PAN,$vksr_adjust_pan_send)
                 if($vksr_menu_show_diagnostics#0)
                 {@vksr_pan_and_expression_diagnostics_string  &}
                     @vksr_pan_and_expression_diagnostics_string:= ...
                                                                  "change_pan( " & $vksr_input_note & " to " & $vksr_adjust_pan/128 & "." & ($vksr_adjust_pan mod 128) & " as " & $vksr_adjust_pan_send & ") "
                     message(@vksr_pan_and_expression_diagnostics_string)
                  {}
                 end if
             end if    
         end if
     case 8 {volume for next note}
             if($vksr_ok_process_volume_and_pan_keyswitches#0)
                 
                 $vksr_keyswitchv2_byte_pos:=3 {for running status}
                 %vksr_adjust_volume_send_next_note[$vksr_input_note]:=$vksr_VOLUME_MAX_IN_MILLIDECIBELS * $vksr_adjust_volume/$vksr_14_BYTES_MAX_DATA
                 %vksr_do_adjust_volume_send_next_note[$vksr_input_note]:=1
                    if($vksr_menu_show_diagnostics#0)
                    {
                         @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string  & ...
                                                                      "change_vol for next " & $vksr_input_note& " to " & $vksr_adjust_volume/128 & " as " & ...
                                                                                  %vksr_adjust_volume_send_next_note[$vksr_input_note] & ") "
                         message(@vksr_pan_and_expression_diagnostics_string)
                     {}
                     end if
               end if
       case 7 {per note volume instruction }
             if($vksr_ok_process_volume_and_pan_keyswitches#0)
                 
                 $vksr_keyswitchv2_byte_pos:=3 {for running status}
                 if (%vksr_is_active[$vksr_input_note] # 0)
                     { Set to the amount to reduce volume by in millicents where data was received as range 0 to 128*128 or $vksr_14_BYTES_MAX_DATA}
                     $vksr_adjust_volume_send:=$vksr_VOLUME_MAX_IN_MILLIDECIBELS * $vksr_adjust_volume/$vksr_14_BYTES_MAX_DATA
                     $vksr_value_edit_Volume:=$vksr_adjust_volume_send
                     if($vksr_menu_show_diagnostics#0)
                     {
                         @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string  & ...
                                                                      "change_vol( " & $vksr_input_note & " to " & $vksr_adjust_volume/128 & " as " & ...
                                                                                  $vksr_adjust_volume_send & ") "
                         message(@vksr_pan_and_expression_diagnostics_string)                        
                     {}
                     end if
                     
                     {Need to convert it into a negative amount which reduces the volume in decibels of the note}
                     $vksr_adjust_volume_send:=-($vksr_VOLUME_MAX_IN_MILLIDECIBELS -$vksr_adjust_volume_send)
                     %vksr_input_note_volume[$vksr_input_note]:=$vksr_adjust_volume_send
                     
                     {Here $vksr_input_note is the note to adjust with the instruction}
                     
                     change_vol(%vksr_note_id[$vksr_input_note],$vksr_adjust_volume_send,0) 
                     set_event_par(%vksr_note_id[$vksr_input_note],$EVENT_PAR_VOLUME,$vksr_adjust_volume_send)
                 end if
             end if
      case 4  {4 byte retune instruction }
       $vksr_ready_to_retune:=1 { actual retuning done below }
       { @vksr_current_instruction_string:=@vksr_current_instruction_string & " ready to retune "}
       $vksr_keyswitchv2_byte_pos:=$vksr_keyswitch_instruction
       { example 4 for 127, 4, can then keep resending the data in groups of 4 as input, output, fine, coarse dont need to keep resending
       the insruction as well
       }
     end select
     
    end if
   end if {$vksr_data_value>=0 }
   
   call vksr_diagnostics_message_for_current_instruction
   
  end if { $vksr_keyswitchv2_byte_pos#0 }
  
 end if {$vksr_midi_tuning_enabled = 119}
 
 if ($vksr_midi_tuning_enabled = 118) {enable_high_note_keyswitches_retuning }
  call vksr_set_ui_tuning_method_label
  if($vksr_menu_show_diagnostics # 0)
   message("enable_high_note_keyswitches_retuning")
  end if
  { **** HIGH NUMBERED KEYSWITCHES METHOD TO SET TUNING **** }
  { 127, 1 = reset tuning to 12-et for entire channel
  126, v = input note
  125, v = output note
  124, v = pitch bend (coarse)
  123, v = pitch bend (fine) - and update tuning
  122, v = pitch bend (extra fine) - optional, send before 123
  where v (= $vksr_EVENT_VELOCITY) > 0
  For v = 0 use
  127, 6 = input note 0
  127, 5 = output note 0
  127, 4 = pitch bend (coarse) 0
  127, 3 = pitch bend (fine) 0
  127, 2 = pitch bend (ultra fine) 0
  }
  if ($vksr_EVENT_NOTE=127)
   if($vksr_EVENT_VELOCITY=6)
    $vksr_input_note:=0
    $vksr_next_output_note:=%vksr_output_note[$vksr_input_note] 
    {  $vksr_next_output_note:=0 { also set the output note to nn }
   end if
   if($vksr_EVENT_VELOCITY=5)
    $vksr_next_output_note:=0
   end if
   if($vksr_EVENT_VELOCITY=4)
    $vksr_next_pitch_bend_coarse:=0
   end if
   if($vksr_EVENT_VELOCITY=2)
    $vksr_next_pitch_bend_extra_fine:=0
   end if
   if($vksr_EVENT_VELOCITY=1) { reset tuning to 12-et }
    if($vksr_menu_show_diagnostics # 0)
     message("reset tuning to 12-et")
    end if 
    call vksr_reset_to_12et
   end if
  end if
  if ($vksr_EVENT_NOTE=126)
   $vksr_input_note:=$vksr_EVENT_VELOCITY
   $vksr_next_output_note:=%vksr_output_note[$vksr_input_note] 
   { $vksr_next_output_note:=$vksr_EVENT_VELOCITY { also set the output note to nn - this was just an idea, seems better to leave it as it is}
  end if
  if ($vksr_EVENT_NOTE=125)
   $vksr_next_output_note:=$vksr_EVENT_VELOCITY
  end if
  if ($vksr_EVENT_NOTE=124)
   $vksr_next_pitch_bend_coarse:=$vksr_EVENT_VELOCITY
  end if
  
  if ($vksr_EVENT_NOTE=122)
   $vksr_next_pitch_bend_extra_fine:=$vksr_EVENT_VELOCITY
  end if
  
  if ($vksr_EVENT_NOTE=123 or ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=3))
   
   $vksr_next_pitch_bend_fine:=$vksr_EVENT_VELOCITY
   if($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=3)
    $vksr_next_pitch_bend_fine:=0
   end if
   $vksr_ready_to_retune:=1
  end if
 end if {enable_high_note_keyswitches_retuning }
 if ($vksr_midi_tuning_enabled = 118 or $vksr_midi_tuning_enabled = 119) {enable_high_note_keyswitches_retuning or enable_high_note_keyswitches_retuning_v2}
  if($vksr_ready_to_retune#0)
   if($vksr_menu_show_diagnostics # 0) 
    @vksr_current_instruction_string:=@vksr_current_instruction_string & " RETUNED "
    call vksr_diagnostics_message_for_current_instruction
   end if
   
   %vksr_output_note[$vksr_input_note]:=$vksr_next_output_note  
   
   { This is the simpler calculation of pitch bend in millicents from input pitch bend }
   $vksr_pitch_bend_combined:=$vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine
   %vksr_pitch_bend_for_output_note[$vksr_input_note]...
       :=$vksr_SEMITONE_IN_MILLICENTS*($vksr_pitch_bend_combined-$vksr_INPUT_PITCH_BEND_0_AT)/($vksr_INPUT_PITCH_STEPS_IN_SEMITONE)
   if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
    { @vksr_messages_events_string:=@vksr_messages_events_string & " PB " & $vksr_pitch_bend_combined & " cents " & %vksr_pitch_bend_for_output_note[$vksr_input_note] /1000 }
   end if
   
   { This is the calculation to do to take accout of the extra fine byte for the pitch bend }
   $vksr_pitch_bend_combined:=$vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine
   { Ideally would do this, but Kontakt values are only integers not floating point so would overflow:
   $vksr_pitch_bend_combined:=($vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine)*128+$vksr_next_pitch_bend_extra_fine
   %vksr_pitch_bend_for_output_note[$vksr_input_note]:=$vksr_SEMITONE_IN_MILLICENTS*($vksr_pitch_bend_combined-$vksr_INPUT_PITCH_BEND_0_AT*128)/($vksr_INPUT_PITCH_STEPS_IN_SEMITONE*128)
   }
   { So instead do this - which could be out by perhaps 1 millicent from the correct value: }
   %vksr_pitch_bend_for_output_note[$vksr_input_note]...
       :=$vksr_SEMITONE_IN_MILLICENTS*($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] + ($vksr_SEMITONE_IN_MILLICENTS*$vksr_next_pitch_bend_extra_fine)/($vksr_INPUT_PITCH_STEPS_IN_SEMITONE*128)
   $vksr_next_pitch_bend_extra_fine:=0 { have used it, so reset now, dont want it to carry over to next note}
   call vksr_tuning_edited {unpress the "reset to 12 et" button}
   if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
    { @vksr_messages_events_string:=@vksr_messages_events_string & " PBEF " & $vksr_pitch_bend_combined & " cents " & %vksr_pitch_bend_for_output_note[$vksr_input_note] /1000 }
    { message("Note  " & $vksr_knob_Note  & " To " & $vksr_knob_To & " Bend " & $vksr_knob_Cents)}
    { message("Note  " & $vksr_knob_Note  & " To " & $vksr_knob_To & " Bend " & $vksr_knob_Cents)}
   end if
   if($vksr_menu_show_diagnostics # 0) 
    @vksr_pitch_glide_diagnostics_string:=@vksr_pitch_glide_diagnostics_string & "R " ...
                                          & $vksr_input_note & " -> "  & %vksr_output_note[$vksr_input_note] & " bend " ...
                                          & %vksr_pitch_bend_for_output_note[$vksr_input_note] & " "
    call vksr_diagnostics_message_for_current_instruction
   end if
   
   $vksr_knob_Note:=$vksr_input_note
   call vksr_set_the_ui_knobs_etc
   call vksr_retune_currently_active_note 
  end if { $vksr_ready_to_retune#0 }
 end if  {$vksr_midi_tuning_enabled = 118 or $vksr_midi_tuning_enabled = 119 enable_high_note_keyswitches_retuning or enable_high_note_keyswitches_retuning_v2}
end function {vksr_process_tuning_keyswitches}



function vksr_on_release
 
 %vksr_is_active[$vksr_EVENT_NOTE]:=0
 %vksr_note_id[$vksr_EVENT_NOTE]:=0
 
end function

{**************************************************************
Undefined controller 119 is used to switch on and off tuning methods
cc 119, 118 -  vel retuning using note velocities for notes 124 to 127.
cc 119, 119 =  vel retuning using note velocities for notes 127, 126 only
cc 119, 120 =  RPN retuning
CC 119, 121 =  CC retuning
cc 119, 0 =  switch off retuning
cc 119, anything else =  If the CC value is not recognized, then switches tuning off
}

{ *** UNDEFINED CONTROLLERS RETUNING METHOD ***}
function vksr_on_controller
 if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
  @vksr_messages_events_string:=@vksr_messages_events_string & " (C " & $vksr_CC_NUM & " V " & %CC[$vksr_CC_NUM] & ")"
  message(@vksr_messages_events_string)
 end if
 if($vksr_CC_NUM = 119)
  
  {on receipt of %CC[119], switches midi tuning on if %CC[119] = 119 and otherwise switches it off}
  $vksr_midi_tuning_enabled:=%CC[119]
  call vksr_set_ui_tuning_method_label
  
 end if
 
 if  ($vksr_midi_tuning_enabled = 120) { enable_controller_retuning }
  call vksr_set_ui_tuning_method_label
  if($vksr_menu_show_diagnostics # 0)
   message("enable_controller_retuning")
  end if
  
  { 113, 0 = reset tuning to 12-et for entire channel
  114, n = input note
  115, n = output note
  116, n = pitch bend (coarse)
  117  n = pitch bend (fine) - and update tuning
  118, n = pitch bend (extra fine) - optional, send before 118
  }
  if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
   if($vksr_CC_NUM >=113)
    if($vksr_CC_NUM <= 118) 
     @vksr_messages_events_string:=@vksr_messages_events_string & " C " & $vksr_CC_NUM & " V " & %CC[$vksr_CC_NUM]
     message(@vksr_messages_events_string)
    end if
   end if
  end if
  
  if ($vksr_CC_NUM=114)
   $vksr_input_note:=%CC[114]
  end if
  if ($vksr_CC_NUM=115)
   $vksr_next_output_note:=%CC[115]
  end if
  if ($vksr_CC_NUM=116)
   $vksr_next_pitch_bend_coarse:=%CC[116]
  end if
  
  if ($vksr_CC_NUM=118)
   $vksr_next_pitch_bend_extra_fine:=%CC[118]
  end if
  
  if ($vksr_CC_NUM=117)
   
   $vksr_next_pitch_bend_fine:=%CC[117]
   %vksr_output_note[$vksr_input_note]:=$vksr_next_output_note
   
   
   { This is the simpler calculation of pitch bend in millicents from input pitch bend }
   $vksr_pitch_bend_combined:=$vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine
   %vksr_pitch_bend_for_output_note[$vksr_input_note]:=$vksr_SEMITONE_IN_MILLICENTS*($vksr_pitch_bend_combined-$vksr_INPUT_PITCH_BEND_0_AT)/($vksr_INPUT_PITCH_STEPS_IN_SEMITONE)
   if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
    
    { @vksr_messages_events_string:=@vksr_messages_events_string & " PB " & $vksr_pitch_bend_combined & " cents " & %vksr_pitch_bend_for_output_note[$vksr_input_note] /1000 }
   end if
   
   { This is the calculation to do to take accout of the extra fine byte for the pitch bend }
   $vksr_pitch_bend_combined:=$vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine
   { Ideally would do this, but Kontakt values are only integers not floating point so would overflow:
   $vksr_pitch_bend_combined:=($vksr_next_pitch_bend_coarse*128+$vksr_next_pitch_bend_fine)*128+$vksr_next_pitch_bend_extra_fine
   %vksr_pitch_bend_for_output_note[$vksr_input_note]:=$vksr_SEMITONE_IN_MILLICENTS*($vksr_pitch_bend_combined-$vksr_INPUT_PITCH_BEND_0_AT*128)/($vksr_INPUT_PITCH_STEPS_IN_SEMITONE*128)
   }
   { So instead do this - which could be out by perhaps 1 millicent from the correct value: }
   %vksr_pitch_bend_for_output_note[$vksr_input_note]:=$vksr_SEMITONE_IN_MILLICENTS*($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] + ($vksr_SEMITONE_IN_MILLICENTS*$vksr_next_pitch_bend_extra_fine)/($vksr_INPUT_PITCH_STEPS_IN_SEMITONE*128)
   call vksr_tuning_edited {unpress the "reset to 12 et" button}
   
   if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
    { @vksr_messages_events_string:=@vksr_messages_events_string & " PBEF " & $vksr_pitch_bend_combined & " cents " & %vksr_pitch_bend_for_output_note[$vksr_input_note] /1000 }
    
    { message("Note  " & $vksr_knob_Note  & " To " & $vksr_knob_To & " Bend " & $vksr_knob_Cents)}
   end if
   
   $vksr_knob_Note:=$vksr_input_note
   call vksr_set_the_ui_knobs_etc
   call vksr_retune_currently_active_note
  end if
  if ($vksr_CC_NUM=113) { reset tuning to 12-et }
   
   if($vksr_menu_show_diagnostics # 0)
    message("reset tuning to 12-et")
   end if 
   call vksr_reset_to_12et
   
  end if
 end if { enable_controller_retuning }
 
end function

{ *** UNDEFINED RPN RETUNING METHOD ***}
on rpn
 
 if ($vksr_midi_tuning_enabled = 120) {enable_rpn_retuning }
  call vksr_set_ui_tuning_method_label
  if($vksr_menu_show_diagnostics # 0)
   message("enable_rpn_retuning")
  end if
  { Uses:
  RPN 100, n = set input note to n, data gives coarse and fine pitch bend to apply to output (default is output note same as input note)
  Then 
  RPN 101, n = set output note to n, data ignored
  RPN 102, 0 = reset tuning to 12-et
  }
  if($vksr_menu_show_diagnostics # 0)
   message(" RPN " & $RPN_ADDRESS & " Value " & $RPN_VALUE)
  end if
  
  if ($RPN_ADDRESS>=(100*128) and $RPN_ADDRESS<101*128)
   { LSB part of the RPN gives the input note}
   $vksr_input_note:=$RPN_ADDRESS-(100*128)
   
   { Data for the RPN gives the bend away from the output note}
   %vksr_pitch_bend_for_output_note[$vksr_input_note]:=$vksr_SEMITONE_IN_MILLICENTS*($RPN_VALUE-$vksr_INPUT_PITCH_BEND_0_AT)/$vksr_INPUT_PITCH_STEPS_IN_SEMITONE
   call vksr_tuning_edited {unpress the "reset to 12 et" button}
   $vksr_knob_Note:=$vksr_input_note
   call vksr_set_the_ui_knobs_etc
   call vksr_retune_currently_active_note
  end if
  
  if ($RPN_ADDRESS>=101*128 and $RPN_ADDRESS<102*128)
   { LSB part of the RPN gives the input note}
   { LSB part of the Data gives the output note
   Tuning update occurs on receipt of the data for the pitch bend
   so dont retune the note here   
   }
   $vksr_input_note:=$RPN_ADDRESS-101*128
   if($vksr_menu_show_diagnostics # 0)
    message("input note " & $vksr_input_note & " output note " & $RPN_VALUE)
   end if
   %vksr_output_note[$vksr_input_note]:=$RPN_VALUE
   call vksr_tuning_edited {unpress the "reset to 12 et" button}
   
   $vksr_knob_Note:=$vksr_input_note
   call vksr_set_the_ui_knobs_etc
  end if { ($RPN_ADDRESS>=101*128 and $RPN_ADDRESS<102*128)}
  
  if ($RPN_ADDRESS=102*128)
   call vksr_reset_to_12et
  end if
  
 end if { enable_rpn_retuning }
 
end on


{**** RESPOND TO THE RESET BUTTON AND KNOBS WHEN ADJUSTED BY USER ****}

function vksr_remake_tuning_definition_and_description_user_edited
 
 {Not sure this is needed, after all menu shows "Edited"
 if($vksr_added_edited_by_user_to_tuning_string#0)   
  @vksr_tuning_description_string:="Edited: " & @vksr_tuning_description_string
  $vksr_added_edited_by_user_to_tuning_string:=1
 end if
 }
 call vksr_remake_tuning_definition_user_edited
 
end function

function vksr_set_the_ui_knobs_user_edited
 
 call vksr_set_the_ui_knobs_etc
 call update_user_denum_denom_for_knob_Note
 call vksr_remake_tuning_definition_and_description_user_edited
 
end function

function vksr_set_the_ui_knobs_for_Note
 
 $vksr_Note_user_edited:=$vksr_knob_Note
 call vksr_set_the_ui_knobs_etc
 call update_user_denum_denom_for_knob_Note
 call vksr_remake_tuning_definition_not_user_edited
 
end function

on ui_control ($vksr_value_edit_Note)
 $vksr_knob_Note:=$vksr_value_edit_Note
 call vksr_set_the_ui_knobs_for_Note
end on { on ui_control ($vksr_knob_Note) }

on ui_control ($vksr_knob_Note)
 
 call vksr_set_the_ui_knobs_for_Note
 
end on { on ui_control ($vksr_knob_Note) }

function vksr_process_To
 
 call vksr_tuning_edited {unpress the "reset to 12 et" button} 
 %vksr_output_note[$vksr_knob_Note]:=$vksr_knob_To
 if($vksr_menu_show_diagnostics # 0)
  { message("Change To - In: " & $vksr_input_note & "  Out on Note ON " & %vksr_output_note_at_note_on[$vksr_input_note] & "  Out Now " & %vksr_output_note[$vksr_input_note] & " Bend " & %vksr_pitch_bend_for_output_note[$vksr_input_note]/1000)}
 end if 
 call vksr_set_input_note_and_retune_currently_active_note
 call vksr_remake_tuning_definition_and_description_user_edited
 call vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
 call update_user_denum_denom_for_knob_Note
 
end function

on ui_control ($vksr_value_edit_To)
 
 $vksr_knob_To:=$vksr_value_edit_To
 call vksr_process_To
 
end on  { on ui_control ($vksr_knob_To) }

on ui_control ($vksr_knob_To)
 
 $vksr_value_edit_To:=$vksr_knob_To
 
 call vksr_process_To
 
end on  { on ui_control ($vksr_knob_To) }



on ui_control ($vksr_value_edit_Cents)
 
 call vksr_tuning_edited {unpress the "reset to 12 et" button}
 $vksr_knob_Cents:=$vksr_value_edit_Cents
 %vksr_pitch_bend_for_output_note[$vksr_knob_Note]:=$vksr_knob_Cents
 call update_user_denum_denom_for_knob_Note
 call vksr_set_the_ui_knobs_user_edited
 call vksr_set_input_note_and_retune_currently_active_note
 call vksr_remake_tuning_definition_and_description_user_edited
 
end on

on ui_control ($vksr_knob_Cents)
 
 call vksr_tuning_edited {unpress the "reset to 12 et" button}
 %vksr_pitch_bend_for_output_note[$vksr_knob_Note]:=$vksr_knob_Cents
 call update_user_denum_denom_for_knob_Note
 call vksr_set_the_ui_knobs_user_edited
 call vksr_set_input_note_and_retune_currently_active_note
 call vksr_remake_tuning_definition_and_description_user_edited
 
end on  { on ui_control ($vksr_knob_Cents) }

on ui_control ($vksr_knob_CentiCents)
 call vksr_tuning_edited 
 
 if ($vksr_knob_Cents<0)
  $vksr_abscents:=-$vksr_knob_Cents
  $vksr_knob_Cents:=- ( 1000*($vksr_abscents/1000) + $vksr_knob_CentiCents*10 + $vksr_knob_LastDigit )
 else
  $vksr_knob_Cents:=1000*($vksr_knob_Cents/1000) + $vksr_knob_CentiCents*10 + $vksr_knob_LastDigit
 end if
 
 { check for end of range}
 if($vksr_knob_Cents=200000)
  $vksr_knob_CentiCents:=0
 end if
 
 if($vksr_knob_Cents=-200000)
  $vksr_knob_CentiCents:=0
 end if
 $vksr_value_edit_Cents:=$vksr_knob_Cents
 call update_user_denum_denom_for_knob_Note
 call vksr_set_input_note_and_retune_currently_active_note
 call vksr_remake_tuning_definition_and_description_user_edited
 
end on { on ui_control ($vksr_knob_Cents) }

on ui_control ($vksr_knob_LastDigit)
 call vksr_tuning_edited {unpress the "reset to 12 et" button}
 
 { $vksr_knob_LastDigit has range 0 to 100 for the ui knob, but in the code here we continually take 
 the last digit so it always ends up in range 0 to 9 at the end of the ui action
 
 This makes it possible to adjust the last digit quickly with small movements of the mouse.
 The user can also pull the knob around past the 9 position which will increase the $vksr_knob_CentiCents 
 knob as well but the number will reset to a number between 0 and 9 when the
 knob is released (if this explanation is hard to follow, try it out and see how it works
 }
 
 if ($vksr_knob_Cents<0)
  $vksr_abscents:=-$vksr_knob_Cents
  $vksr_knob_Cents:=- ( 1000*($vksr_abscents/1000) + $vksr_knob_CentiCents*10 + $vksr_knob_LastDigit )
 else
  $vksr_knob_Cents:=1000*($vksr_knob_Cents/1000) + $vksr_knob_CentiCents*10 + $vksr_knob_LastDigit
 end if
 
 $vksr_knob_CentiCents:=$vksr_knob_CentiCents + $vksr_knob_LastDigit/10
 $vksr_knob_LastDigit:=$vksr_knob_LastDigit - 10*($vksr_knob_LastDigit/10)
 
 { check for end of range}
 if($vksr_knob_Cents=200000)
  $vksr_knob_CentiCents:=0
 end if
 
 if($vksr_knob_Cents=-200000)
  $vksr_knob_CentiCents:=0
 end if
 $vksr_value_edit_Cents:=$vksr_knob_Cents
 call update_user_denum_denom_for_knob_Note
 call vksr_set_input_note_and_retune_currently_active_note
 call vksr_remake_tuning_definition_and_description_user_edited
 
end on { on ui_control ($vksr_knob_CentiCents) }

function find_note_and_pitch_increment_for_offset_in_millicents
 
 $vksr_Note_increment:=$vksr_offset_in_millicents/($vksr_SEMITONE_IN_MILLICENTS)
 $vksr_pitch_increment:=$vksr_offset_in_millicents-$vksr_Note_increment*($vksr_SEMITONE_IN_MILLICENTS)
 if($vksr_pitch_increment>$vksr_SEMITONE_IN_MILLICENTS/2)
  $vksr_pitch_increment:=$vksr_pitch_increment-$vksr_SEMITONE_IN_MILLICENTS
  $vksr_Note_increment:=$vksr_Note_increment+1
 end if
 if($vksr_pitch_increment<-$vksr_SEMITONE_IN_MILLICENTS/2)
  $vksr_pitch_increment:=$vksr_pitch_increment+$vksr_SEMITONE_IN_MILLICENTS
  $vksr_Note_increment:=$vksr_Note_increment-1
 end if
 
end function


function vksr_convert_ratio_to_vksr_offset_in_millicents
 
 {set $vksr_denum and $vksr_denom before calling this }
 {returns value as $vksr_offset_in_millicents}
 
 call vksr_convert_ratio_to_millicents
 
 $vksr_offset_in_millicents:=$vksr_result
 
end function

function on_denum_denom_changed
 
 if($vksr_value_edit_denum>0 and $vksr_value_edit_denom>0)
  
  $vksr_denum:=$vksr_value_edit_denum
  $vksr_denom:=$vksr_value_edit_denom
  
  call vksr_convert_ratio_to_vksr_offset_in_millicents
  
  {calculate as offset from midi note 0}
  
  $vksr_offset_in_millicents:=$vksr_1o1_millicents+$vksr_offset_in_millicents
  
  call find_note_and_pitch_increment_for_offset_in_millicents
  message("offset " & $vksr_offset_in_millicents & " note increment " & $vksr_Note_increment & " pitch increment " &$vksr_pitch_increment )
  
  %vksr_output_note[$vksr_knob_Note]:=$vksr_Note_increment 
  %vksr_pitch_bend_for_output_note[$vksr_knob_Note]:=$vksr_pitch_increment      
  %vksr_total_millicents_on_set_denum[$vksr_knob_Note]:=...
                                                      $vksr_SEMITONE_IN_MILLICENTS*%vksr_output_note[$vksr_knob_Note]+%vksr_pitch_bend_for_output_note[$vksr_knob_Note]
  call vksr_tuning_edited
  call vksr_set_the_ui_knobs_user_edited
  call vksr_set_input_note_and_retune_currently_active_note
  call vksr_remake_tuning_definition_and_description_user_edited
 end if
 
end function

on ui_control ($vksr_value_edit_denum)
 
 %vksr_denum_for_output_note[$vksr_knob_Note]:=$vksr_value_edit_denum
 call on_denum_denom_changed
 
end on

on ui_control ($vksr_value_edit_denom)
 
 %vksr_denom_for_output_note[$vksr_knob_Note]:=$vksr_value_edit_denom
 call on_denum_denom_changed  
 
end on

on ui_control ($vksr_button_ResetTo12EQ)
 
 $vksr_button_ResetTo12EQ:=1 {press the "reset to 12 et" button}
 call vksr_set_button_ResetTo12EQ_skin
 
 { This button cant be unpressed.
 Idea is to show as pressed on init, or if reset to 12 et either through 
 pressing this button or through midi in event.. 
 gets unpressed when you change the tuning of one of the notes.
 - note, if you adjust the notes individually to 12-et then wont show it as pressed
 }
 
 call vksr_reset_to_12et
 
end on { on ui_control ($vksr_button_ResetTo12EQ) }

function vksr_2_to_power_arg
 
 {finds 2^$vksr_arg}
 $vksr_result:=sh_left(1,$vksr_arg)
 {
 $vksr_temp2:=$vksr_arg
 $vksr_temp:=1
 while ($vksr_arg>=1)
  $vksr_temp:=$vksr_temp*2
  $vksr_arg:=$vksr_arg-1
 end while
 $vksr_result:=$vksr_temp
 $vksr_arg:=$vksr_temp2
 } {restore arg so can be reused}
end function

function vksr_exp_4_to_power_arg
 
 $vksr_result:=sh_left(1,$vksr_arg*2)
 {
 $vksr_temp2:=$vksr_arg
 $vksr_temp:=1
 while ($vksr_arg>=1)
  $vksr_temp:=$vksr_temp*4
  $vksr_arg:=$vksr_arg-1
 end while
 $vksr_result:=$vksr_temp
 $vksr_arg:=$vksr_temp2 
 }{restore arg so can be reused}
end function

function vksr_exp_3_to_power_arg
 $vksr_temp2:=$vksr_arg
 $vksr_temp:=1
 while ($vksr_arg>=1)
  $vksr_temp:=$vksr_temp*3
  $vksr_arg:=$vksr_arg-1
 end while
 $vksr_result:=$vksr_temp
 $vksr_arg:=$vksr_temp2 {restore arg so can be reused}
end function

function vksr_make_scale
 
 $vksr_added_edited_by_user_to_tuning_string:=0
 $vksr_1o1_pos:=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60 
 $vksr_1o1_pos_make_scale:=$vksr_1o1_pos
 $vksr_1o1_pos_white:=-1
 $vksr_1o1_pos_black:=-1
 $vksr_is_black_note_for_scale_on_black_keys:=0
 $vksr_scale_notes:=12 {for octave repeating scales given in tables}
 $vksr_scale_notes_black:=12
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_Xylophone_from_West_Africa)
  $vksr_scale_notes:=7
 end if
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_Seven_tone_tuning_from_Thailand)
  $vksr_scale_notes:=14
 end if
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_Modern_Pelog_scale__Dan_Schmidt)
  $vksr_scale_notes:=7
 end if
 if($vksr_menu_scale_Black=$vksr_scale_black_Pygmie_scale)
  $vksr_scale_notes_black:=5
 end if
 if($vksr_menu_scale_Black=$vksr_scale_black_Japanese_Koto_scale)
  $vksr_scale_notes_black:=5
 end if
 if($vksr_menu_scale_Black=$vksr_scale_black_Golden_horogram_3__5)
  $vksr_scale_notes_black:=5
 end if
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_Golden_horogram_9__7)
 $vksr_scale_notes:=7
 end if


 if($vksr_menu_show_diagnostics # 0)
  {message("1/1 pos " & $vksr_1o1_pos& " millicents " & $vksr_1o1_millicents)}
  { message(" 1/1 pos white " & $vksr_1o1_pos_white& " black: " &$vksr_1o1_pos_black )}
  {
  message("$vksr_menu_scale_Black " & $vksr_menu_scale_Black & " $vksr_scale_black_SAME_AS_WHITE " & $vksr_scale_black_SAME_AS_WHITE & " $vksr_scale_notes " & $vksr_scale_notes)
  }
 end if   
 
 if($vksr_menu_scale_Black#$vksr_scale_black_SAME_AS_WHITE...
    or ($vksr_menu_scale_Black=$vksr_scale_black_SAME_AS_WHITE and $vksr_scale_notes mod 12 #0)...
        ) {the or(...) part here is for scales such as Modern pelog only want to play it on white keys - need to find $vksr_1o1_pos_white}
  
  {set $vksr_1o1_pos_black (black 1/1 pos) to next after white pos}
  {- might be better to put this as control into ui?}
  if(%vksr_white_black_pos[$vksr_1o1_pos]>=0) {white}
   
   $vksr_1o1_pos_white:=$vksr_1o1_pos
   $vksr_i:=$vksr_1o1_pos  
   while ($vksr_i<128)
    if(%vksr_white_black_pos[$vksr_i]<0 and $vksr_1o1_pos_black<0)
     $vksr_1o1_pos_black:=$vksr_i
    end if
    $vksr_i:=$vksr_i+1
   end while
  else
   $vksr_1o1_pos_black:=$vksr_1o1_pos
   $vksr_i:=$vksr_1o1_pos  
   $vksr_1o1_pos_white:=-1
   while ($vksr_i<128)
    if(%vksr_white_black_pos[$vksr_i]>=0 and $vksr_1o1_pos_white<0)
     $vksr_1o1_pos_white:=$vksr_i
    end if
    $vksr_i:=$vksr_i+1
   end while
  end if
  $vksr_1o1_pos_make_scale:=$vksr_1o1_pos_white
 end if {$vksr_menu_scale_Black#$vksr_scale_black_SAME_AS_WHITE or ...}
 if($vksr_1o1_pos_white<0)
  $vksr_1o1_pos_white:=0
 end if
 if($vksr_1o1_pos_black<0)
  $vksr_1o1_pos_black:=0
 end if
 
 $vksr_1o1_millicents:=$vksr_1o1_pos*$vksr_SEMITONE_IN_MILLICENTS...
                     + $vksr_value_edit_1o1_transpose
 
 $vksr_i:=0  
 while ($vksr_i<128)
  
  $vksr_is_black_note_for_scale_on_black_keys:=0
  %vksr_is_active[$vksr_i]:=0
  %vksr_note_id[$vksr_i]:=0
  $vksr_scale_degree:=$vksr_i -$vksr_1o1_pos_make_scale
  
  if($vksr_menu_scale_Black#$vksr_scale_black_SAME_AS_WHITE)
   if(%vksr_white_black_pos[$vksr_i]<0){black notes}  {+ve for white, -ve for black}
    $vksr_is_black_note_for_scale_on_black_keys:=1
   end if
  end if
  
  $vksr_scale_notes_for_i:=$vksr_scale_notes 
  
  
  if($vksr_is_black_note_for_scale_on_black_keys#0)
   $vksr_scale_notes_for_i:=$vksr_scale_notes_black
  end if  
  
  if($vksr_menu_scale_Black#$vksr_scale_black_SAME_AS_WHITE)
   if($vksr_is_black_note_for_scale_on_black_keys#0)
    $vksr_scale_degree:=-(%vksr_white_black_pos[$vksr_i]-%vksr_white_black_pos[$vksr_1o1_pos_black])
   else
    if($vksr_scale_notes_for_i mod 12 #0)
     $vksr_scale_degree:=(%vksr_white_black_pos[$vksr_i]-%vksr_white_black_pos[$vksr_1o1_pos_white])
    end if
   end if
  end if
  
  if($vksr_menu_scale_Black=$vksr_scale_black_SAME_AS_WHITE and $vksr_scale_notes_for_i mod 12 #0)
   {scale with different number of notes from 12, is confusing to play it consecutively for most prob. 
   as the multiples of 2/1 keep going in different places
   }
   {just use the white notes offset - and if is black key do same as previous white}
   $vksr_1o1_pos_make_scale:=$vksr_1o1_pos_white
   if(%vksr_white_black_pos[$vksr_i]<0) {black key}
    $vksr_scale_degree:=$vksr_scale_degree_was
   else  {white key}
    $vksr_scale_degree:=(%vksr_white_black_pos[$vksr_i]-%vksr_white_black_pos[$vksr_1o1_pos_white])
   end if
  end if
  $vksr_scale_degree_was:=$vksr_scale_degree
  

  $vksr_offset_mod_nnotes:=( $vksr_scale_degree+ (127/$vksr_scale_notes_for_i+1)*$vksr_scale_notes_for_i) {This just a multiple of $vksr_scale_notes to make it positive} ... 
                            mod $vksr_scale_notes_for_i { added multiple of $vksr_scale_notes enough to be sure it is positive}
  
  
  if($vksr_menu_scale_Black#$vksr_scale_black_SAME_AS_WHITE and $vksr_scale_notes_for_i#12)
   {so for the option to play different scales on black and white,
   white notes play consecutive notes of scale
   so override calc above of 
   - except don't go in here for 12 tone scales, as it makes more sense to  just tune
   - the white notes normally and in that case prev calc of $vksr_offset_mod_nnotes is okay
   }
   $vksr_offset_mod_nnotes:=$vksr_scale_degree mod $vksr_scale_notes_for_i
   if($vksr_offset_mod_nnotes<0)
    $vksr_offset_mod_nnotes:=$vksr_offset_mod_nnotes+ $vksr_scale_notes_for_i
   end if
  end if
  
  {Find the root denom and denum for this octave repeat of the scale}
  { This is the 1/4, 1/2, 1/1, 2/1, 4/1 etc }
  $vksr_denom:=1
  $vksr_denum:=1
  {find denom and denum by counting number of repeats of the scale 
  (i.e. number of multiples of $vksr_scale_notes_for_i)
  away from $vksr_1o1_pos_make_scale
  and multiplying or dividing by 2 the requisite number of times
  }
  if($vksr_i<$vksr_1o1_pos_make_scale) {below the 1/1}
   $vksr_arg:=(-$vksr_scale_degree)/$vksr_scale_notes_for_i
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_Seven_tone_tuning_from_Thailand)

    call vksr_exp_4_to_power_arg {it's a 2 octave scale}
   else
    call vksr_2_to_power_arg
   end if         
   $vksr_denom:=$vksr_result
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_Seven_tone_tuning_from_Thailand)
   end if
  else {above the 1/1}
   $vksr_arg:=$vksr_scale_degree/$vksr_scale_notes_for_i
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_Seven_tone_tuning_from_Thailand)
    call vksr_exp_4_to_power_arg {it's a 2 octave scale}
   else
    call vksr_2_to_power_arg
   end if  
   $vksr_denum:=$vksr_result
  end if
  if($vksr_menu_show_diagnostics # 0 and $vksr_i=48)
   {
   message("i " & $vksr_i & " black or white pos " & %vksr_white_black_pos[$vksr_i]  & " offset " & $vksr_scale_degree & " offset for black " & $vksr_scale_degree...
           & " 1/1 black pos " & $vksr_1o1_pos_black & " value " & %vksr_white_black_pos[$vksr_1o1_pos_black]...
           & " offset mod notes " & $vksr_offset_mod_nnotes & " root " & $vksr_denum & "/" & $vksr_denom ...
          )
   {}
  end if
    
  if ($vksr_offset_mod_nnotes=0)
   if($vksr_scale_notes_for_i=12)
    $vksr_offset_in_millicents:=$vksr_SEMITONE_IN_MILLICENTS*$vksr_scale_degree
   else
    {use our $vksr_denum and $vksr_denom to calculate the offset}
    call vksr_convert_ratio_to_vksr_offset_in_millicents
   end if
  end if
  
  if($vksr_offset_mod_nnotes#0)
   
   if($vksr_is_black_note_for_scale_on_black_keys#0)
    if($vksr_menu_scale_Black=$vksr_scale_black_Pygmie_scale)
     $vksr_denom:=$vksr_denom*%vksr_scale_Pygmie_scale_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Pygmie_scale_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Pygmie_scale[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Pygmie scale"
     @vksr_tuning_definition_string:="8/7 21/16 3/2 7/4 2/1"
    end if
    
     if($vksr_menu_scale_Black=$vksr_scale_black_Golden_horogram_3__5)
     $vksr_denom:=$vksr_denom*%vksr_scale_Golden_horogram_3__5_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Golden_horogram_3__5_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Golden_horogram_3__5[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Golden horogram 3, 5"
     @vksr_tuning_definition_string:="259.86  519.7  779.55  1039.4  2/1"
    end if   
    
    if($vksr_menu_scale_Black=$vksr_scale_black_Japanese_Koto_scale)
     $vksr_denom:=$vksr_denom*%vksr_scale_Japanese_Koto_scale_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Japanese_Koto_scale_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Japanese_Koto_scale[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Japanese Koto scale"
     @vksr_tuning_definition_string:="9/8 6/5 3/2 8/5 2/1"
    end if
    
   else {case $vksr_is_black_note_for_scale_on_black_keys=0}
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Xylophone_from_West_Africa)
     $vksr_denom:=$vksr_denom*%vksr_scale_Xylophone_from_West_Africa_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Xylophone_from_West_Africa_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Xylophone_from_West_Africa[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Xylophone from West Africa"
     @vksr_tuning_definition_string:="152.0  287.0  533.0  724.0  890.0  1039.0  2/1"
    end if
    
      {note this uses /24 for $vksr_offset_in_millicents because it is a 2 octave scale}
      {$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/24}
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Seven_tone_tuning_from_Thailand)
     $vksr_denom:=$vksr_denom*%vksr_scale_Seven_tone_tuning_from_Thailand_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Seven_tone_tuning_from_Thailand_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Seven_tone_tuning_from_Thailand[$vksr_offset_mod_nnotes]+2*$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/24)
     {note this is /24 because it is a 2 octave scale}
     if($vksr_i<$vksr_1o1_pos_make_scale)
      $vksr_offset_in_millicents:=$vksr_offset_in_millicents-$vksr_OCTAVE_IN_MILLICENTS
     end if
     @vksr_tuning_description_string:="Seven tone tuning from Thailand (two octaves)"
     @vksr_tuning_definition_string:="129.096  262.077  415.113  703.196  804.353  984.182  2/1 1329.1  1462.08  1616.58  1904.44  2002.01  2186.29  4/1"
    end if
     
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Golden_horogram_9__7)
     $vksr_denom:=$vksr_denom*%vksr_scale_Golden_horogram_9__7_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Golden_horogram_9__7_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Golden_horogram_9__7[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Golden horogram 9, 7"
     @vksr_tuning_definition_string:="81.32  362.55  543.97  725.29  906.61  1087.94  2/1"
    end if
  
    if($vksr_menu_show_diagnostics # 0 and $vksr_i=48)
   {
   message("i " & $vksr_i & " black or white pos " & %vksr_white_black_pos[$vksr_i]  & " offset " & $vksr_scale_degree & " offset for black " & $vksr_scale_degree...
           & " 1/1 black pos " & $vksr_1o1_pos_black & " value " & %vksr_white_black_pos[$vksr_1o1_pos_black]...
           & " offset mod notes " & $vksr_offset_mod_nnotes & " root " & $vksr_denum & "/" & $vksr_denom ...
           & " scale val " & %vksr_scale_Seven_tone_tuning_from_Thailand[$vksr_offset_mod_nnotes] ...
           )
   }
  end if
            
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Modern_Pelog_scale__Dan_Schmidt)
     $vksr_denom:=$vksr_denom*%vksr_scale_Modern_Pelog_scale__Dan_Schmidt_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Modern_Pelog_scale__Dan_Schmidt_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Modern_Pelog_scale__Dan_Schmidt[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Modern Pelog scale (Dan Schmidt) - harmonic fragment"
     @vksr_tuning_definition_string:="11/10 6/5 7/5 3/2 8/5 9/5 2/1"
    end if
    
    
    { Octave repeating scales. The calc here doesn't work for $vksr_i mod 12 = 0 }
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Modern_Indian_Gamut___ragas)
     $vksr_denom:=$vksr_denom*%vksr_scale_Modern_Indian_Gamut___ragas_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Modern_Indian_Gamut___ragas_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Modern_Indian_Gamut___ragas[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Modern Indian Gamut - ragas"
     @vksr_tuning_definition_string:="16/15 9/8 6/5 5/4 4/3 45/32 3/2 8/5 27/16 9/5 15/8 2/1"
    end if
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Just_intonation_with_I__II__V_pure)
     $vksr_denom:=$vksr_denom*%vksr_scale_Just_intonation_with_I__II__V_pure_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Just_intonation_with_I__II__V_pure_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Just_intonation_with_I__II__V_pure[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Just intonation with I, II, V pure pure"
     @vksr_tuning_definition_string:="135/128 9/8 6/5 5/4 27/20 45/32 3/2 8/5 27/16 9/5 15/8 2/1"
    end if
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Just_intonation_with_I__IV__V_pure)
     $vksr_denom:=$vksr_denom*%vksr_scale_Just_intonation_with_I__IV__V_pure_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Just_intonation_with_I__IV__V_pure_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Just_intonation_with_I__IV__V_pure[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Just intonation with I, IV, V pure"
     @vksr_tuning_definition_string:="16/15 9/8 6/5 5/4 4/3 45/32 3/2 8/5 5/3 9/5 15/8 2/1"
    end if
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical)
     $vksr_denom:=$vksr_denom*%vksr_scale_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Werckmeister_III__1691__Bach_s_time__late_baroque_o_early_classical[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Werckmeister III (1691, Bach's time, late baroque / early classical)"
     @vksr_tuning_definition_string:="256/243 192.18  32/27 390.225  4/3 1024/729 696.09  128/81 888.27  16/9 1092.18  2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Rameau_Temperament__in_Nouveau_Systeme__1726)
     $vksr_denom:=$vksr_denom*%vksr_scale_Rameau_Temperament__in_Nouveau_Systeme__1726_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Rameau_Temperament__in_Nouveau_Systeme__1726_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Rameau_Temperament__in_Nouveau_Systeme__1726[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Rameau Temperament  in Nouveau Systeme (1726)"
     @vksr_tuning_definition_string:="92.472  193.157  302.053  5/4 503.422  587.682  696.578  797.263  889.735  1006.843  1082.892  2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Moore__representative_Victorian_well_temperament___1885)
     $vksr_denom:=$vksr_denom*%vksr_scale_Moore__representative_Victorian_well_temperament___1885_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Moore__representative_Victorian_well_temperament___1885_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Moore__representative_Victorian_well_temperament___1885[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Moore (representative Victorian well-temperament) (1885)"
     @vksr_tuning_definition_string:="98.604  9/8 302.514  399.441  4/3 603.351  3/2 800.559  897.486  1004.47  1101.4  2/1"
    end if
   

    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885)
     $vksr_denom:=$vksr_denom*%vksr_scale_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Broadwood_s_Best__Ellis_tuner_number_4___Victorian__1885[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-60)/12)
     @vksr_tuning_description_string:="Broadwood's Best (Ellis tuner number 4), Victorian (1885)"
     @vksr_tuning_definition_string:="95.965  197.99  297.955  392.98  498.945  594.97  699.995  796.96  894.985  998.95  1093.975  2/1"
    end if
    
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Lambert_Chaumont_organ_temperament__1695)
     $vksr_denom:=$vksr_denom*%vksr_scale_Lambert_Chaumont_organ_temperament__1695_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Lambert_Chaumont_organ_temperament__1695_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Lambert_Chaumont_organ_temperament__1695[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Lambert Chaumont organ temperament (1695)"
     @vksr_tuning_definition_string:="76.049  193.157  290.909  5/4 503.422  579.471  696.578  25/16 889.735  997.165  1082.892  2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Quarter_comma_mean_tone__renaissance_o_baroque)
     $vksr_denom:=$vksr_denom*%vksr_scale_Quarter_comma_mean_tone__renaissance_o_baroque_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Quarter_comma_mean_tone__renaissance_o_baroque_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Quarter_comma_mean_tone__renaissance_o_baroque[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Quarter comma mean-tone (renaissance / baroque)"
     @vksr_tuning_definition_string:="76.049  193.157  310.265  5/4 503.422  579.471  696.578  25/16 889.735  1006.843  1082.892  2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Kirnberger__1779)
     $vksr_denom:=$vksr_denom*%vksr_scale_Kirnberger__1779_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Kirnberger__1779_denums[$vksr_offset_mod_nnotes]
     
     $vksr_offset_in_millicents:=%vksr_scale_Kirnberger__1779[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Kirnberger (1779)"
     @vksr_tuning_definition_string:="256/243 193.849  32/27 386.314  4/3 45/32 696.663  128/81 889.65  16/9 15/8 2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Kirnberger_3__1779)
     $vksr_denom:=$vksr_denom*%vksr_scale_Kirnberger_3__1779_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Kirnberger_3__1779_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Kirnberger_3__1779[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Kirnberger 3 (1779)"
     @vksr_tuning_definition_string:="135/128 193.157  32/27 5/4 4/3 45/32 696.578  405/256 889.735  16/9 15/8 2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Vallotti_and_Young__1800__late_classical)
     $vksr_denom:=$vksr_denom*%vksr_scale_Vallotti_and_Young__1800__late_classical_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Vallotti_and_Young__1800__late_classical_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Vallotti_and_Young__1800__late_classical[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Vallotti and Young (1800, late classical)"
     @vksr_tuning_definition_string:="94.135  196.09  298.045  392.18  501.955  592.18  698.045  796.09  894.135  1000.0  1090.22  2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Sixth_comma_mean_tone__Mozart_s_time__classical)
     $vksr_denom:=$vksr_denom*%vksr_scale_Sixth_comma_mean_tone__Mozart_s_time__classical_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Sixth_comma_mean_tone__Mozart_s_time__classical_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Sixth_comma_mean_tone__Mozart_s_time__classical[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Sixth comma mean-tone (Mozart's time, classical)"
     @vksr_tuning_definition_string:="88.594  196.741  304.888  393.482  501.629  45/32 698.371  786.965  895.112  1003.26  1091.85  2/1"
    end if
    
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Pythagorean__Middle_ages)
     $vksr_denom:=$vksr_denom*%vksr_scale_Pythagorean__Middle_ages_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Pythagorean__Middle_ages_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Pythagorean__Middle_ages[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Pythagorean (Middle ages)"
     @vksr_tuning_definition_string:="1/1, 256/243, 9/8, 32/27, 81/64, 4/3, 729/512, 3/2, 128/81, 27/16, 16/9, 243/128, 2/1"
    end if
    
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat)
     $vksr_denom:=$vksr_denom*%vksr_scale_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_Well_temperament_Op_de_Coul__1998__Fifths_5o14__4o14_and_5o14_Pyth_comma_flat[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="Well-temperament Op de Coul, 1998. Fifths 5/14, 4/14 and 5/14 Pyth comma flat"
     @vksr_tuning_definition_string:="98.604  9/8 302.514  399.441  4/3 603.351  3/2 800.559  897.486  1004.47  1101.4  2/1"
    end if
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_David_Canright_s_7_limit_twelve_tone)
     $vksr_denom:=$vksr_denom*%vksr_scale_David_Canright_s_7_limit_twelve_tone_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_David_Canright_s_7_limit_twelve_tone_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_David_Canright_s_7_limit_twelve_tone[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="David Canright's 7 limit twelve tone"
     @vksr_tuning_definition_string:="28/27 9/8 7/6 5/4 4/3 45/32 3/2 14/9 5/3 7/4 15/8 2/1"
    end if   
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black)
     $vksr_denom:=$vksr_denom*%vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="American Gamelan Si Betty as Pelog on white, Slendro on Black"
     @vksr_tuning_definition_string:="67.367  184.672  298.754  267.713  603.902  566.475  703.919  795.496  796.613  971.688  1063.085  2/1"
    end if
    
    
    if($vksr_menu_scale_tuning=$vksr_scale_tuning_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black)
     $vksr_denom:=$vksr_denom*%vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black_denoms[$vksr_offset_mod_nnotes]
     $vksr_denum:=$vksr_denum*%vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black_denums[$vksr_offset_mod_nnotes]
     $vksr_offset_in_millicents:=%vksr_scale_American_Gamelan_Si_Betty_as_Pelog_on_white__Slendro_on_Black[$vksr_offset_mod_nnotes]+$vksr_OCTAVE_IN_MILLICENTS*(($vksr_i-$vksr_1o1_pos_make_scale)/12)
     @vksr_tuning_description_string:="American Gamelan Si Betty as Pelog on white, Slendro on Black"
     @vksr_tuning_definition_string:="67.367  184.672  298.754  267.713  603.902  566.475  703.919  795.496  796.613  971.688  1063.085  2/1"
    end if
    
   end if {case $vksr_is_black_note_for_scale_on_black_keys = 0}
   
   if($vksr_i<$vksr_1o1_pos_make_scale)
    {is out by one octave after calc unless you do this:}
    $vksr_offset_in_millicents:=$vksr_offset_in_millicents-$vksr_OCTAVE_IN_MILLICENTS
   end if
  end if {if($vksr_offset_mod_nnotes#0 0)} { Octave repeating 12 tone scales.}
  
  { Other scales - override the above calc. }
  if(($vksr_menu_scale_tuning=$vksr_scale_tuning_HARMONIC_AND_SUBHARMONICS and $vksr_is_black_note_for_scale_on_black_keys=0) ...
     or ($vksr_menu_scale_Black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS and $vksr_is_black_note_for_scale_on_black_keys#0)...
         )
   {harmonics and subharmonics}
   
   {work out denum and denom from scale degree
   example if $vksr_value_edit_first_harmonic=8
   then want (8+$vksr_scale_degree)/8 for $vksr_scale_degree>=0
   and 8/(8+$vksr_scale_degree) for $vksr_scale_degree<0
   }
   
   $vksr_temp:=$vksr_scale_degree
   $vksr_temp2:=$vksr_value_edit_first_harmonic
   if($vksr_menu_scale_Black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS and %vksr_white_black_pos[$vksr_i]<0)
    { on black keys}
    $vksr_temp2:=$vksr_value_edit_first_harmonic_black     
   end if
   if($vksr_temp>=0)
    $vksr_denum:=$vksr_temp2+$vksr_temp {$vksr_temp2:=$vksr_value_edit_first_harmonic }
    $vksr_denom:=$vksr_temp2
   else
    $vksr_temp:=-$vksr_temp
    $vksr_denom:=$vksr_temp2+$vksr_temp
    $vksr_denum:=$vksr_temp2
   end if
   
   {Now find the offset using logs - this routine converts $vksr_denum/$vksr_denom
   to $vksr_offset_in_millicents, which is what we want
   }
   call vksr_convert_ratio_to_vksr_offset_in_millicents
  end if
  
  if($vksr_is_black_note_for_scale_on_black_keys=0)
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o1)
    $vksr_denum:=0
    $vksr_denom:=0
    {Actual value of 3/1 in cents is
    1901.955000865
    and here using approximation: 1901955/1000
    }
    $vksr_offset_in_millicents:=($vksr_scale_degree*$vksr_3o1_IN_MILLICENTS)/$vksr_value_edit_n_equal
   end if
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o2)
    $vksr_denum:=0
    $vksr_denom:=0
    { here using 701955/1000 as approximation for 701.95500086}
    $vksr_offset_in_millicents:=($vksr_scale_degree*$vksr_3o2_IN_MILLICENTS)/$vksr_value_edit_n_equal
   end if
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL)
    $vksr_denum:=0
    $vksr_denom:=0
    $vksr_offset_in_millicents:=($vksr_scale_degree*$vksr_OCTAVE_IN_MILLICENTS)/$vksr_value_edit_n_equal
   end if
  end if
  
  {Separate scale on black}
  
  if($vksr_is_black_note_for_scale_on_black_keys#0)
   {same calculation as above but usees $vksr_scale_degree and $vksr_value_edit_n_equal_black}
   if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o1)
    $vksr_denum:=0
    $vksr_denom:=0
    $vksr_offset_in_millicents:=($vksr_scale_degree*$vksr_3o1_IN_MILLICENTS)/$vksr_value_edit_n_equal_black
   end if
   if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o2)
    $vksr_denum:=0
    $vksr_denom:=0
    $vksr_offset_in_millicents:=($vksr_scale_degree*$vksr_3o2_IN_MILLICENTS)/$vksr_value_edit_n_equal_black
   end if
   if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL)
    $vksr_denum:=0
    $vksr_denom:=0
    $vksr_offset_in_millicents:=($vksr_scale_degree*$vksr_OCTAVE_IN_MILLICENTS)/$vksr_value_edit_n_equal_black
   end if
  end if
  
  if($vksr_is_black_note_for_scale_on_black_keys=0)
   if($vksr_scale_tuning_12_EQUAL= $vksr_menu_scale_tuning )
    if($vksr_i mod 12 # 0)
     $vksr_denum:=0
     $vksr_denom:=0
    end if
    {twelve equal notes}
    $vksr_offset_in_millicents:=$vksr_SEMITONE_IN_MILLICENTS*($vksr_i-$vksr_1o1_pos_make_scale)
   end if
  end if
  if($vksr_menu_show_diagnostics # 0 and $vksr_i=72)
   {
   message("i " & $vksr_i & " offset " & $vksr_offset_in_millicents & " 1/1 " & $vksr_offset_in_millicents)
   }
  end if   
  {Display of 1/1, 2/1, 3/1, 3/2 etc from millicents values - can't do all ratios
  but this saves need to work them out for the equal divisions of 3/1, 3/2 and 2/1
  and can catch other examples if missed out in scales
  }
  {}
  if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL)
   if($vksr_scale_degree=0)
    @vksr_tuning_definition_string:="1/1"
   end if
   if($vksr_scale_degree>0 and $vksr_offset_in_millicents < $vksr_OCTAVE_IN_MILLICENTS)
    call vksr_find_cents_string_for_offset_in_millicents    
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & ", " & @vksr_cents_or_ratio_for_offset_in_milliseconds
   end if
   if($vksr_offset_in_millicents=$vksr_OCTAVE_IN_MILLICENTS)
    @vksr_tuning_definition_string:=@vksr_tuning_definition_string & ", 2/1"
   end if
  end if
  {}
  
  
  if($vksr_offset_in_millicents mod $vksr_3o2_IN_MILLICENTS = 0)
   if($vksr_offset_in_millicents<0)
    $vksr_denum:=2
    $vksr_denom:=3
    $vksr_arg:=-$vksr_offset_in_millicents/$vksr_3o2_IN_MILLICENTS
    $vksr_arg:=$vksr_arg-1
    call vksr_2_to_power_arg
    $vksr_denum:=$vksr_denum*$vksr_result
    call vksr_exp_3_to_power_arg
    $vksr_denom:=$vksr_denom*$vksr_result
   else
    $vksr_denum:=3
    $vksr_denom:=2
    $vksr_arg:=$vksr_offset_in_millicents/$vksr_3o2_IN_MILLICENTS
    $vksr_arg:=$vksr_arg-1
    call vksr_exp_3_to_power_arg
    $vksr_denum:=$vksr_denum*$vksr_result
    call vksr_2_to_power_arg
    $vksr_denom:=$vksr_denom*$vksr_result
   end if
  end if   
  
  if($vksr_offset_in_millicents mod $vksr_3o1_IN_MILLICENTS = 0)
   if($vksr_offset_in_millicents<0)
    $vksr_denum:=1
    $vksr_denom:=3
    $vksr_arg:=-$vksr_offset_in_millicents/$vksr_3o1_IN_MILLICENTS
    $vksr_arg:=$vksr_arg-1
    call vksr_exp_3_to_power_arg
    $vksr_denom:=$vksr_denom*$vksr_result
   else
    $vksr_denum:=3
    $vksr_denom:=1
    $vksr_arg:=$vksr_offset_in_millicents/$vksr_3o1_IN_MILLICENTS
    $vksr_arg:=$vksr_arg-1
    call vksr_exp_3_to_power_arg
    $vksr_denum:=$vksr_denum*$vksr_result
   end if
  end if
  if($vksr_offset_in_millicents mod $vksr_OCTAVE_IN_MILLICENTS = 0)
   $vksr_denom:=1
   $vksr_denum:=1
   if($vksr_offset_in_millicents<0)
    $vksr_arg:=-$vksr_offset_in_millicents/$vksr_OCTAVE_IN_MILLICENTS
    $vksr_arg:=$vksr_arg
    call vksr_2_to_power_arg
    $vksr_denom:=$vksr_result
   else
    $vksr_arg:=$vksr_offset_in_millicents/$vksr_OCTAVE_IN_MILLICENTS
    $vksr_arg:=$vksr_arg
    call vksr_2_to_power_arg
    $vksr_denum:=$vksr_result
   end if
  end if
  call vksr_remove_common_multiples_denum_denom
  
  $vksr_offset_in_millicents:=$vksr_offset_in_millicents+$vksr_value_edit_1o1_transpose
  
  call find_note_and_pitch_increment_for_offset_in_millicents
  %vksr_output_note[$vksr_i]:=$vksr_1o1_pos_make_scale+$vksr_Note_increment
  
  %vksr_pitch_bend_for_output_note[$vksr_i]:=$vksr_pitch_increment
  
  %vksr_denum_for_output_note[$vksr_i]:=$vksr_denum
  %vksr_denom_for_output_note[$vksr_i]:=$vksr_denom
  %vksr_total_millicents_on_set_denum[$vksr_i]:=...
                                              $vksr_SEMITONE_IN_MILLICENTS*%vksr_output_note[$vksr_i]+%vksr_pitch_bend_for_output_note[$vksr_i]
  %vksr_output_note_at_note_on[$vksr_i]:=%vksr_output_note[$vksr_i]
  
  $vksr_i:=$vksr_i+1
  
 end while
 
 
 $vksr_button_ResetTo12EQ:=0 {unpress the "reset to 12 et" button}
 call vksr_set_button_ResetTo12EQ_skin
 if($vksr_value_edit_Note<0 and $vksr_Note_played <0 and $vksr_1o1_pos_make_scale>=0 and $vksr_1o1_pos_make_scale<127)
  $vksr_knob_Note:=$vksr_1o1_pos_make_scale+1
 end if
 call vksr_set_the_ui_knobs_etc
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL...
    or $vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o1...
    or $vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o2...
    )
  @vksr_tuning_description_string:=$vksr_value_edit_n_equal & " equal"
 end if
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o2)
  @vksr_tuning_description_string:=@vksr_tuning_description_string & " divisions of 3/2"
 end if 
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o1)
  @vksr_tuning_description_string:=@vksr_tuning_description_string & " divisions of 3/1"
 end if 
 
 if($vksr_menu_scale_tuning=$vksr_scale_tuning_HARMONIC_AND_SUBHARMONICS)
  @vksr_tuning_description_string:="Harmonics & subharmonics ..."
  
  $vksr_temp:=$vksr_value_edit_first_harmonic+3
  $vksr_temp2:=$vksr_value_edit_first_harmonic
  while($vksr_temp>$vksr_value_edit_first_harmonic)
   $vksr_denum:=$vksr_temp2
   $vksr_denom:=$vksr_temp
   call vksr_remove_common_multiples_denum_denom
   @vksr_tuning_description_string:=@vksr_tuning_description_string & " " & $vksr_denum & "/" & $vksr_denom & ", "
   $vksr_temp:=$vksr_temp-1
  end while
  $vksr_temp:=$vksr_value_edit_first_harmonic
  $vksr_temp2:=$vksr_value_edit_first_harmonic
  while($vksr_temp<=$vksr_value_edit_first_harmonic+3)
   $vksr_denum:=$vksr_temp
   $vksr_denom:=$vksr_temp2
   call vksr_remove_common_multiples_denum_denom
   @vksr_tuning_description_string:=@vksr_tuning_description_string & " " & $vksr_denum & "/" & $vksr_denom & ", "
   $vksr_temp:=$vksr_temp+1
  end while
  @vksr_tuning_description_string:=@vksr_tuning_description_string & "..."
  
  @vksr_tuning_definition_string:="..."
  
  $vksr_temp:=$vksr_value_edit_first_harmonic+10
  $vksr_temp2:=$vksr_value_edit_first_harmonic
  while($vksr_temp>$vksr_value_edit_first_harmonic)
   $vksr_denum:=$vksr_temp2
   $vksr_denom:=$vksr_temp
   call vksr_remove_common_multiples_denum_denom
   @vksr_tuning_definition_string:=@vksr_tuning_definition_string & " " & $vksr_denum & "/" & $vksr_denom & ", "
   $vksr_temp:=$vksr_temp-1
  end while
  $vksr_temp:=$vksr_value_edit_first_harmonic
  $vksr_temp2:=$vksr_value_edit_first_harmonic
  while($vksr_temp<=$vksr_value_edit_first_harmonic+10)
   $vksr_denum:=$vksr_temp
   $vksr_denom:=$vksr_temp2
   call vksr_remove_common_multiples_denum_denom
   @vksr_tuning_definition_string:=@vksr_tuning_definition_string & " " & $vksr_denum & "/" & $vksr_denom & ", "
   $vksr_temp:=$vksr_temp+1
  end while
  @vksr_tuning_definition_string:=@vksr_tuning_definition_string & "..."
  {
  @vksr_tuning_description_string:="Harmonics & subharmonics ...4/5, 8/9, 1/1, 9/8, 5/4, 11/8,..."
  @vksr_tuning_definition_string:="...4/9, 8/17, 1/2, 8/15, 4/7, 8/13, 2/3, 8/11, 4/5, 8/9, 1/1, 9/8, 5/4, 11/8, 3/2, 13/8, 7/4, 15/8, 2/1, 17/8, 9/4..."
  }
 end if
 
 if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL)
  @vksr_tuning_description_string:="Black: " & $vksr_value_edit_n_equal_black & " equal, White: " & @vksr_tuning_description_string
 end if
 if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o1)
  @vksr_tuning_description_string:="Black: " & $vksr_value_edit_n_equal_black & " equal div. of 3/1, White: " & @vksr_tuning_description_string
 end if
 if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o2)
  @vksr_tuning_description_string:="Black: " & $vksr_value_edit_n_equal_black & " equal div. of 3/2, White: " & @vksr_tuning_description_string
 end if
 if($vksr_menu_scale_Black=$vksr_scale_black_Pygmie_scale)
  @vksr_tuning_description_string:="Black: Pygmie, White: " & @vksr_tuning_description_string
 end if 
 if($vksr_menu_scale_Black=$vksr_scale_black_Japanese_Koto_scale)
  @vksr_tuning_description_string:="Black: Japanese Koto, White: " & @vksr_tuning_description_string
 end if
 if($vksr_menu_scale_Black#$vksr_scale_black_SAME_AS_WHITE)
  call vksr_remake_tuning_definition_not_user_edited 
  {because is a mix of two scales}
  
 end if
 
 if($vksr_menu_scale_Black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS)
  @vksr_tuning_description_string:="Black: Harmonics & subh. " & ", White: " & @vksr_tuning_description_string
 end if
 
 
 set_text ($vksr_label_tuning_description,@vksr_tuning_description_string)
 set_text ($vksr_label_tuning_definition,"Scale: " & @vksr_tuning_definition_string)
 $vksr_next_pitch_bend_extra_fine:=0
 $vksr_keyswitch_instruction:=0
 $vksr_keyswitchv2_byte_pos:=0
 
end function

on ui_control($vksr_value_edit_n_equal)
 call vksr_make_scale
end on


on ui_control($vksr_menu_midi_tuning_method)
 
 if($vksr_menu_midi_tuning_method=0)
  $vksr_midi_tuning_enabled:=0
 end if
 if($vksr_menu_midi_tuning_method=1)
  $vksr_midi_tuning_enabled:=119
 end if
 call vksr_set_ui_tuning_method_label
 
end on 


function vksr_move_controls  


 
 call hide_user_interface
 $vksr_temp2:=1
 $vksr_temp:=6 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 if($vksr_menu_show_more=$vksr_sm_LESS)
  $vksr_temp:=4 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
  $vksr_temp2:=1
  $vksr_temp:=5 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_MORE)
  
  $vksr_temp:=6 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
  $vksr_temp:=3 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if    
 if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
  $vksr_temp:=4 {position for $vksr_menu_scale_Black and $vksr_value_edit_n_equal_black}
 end if  
 
 if($vksr_menu_show_more=$vksr_sm_DOCUMENTATION)
  move_control($vksr_label_documentation,1,1)
  move_control($vksr_menu_show_more,6,1)
  move_control($vksr_button_ResetTo12EQ,6,2) 
 else {$vksr_menu_show_more#$vksr_sm_DOCUMENTATION}
  {
  if($vksr_menu_show_more=$vksr_sm_PER_NOTE_PAN_AND_VOLUME)
   move_control($vksr_menu_show_more,6,1)
  else
  end if
  }
  if($vksr_menu_show_more=$vksr_sm_TWEAKS)
   move_control($vksr_button_ResetTo12EQ,0,0)
   move_control($vksr_label_instrument_range,1,1)
   move_control($vksr_value_edit_instrument_range_min,2,2)
   move_control($vksr_value_edit_instrument_range_max,3,1)
   move_control($vksr_label_instrument_range2,1,2)
   move_control($vksr_menu_show_diagnostics,5,1)
   move_control($vksr_menu_detect_ratios,1,4)  
   if($vksr_menu_detect_ratios#0)
    move_control($vksr_label_ratio_recognition_sensitivity,2,4)
    move_control($vksr_value_edit_ratio_recognition_sensitivity,4,4)
    move_control($vksr_label_ratio_recognition_sensitivity2,5,4) 
    move_control($vksr_value_edit_ratios_prime_limit,6,4) 
    move_control($vksr_label_ratios_prime_limit,5,5)  
   else
    move_control($vksr_label_ratio_recognition_sensitivity,0,0)
    move_control($vksr_value_edit_ratio_recognition_sensitivity,0,0)
    move_control($vksr_label_ratio_recognition_sensitivity2,0,0) 
    move_control($vksr_value_edit_ratios_prime_limit,0,0) 
    move_control($vksr_label_ratios_prime_limit,0,0)  
   end if
   
   move_control($vksr_label_note_played,1,5) 
   move_control($vksr_value_edit_denum,2,5) 
   move_control($vksr_value_edit_denom,3,5) 
   move_control($vksr_menu_apply_skin,4,1)
   
   move_control($vksr_menu_show_more,6,1)
   move_control($vksr_value_edit_digits_precision,6,2) 
  else {$vksr_menu_show_more#$vksr_sm_TWEAKS and $vksr_menu_show_more#$vksr_sm_DOCUMENTATION}
   move_control($vksr_menu_scale_Black,$vksr_temp2,$vksr_temp+$vksr_ui_extra_vertical_shift)
   move_control($vksr_value_edit_n_equal_black,0,0)
   if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL...
      or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o1...
      or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o2...
      )
    move_control($vksr_value_edit_n_equal_black,$vksr_temp2+1,$vksr_temp+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_scale_black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS)
    move_control($vksr_value_edit_first_harmonic_black,$vksr_temp2+1,$vksr_temp+$vksr_ui_extra_vertical_shift) 
   end if
   
   {(1,$vksr_temp2+6+$vksr_ui_extra_vertical_shift)}
   
   $vksr_temp:=0 { for $vksr_value_edit_1o1_transpose etc extra shift}
   $vksr_temp2:=0 { for $vksr_menu_scale_Black etc extra shift}
   if($vksr_menu_show_more=$vksr_sm_LESS)
    $vksr_temp:=-2
    $vksr_temp2:=-2
   end if    
   if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
    $vksr_temp:=-1
    $vksr_temp2:=0
   end if    
   if($vksr_menu_show_more=$vksr_sm_MORE)
    $vksr_temp:=0
    $vksr_temp2:=0
   end if    
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
    $vksr_temp:=-3
    $vksr_temp2:=-3
   end if    
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
    $vksr_temp:=-2
    $vksr_temp2:=-2
   end if   
   
   
   move_control($vksr_value_edit_1o1_transpose,0,0) 
    {Not really needed, got Tune control anyway in main kontakt instrument display
    {was: move_control($vksr_value_edit_1o1_transpose,4,$vksr_temp+6+$vksr_ui_extra_vertical_shift)}

   move_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,5,$vksr_temp+6+$vksr_ui_extra_vertical_shift)
   move_control($vksr_menu_1o1_pos_note_name,6,$vksr_temp+6+$vksr_ui_extra_vertical_shift)
   
   if($vksr_menu_show_more=$vksr_sm_MORE_WITH_SCALE)
    {isn't enough space in ui for these}
    move_control($vksr_value_edit_n_equal_black,0,0)
    move_control($vksr_menu_scale_Black,0,0)
    move_control($vksr_value_edit_1o1_transpose,0,0)
    move_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60,0,0 )   
    move_control($vksr_menu_1o1_pos_note_name,0,0)
    move_control($vksr_value_edit_Pan,0,0)  
    move_control($vksr_value_edit_Volume,0,0)  
   end if 
   
   if($vksr_menu_show_more=$vksr_sm_LESS or $vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE ...
      or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
    {hide knobs}
    $vksr_move_ui_vertically:=-2
    move_control($vksr_knob_Note,0,0) 
    move_control($vksr_knob_To,0,0) 
    move_control($vksr_knob_Cents,0,0) 
    move_control($vksr_knob_CentiCents,0,0) 
    move_control($vksr_knob_LastDigit,0,0) 
   else
    $vksr_move_ui_vertically:=0
    move_control($vksr_knob_Note,1,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_To,2,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_Cents,3,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_CentiCents,4,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_knob_LastDigit,5,1+$vksr_ui_extra_vertical_shift) 
   end if
   
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY ...
      or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
    {tuning only}
    $vksr_move_ui_vertically:=-3
    move_control($vksr_button_ResetTo12EQ,0,0) 
    move_control($vksr_value_edit_Note,0,0) 
    move_control($vksr_value_edit_To,0,0) 
    move_control($vksr_value_edit_Cents,0,0) 
    move_control($vksr_value_edit_denum,0,0) 
    move_control($vksr_value_edit_denom,0,0) 
    if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
     move_control($vksr_menu_show_more,6,2+$vksr_ui_extra_vertical_shift) 
    end if
    if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE)
     
     move_control($vksr_menu_scroll_scale,1,4+$vksr_ui_extra_vertical_shift) 
     if($vksr_menu_scroll_scale=$vksr_SCROLL_SCALE_TO_SELECTED_NOTE+$vksr_ui_extra_vertical_shift)
      move_control($vksr_value_edit_scroll_scale_midi_start,2,4+$vksr_ui_extra_vertical_shift) 
      move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
     else
      move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
      move_control($vksr_value_edit_scroll_scale_show_before,2,4+$vksr_ui_extra_vertical_shift) 
     end if
     move_control($vksr_menu_show_more,6,1+$vksr_ui_extra_vertical_shift) 
    end if
   else {#($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE+$vksr_ui_extra_vertical_shift) }
    {tuning + user note edit controls}
    move_control($vksr_button_ResetTo12EQ,6,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_Note,1,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_To,2,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_Cents,3,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_denum,4,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_denom,5,3+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
    if($vksr_menu_show_more=$vksr_sm_MORE_WITH_SCALE)
     {Only available space to far right below more drop menu}
     
     move_control($vksr_button_ResetTo12EQ,6,2)
     move_control($vksr_menu_scale_Black,6,3)
     if($vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL...
        or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o1...
        or $vksr_menu_scale_Black=$vksr_scale_black_N_EQUAL_of_3o2...
        )
      move_control($vksr_value_edit_n_equal_black,6,4) 
     end if 
     if($vksr_menu_scale_black=$vksr_scale_black_HARMONIC_AND_SUBHARMONICS)
      move_control($vksr_value_edit_first_harmonic_black,6,4) 
     end if
    end if
    if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
     {has 12-et reset button at far right so  move to positions 4, 5 }
     if($vksr_menu_scroll_scale=$vksr_SCROLL_SCALE_TO_SELECTED_NOTE)
      move_control($vksr_value_edit_scroll_scale_midi_start,4,5+$vksr_ui_extra_vertical_shift) 
      move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
     else
      move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
      move_control($vksr_value_edit_scroll_scale_show_before,4,5+$vksr_ui_extra_vertical_shift) 
     end if
     move_control($vksr_menu_scroll_scale,3,5+$vksr_ui_extra_vertical_shift) 
    end if
    {move_control($vksr_menu_show_more,6,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) }
    move_control($vksr_menu_show_more,6,1+$vksr_ui_extra_vertical_shift) 
   end if
   
   move_control($vksr_label_tuning_method,3,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_menu_midi_tuning_method,6,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_label_tuning_definition,3,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_label_tuning_description,3,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_menu_scale_tuning,1,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_label_note_played,1,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   move_control($vksr_value_edit_digits_precision,2,5+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   
   move_control($vksr_value_edit_n_equal,0,0)  
   move_control($vksr_value_edit_first_harmonic,0,0)  
   
   if($vksr_menu_show_more=$vksr_sm_LESS or $vksr_menu_show_more=$vksr_sm_MORE ...
      or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY)
    {no tuning definition}
    move_control($vksr_label_tuning_definition,0,0) 
    move_control($vksr_menu_scroll_scale,0,0) 
    move_control($vksr_value_edit_scroll_scale_midi_start,0,0) 
    move_control($vksr_value_edit_scroll_scale_show_before,0,0) 
   end if 

   if($vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL...
      or $vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o1...
      or $vksr_menu_scale_tuning=$vksr_scale_tuning_N_EQUAL_of_3o2...
      )
    move_control($vksr_value_edit_n_equal,2,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if
   
   if($vksr_menu_scale_tuning=$vksr_scale_tuning_HARMONIC_AND_SUBHARMONICS)
    move_control($vksr_value_edit_first_harmonic,2,4+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if
   
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_ONLY or $vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE+$vksr_ui_extra_vertical_shift)
    move_control($vksr_menu_show_more,6,1+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_label_tuning_method,3,2+$vksr_ui_extra_vertical_shift) 
    {hide everything except tuning drop down and related controls}
    move_control($vksr_menu_midi_tuning_method,6,2+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_label_note_played,1,2+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_value_edit_digits_precision,0,0){(2,2+$vksr_ui_extra_vertical_shift) }
   end if
   
   if($vksr_menu_show_more=$vksr_sm_TUNING_MENU_WITH_SCALE+$vksr_ui_extra_vertical_shift)
    move_control($vksr_label_tuning_method,3,2+$vksr_ui_extra_vertical_shift) 
    move_control($vksr_label_tuning_definition,1,3+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_show_more=$vksr_sm_LESS or $vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
    {Could hide it?}
    move_control($vksr_value_edit_digits_precision,0,0)   
    {}
   end if  
   {end if }
   if($vksr_menu_show_more=$vksr_sm_LESS_WITH_SCALE)
    move_control($vksr_value_edit_Pan,3,8+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift)  
    move_control($vksr_value_edit_Volume,4,8+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_show_more=$vksr_sm_LESS)
    move_control($vksr_value_edit_Pan,3,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift)  
    move_control($vksr_value_edit_Volume,4,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if 
   if($vksr_menu_show_more=$vksr_sm_MORE)
    move_control($vksr_value_edit_Pan,3,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift)  
    move_control($vksr_value_edit_Volume,4,6+$vksr_move_ui_vertically+$vksr_ui_extra_vertical_shift) 
   end if 
  end if  {$vksr_menu_show_more#$vksr_sm_TWEAKS}
 end if {$vksr_menu_show_more#$vksr_sm_DOCUMENTATION}
 
end function
    
function on_menu_scale_change
 
 $vksr_Note_user_edited:=-1 {not yet edited}
 $vksr_Note_played:=-1 {not yet pleyd}
 
 call find_vksr_istart_for_tuning_definition_string
 if($vksr_istart#$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60)
  call vksr_remake_tuning_definition_not_user_edited
 end if
 call vksr_move_controls   
 if($vksr_menu_scale_tuning=0 and $vksr_menu_scale_Black=0)
  call vksr_reset_to_12et
 end if
 
 if(($vksr_menu_scale_tuning>0 or $vksr_menu_scale_Black#0) and $vksr_menu_scale_tuning#$vksr_scale_tuning_EDITED)
  call vksr_make_scale
  
 end if 
 
end function

on ui_control($vksr_menu_scale_tuning)
 
 call on_menu_scale_change
 
end on

on ui_control($vksr_menu_scale_Black)
 
 call on_menu_scale_change
 
end on

function vksr_on_scroll_scale_etc
 
 call vksr_remake_tuning_definition_not_user_edited
 
 if($vksr_menu_scale_tuning>0 and $vksr_menu_scale_tuning#$vksr_scale_tuning_EDITED)
  if($vksr_value_edit_scroll_scale_midi_start=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60 and $vksr_Note_played<0)
   {If user scrolls scale to its default position and no notes played, then can use the original
   definition string as set for the scale, easiest way to do that is just
   to call vksr_make_scale again
   }
   call vksr_make_scale
  end if
 end if
 
 call vksr_move_controls
 
end function


on ui_control($vksr_value_edit_n_equal)
 
 call vksr_make_scale
 
end on

on ui_control($vksr_value_edit_n_equal_black)
 
 call vksr_make_scale
 
end on

on ui_control($vksr_value_edit_first_harmonic_black)
 
 call vksr_make_scale
 
end on


on ui_control($vksr_value_edit_first_harmonic)
 
 call vksr_make_scale
 
end on 

on ui_control($vksr_value_edit_1o1_transpose)
 
 call vksr_make_scale
 
end on

on ui_control($vksr_menu_1o1_pos_note_name)
 
 $vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60:=12*($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60/12)...
                                                         +     $vksr_menu_1o1_pos_note_name
 call vksr_make_scale
 
end on
on ui_control($vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60)
 
 $vksr_menu_1o1_pos_note_name:=$vksr_value_edit_1o1_pos_for_make_scale_DEFAULT_60 mod 12    
 call vksr_make_scale
 
end on



on ui_control ($vksr_value_edit_ratios_prime_limit)
 
 $vksr_ok_for_n_limit_up_to_date:=0
 call vksr_convert_millicents_to_ratio {just to update the $vksr_label_ratios_prime_limit}
 
 call vksr_on_scroll_scale_etc
 
end on

on ui_control ($vksr_value_edit_ratio_recognition_sensitivity)
 
 call vksr_on_scroll_scale_etc
 
end on

on ui_control ($vksr_menu_detect_ratios)
 call vksr_move_controls
   
end on

on ui_control ($vksr_value_edit_digits_precision)
 
 if($vksr_Note_played>=0)
  
  $vksr_arg:=$vksr_Note_played
  call vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
  if($vksr_value_edit_instrument_range_min<=%vksr_output_note[$vksr_Note_played] and %vksr_output_note[$vksr_Note_played] <=$vksr_value_edit_instrument_range_max)
   set_text ($vksr_label_note_played,...
             "Played: " & @vksr_cents_or_ratio_for_offset_in_milliseconds...
             & " (" & !vksr_note_names[%vksr_output_note[$vksr_Note_played] mod 12] & ") ")
  end if
 end if   
 call vksr_on_scroll_scale_etc
 
end on

on ui_control($vksr_value_edit_scroll_scale_midi_start)
 
 call vksr_on_scroll_scale_etc
 
end on

on ui_control($vksr_value_edit_scroll_scale_show_before)
 
 call vksr_on_scroll_scale_etc
 
end on


on ui_control($vksr_menu_show_diagnostics)
 
 message("")
 
end on

on ui_control($vksr_menu_scroll_scale)
 
 call vksr_on_scroll_scale_etc
 
end on

on ui_control($vksr_menu_show_more)
 
 call vksr_move_controls
 
end on


function vksr_retune_note_on
 
 if($vksr_menu_show_diagnostics # 0 and $vksr_show_messages_events_string#0) 
  @vksr_messages_events_string:=@vksr_messages_events_string & " n " & $vksr_EVENT_NOTE & "," { " [Note " & $vksr_EVENT_NOTE & ", " & $vksr_EVENT_VELOCITY & "]"}
  message(@vksr_messages_events_string)
 end if
 
 
 %vksr_is_active[$vksr_EVENT_NOTE]:=1
 { Record the output note now, to use to work out the total pitch bend later on
 using the change of output note since the note on
 }
 %vksr_output_note_at_note_on[$vksr_EVENT_NOTE]:=%vksr_output_note[$vksr_EVENT_NOTE]
 %vksr_pitch_bend_for_output_note_at_note_on[$vksr_EVENT_NOTE]:=%vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE]
 
 $vksr_knob_Note:=$vksr_EVENT_NOTE
 $vksr_Note_played:=$vksr_knob_Note 
 call vksr_set_the_ui_knobs_etc  
 call vksr_remake_tuning_definition_not_user_edited
 {Check that the note is not a keyswitch after retuning - if it is
 then is "out of range silent"
 }
 $vksr_arg:=$vksr_EVENT_NOTE
 call vksr_find_cents_or_ratios_string_for_arg_check_knob_Note
 if($vksr_value_edit_instrument_range_min<=%vksr_output_note[$vksr_EVENT_NOTE] and %vksr_output_note[$vksr_EVENT_NOTE] <=$vksr_value_edit_instrument_range_max)
     {This is where we create the new note, having ignored the old one. Use play_note(..) and that returns the id of the new note}
     %vksr_note_id[$vksr_EVENT_NOTE]:=play_note(%vksr_output_note[$vksr_EVENT_NOTE],$vksr_EVENT_VELOCITY,0,-1)
     
     {Now we have to make sure it has same pan positions and volume as before (as in KSP Reference Manual under ignore_event() }
     if($vksr_restore_expression_and_pan#0)
         if($vksr_ok_process_volume_and_pan_keyswitches#0)
             if($vksr_menu_show_diagnostics#0)
                 { @vksr_pan_and_expression_diagnostics_string:="change_pan( " & $vksr_EVENT_NOTE & " to " & %vksr_input_note_pan[$vksr_EVENT_NOTE] & ") "}
                 
          end if
             change_vol(%vksr_note_id[$vksr_EVENT_NOTE],%vksr_input_note_volume[$vksr_EVENT_NOTE],0) 
             change_pan(%vksr_note_id[$vksr_EVENT_NOTE],%vksr_input_note_pan[$vksr_EVENT_NOTE],0) 
         else
             change_vol(%vksr_note_id[$vksr_EVENT_NOTE],get_event_par(%vksr_note_id[$vksr_EVENT_NOTE],$EVENT_PAR_VOLUME),0) 
             $vksr_value_edit_Volume:=get_event_par(%vksr_note_id[$vksr_EVENT_NOTE],$EVENT_PAR_VOLUME)
             change_pan(%vksr_note_id[$vksr_EVENT_NOTE],get_event_par(%vksr_note_id[$vksr_EVENT_NOTE],$EVENT_PAR_PAN),0) 
             $vksr_value_edit_Pan:=get_event_par(%vksr_note_id[$vksr_EVENT_NOTE],$EVENT_PAR_PAN)
             
         end if
     end if 
  
    set_text ($vksr_label_note_played,...
            "Played: " & @vksr_cents_or_ratio_for_offset_in_milliseconds...
            & " (" & !vksr_note_names[%vksr_output_note[$vksr_EVENT_NOTE] mod 12] & ") ")
  
    change_tune...
    (%vksr_note_id[$vksr_EVENT_NOTE]...
     ,%vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE]...
     ,0) 
 else
  message("OUT OF RANGE note to play -  " & $vksr_EVENT_NOTE & " -> " ...
          & %vksr_output_note[$vksr_EVENT_NOTE] & " - IGNORED  - INSTRUMENT RANGE " &$vksr_value_edit_instrument_range_min ...
          & " to " & $vksr_value_edit_instrument_range_max...
          )    
  set_text ($vksr_label_note_played,...
            "OUT OF RANGE: " & @vksr_cents_or_ratio_for_offset_in_milliseconds)
  
 end if
end function

on note
    
    $vksr_EVENT_NOTE:=$EVENT_NOTE
    if($vksr_EVENT_NOTE<=125)
        if($vksr_menu_show_diagnostics#0)
            @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string  & ...
                                                       "NOTE ON PLAYED (not ignored): " & $vksr_EVENT_NOTE & " "
            message(@vksr_pan_and_expression_diagnostics_string)
        end if
    end if
    $vksr_EVENT_VELOCITY:=$EVENT_VELOCITY
    $vksr_EVENT_ID:=$EVENT_ID
    %vksr_input_note_volume[$vksr_EVENT_NOTE]:=get_event_par($vksr_EVENT_ID,$EVENT_PAR_VOLUME) {default here is 0 := no reduction or increase in volume in decibels}
    %vksr_input_note_pan[$vksr_EVENT_NOTE]:=get_event_par($vksr_EVENT_ID,$EVENT_PAR_PAN)
    %vksr_note_id[$vksr_EVENT_NOTE]:=$EVENT_ID 
    if ($vksr_EVENT_NOTE<126 and $EVENT_VELOCITY>0)
        {Deal with the results of use of instructions 127, 11 followed by 3 bytes of data to set the pan for an upcoming note
        and 127, 8, followed by 3 bytes of data to set expression for an upcoming note
        }
        
        {Note - at this stage don't have the note id for the note as it will be ignored in the next code block and a 
        new note created in the function vksr_retune_note_on
        Instead just set %vksr_input_note_volume[] and %vksr_input_note_pan[] - overriding the defaults found with get_event_par above
        - and these then will be used to set the pan and expression of the new note after it is played
        }
        if(%vksr_do_adjust_volume_send_next_note[$vksr_EVENT_NOTE]#0)
            { have data from the instruction 127, 8 to set expression for an upcoming note}
            $vksr_adjust_volume_send:=%vksr_adjust_volume_send_next_note[$vksr_input_note]
            %vksr_input_note_volume[$vksr_input_note]:=$vksr_adjust_volume_send
            {
            set_event_par(%vksr_note_id[$vksr_input_note],$EVENT_PAR_VOLUME,$vksr_adjust_volume_send)
            change_vol(%vksr_note_id[$vksr_input_note],$vksr_adjust_volume_send,0) 
            if($vksr_menu_show_diagnostics#0)
                @vksr_pan_and_expression_diagnostics_string:=...
                                                           @vksr_pan_and_expression_diagnostics_string  & ...
                                                           " change_volume NOW FOR NEXT NOTE: " & $vksr_input_note & ...
                                                           " to " & $vksr_adjust_volume_send & ") "
                message(@vksr_pan_and_expression_diagnostics_string)
            end if
            {}
        end if
        if(%vksr_do_adjust_pan_send_next_note[$vksr_EVENT_NOTE]#0)
            { have data from the instruction 127, 11 to set stereo pan for an upcoming note}
           $vksr_adjust_pan_send:=%vksr_adjust_pan_send_next_note[$vksr_input_note]
            %vksr_input_note_pan[$vksr_input_note]:=$vksr_adjust_pan_send
            $vksr_value_edit_Pan:=$vksr_adjust_pan_send
        end if
    end if
    {
    if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=6)
        if($vksr_menu_show_diagnostics#0)
            @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string & " on note expression instruction: " ...
                                                       & $vksr_EVENT_NOTE & " vel " ...
                                                       & $vksr_EVENT_VELOCITY
            message(@vksr_pan_and_expression_diagnostics_string)
        end if
    end if
    if ($vksr_EVENT_NOTE=127 and $vksr_EVENT_VELOCITY=5)
        if($vksr_menu_show_diagnostics#0)
            @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string & " on note pan instruction: " ...
                                                       & $vksr_EVENT_NOTE & " vel " ...
                                                       & $vksr_EVENT_VELOCITY
            message(@vksr_pan_and_expression_diagnostics_string)
        end if
    end if
    {}
    
    if( ($vksr_midi_tuning_enabled = 119 and $vksr_EVENT_NOTE >=126 ) or ($vksr_midi_tuning_enabled = 118 and $vksr_EVENT_NOTE >=122) )
        {enable_high_note_keyswitches_retuning_v2 or enable_high_note_keyswitches_retuning }
        
        ignore_event($EVENT_ID)
        call vksr_process_tuning_keyswitches
        
    else   { else case for ($vksr_midi_tuning_enabled = 119 and $vksr_EVENT_NOTE >=126 ) or ($vksr_midi_tuning_enabled = 118 and $vksr_EVENT_NOTE >=122)} 
        
        if($vksr_value_edit_instrument_range_min<=$vksr_EVENT_NOTE and $vksr_EVENT_NOTE <=$vksr_value_edit_instrument_range_max)
            
            ignore_event($EVENT_ID)
            {
            $vksr_value_edit_Volume:=%vksr_input_note_volume[$vksr_EVENT_NOTE]
            $vksr_value_edit_Pan:=%vksr_input_note_pan[$vksr_EVENT_NOTE]
            }
            if($vksr_retune_release#0)
                { http://www.vi-control.net/forum/viewtopic.php?p=178475&highlight=
                Need to make sure release sample is same tuning as the note itself
                }
                { activate normal and deactivate release groups } 
                disallow_group($ALL_GROUPS) 
                $vksr_normal_group:=0 
                while ($vksr_normal_group <= $NUM_GROUPS-1) 
                    if (_get_engine_par($ENGINE_PAR_RELEASE_TRIGGER, $vksr_normal_group, -1, -1) = 0) 
                        allow_group($vksr_normal_group) 
                    end if 
                    inc($vksr_normal_group) 
                end while 
            end if {$vksr_retune_release#0}
            
            call vksr_retune_note_on  {note this creatse a new note with note id recorded as  %vksr_note_id[$vksr_EVENT_NOTE]}
        else 
        end if {$vksr_value_edit_instrument_range_min<=$vksr_EVENT_NOTE and $vksr_EVENT_NOTE <=$vksr_value_edit_instrument_range_max }
        if ($vksr_EVENT_NOTE<126 and $EVENT_VELOCITY>0)
            if(%vksr_do_adjust_volume_send_next_note[$vksr_EVENT_NOTE]#0)
                $vksr_input_note:=$vksr_EVENT_NOTE
                $vksr_adjust_volume_send:=%vksr_adjust_volume_send_next_note[$vksr_input_note]
                %vksr_adjust_volume_send_next_note[$vksr_input_note]:=0
                %vksr_do_adjust_volume_send_next_note[$vksr_input_note]:=0
                $vksr_value_edit_Volume:=$vksr_adjust_volume_send
                
                {Need to convert it into a negative amount which reduces the volume in decibels of the note}        
                $vksr_adjust_volume_send:=-($vksr_VOLUME_MAX_IN_MILLIDECIBELS -$vksr_adjust_volume_send)
                %vksr_input_note_volume[$vksr_input_note]:=$vksr_adjust_volume_send
                set_event_par(%vksr_note_id[$vksr_input_note],$EVENT_PAR_VOLUME,$vksr_adjust_volume_send)
                {
                change_vol(%vksr_note_id[$vksr_input_note],$vksr_adjust_volume_send,0) 
                if($vksr_menu_show_diagnostics#0)
                    @vksr_pan_and_expression_diagnostics_string:=...
                                                               @vksr_pan_and_expression_diagnostics_string  & ...
                                                               " change_volume NOW FOR NEXT NOTE: " & $vksr_input_note & ...
                                                               " to " & $vksr_adjust_volume_send & ") "
                    message(@vksr_pan_and_expression_diagnostics_string)
                end if
                {}
            end if
            if(%vksr_do_adjust_pan_send_next_note[$vksr_EVENT_NOTE]#0)
                
                $vksr_input_note:=$vksr_EVENT_NOTE
                $vksr_adjust_pan_send:=%vksr_adjust_pan_send_next_note[$vksr_input_note]
                %vksr_adjust_pan_send_next_note[$vksr_input_note]:=0
                %vksr_do_adjust_pan_send_next_note[$vksr_input_note]:=0
                %vksr_input_note_pan[$vksr_input_note]:=$vksr_adjust_pan_send
                $vksr_value_edit_Pan:=$vksr_adjust_pan_send
                if($vksr_menu_show_diagnostics#0)
                    @vksr_pan_and_expression_diagnostics_string:=@vksr_pan_and_expression_diagnostics_string  & ...
                                                               "change_pan NOW FOR NEXT NOTE: " & $vksr_input_note &...
                                                               " to " & $vksr_adjust_pan_send & ") "
                    message(@vksr_pan_and_expression_diagnostics_string)
                end if
                change_pan(%vksr_note_id[$vksr_input_note],%vksr_input_note_pan[$vksr_input_note],0) 
                set_event_par(%vksr_note_id[$vksr_input_note],$EVENT_PAR_PAN,%vksr_input_note_pan[$vksr_input_note])
            end if
        end if
        
    end if  {else case for ($vksr_midi_tuning_enabled = 119 and $vksr_EVENT_NOTE >=126 ) or ($vksr_midi_tuning_enabled = 118 and $vksr_EVENT_NOTE >=122)}
    
end on {on note}

on release
 
 $vksr_EVENT_NOTE:=$EVENT_NOTE
 call vksr_on_release
 
 if( ($vksr_midi_tuning_enabled = 119 and $vksr_EVENT_NOTE >=126 ) or ($vksr_midi_tuning_enabled = 118 and $vksr_EVENT_NOTE >=122) )
  { this is a keyswitch note release }
 else 
  if($vksr_retune_release#0)
   { http://www.vi-control.net/forum/viewtopic.php?p=178475&highlight=
   Need to make sure release sample is same tuning as the note itself
   }
   { activate release groups and deactivate normal groups } 
   disallow_group($ALL_GROUPS) 
   $vksr_release_group:=0 
   while ($vksr_release_group <= $NUM_GROUPS-1) 
    if (_get_engine_par($ENGINE_PAR_RELEASE_TRIGGER, $vksr_release_group, -1, -1) # 0) 
     allow_group($vksr_release_group)       
    end if 
    inc($vksr_release_group) 
   end while 
   { "manually" trigger release sample with same pitch and velocity as original event }       
   $vksr_OFF_ID:=play_note($vksr_EVENT_NOTE, $vksr_EVENT_VELOCITY, 0, 0)   
   
   { Now retune the release sample using this new  $vksr_OFF_ID }
   %vksr_note_id[$vksr_EVENT_NOTE]:=$vksr_OFF_ID
   if (%vksr_output_note_at_note_on[$vksr_EVENT_NOTE]=%vksr_output_note[$vksr_EVENT_NOTE])
    change_tune(%vksr_note_id[$vksr_EVENT_NOTE],%vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE],0)
   else
    $vksr_Note_shift:=%vksr_output_note[$vksr_EVENT_NOTE]-%vksr_output_note_at_note_on[$vksr_EVENT_NOTE]
    change_tune(%vksr_note_id[$vksr_EVENT_NOTE],%vksr_pitch_bend_for_output_note[$vksr_EVENT_NOTE]+($vksr_Note_shift*$vksr_OCTAVE_IN_MILLICENTS),0)
   end if
  end if
 end if
 
end on

on controller
 
 
 $vksr_CC_NUM:=$CC_NUM
 call vksr_on_controller
 
end on