{***********************************************
Battery 3 - Slot 5 (Humanize)
Author: Native Instruments
Written by: Nicki Marinic, Josef Natterer (natterer-script@web.de)
Modified: August 11, 2006
Version: 0.9.11
*************************************************}

on init
	message("")
	declare ui_button $Onoff
	set_text($onoff,"On")
	declare ui_knob $Timing (0,100,1)
	$Timing := 10
	set_text($timing,"Time")
	set_knob_unit($Timing,$KNOB_UNIT_PERCENT)
	set_knob_defval ($Timing,50)
	declare ui_knob $Velocity (0,100,1)
	$Velocity := 10
	set_knob_unit($Velocity,$KNOB_UNIT_PERCENT)
	set_knob_defval ($Velocity,50)
	declare ui_knob $Tuning (0,100,1)
	$Tuning := 10
	set_text($tuning,"Tune")
	set_knob_unit($Tuning,$KNOB_UNIT_PERCENT)
	set_knob_defval ($Tuning,50)
	declare ui_knob $Volume (0,100,1)
	$Volume := 10
	set_knob_unit($Volume,$KNOB_UNIT_PERCENT)
	set_knob_defval ($Volume,50)
	
	declare %onoff_store[128]
	declare %timing_store[128]
	declare %velocity_store[128]
	declare %tuning_store[128]
	declare %volume_store[128]
	make_persistent(%onoff_store)
	make_persistent(%timing_store)
	make_persistent(%tuning_store)
	make_persistent(%velocity_store)
	make_persistent(%volume_store)
		
	move_control($onoff,1,1)
	move_control($Velocity,2,1)
	move_control($Tuning,3,1)
	move_control($Timing,4,1)
	move_control($Volume,5,1)
	declare $a
	declare $Tune_amount
	declare $Time_amount
	declare $max_vel_amount
	declare $new_time
	declare $new_vel
	declare $new_tune
	declare $new_vol
	declare $new_event
	$a := 0
	while($a < $NUM_GROUPS)
		%onoff_store[$a] := 0
		%timing_store[$a] := 50
		%velocity_store[$a] := 50
		%tuning_store[$a] := 50
		%volume_store[$a] := 50
		inc($a)
	end while

	_read_persistent_var(%onoff_store)
	_read_persistent_var(%timing_store)
	_read_persistent_var(%tuning_store)
	_read_persistent_var(%velocity_store)
	_read_persistent_var(%volume_store)

	$onoff := %onoff_store[$REF_GROUP_IDX]
	$timing := %timing_store[$REF_GROUP_IDX]
	$tuning := %tuning_store[$REF_GROUP_IDX]
	$velocity := %velocity_store[$REF_GROUP_IDX]
	$volume := %volume_store[$REF_GROUP_IDX]
end on

on note
	if (%EVENT_PAR[0] = 1000)
		exit
	end if
	if (%onoff_store[%EVENT_PAR[0]] = 0)
		change_vol ($EVENT_ID, %EVENT_PAR[1], 1)
		change_tune ($EVENT_ID, %EVENT_PAR[2], 1)
		if (%EVENT_PAR[3] > 0)
			fade_out($EVENT_ID,%EVENT_PAR[3],1)
		end if
		if (%EVENT_PAR[3] < 0)
			fade_in($EVENT_ID,%EVENT_PAR[3])
		end if
		exit
	end if
	ignore_event ($EVENT_ID)

	{----Time----}

	if (%Timing_store[%EVENT_PAR[0]] < 51)
		$Time_amount := ((%Timing_store[%EVENT_PAR[0]]) * (12) / (50))
	else
		$Time_amount := ((%Timing_store[%EVENT_PAR[0]] - 51) * (47 - 12) / (100 - 51)) + 12
	end if
	
	$new_time := random (0,$Time_amount * 1000)
	
	if ($new_time > 1)
		wait($new_time)
	end if

	{----Velocity----}
	
	select (%Velocity_store[%EVENT_PAR[0]])
		case 0 to 9
			if (random(1,3) = 2)
				$new_vel :=	$EVENT_VELOCITY + (random (-%Velocity_store[%EVENT_PAR[0]],%Velocity_store[%EVENT_PAR[0]] / 2))
			else
				$new_vel := $EVENT_VELOCITY
			end if
			
		case 10 to 50
			$max_vel_amount := ((%Velocity_store[%EVENT_PAR[0]] - 10) * (20 - 10) / (50 - 10)) + 10
			$new_vel := $EVENT_VELOCITY + (random (-$max_vel_amount,$max_vel_amount) * (%Velocity_store[%EVENT_PAR[0]] *100/ 100)/100)
		
		case 51 to 100
		
			$max_vel_amount := ((%Velocity_store[%EVENT_PAR[0]] - 51) * (72 - 21) / (100 - 51)) + 21
			$new_vel := $EVENT_VELOCITY + (random (-$max_vel_amount,$max_vel_amount) * (%Velocity_store[%EVENT_PAR[0]] *100/ 100)/100)
			
	end select
	
	{check the velocity range}
	
	if ($new_vel > 127)
		$new_vel := 127
	end if
	if ($new_vel < 1)
		$new_vel := 1
	end if
	
	{----Tune----}
	
	if (%Tuning_store[%EVENT_PAR[0]] < 51)
		$new_tune := random (-250,250) * %Tuning_store[%EVENT_PAR[0]]
	else
		$Tune_amount := ((%Tuning_store[%EVENT_PAR[0]] - 51) * (1000 - 250) / (100 - 51)) + 250
		$new_tune := random (-$Tune_amount,$Tune_amount) * %Tuning_store[%EVENT_PAR[0]]
	end if

	{----Volume----}
	$new_vol := random (-60,60) * %Volume_store[%EVENT_PAR[0]]
	

	$new_event := play_note ($EVENT_NOTE, $new_vel, 0, -1)
	change_vol ($new_event, %EVENT_PAR[1] + $new_vol, 1)
	change_tune ($new_event, %EVENT_PAR[2] + $new_tune, 1)
	if (%EVENT_PAR[3] > 0)
		fade_out($new_event,%EVENT_PAR[3],1)
		set_event_par($new_event,3,0)
	end if

end on

on release
	if (%EVENT_PAR[3] > 0)
		fade_out($EVENT_ID,%EVENT_PAR[3],1)
	end if
end on

on ui_control ($Onoff)
	$a := 0
	while($a < $NUM_GROUPS)
		if (%GROUPS_SELECTED[$a] = 1)
			%onoff_store[$a] := $onoff
		end if
		inc($a)
	end while
end on
on ui_control ($Timing)
	$a := 0
	while($a < $NUM_GROUPS)
		if (%GROUPS_SELECTED[$a] = 1)
			%Timing_store[$a] := $Timing
		end if
		inc($a)
	end while
end on
on ui_control ($Velocity)
	$a := 0
	while($a < $NUM_GROUPS)
		if (%GROUPS_SELECTED[$a] = 1)
			%Velocity_store[$a] := $Velocity
		end if
		inc($a)
	end while
end on
on ui_control ($Tuning)
	$a := 0
	while($a < $NUM_GROUPS)
		if (%GROUPS_SELECTED[$a] = 1)
			%Tuning_store[$a] := $Tuning
		end if
		inc($a)
	end while
end on
on ui_control ($Volume)
	$a := 0
	while($a < $NUM_GROUPS)
		if (%GROUPS_SELECTED[$a] = 1)
			%Volume_store[$a] := $Volume
		end if
		inc($a)
	end while
end on

on ui_update
	$onoff := %onoff_store[$REF_GROUP_IDX]
	$timing := %timing_store[$REF_GROUP_IDX]
	$tuning := %tuning_store[$REF_GROUP_IDX]
	$velocity := %velocity_store[$REF_GROUP_IDX]
	$volume := %volume_store[$REF_GROUP_IDX]
end on
