{===========================} { key shuffler rev.a } { 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 {..........................................................} { bipolar range in semitones for 'shuffle pitch' function } { e.g. value of 1 sets range to -1 to 1 } declare const $DETUNE_RANGE := 24 {..........................................................} { a tip for scripting newbies: } { if you'd like script controls to appear on front panel } { delete brackets from the following line } { make_perfview } {..........................................................} { if it doesn't go along with instrument's background, } { you can slide the background down by a number of pixels } { set_skin_offset(655) } {..........................................................} set_script_title("Key Shuffler") set_ui_height_px(103) {variables} declare $count declare $learning declare %map[128] declare %map_pitch[128] declare %map_exceptions[128] declare $first_note declare $first_draw declare $mem declare const $DETUNE_FACTOR := 10000 / $DETUNE_RANGE {ui coordinates} declare const $x_pos := 66 declare const $y_pos := 23 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 +1) 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 +1) $HIGH_note := 127 declare ui_knob $rounds (1,1000,1) move_control_px($rounds, $x_pos + $x_spc *2, $y_pos + $y_spc +1) $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 +1) declare ui_knob $pitch_range (0,$DETUNE_RANGE *10,10) set_text($pitch_range,"range") move_control_px($pitch_range, $x_pos + $x_spc *4, $y_pos + $y_spc +1) set_knob_unit($pitch_range,$KNOB_UNIT_ST) {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_pitch (1,1) set_text ($l_pitch," shuffle pitch") move_control_px($l_pitch, $x_pos + $x_spc *4, $y_pos) declare ui_label $l_nfo1 (1,1) move_control_px($l_nfo1, $x_pos, $y_pos - $y_spc +1) declare ui_label $l_nfo3 (1,1) move_control_px($l_nfo3, $x_pos + $x_spc *2, $y_pos - $y_spc) 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, $y_pos - $y_spc) declare ui_switch $shuffle set_text ($shuffle," shuffle keys") move_control_px($shuffle, $x_pos + $x_spc *5, $y_pos - $y_spc) declare ui_switch $reset set_text ($reset," restore keys") move_control_px($reset, $x_pos + $x_spc *5, $y_pos) declare ui_switch $learn_ex set_text ($learn_ex," learn exceptions") move_control_px($learn_ex, $x_pos + $x_spc *5, $y_pos + $y_spc) declare ui_switch $clear_ex set_text ($clear_ex," clear exceptions") move_control_px($clear_ex, $x_pos + $x_spc *5, $y_pos + $y_spc *2) declare ui_switch $pitch_polarity move_control_px($pitch_polarity, $x_pos + $x_spc *4, $y_pos - $y_spc) set_text ($pitch_polarity,"pitch md. polarity") {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) make_persistent (%map_pitch) make_persistent (%map_exceptions) make_persistent ($pitch_range) make_persistent ($pitch_polarity) {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) if(%map_exceptions[$EVENT_NOTE] =0 and in_range($EVENT_NOTE,$LOW_note,$HIGH_note)) $count := 1 - $pitch_polarity *2 change_note($EVENT_ID,%map[$EVENT_NOTE]) change_tune($EVENT_ID,%map_pitch[$EVENT_NOTE] * $DETUNE_FACTOR * $pitch_range * $count,1) end if 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 case 3 %map_exceptions[$EVENT_NOTE] := 1 set_text ($l_nfo3,"key " & $EVENT_NOTE & " added to exceptions") 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 ($learn_ex) $learning := $learn_ex *3 if($learn_ex =1) set_text ($l_nfo3,"press keys to exclude from shuffling") else set_text ($l_nfo3," ") end if end on on ui_control ($clear_ex) $count := 0 while ($count < 128) %map_exceptions[$count] := 0 inc($count) end while set_text ($l_nfo3,"exceptions cleared") $clear_ex := 0 $learn_ex := 0 $learning := 0 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) {shuffle key map} 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 {write pitch map} $seed := $preset + $modifier $count := 0 $rnd_min := 0 $rnd_max := $DETUNE_RANGE *2 while ($count < 127) call generate_random %map_pitch[$count] := $rnd - $DETUNE_RANGE inc($count) end while $shuffle := 0 set_text ($l_nfo3,"keys have been shuffled") end on