{===========================} { key shuffler } { by SzcZ (c) 2014 } { www.fairlyconfusing.net } {===========================} on init {..........................................................} { this is constant modifier, it can be any positive number } { edit its value to change the outcome of shuffle methods } declare const $modifier := 101202 {..........................................................} set_script_title("key shuffler") set_ui_height_px(100) {variables} declare $count declare $learning declare %map[128] declare $first_note declare $first_draw declare $mem {ui coordinates} declare const $x_pos := 66 declare const $y_pos := 20 declare const $x_spc := 92 declare const $x_lab := 200 declare const $y_spc := 22 {knobs} declare ui_knob $LOW_note (0,127,1) set_text($LOW_note,"low") move_control_px($LOW_note, $x_pos, $y_pos + $y_spc) declare ui_knob $HIGH_note (0,127,1) set_text($HIGH_note,"high") move_control_px($HIGH_note, $x_pos + $x_spc, $y_pos + $y_spc) $HIGH_note := 127 declare ui_knob $rounds (1,1000,1) move_control_px($rounds, $x_pos + $x_spc *2, $y_pos + $y_spc) $rounds := 256 declare ui_knob $preset (1,1000,1) set_text($preset,"number") move_control_px($preset, $x_pos + $x_spc *3, $y_pos + $y_spc) {labels} declare ui_label $l_range (1,1) set_text ($l_range," shuffle range") move_control_px($l_range, $x_pos, $y_pos) set_control_par(get_ui_id($l_range), $CONTROL_PAR_WIDTH, $x_spc *2 -4) declare ui_label $l_power (1,1) set_text ($l_power," shuffle intensity") move_control_px($l_power, $x_pos + $x_spc *2, $y_pos) declare ui_label $l_preset (1,1) set_text ($l_preset," shuffle method") move_control_px($l_preset, $x_pos + $x_spc *3, $y_pos) declare ui_label $l_nfo1 (1,1) move_control_px($l_nfo1, $x_pos + $x_spc *4, $y_pos) declare ui_label $l_nfo3 (1,1) move_control_px($l_nfo3, $x_pos + $x_spc *4, $y_pos + $y_spc *2) hide_part ($l_nfo1,$HIDE_PART_BG) hide_part ($l_nfo3,$HIDE_PART_BG) set_control_par(get_ui_id($l_nfo1), $CONTROL_PAR_WIDTH, $x_lab) set_control_par(get_ui_id($l_nfo3), $CONTROL_PAR_WIDTH, $x_lab) set_control_par(get_ui_id($l_nfo1), $CONTROL_PAR_FONT_TYPE, 22) set_control_par(get_ui_id($l_nfo3), $CONTROL_PAR_FONT_TYPE, 2) set_text ($l_nfo1,"KEY SHUFFLER") set_text ($l_nfo3," ") {buttons} declare ui_button $learn set_text ($learn," learn range") move_control_px($learn, $x_pos + $x_spc *4, $y_pos + $y_spc) declare ui_switch $shuffle set_text ($shuffle," shuffle keys") move_control_px($shuffle, $x_pos + $x_spc *5, $y_pos) declare ui_switch $reset set_text ($reset," restore keys") move_control_px($reset, $x_pos + $x_spc *5, $y_pos + $y_spc) {random generator const and variables} declare const $PMa := 16807 declare const $PMm := 2147483647 declare const $PMq := $PMm / $PMa declare const $PMr := $PMm mod $PMa declare $seed := 101202199 declare $rnd declare $rnd_min declare $rnd_max := 1000000 {save settings} make_persistent ($LOW_note) make_persistent ($HIGH_note) make_persistent ($rounds) make_persistent (%map) make_persistent ($preset) {fill the key map} $count := 0 while ($count < 128) %map[$count] := $count inc($count) end while end on {random number generator} function generate_random $rnd := $PMa * ($seed mod $PMq) - $PMr * ($seed / $PMq) if ($rnd > 0) $seed := $rnd else $seed := $rnd + $PMm end if $rnd := ($rnd_max - $rnd_min) +1 $rnd := ($seed mod $rnd) + $rnd_min end function function clear_map $count := 0 while ($count < 128) %map[$count] := $count inc($count) end while end function on note select($learning) case 0 set_text ($l_nfo3,"incoming note number: " & $EVENT_NOTE) change_note($EVENT_ID,%map[$EVENT_NOTE]) case 1 $first_note := $EVENT_NOTE inc($learning) set_text ($l_nfo3,"waiting for second note") case 2 if($first_note # $EVENT_NOTE) if($first_note > $EVENT_NOTE) $HIGH_note := $first_note $LOW_note := $EVENT_NOTE else $HIGH_note := $EVENT_NOTE $LOW_note := $first_note end if $learning := 0 set_text ($l_nfo3,"range set") $learn := 0 end if end select end on on ui_control ($learn) if($learn =1) $learning := 1 set_text ($l_nfo3,"waiting for first note") else $learning := 0 set_text ($l_nfo3,"range learn canceled") end if end on on ui_control ($reset) call clear_map $reset := 0 set_text ($l_nfo3,"keys have been un-shuffled") end on on ui_control ($shuffle) call clear_map $seed := $preset + $modifier $count := 0 $rnd_min := $LOW_note $rnd_max := $HIGH_note while ($count < $rounds) call generate_random $first_draw := $rnd call generate_random if($rnd = $first_draw) inc($rnd) if($rnd > $rnd_max) $rnd := $rnd_min end if end if $mem := %map[$first_draw] %map[$first_draw] := %map[$rnd] %map[$rnd] := $mem inc($count) end while $shuffle := 0 set_text ($l_nfo3,"keys have been shuffled") end on