1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Automating Time Machine speed using script

Discussion in 'Scripting Workshop' started by Denny Tiberius Crane, Jun 21, 2012.

  1. Denny Tiberius Crane

    Denny Tiberius Crane NI Product Owner

    Messages:
    61
    I'm using Kontakt as a live onstage loop player

    I would like to hit a key ONCE on the pc keyboard (or piano) that trigggers an automation that GRADUALLY slows down the speed of time machine 2.

    A bit like the session recorder script that records and plays back sequences. I've been able to get session recorder to play notes but I can't get it to record the time machine speed adjustments I make using cc automation from my external midi controller.

    I don't want to do this using a daw like cubase or logic.

    Basically I'm looking for an automated response from hitting a single key. I already know how to link time machine's speed function to control knobs on my synth and midi pedal but because I'm already doing a hundred things on stage, I'd like this to be automated for me.

    Help me Obi Wan:|
     
  2. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Try this:

    Code:
    on init
    
    	declare ui_knob $Speed (0,1000000,1)
    	declare ui_switch $SetStart
    	declare ui_switch $SetEnd
    	declare ui_knob $Time (100,10000,1)
    	declare ui_knob $Key (0,127,1)
    	declare ui_switch $Reset
    
    	declare $i
    	declare $j
    	declare $start := 500000
    	declare $end := 500000
    	declare $diff
    	declare !notes[12]
    	!notes[0] := "C"
    	!notes[1] := "C#"
    	!notes[2] := "D"
    	!notes[3] := "D#"
    	!notes[4] := "E"
    	!notes[5] := "F"
    	!notes[6] := "F#"
    	!notes[7] := "G"
    	!notes[8] := "G#"
    	!notes[9] := "A"
    	!notes[10] := "A#"
    	!notes[11] := "B"
    	declare !names[128]
    
    	$i := 0
    	while ($i < 128)
    		!names[$i] := "  " & !notes[$i mod 12] & ($i / 12) - 2
    		inc($i)
    	end while
    
    	set_knob_unit($Speed,$KNOB_UNIT_PERCENT)
    	set_knob_unit($Time,$KNOB_UNIT_MS)
    
    	set_knob_defval($Speed,500000)
    	set_knob_defval($Time,1000)
    	set_knob_defval($Key,24)
    
    	set_control_par(get_ui_id($SetStart),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    	set_control_par(get_ui_id($SetEnd),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    	set_control_par(get_ui_id($Reset),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    
    	$Speed := get_engine_par($ENGINE_PAR_SPEED,0,-1,-1)
    	$Time := 1000
    	$Key := 24
    
    	make_persistent($Speed)
    	make_persistent($Time)
    	make_persistent($Key)
    	make_persistent($start)
    	make_persistent($end)
    
    	read_persistent_var($Key)
    
    	set_knob_label($Speed,get_engine_par_disp($ENGINE_PAR_SPEED,0,-1,-1))
    	set_knob_label($Key,!names[$Key])
    
    	set_text($SetStart,"Set Start Speed")
    	set_text($SetEnd,"Set End Speed")
    
    	move_control($SetStart,2,1)
    	move_control($SetEnd,2,2)
    
    	$i := 0
    	while ($i < 128)
    		if ($i = $Key)
    			set_key_color($i,$KEY_COLOR_GREEN)
    		else
    			set_key_color($i,$KEY_COLOR_NONE)
    		end if
    		inc($i)
    	end while
    
    	message("")
    end on
    
    
    function SetSpeed()
    	$i := 0
    	while ($i < $NUM_GROUPS)
    		set_engine_par($ENGINE_PAR_SPEED,$Speed,$i,-1,-1)
    		inc($i)
    	end while
    	set_knob_label($Speed,get_engine_par_disp($ENGINE_PAR_SPEED,0,-1,-1))
    end function
    
    function SetKey()
    	set_knob_label($Key,!names[$Key])
    	$i := 0
    	while ($i < 128)
    		if ($i = $Key)
    			set_key_color($i,$KEY_COLOR_GREEN)
    		else
    			set_key_color($i,$KEY_COLOR_NONE)
    		end if
    		inc($i)
    	end while
    end function
    
    
    on ui_control ($Speed)
    	call SetSpeed()
    end on
    
    on ui_control ($SetStart)
    	$start := $Speed
    	wait(500000)
    	$SetStart := 0
    end on
    
    on ui_control ($SetEnd)
    	$end := $Speed
    	wait(500000)
    	$SetEnd := 0
    	$Speed := $start
    	call SetSpeed()
    end on
    
    on ui_control ($Key)
    	call SetKey()
    end on
    
    on ui_control ($Reset)
    	$Speed := 500000
    	$start := 500000
    	$end := 500000
    	$Time := 1000
    	$Key := 24
    
    	call SetSpeed()
    	call SetKey()
    
    	$Reset := 0
    end on
    
    
    on note
    	if ($EVENT_NOTE = $Key)
    		$diff := ($end - $start) / $Time
    		message($diff)
    		$j := 0
    		while ($j < $Time)
    			$Speed := $start + ($diff * $j)
    			call SetSpeed()
    			inc($j)
    			wait(1000)
    		end while
    	end if
    end on
     
  3. Denny Tiberius Crane

    Denny Tiberius Crane NI Product Owner

    Messages:
    61
    OH MY GOD!:D

    Thankyou so very much for posting this.

    You are an angel mate.
    A true star.

    Just to push my luck, is there a way of triggering this script via cc midi instead of a piano keyboard stroke?

    N
     
  4. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Very likely, but you're out of free bonus points :p