{ ---------------------------------------------------------------------------- }
{ GALLUS ~ HARPSICHORD                                                         }
{ This script is part of the GALLUS ~ PROJET                                   }
{ Author : HEPARO / Hervé PARONNAUD                                            }
{ Contact / Support : contact@heparo.com                                       }
{ This script is licensed under a Creative Commons CC BY 4.0 License           }
{ https://creativecommons.org/licenses/by/4.0/                                 }
{ ---------------------------------------------------------------------------- }
on init

	set_snapshot_type(1) {init callback not executed upon snapshot loading}	
	make_perfview
	set_script_title("GALLUS ~ HARPSICHORD")
	set_ui_width_px(964)
	set_ui_height_px(242) { wallpaper_height_px - 68 }
	set_control_par_str($INST_ICON_ID,$CONTROL_PAR_PICTURE,"ico")
	message("")
	
	{ CONSTANTS ---------------------------------------------------------------- }
	
	declare const $TRUE := 1
	declare const $FALSE := 0
	
	declare const $ONE := 1
	declare const $ZERO := 0
	
	declare const $PLUS := 1
	declare const $MINUS := -1
	
	declare const $UP := -1
	declare const $STATIC := 0
	declare const $DOWN := 1
	
	declare @EMPTY
	@EMPTY := ""
	
	{ SPECIFIC CONSTANTS ------------------------------------------------------- }
	
	declare const $SLOT_LIMITER := 7
	
	{ INSTRUMENT INDEXES ------------------------------------------------------- }
	
	{ DYNAMICS GAINER }
	declare const $SLOT_GN_DYN := 0
	
	{ DYNAMICS CUTOFF }
	declare const $SLOT_CO_DYN := 1
	
	{ EXPRESSION GAINER }
	declare const $SLOT_GN_EXP := 2
	
	{ CHARACTER / FUNDAMENTAL EQ1 }
	declare const $SLOT_FUND_EQ := 2
	
	{ REVERB }
	declare const $SLOT_REVERB := 0
	
	{ STEREO }
	declare const $SLOT_STEREO := 5	
	
	{ SEND LEVELS }
	declare const $SLOT_SEND := 7
	
	{ VARIABLES ---------------------------------------------------------------- }
	
	declare $index0
	declare $index1
	declare $index2
	
	declare $intVar0
	declare $intVar1
	declare $intVar2
	
	declare ~realVar0
	declare ~realVar1
	declare ~realVar2
	
	declare @strVar0
	declare @strVar1
	declare @strVar2
	
	declare $initFlag := $TRUE
	
	declare $previousEvent
	declare $PreviousNote
		
	declare $newVelocity
	
	declare $forcedVelocity := 64	

	declare $knobSelLastValue := $ZERO	
	declare $knobSelDirection := $ZERO
		
	declare $verifiedModuleIndex := $ZERO
	declare $verifiedModuleMaxIndex := $ZERO
	
	declare $verifiedModuleIndex1 := $ZERO
	declare $verifiedModuleIndex2 := $ZERO
	declare $verifiedModuleIndex3 := $ZERO
	declare $verifiedModuleIndex4 := $ZERO
	declare $verifiedModuleIndex5 := $ZERO	
	
	{ UI VARIABLES ------------------------------------------------------------- }
	
	declare const $FONT_BLACK := 16
	declare const $FONT_WHITE := 17
	declare $fontColor := $FONT_WHITE
	
	declare const $ALIGN_RIGHT := 2
	declare const $ALIGN_MIDDLE := 1
	declare const $ALIGN_LEFT := 0
	declare $fontAlign := $ALIGN_RIGHT
	
	declare $PosY := 80
	declare const $IncrY := 20
	
	declare const $Col1X := 125
	declare const $Col2X := 275
	declare const $Col3X := 600
	declare const $Col4X := 650
	declare const $Col5X := 770
	
	declare %Timer[4] := (0)
	declare const $TIMER_DEFAULT := 0
	declare const $TIMER_VELOCITY := 1
	
	declare const $TEMPO_DEFAULT := 1500000
	declare const $TEMPO_VELOCITY := 3000000
	
	declare $Silent := $FALSE
	
	{ MODULES ------------------------------------------------------------------ }
	
	declare const $MODULE_DYNAMICS := 0
	declare const $MODULE_EXPRESSION := 1
	declare const $MODULE_FORCED_VELOCITY := 2	
	declare const $MODULE_STEREO_S := 3	
	declare const $MODULE_STEREO_P := 4	
	declare const $MODULE_REVERB_SLEVEL := 5
	declare const $MODULE_REVERB_MODE := 6		
	declare const $MODULE_VELO_SAMPlE_START := 7
	declare const $MODULE_VELO_VOLUME := 8
	declare const $MODULE_VELO_ATTACK := 9
	declare const $MODULE_VELO_RELEASE := 10
	declare const $MODULE_FUNDAMENTAL_GAIN := 11	
	declare const $MODULE_FUNDAMENTAL_WIDTH := 12	
	
	declare const $MODULES_COUNT := 13
	declare const $MODULES_VISIBLE := 5
	
	declare const $KNOBSEL_MAX_INDEX := $MODULES_COUNT - $MODULES_VISIBLE
	
	declare $moduleIndex := $ZERO	
	declare $moduleEditedIndex := $ZERO
	declare $moduleEditedValue := $ZERO
	
	declare !modulesLabels[$MODULES_COUNT]	
	$index0 := $ZERO
	while($index0 < $MODULES_COUNT)
		!modulesLabels[$index0] := @EMPTY
		inc ($index0)
	end while	
	!modulesLabels[$MODULE_DYNAMICS] := "DYNAMICS | CC1 | Default = 64"
	!modulesLabels[$MODULE_EXPRESSION] := "EXPRESSION | CC11 | Default = 64"
	!modulesLabels[$MODULE_FORCED_VELOCITY]  := "VELOCITY | Forced | Default = 64 (mf)"
	!modulesLabels[$MODULE_STEREO_S] := "STEREO | Spread | Default = 0%"
	!modulesLabels[$MODULE_STEREO_P] := "STEREO | Pan | Default = Center"
	!modulesLabels[$MODULE_REVERB_SLEVEL] := "REVERB | Send Level | Default = -18dB"
	!modulesLabels[$MODULE_REVERB_MODE] := "REVERB | Mode | Default = Hall"
	!modulesLabels[$MODULE_VELO_SAMPlE_START] := "MODULATION | Velocity for Sample Start  | Default = 20%"
	!modulesLabels[$MODULE_VELO_VOLUME] := "MODULATION | Velocity for Volume  | Default = 20%"
	!modulesLabels[$MODULE_VELO_ATTACK] := "MODULATION | Velocity for Attack  | Default = 20%"
	!modulesLabels[$MODULE_VELO_RELEASE] := "MODULATION | Velocity for Release  | Default = 20%"
	!modulesLabels[$MODULE_FUNDAMENTAL_GAIN] := "FUNDAMENTAL | Gain | Default amount = 3 dB"
	!modulesLabels[$MODULE_FUNDAMENTAL_WIDTH] := "FUNDAMENTAL | Width | Default amount = 0.5 oc"	
	
	declare %modulesDefaultStatus[$MODULES_COUNT] := ($FALSE)
	
	declare %modulesDefaultValues[$MODULES_COUNT] := ($ZERO)
	%modulesDefaultValues[$MODULE_FORCED_VELOCITY] := 510000
	%modulesDefaultValues[$MODULE_DYNAMICS] := 510000
	%modulesDefaultValues[$MODULE_EXPRESSION] := 510000
	%modulesDefaultValues[$MODULE_STEREO_S] := 500000
	%modulesDefaultValues[$MODULE_STEREO_P] := 500000
	%modulesDefaultValues[$MODULE_REVERB_SLEVEL] := 315551 {397115 = -12dB}
	%modulesDefaultValues[$MODULE_REVERB_MODE] := 0
	%modulesDefaultValues[$MODULE_VELO_SAMPlE_START] := 447000 { 20% }
	%modulesDefaultValues[$MODULE_VELO_VOLUME] := 447000 { 20% }
	%modulesDefaultValues[$MODULE_VELO_ATTACK] := 447000 { 20% }
	%modulesDefaultValues[$MODULE_VELO_RELEASE] := 447000 { 20% }
	%modulesDefaultValues[$MODULE_FUNDAMENTAL_GAIN] := 582500 { 3db }
	%modulesDefaultValues[$MODULE_FUNDAMENTAL_WIDTH] := 62500 { 0.5oc }
	
	declare %modulesValues[$MODULES_COUNT] := ($ZERO)	
	
	declare %modulesStatus[$MODULES_COUNT] := ($FALSE)		
	
	$index0 := $ZERO
	while( $index0 < $MODULES_COUNT )
		%modulesStatus[$index0] := %modulesDefaultStatus[$index0]
		%modulesValues[$index0] := %modulesDefaultValues[$index0]
		inc($index0)
	end while
	
	declare %modulesAllowed[$MODULES_COUNT] := ($TRUE)	
		
	{ BACKGROUND ------------------------------------------------------------- }
	
	declare ui_label $bgLbl(1,1)
	set_text ($bgLbl,"")
	declare $bgLbl_id := get_ui_id($bgLbl)
	set_control_par_str($bgLbl_id,$CONTROL_PAR_PICTURE,"Bg_Default")
	set_control_par($bgLbl_id,$CONTROL_PAR_Z_LAYER,-1)
	set_control_par($bgLbl_id,$CONTROL_PAR_WIDTH,635)
	set_control_par($bgLbl_id,$CONTROL_PAR_HEIGHT,64)
	set_control_par($bgLbl_id,$CONTROL_PAR_POS_X,0)
	set_control_par($bgLbl_id,$CONTROL_PAR_POS_Y,263)
	
	{ VELOCITY ----------------------------------------------------------------- }
	
	declare ui_slider $KnobVelocity(0,127)
	declare $KnobVelocity_id := get_ui_id($KnobVelocity)	
	set_control_par_str($KnobVelocity_id,$CONTROL_PAR_PICTURE,"Velocity")
	set_control_par($KnobVelocity_id,$CONTROL_PAR_POS_X,10)
	set_control_par($KnobVelocity_id,$CONTROL_PAR_POS_Y,28)
	set_control_par($KnobVelocity_id,$CONTROL_PAR_MOUSE_BEHAVIOUR,-1000)
	set_control_par($KnobVelocity_id,$CONTROL_PAR_DEFAULT_VALUE,$ZERO)
	set_control_par($KnobVelocity_id,$CONTROL_PAR_VALUE,$ZERO)
	
	{ SELECTOR ----------------------------------------------------------------- }
	
	declare ui_slider $KnobSel(0,$KNOBSEL_MAX_INDEX)
	declare $KnobSel_id := get_ui_id($KnobSel)	
	set_control_par_str($KnobSel_id,$CONTROL_PAR_PICTURE,"Knob")
	set_control_par($KnobSel_id,$CONTROL_PAR_POS_X,134)
	set_control_par($KnobSel_id,$CONTROL_PAR_POS_Y,84)	
	set_control_par($KnobSel_id,$CONTROL_PAR_MOUSE_BEHAVIOUR,-1000)
	set_control_par($KnobSel_id,$CONTROL_PAR_DEFAULT_VALUE,$ZERO)
	set_control_par($KnobSel_id,$CONTROL_PAR_VALUE,$ZERO)	
		
	{ EDITED VALUE ------------------------------------------------------------- }
	
	declare ui_label $editedValLbl(1,1)
	set_text ($editedValLbl,@EMPTY)
	hide_part($editedValLbl,$HIDE_PART_BG)
	declare $editedValLbl_id := get_ui_id($editedValLbl)
	set_control_par($editedValLbl_id,$CONTROL_PAR_WIDTH,110)
	set_control_par($editedValLbl_id,$CONTROL_PAR_POS_X,725)
	set_control_par($editedValLbl_id,$CONTROL_PAR_POS_Y,180)
	set_control_par($editedValLbl_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($editedValLbl_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	
	{ ITEM 1 ------------------------------------------------------------------- }	
	
	while( $verifiedModuleIndex < $MODULES_COUNT and %modulesAllowed[$verifiedModuleIndex] = $FALSE )
		inc($verifiedModuleIndex)
	end while
	
	declare ui_label $item1Lbl(1,1)	
	set_text($item1Lbl,!modulesLabels[$verifiedModuleIndex])
	hide_part($item1Lbl,$HIDE_PART_BG)
	declare $item1Lbl_id := get_ui_id($item1Lbl)	
	set_control_par($item1Lbl_id,$CONTROL_PAR_WIDTH,300)
	set_control_par($item1Lbl_id,$CONTROL_PAR_POS_X,$Col2X)
	set_control_par($item1Lbl_id,$CONTROL_PAR_POS_Y,$PosY+1)
	set_control_par($item1Lbl_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($item1Lbl_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	
	declare ui_switch $item1Toggle	
	declare $item1Toggle_id := get_ui_id($item1Toggle)		
	set_control_par_str($item1Toggle_id,$CONTROL_PAR_PICTURE,"Switch")
	set_control_par($item1Toggle_id,$CONTROL_PAR_WIDTH,36)
	set_control_par($item1Toggle_id,$CONTROL_PAR_HEIGHT,18)
	set_control_par($item1Toggle_id,$CONTROL_PAR_POS_X,$Col3X)
	set_control_par($item1Toggle_id,$CONTROL_PAR_POS_Y,$PosY-2)
	set_control_par($item1Toggle_id,$CONTROL_PAR_TEXTPOS_Y,1000)
	set_control_par($item1Toggle_id,$CONTROL_PAR_VALUE,%modulesStatus[$verifiedModuleIndex])
	
	declare ui_slider $item1Slider(0,1000000)	
	declare $item1Slider_id := get_ui_id($item1Slider)
	set_control_par_str($item1Slider_id,$CONTROL_PAR_PICTURE,"Slider")
	set_control_par($item1Slider_id,$CONTROL_PAR_WIDTH,112)
	set_control_par($item1Slider_id,$CONTROL_PAR_HEIGHT,32)
	set_control_par($item1Slider_id,$CONTROL_PAR_POS_X,$Col4X)
	set_control_par($item1Slider_id,$CONTROL_PAR_POS_Y,$PosY-8)
	set_control_par($item1Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	set_control_par($item1Slider_id,$CONTROL_PAR_VALUE,%modulesValues[$verifiedModuleIndex])
		
	$PosY := $PosY + $IncrY
	
	{ ITEM 2 ------------------------------------------------------------------- }
	
	inc($verifiedModuleIndex)
	while( $verifiedModuleIndex < $MODULES_COUNT and %modulesAllowed[$verifiedModuleIndex] = $FALSE )
		inc($verifiedModuleIndex)
	end while
	
	declare ui_label $item2Lbl(1,1)	
	set_text($item2Lbl,!modulesLabels[$verifiedModuleIndex])
	hide_part($item2Lbl,$HIDE_PART_BG)
	declare $item2Lbl_id := get_ui_id($item2Lbl)	
	set_control_par($item2Lbl_id,$CONTROL_PAR_WIDTH,300)
	set_control_par($item2Lbl_id,$CONTROL_PAR_POS_X,$Col2X)
	set_control_par($item2Lbl_id,$CONTROL_PAR_POS_Y,$PosY+1)
	set_control_par($item2Lbl_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($item2Lbl_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	
	declare ui_switch $item2Toggle
	declare $item2Toggle_id := get_ui_id($item2Toggle)		
	set_control_par_str($item2Toggle_id,$CONTROL_PAR_PICTURE,"Switch")
	set_control_par($item2Toggle_id,$CONTROL_PAR_WIDTH,36)
	set_control_par($item2Toggle_id,$CONTROL_PAR_HEIGHT,18)
	set_control_par($item2Toggle_id,$CONTROL_PAR_POS_X,$Col3X)
	set_control_par($item2Toggle_id,$CONTROL_PAR_POS_Y,$PosY-1)
	set_control_par($item2Toggle_id,$CONTROL_PAR_TEXTPOS_Y,1000)
	set_control_par($item2Toggle_id,$CONTROL_PAR_VALUE,%modulesStatus[$verifiedModuleIndex])
	
	declare ui_slider $item2Slider(0,1000000)
	declare $item2Slider_id := get_ui_id($item2Slider)
	set_control_par_str($item2Slider_id,$CONTROL_PAR_PICTURE,"Slider")
	set_control_par($item2Slider_id,$CONTROL_PAR_WIDTH,112)
	set_control_par($item2Slider_id,$CONTROL_PAR_HEIGHT,32)
	set_control_par($item2Slider_id,$CONTROL_PAR_POS_X,$Col4X)
	set_control_par($item2Slider_id,$CONTROL_PAR_POS_Y,$PosY-7)
	set_control_par($item2Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	set_control_par($item2Slider_id,$CONTROL_PAR_VALUE,%modulesValues[$verifiedModuleIndex])
		
	$PosY := $PosY + $IncrY
	
	{ ITEM 3 ------------------------------------------------------------------- }
	
	inc($verifiedModuleIndex)
	while( $verifiedModuleIndex < $MODULES_COUNT and %modulesAllowed[$verifiedModuleIndex] = $FALSE )
		inc($verifiedModuleIndex)
	end while
	
	declare ui_label $item3Lbl(1,1)	
	set_text($item3Lbl,!modulesLabels[$verifiedModuleIndex])
	hide_part($item3Lbl,$HIDE_PART_BG)
	declare $item3Lbl_id := get_ui_id($item3Lbl)	
	set_control_par($item3Lbl_id,$CONTROL_PAR_WIDTH,300)
	set_control_par($item3Lbl_id,$CONTROL_PAR_POS_X,$Col2X)
	set_control_par($item3Lbl_id,$CONTROL_PAR_POS_Y,$PosY+1)
	set_control_par($item3Lbl_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($item3Lbl_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	
	declare ui_switch $item3Toggle
	declare $item3Toggle_id := get_ui_id($item3Toggle)		
	set_control_par_str($item3Toggle_id,$CONTROL_PAR_PICTURE,"Switch")
	set_control_par($item3Toggle_id,$CONTROL_PAR_WIDTH,36)
	set_control_par($item3Toggle_id,$CONTROL_PAR_HEIGHT,18)
	set_control_par($item3Toggle_id,$CONTROL_PAR_POS_X,$Col3X)
	set_control_par($item3Toggle_id,$CONTROL_PAR_POS_Y,$PosY-1)
	set_control_par($item3Toggle_id,$CONTROL_PAR_TEXTPOS_Y,1000)
	set_control_par($item3Toggle_id,$CONTROL_PAR_VALUE,%modulesStatus[$verifiedModuleIndex])
	
	declare ui_slider $item3Slider(0,1000000)
	declare $item3Slider_id := get_ui_id($item3Slider)
	set_control_par_str($item3Slider_id,$CONTROL_PAR_PICTURE,"Slider")
	set_control_par($item3Slider_id,$CONTROL_PAR_WIDTH,112)
	set_control_par($item3Slider_id,$CONTROL_PAR_HEIGHT,32)
	set_control_par($item3Slider_id,$CONTROL_PAR_POS_X,$Col4X)
	set_control_par($item3Slider_id,$CONTROL_PAR_POS_Y,$PosY-7)
	set_control_par($item3Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	set_control_par($item3Slider_id,$CONTROL_PAR_VALUE,%modulesValues[$verifiedModuleIndex])
		
	$PosY := $PosY + $IncrY
	
	{ ITEM 4 ------------------------------------------------------------------- }
	
	inc($verifiedModuleIndex)
	while( $verifiedModuleIndex < $MODULES_COUNT and %modulesAllowed[$verifiedModuleIndex] = $FALSE )
		inc($verifiedModuleIndex)
	end while
	
	declare ui_label $item4Lbl(1,1)	
	set_text($item4Lbl,!modulesLabels[$verifiedModuleIndex])
	hide_part($item4Lbl,$HIDE_PART_BG)
	declare $item4Lbl_id := get_ui_id($item4Lbl)	
	set_control_par($item4Lbl_id,$CONTROL_PAR_WIDTH,300)
	set_control_par($item4Lbl_id,$CONTROL_PAR_POS_X,$Col2X)
	set_control_par($item4Lbl_id,$CONTROL_PAR_POS_Y,$PosY+1)
	set_control_par($item4Lbl_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($item4Lbl_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	
	declare ui_switch $item4Toggle
	declare $item4Toggle_id := get_ui_id($item4Toggle)		
	set_control_par_str($item4Toggle_id,$CONTROL_PAR_PICTURE,"Switch")
	set_control_par($item4Toggle_id,$CONTROL_PAR_WIDTH,36)
	set_control_par($item4Toggle_id,$CONTROL_PAR_HEIGHT,18)
	set_control_par($item4Toggle_id,$CONTROL_PAR_POS_X,$Col3X)
	set_control_par($item4Toggle_id,$CONTROL_PAR_POS_Y,$PosY-1)
	set_control_par($item4Toggle_id,$CONTROL_PAR_TEXTPOS_Y,1000)
	set_control_par($item4Toggle_id,$CONTROL_PAR_VALUE,%modulesStatus[$verifiedModuleIndex])
	
	declare ui_slider $item4Slider(0,1000000)
	declare $item4Slider_id := get_ui_id($item4Slider)
	set_control_par_str($item4Slider_id,$CONTROL_PAR_PICTURE,"Slider")
	set_control_par($item4Slider_id,$CONTROL_PAR_WIDTH,112)
	set_control_par($item4Slider_id,$CONTROL_PAR_HEIGHT,32)
	set_control_par($item4Slider_id,$CONTROL_PAR_POS_X,$Col4X)
	set_control_par($item4Slider_id,$CONTROL_PAR_POS_Y,$PosY-7)
	set_control_par($item4Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	set_control_par($item4Slider_id,$CONTROL_PAR_VALUE,%modulesValues[$verifiedModuleIndex])
		
	$PosY := $PosY + $IncrY
	
	{ ITEM 5 ------------------------------------------------------------------- }
	
	
	inc($verifiedModuleIndex)
	while( $verifiedModuleIndex < $MODULES_COUNT and %modulesAllowed[$verifiedModuleIndex] = $FALSE )
		inc($verifiedModuleIndex)
	end while
	
	declare ui_label $item5Lbl(1,1)	
	set_text($item5Lbl,!modulesLabels[$verifiedModuleIndex])
	hide_part($item5Lbl,$HIDE_PART_BG)
	declare $item5Lbl_id := get_ui_id($item5Lbl)	
	set_control_par($item5Lbl_id,$CONTROL_PAR_WIDTH,300)
	set_control_par($item5Lbl_id,$CONTROL_PAR_POS_X,$Col2X)
	set_control_par($item5Lbl_id,$CONTROL_PAR_POS_Y,$PosY+1)
	set_control_par($item5Lbl_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($item5Lbl_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	
	declare ui_switch $item5Toggle
	declare $item5Toggle_id := get_ui_id($item5Toggle)		
	set_control_par_str($item5Toggle_id,$CONTROL_PAR_PICTURE,"Switch")
	set_control_par($item5Toggle_id,$CONTROL_PAR_WIDTH,36)
	set_control_par($item5Toggle_id,$CONTROL_PAR_HEIGHT,18)
	set_control_par($item5Toggle_id,$CONTROL_PAR_POS_X,$Col3X)
	set_control_par($item5Toggle_id,$CONTROL_PAR_POS_Y,$PosY-1)
	set_control_par($item5Toggle_id,$CONTROL_PAR_TEXTPOS_Y,1000)
	set_control_par($item5Toggle_id,$CONTROL_PAR_VALUE,%modulesStatus[$verifiedModuleIndex])
	
	declare ui_slider $item5Slider(0,1000000)
	declare $item5Slider_id := get_ui_id($item5Slider)
	set_control_par_str($item5Slider_id,$CONTROL_PAR_PICTURE,"Slider")
	set_control_par($item5Slider_id,$CONTROL_PAR_WIDTH,112)
	set_control_par($item5Slider_id,$CONTROL_PAR_HEIGHT,32)
	set_control_par($item5Slider_id,$CONTROL_PAR_POS_X,$Col4X)
	set_control_par($item5Slider_id,$CONTROL_PAR_POS_Y,$PosY-7)
	set_control_par($item5Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	set_control_par($item5Slider_id,$CONTROL_PAR_VALUE,%modulesValues[$verifiedModuleIndex])
		
	$PosY := $PosY + $IncrY

	{ CONFIGURATION ------------------------------------------------------------ }
	
	declare ui_label $configBg(1,1)
	set_text ($configBg,"")
	declare $configBg_id := get_ui_id($configBg)
	set_control_par($configBg_id,$CONTROL_PAR_Z_LAYER,-1)
	set_control_par($configBg_id,$CONTROL_PAR_WIDTH,964)
	set_control_par($configBg_id,$CONTROL_PAR_HEIGHT,75)
	set_control_par($configBg_id,$CONTROL_PAR_POS_X,0)
	set_control_par($configBg_id,$CONTROL_PAR_POS_Y,0)	
	set_control_par($configBg_id,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)
	
	declare ui_button $configButton
	set_text ($configButton,@EMPTY)
	declare $configButton_id := get_ui_id($configButton)	
	set_control_par_str($configButton_id,$CONTROL_PAR_PICTURE,"Config")
	set_control_par($configButton_id, $CONTROL_PAR_POS_X, 880)
	set_control_par($configButton_id, $CONTROL_PAR_POS_Y, 3)
	
	declare ui_panel $configPanel
	declare $configPanel_id := get_ui_id($configPanel)	
	move_control($configPanel,0,0)
	set_control_par($configPanel_id,$CONTROL_PAR_Z_LAYER,1)
	
	declare ui_label $configLabel (6,1)
	set_text ($configLabel,"CONFIGURATION")
	declare $configLabel_id := get_ui_id($configLabel)	
	set_control_par($configLabel_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($configLabel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_RIGHT)
	set_control_par($configLabel_id,$CONTROL_PAR_WIDTH,115)
	set_control_par($configLabel_id, $CONTROL_PAR_POS_X, 1)
	set_control_par($configLabel_id, $CONTROL_PAR_POS_Y, 3)
	
	declare ui_button $configDoneButton
	set_text ($configDoneButton,"Done")	
	declare $configDoneButton_id := get_ui_id($configDoneButton)	
	set_control_par($configDoneButton_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($configDoneButton_id,$CONTROL_PAR_WIDTH,100)
	set_control_par($configDoneButton_id, $CONTROL_PAR_POS_X, 800)
	set_control_par($configDoneButton_id, $CONTROL_PAR_POS_Y, 55)
	
	declare ui_button $defaultValuesButton
	set_text ($defaultValuesButton,"Default Values")	
	declare $defaultValuesButton_id := get_ui_id($defaultValuesButton)	
	set_control_par($defaultValuesButton_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($defaultValuesButton_id,$CONTROL_PAR_WIDTH,100)
	set_control_par($defaultValuesButton_id, $CONTROL_PAR_POS_X, 800)
	set_control_par($defaultValuesButton_id, $CONTROL_PAR_POS_Y, 37)
	
	declare ui_label $titleEditLabel (1,1)
	set_text ($titleEditLabel,"Title")
	declare $titleEditLabel_id := get_ui_id($titleEditLabel)	
	set_control_par($titleEditLabel_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($titleEditLabel_id,$CONTROL_PAR_WIDTH,100)
	set_control_par($titleEditLabel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_RIGHT)
	set_control_par($titleEditLabel_id, $CONTROL_PAR_POS_X, 122)
	set_control_par($titleEditLabel_id, $CONTROL_PAR_POS_Y, 3)
	
	declare ui_text_edit @titleEdit
	declare $titleEdit_id := get_ui_id(@titleEdit)	
	set_control_par($titleEdit_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par_str($titleEdit_id,$CONTROL_PAR_TEXT,"empty")
	set_control_par($titleEdit_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($titleEdit_id,$CONTROL_PAR_WIDTH,170)
	set_control_par($titleEdit_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	set_control_par_str($titleEdit_id,$CONTROL_PAR_PICTURE,"Bg_Default")	
	set_control_par($titleEdit_id, $CONTROL_PAR_POS_X, 225)
	set_control_par($titleEdit_id, $CONTROL_PAR_POS_Y, 3)
	
	declare ui_label $subtitleEditLabel (1,1)
	set_text ($subtitleEditLabel,"Subtitle")
	declare $subtitleEditLabel_id := get_ui_id($subtitleEditLabel)	
	set_control_par($subtitleEditLabel_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($subtitleEditLabel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_RIGHT)
	set_control_par($subtitleEditLabel_id,$CONTROL_PAR_WIDTH,100)
	set_control_par($subtitleEditLabel_id, $CONTROL_PAR_POS_X, 122)
	set_control_par($subtitleEditLabel_id, $CONTROL_PAR_POS_Y, 23)
	
	declare ui_text_edit @subtitleEdit	
	declare $subtitleEdit_id := get_ui_id(@subtitleEdit)	
	set_control_par($subtitleEdit_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par_str($subtitleEdit_id,$CONTROL_PAR_TEXT,"empty")
	set_control_par($subtitleEdit_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($subtitleEdit_id,$CONTROL_PAR_WIDTH,170)
	set_control_par($subtitleEdit_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_LEFT)
	set_control_par_str($subtitleEdit_id,$CONTROL_PAR_PICTURE,"Bg_Default")	
	set_control_par($subtitleEdit_id, $CONTROL_PAR_POS_X, 225)
	set_control_par($subtitleEdit_id, $CONTROL_PAR_POS_Y, 23)	
	
	declare ui_switch $dynamicsDrivenByVel
	set_text ($dynamicsDrivenByVel,"No CC1 = VELO")	
	set_control_help($dynamicsDrivenByVel,"If [ON] then if DYNAMICS CC1 is disabled then DYNAMICS is computed with Velocity. If [OFF] then Dynamics CC1 is not computed at all")
	move_control($dynamicsDrivenByVel,4,2)
	declare $dynamicsDrivenByVel_id := get_ui_id($dynamicsDrivenByVel)	
	set_control_par($dynamicsDrivenByVel_id,$CONTROL_PAR_DEFAULT_VALUE,$TRUE)
	set_control_par($dynamicsDrivenByVel_id,$CONTROL_PAR_VALUE,$TRUE)
	set_control_par($dynamicsDrivenByVel_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($dynamicsDrivenByVel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_RIGHT)
	set_control_par($dynamicsDrivenByVel_id,$CONTROL_PAR_WIDTH,100)
	set_control_par($dynamicsDrivenByVel_id, $CONTROL_PAR_POS_X, 425)
	set_control_par($dynamicsDrivenByVel_id, $CONTROL_PAR_POS_Y, 3)
	
	
	declare ui_switch $expressionDrivenByVel
	set_text ($expressionDrivenByVel,"No CC11 = VELO")
	set_control_help($dynamicsDrivenByVel,"If [ON] then if EXPRESSION CC11 is disabled then EXPRESSION is computed with Velocity. If [OFF] then EXPRESSION CC11 is not computed at all")
	move_control($expressionDrivenByVel,4,3)
	declare $expressionDrivenByVel_id := get_ui_id($expressionDrivenByVel)	
	set_control_par($expressionDrivenByVel_id,$CONTROL_PAR_DEFAULT_VALUE,$TRUE)
	set_control_par($expressionDrivenByVel_id,$CONTROL_PAR_VALUE,$TRUE)
	set_control_par($expressionDrivenByVel_id,$CONTROL_PAR_PARENT_PANEL,$configPanel_id)
	set_control_par($expressionDrivenByVel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_RIGHT)
	set_control_par($expressionDrivenByVel_id,$CONTROL_PAR_WIDTH,100)
	set_control_par($expressionDrivenByVel_id, $CONTROL_PAR_POS_X, 425)
	set_control_par($expressionDrivenByVel_id, $CONTROL_PAR_POS_Y, 23)
	
	{ TITLES & SUBTITLES ------------------------------------------------------- }
	
	$PosY := $PosY + $IncrY	
	
	declare ui_label $titleLabel (1,1)
	set_text ($titleLabel,@titleEdit)
	hide_part($titleLabel,$HIDE_PART_BG)
	declare $titleLabel_id := get_ui_id($titleLabel)
	set_control_par($titleLabel_id,$CONTROL_PAR_WIDTH,110)
	set_control_par($titleLabel_id,$CONTROL_PAR_POS_X,4)
	set_control_par($titleLabel_id,$CONTROL_PAR_POS_Y,$PosY)
	set_control_par($titleLabel_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($titleLabel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_MIDDLE)
	
	declare ui_label $subtitleLabel (1,1)
	set_text($subtitleLabel,@subtitleEdit)
	hide_part($subtitleLabel,$HIDE_PART_BG)
	declare $subtitleLabel_id := get_ui_id($subtitleLabel)
	set_control_par($subtitleLabel_id,$CONTROL_PAR_WIDTH,110)
	set_control_par($subtitleLabel_id,$CONTROL_PAR_POS_X,4)
	set_control_par($subtitleLabel_id,$CONTROL_PAR_POS_Y,$PosY + 15)
	set_control_par($subtitleLabel_id,$CONTROL_PAR_FONT_TYPE,$fontColor)
	set_control_par($subtitleLabel_id,$CONTROL_PAR_TEXT_ALIGNMENT,$ALIGN_MIDDLE)
	
	{ PERSISTENCE -------------------------------------------------------------- }
	
	make_persistent(%modulesStatus)
	make_persistent(%modulesValues)
	make_persistent(%modulesAllowed)
	
	make_persistent($item1Toggle)
	make_persistent($item1Slider)
	
	make_persistent($item2Toggle)
	make_persistent($item2Slider)
	
	make_persistent($item3Toggle)
	make_persistent($item3Slider)
	
	make_persistent($item4Toggle)
	make_persistent($item4Slider)
	
	make_persistent($item5Toggle)
	make_persistent($item5Slider)	
	
	make_persistent(@titleEdit)	
	make_persistent(@subtitleEdit)
	
	make_persistent($dynamicsDrivenByVel)
	make_persistent($expressionDrivenByVel)
	
	{ -------------------------------------------------------------------------- }
	
	message(@EMPTY)	

end on

{ ---------------------------------------------------------------------------- }
function PrintInfos

	if( $Silent = $FALSE )

		inc(%Timer[$TIMER_DEFAULT])
		
		if( %Timer[$TIMER_DEFAULT]>3 )
			%Timer[$TIMER_DEFAULT] := 3
		end if
		
		if( %Timer[$TIMER_DEFAULT]<1 )
			%Timer[$TIMER_DEFAULT] := 1
		end if
		
		set_text($editedValLbl,@strVar0)	
		message(@strVar0)		
		wait($TEMPO_DEFAULT)
		
		dec(%Timer[$TIMER_DEFAULT])
		if( %Timer[$TIMER_DEFAULT]=$ZERO )
			set_text($editedValLbl,@EMPTY)
			message(@EMPTY)	
		end if
		
	end if

end function

{ ---------------------------------------------------------------------------- }
function VerifyModuleIndex
	while( $verifiedModuleIndex < $MODULES_COUNT and %modulesAllowed[$verifiedModuleIndex] = $FALSE )
		inc($verifiedModuleIndex)
	end while
end function

{ ---------------------------------------------------------------------------- }
function VerifyModuleMaxIndex
	$intVar0 := $ZERO	
	$index0 := $MODULES_COUNT - 1
	while( $index0 > $ZERO )	
		if( not ($intVar0 = $MODULES_VISIBLE) )
			if( %modulesAllowed[$index0] = $TRUE )
				$verifiedModuleMaxIndex := $index0			
				inc($intVar0)
			end if
		end if		
		dec($index0)
	end while	
end function

{ ---------------------------------------------------------------------------- }
function Repaint

	$verifiedModuleIndex := $moduleIndex	
	call VerifyModuleIndex
	$verifiedModuleIndex1 := $verifiedModuleIndex
	
	set_text($item1Lbl,!modulesLabels[$verifiedModuleIndex])	
	$item1Toggle := %modulesStatus[$verifiedModuleIndex]
	$item1Slider := %modulesValues[$verifiedModuleIndex]
	set_control_par($item1Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])	
	
	inc($verifiedModuleIndex)	
	call VerifyModuleIndex
	$verifiedModuleIndex2 := $verifiedModuleIndex
	
	set_text($item2Lbl,!modulesLabels[$verifiedModuleIndex])
	$item2Toggle := %modulesStatus[$verifiedModuleIndex]
	$item2Slider := %modulesValues[$verifiedModuleIndex]
	set_control_par($item2Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])	
	
	inc($verifiedModuleIndex)	
	call VerifyModuleIndex
	$verifiedModuleIndex3 := $verifiedModuleIndex
	
	set_text($item3Lbl,!modulesLabels[$verifiedModuleIndex])
	$item3Toggle := %modulesStatus[$verifiedModuleIndex]
	$item3Slider := %modulesValues[$verifiedModuleIndex]
	set_control_par($item3Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])	
	
	inc($verifiedModuleIndex)	
	call VerifyModuleIndex
	$verifiedModuleIndex4 := $verifiedModuleIndex
	
	set_text($item4Lbl,!modulesLabels[$verifiedModuleIndex])
	$item4Toggle := %modulesStatus[$verifiedModuleIndex]
	$item4Slider := %modulesValues[$verifiedModuleIndex]
	set_control_par($item4Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	
	inc($verifiedModuleIndex)	
	call VerifyModuleIndex
	$verifiedModuleIndex5 := $verifiedModuleIndex
	
	set_text($item5Lbl,!modulesLabels[$verifiedModuleIndex])
	$item5Toggle := %modulesStatus[$verifiedModuleIndex]
	$item5Slider := %modulesValues[$verifiedModuleIndex]
	set_control_par($item5Slider_id,$CONTROL_PAR_DEFAULT_VALUE,%modulesDefaultValues[$verifiedModuleIndex])
	
	{ Titles ------------------------------------------------------------------- }
	
	set_text($titleLabel,@titleEdit)
	set_text($subtitleLabel,@subtitleEdit)
	
end function

{ ---------------------------------------------------------------------------- }
function PerformPostInit

	{ $index0 := 0 }
	{ while($index0<$NUM_GROUPS) }
		{ set_engine_par($ENGINE_PAR_ATTACK,0,$index0,find_mod($index0,"ENV_AHDSR"),-1) }
		{ set_engine_par($ENGINE_PAR_DECAY,548100,$index0,find_mod($index0,"ENV_AHDSR"),-1) }
		{ set_engine_par($ENGINE_PAR_RELEASE,548100,$index0,find_mod($index0,"ENV_AHDSR"),-1)		 }
		{ inc($index0) }
	{ end while	 }

	%modulesDefaultStatus[$MODULE_REVERB_SLEVEL] := $TRUE	
	%modulesDefaultStatus[$MODULE_REVERB_MODE] := $TRUE
	
	{ Put limiter to -6 dB output }
	{ set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN,315000,-1,$SLOT_LIMITER,$NI_MAIN_BUS) }
	{ Put limiter to -3 dB output }
	set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN,354000,-1,$SLOT_LIMITER,$NI_MAIN_BUS)
	
	
	call VerifyModuleMaxIndex
	call Repaint
	
end function

{ ---------------------------------------------------------------------------- }
on ui_control ($configButton)	
	move_control($configPanel,1,1)
	set_control_par($configBg_id,$CONTROL_PAR_HIDE,$HIDE_PART_NOTHING)
end on

{ ---------------------------------------------------------------------------- }
on ui_control ($configDoneButton)	

	{ UI Cleaning -------------------------------------------------------------- }

	$configButton := 0
	$configDoneButton := 0
	move_control($configPanel,0,0)	
	set_control_par($configBg_id,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)
	
	{ Config stuff ------------------------------------------------------------- }
	
	call PerformPostInit
	call Repaint
	
end on

{ ---------------------------------------------------------------------------- }
function SetDynamics

	{ 7875 = 1000000 / 127 in order to have min CC value = 1 }
	if( %modulesValues[$MODULE_DYNAMICS] < 7875 )
		%modulesValues[$MODULE_DYNAMICS] := 7875
	end if
	
	$intVar0 := %modulesValues[$MODULE_DYNAMICS] * 127 / 1000000

	if( %modulesStatus[$MODULE_DYNAMICS] = $FALSE )	
		set_engine_par($ENGINE_PAR_GN_GAIN,$intVar0*1600+200000,-1,$SLOT_GN_DYN,$NI_INSERT_BUS)
		set_engine_par($ENGINE_PAR_CUTOFF,$intVar0*1600+800000,-1,$SLOT_CO_DYN,$NI_INSERT_BUS)	
	else	
		inc(%Timer[$TIMER_DEFAULT])	
		set_engine_par($ENGINE_PAR_GN_GAIN,$intVar0*1600+200000,-1,$SLOT_GN_DYN,$NI_INSERT_BUS)
		set_engine_par($ENGINE_PAR_CUTOFF,$intVar0*1600+800000,-1,$SLOT_CO_DYN,$NI_INSERT_BUS)	
		
		@strVar0 := "CC1 " & $intVar0
		call PrintInfos	
		
	end if
	
end function

{ ---------------------------------------------------------------------------- }
function SetExpression

	{ 7875 = 1000000 / 127 in order to have min CC value = 1 }
	if( %modulesValues[$MODULE_EXPRESSION] < 7875 )
		%modulesValues[$MODULE_EXPRESSION] := 7875
	end if
	
	$intVar0 := %modulesValues[$MODULE_EXPRESSION] * 127 / 1000000	

	if( %modulesStatus[$MODULE_EXPRESSION] = $FALSE )
		set_engine_par($ENGINE_PAR_GN_GAIN,$intVar0*1000+270000,-1,$SLOT_GN_EXP,$NI_INSERT_BUS)	
	else	
		inc(%Timer[$TIMER_DEFAULT])		
		set_engine_par($ENGINE_PAR_GN_GAIN,$intVar0*1000+270000,-1,$SLOT_GN_EXP,$NI_INSERT_BUS)

		@strVar0 := "CC11 " & $intVar0
		call PrintInfos	
		
	end if
	
end function

{ ---------------------------------------------------------------------------- }
function SetForcedVelocity
	
	{ 7875 = 1000000 / 127 in order to have min CC value = 1 }
	if( %modulesValues[$MODULE_FORCED_VELOCITY] < 7875 )
		%modulesValues[$MODULE_FORCED_VELOCITY] := 7875
	end if
	
	$forcedVelocity := %modulesValues[$MODULE_FORCED_VELOCITY] * 127 / 1000000	
	
	@strVar0 := $forcedVelocity
	call PrintInfos	
		
end function

{ ---------------------------------------------------------------------------- }
function SetVelocityToSampleStart
	
	$index0 := 0
	while($index0<$NUM_GROUPS)
		set_engine_par($ENGINE_PAR_MOD_TARGET_INTENSITY,%modulesValues[$MODULE_VELO_SAMPlE_START],$index0,find_mod($index0,"VEL_PITCH"),-1)
		inc($index0)
	end while
	
	@strVar0 :=  get_engine_par_disp($ENGINE_PAR_MOD_TARGET_INTENSITY,0,find_mod(0,"VEL_PITCH"),-1) & " %"
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetVelocityToVolume
	
	$index0 := 0
	while($index0<$NUM_GROUPS)
		set_engine_par($ENGINE_PAR_MOD_TARGET_INTENSITY,%modulesValues[$MODULE_VELO_VOLUME],$index0,find_mod($index0,"VEL_VOLUME"),-1)
		inc($index0)
	end while	
	
	@strVar0 :=  get_engine_par_disp($ENGINE_PAR_MOD_TARGET_INTENSITY,0,find_mod(0,"VEL_VOLUME"),-1) & " %"
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetVelocityToAttack
	
	$index0 := 0
	while($index0<$NUM_GROUPS)
		set_engine_par($ENGINE_PAR_MOD_TARGET_INTENSITY,%modulesValues[$MODULE_VELO_ATTACK],$index0,find_mod($index0,"VEL_ATTACK"),-1)
		inc($index0)
	end while
	
	@strVar0 :=  get_engine_par_disp($ENGINE_PAR_MOD_TARGET_INTENSITY,0,find_mod(0,"VEL_ATTACK"),-1) & " %"
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetVelocityToRelease
	
	$index0 := 0
	while($index0<$NUM_GROUPS)
		set_engine_par($ENGINE_PAR_MOD_TARGET_INTENSITY,%modulesValues[$MODULE_VELO_RELEASE],$index0,find_mod($index0,"RVEL_ATTACK"),-1)
		inc($index0)
	end while	
	
	@strVar0 :=  get_engine_par_disp($ENGINE_PAR_MOD_TARGET_INTENSITY,0,find_mod(0,"RVEL_ATTACK"),-1) & " %"
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
{ function SetFakeLegato }
{ end function }

{ ---------------------------------------------------------------------------- }
{ function SetHumanize }
{ end function }

{ ---------------------------------------------------------------------------- }
function SetStereoSpread
	
	set_engine_par($ENGINE_PAR_STEREO,%modulesValues[$MODULE_STEREO_S],-1,$SLOT_STEREO,$NI_INSERT_BUS)
	
	@strVar0 := get_engine_par_disp($ENGINE_PAR_STEREO,-1,$SLOT_STEREO,$NI_INSERT_BUS) & " %"
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetStereoPan
	
	set_engine_par($ENGINE_PAR_STEREO_PAN,%modulesValues[$MODULE_STEREO_P],-1,$SLOT_STEREO,$NI_INSERT_BUS)
	
	@strVar0 := get_engine_par_disp($ENGINE_PAR_STEREO_PAN,-1,$SLOT_STEREO,$NI_INSERT_BUS)
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetReverbSendLvl
	
	set_engine_par($ENGINE_PAR_SENDLEVEL_0,%modulesValues[$MODULE_REVERB_SLEVEL],-1,$SLOT_SEND,$NI_INSERT_BUS)
	
	@strVar0 :=  get_engine_par_disp($ENGINE_PAR_SENDLEVEL_0,-1,7,$NI_INSERT_BUS) & " dB"
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetReverbMode
	
	if( %modulesValues[$MODULE_REVERB_MODE]>500000 )
		set_engine_par($ENGINE_PAR_RV2_TYPE,$NI_REVERB2_TYPE_ROOM,-1,$SLOT_REVERB,$NI_SEND_BUS)
		@strVar0 :=  "Room"
	else
		set_engine_par($ENGINE_PAR_RV2_TYPE,$NI_REVERB2_TYPE_HALL,-1,$SLOT_REVERB,$NI_SEND_BUS)
		@strVar0 :=  "Hall"
	end if
	
	call PrintInfos	
	
end function

{ ---------------------------------------------------------------------------- }
function SetFundamentalGain
	
	$index0 := 0
	while($index0<$NUM_GROUPS)
		set_engine_par($ENGINE_PAR_GAIN1,%modulesValues[$MODULE_FUNDAMENTAL_GAIN],$index0,$SLOT_FUND_EQ,$MINUS)
		inc($index0)
	end while	
	
	@strVar0 := get_engine_par_disp($ENGINE_PAR_GAIN1,$ZERO,$SLOT_FUND_EQ,$MINUS) & " dB"
	call PrintInfos
	
end function

{ ---------------------------------------------------------------------------- }
function SetFundamentalWidth
	
	$index0 := 0
	while($index0<$NUM_GROUPS)
		set_engine_par($ENGINE_PAR_BW1,%modulesValues[$MODULE_FUNDAMENTAL_WIDTH],$index0,$SLOT_FUND_EQ,$MINUS)
		inc($index0)
	end while	
	
	@strVar0 := get_engine_par_disp($ENGINE_PAR_BW1,$ZERO,$SLOT_FUND_EQ,$MINUS) & " oc"
	call PrintInfos
	
end function

{ ---------------------------------------------------------------------------- }
function PerformToggleChange

	%modulesStatus[$moduleEditedIndex] := abs(%modulesStatus[$moduleEditedIndex]-1)
	
	select( $moduleEditedIndex )
	
		{ DYNAMICS --------------------------------------------------------------- }
		case $MODULE_DYNAMICS	
			if( $dynamicsDrivenByVel = $TRUE )		
				%modulesValues[$MODULE_DYNAMICS] := %modulesDefaultValues[$MODULE_DYNAMICS]
			else
				set_engine_par($ENGINE_PAR_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,$SLOT_GN_DYN,$NI_INSERT_BUS)
				set_engine_par($ENGINE_PAR_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,$SLOT_CO_DYN,$NI_INSERT_BUS)
			end if

		{ EXPRESSION ------------------------------------------------------------- }
		case $MODULE_EXPRESSION
			if( $expressionDrivenByVel = $TRUE )		
				%modulesValues[$MODULE_EXPRESSION] := %modulesDefaultValues[$MODULE_EXPRESSION]
			else
				set_engine_par($ENGINE_PAR_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,$SLOT_GN_EXP,$NI_INSERT_BUS)
			end if
		
		{ STEREO ----------------------------------------------------------------- }	
		case $MODULE_STEREO_S
			set_engine_par($ENGINE_PAR_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,$SLOT_STEREO,$NI_INSERT_BUS)	
			%modulesStatus[$moduleEditedIndex+1] := %modulesStatus[$moduleEditedIndex]			
		case $MODULE_STEREO_P
			set_engine_par($ENGINE_PAR_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,$SLOT_STEREO,$NI_INSERT_BUS)
			%modulesStatus[$moduleEditedIndex-1] := %modulesStatus[$moduleEditedIndex]
		
		{ REVERBERATION ---------------------------------------------------------- }
		case $MODULE_REVERB_SLEVEL
			set_engine_par($ENGINE_PAR_SEND_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,0,-1)
			%modulesStatus[$moduleEditedIndex+1] := %modulesStatus[$moduleEditedIndex]		
		case $MODULE_REVERB_MODE
			set_engine_par($ENGINE_PAR_SEND_EFFECT_BYPASS,abs(%modulesStatus[$moduleEditedIndex]-1),-1,0,-1)
			%modulesStatus[$moduleEditedIndex-1] := %modulesStatus[$moduleEditedIndex]			
			
	end select
	
	call Repaint

end function

{ ---------------------------------------------------------------------------- }
function PerformSliderChange

	%modulesValues[$moduleEditedIndex] := $moduleEditedValue

	select( $moduleEditedIndex )
	
		{ DYNAMICS --------------------------------------------------------------- }
		case $MODULE_DYNAMICS	
			call SetDynamics
			
		{ EXPRESSION ------------------------------------------------------------- }
		case $MODULE_EXPRESSION
			call SetExpression	

		{ VELOCITY --------------------------------------------------------------- }
		case $MODULE_FORCED_VELOCITY
			call SetForcedVelocity			
					
		case $MODULE_VELO_SAMPlE_START
			call SetVelocityToSampleStart
			
		case $MODULE_VELO_VOLUME
			call SetVelocityToVolume
			
		case $MODULE_VELO_ATTACK
			call SetVelocityToAttack
			
		case $MODULE_VELO_RELEASE
			call SetVelocityToRelease
			
		{ STEREO ----------------------------------------------------------------- }	
		case $MODULE_STEREO_S
			call SetStereoSpread
			
		case $MODULE_STEREO_P
			call SetStereoPan
			
		{ REVERBERATION ---------------------------------------------------------- }	
		case $MODULE_REVERB_SLEVEL
			call SetReverbSendLvl
		
		case $MODULE_REVERB_MODE
			call SetReverbMode			
			
		{ FUNDAMENTAL ------------------------------------------------------------ }	
		case $MODULE_FUNDAMENTAL_GAIN
			call SetFundamentalGain
		
		case $MODULE_FUNDAMENTAL_WIDTH
			call SetFundamentalWidth
			
	end select
	
end function
	
{------------------------------------------------------------------------------}
on ui_control ($item1Toggle)

	$moduleEditedIndex := $verifiedModuleIndex1
	call PerformToggleChange
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item2Toggle)

	$moduleEditedIndex := $verifiedModuleIndex2
	call PerformToggleChange
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item3Toggle)

	$moduleEditedIndex := $verifiedModuleIndex3
	call PerformToggleChange
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item4Toggle)

	$moduleEditedIndex := $verifiedModuleIndex4
	call PerformToggleChange
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item5Toggle)

	$moduleEditedIndex := $verifiedModuleIndex5
	call PerformToggleChange
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item1Slider)

	$moduleEditedIndex := $verifiedModuleIndex1
	$moduleEditedValue := $item1Slider
	if( %modulesStatus[$moduleEditedIndex] = $FALSE )
		$item1Slider := %modulesValues[$moduleEditedIndex]
		@strVar0 := "Disabled"
		call PrintInfos
		exit
	else
		call PerformSliderChange
	end if	
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item2Slider)

	$moduleEditedIndex := $verifiedModuleIndex2
	$moduleEditedValue := $item2Slider
	if( %modulesStatus[$moduleEditedIndex] = $FALSE )
		$item2Slider := %modulesValues[$moduleEditedIndex]
		@strVar0 := "Disabled"
		call PrintInfos
		exit
	else
		call PerformSliderChange
	end if
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item3Slider)

	$moduleEditedIndex := $verifiedModuleIndex3
	$moduleEditedValue := $item3Slider
	if( %modulesStatus[$moduleEditedIndex] = $FALSE )
		$item3Slider := %modulesValues[$moduleEditedIndex]
		@strVar0 := "Disabled"
		call PrintInfos
		exit
	else
		call PerformSliderChange
	end if
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item4Slider)

	$moduleEditedIndex := $verifiedModuleIndex4
	$moduleEditedValue := $item4Slider
	if( %modulesStatus[$moduleEditedIndex] = $FALSE )
		$item4Slider := %modulesValues[$moduleEditedIndex]
		@strVar0 := "Disabled"
		call PrintInfos
		exit
	else
		call PerformSliderChange
	end if
	
end on

{------------------------------------------------------------------------------}
on ui_control ($item5Slider)

	$moduleEditedIndex := $verifiedModuleIndex5
	$moduleEditedValue := $item5Slider
	if( %modulesStatus[$moduleEditedIndex] = $FALSE )
		$item5Slider := %modulesValues[$moduleEditedIndex]
		@strVar0 := "Disabled"
		call PrintInfos
		exit
	else
		call PerformSliderChange
	end if
	
end on

{ ---------------------------------------------------------------------------- }
on ui_control ($KnobSel)
	
	$intVar0 := abs($KnobSel - $knobSelLastValue)

	if( $KnobSel > $knobSelLastValue )
		$knobSelDirection := $DOWN		
		if( $intVar0 > 1 )
			$KnobSel := $knobSelLastValue + 1
		end if		
	else
		if( $KnobSel < $knobSelLastValue )
			$knobSelDirection := $UP			
			if( $intVar0 > 1 )
				$KnobSel := $knobSelLastValue - 1
			end if			
		else
			$knobSelDirection := $STATIC 
		end if
	end if
	
	$knobSelLastValue := $KnobSel
	
	$moduleIndex := $moduleIndex + $knobSelDirection
	
	if( $moduleIndex > $verifiedModuleMaxIndex )
		 $moduleIndex := $verifiedModuleMaxIndex
	end if
	
	if( $moduleIndex < $ZERO ) 
		$moduleIndex := $ZERO
	end if
	
	call Repaint
		
end on

{ ---------------------------------------------------------------------------- }
on ui_control ($defaultValuesButton)

	$defaultValuesButton := 0	

	$Silent := $TRUE	
	
	$index1 := 0
	while( $index1 < $MODULES_COUNT )
		%modulesValues[$index1] := %modulesDefaultValues[$index1]		
		%modulesStatus[$index1] := %modulesDefaultStatus[$index1]		
		$moduleEditedValue := %modulesValues[$index1]
		$moduleEditedIndex := $index1
		call PerformSliderChange
		inc ($index1)
	end while	
	
	$Silent := $FALSE
	
	{ call Repaint }
		
end on

{ ---------------------------------------------------------------------------- }
on controller

	if( $CC_NUM=1 )
		%modulesValues[$MODULE_DYNAMICS] := %CC[1] * 1000000 / 127
		if( %modulesStatus[$MODULE_DYNAMICS] = $TRUE )
			call Repaint
			call SetDynamics
		end if		
	end if	
	
	if( $CC_NUM=11 )
		%modulesValues[$MODULE_EXPRESSION] := %CC[11] * 1000000 / 127
		if( %modulesStatus[$MODULE_EXPRESSION] = $TRUE )
			call Repaint
			call SetExpression
		end if
	end if	
	
end on

{ ---------------------------------------------------------------------------- }
on note

	{ VELOCITY ----------------------------------------------------------------- }
	
	$newVelocity := $EVENT_VELOCITY
	
	if( %modulesStatus[$MODULE_FORCED_VELOCITY] = $TRUE )	
		$newVelocity := $forcedVelocity		
	end if	
	
	if( $newVelocity > 127 )
		$newVelocity := 127
	end if
	if( $newVelocity < 1 )
		$newVelocity := 1
	end if
	
	$KnobVelocity := $newVelocity

	{ DYNAMICS ----------------------------------------------------------------- }		
			
	if( %modulesStatus[$MODULE_DYNAMICS] = $FALSE and $dynamicsDrivenByVel = $TRUE )
		%modulesValues[$MODULE_DYNAMICS] := $newVelocity * 1000000 / 127
		call Repaint
		call SetDynamics
	end if		
	
	{ EXPRESSION --------------------------------------------------------------- }	
			
	if( %modulesStatus[$MODULE_EXPRESSION] = $FALSE and $expressionDrivenByVel = $TRUE )
		%modulesValues[$MODULE_EXPRESSION] := $newVelocity * 1000000 / 127
		call Repaint
		call SetExpression
	end if
	
	{ EVENT ------------------------------------------------------------------ }
	
	$previousEvent := $EVENT_ID	
	change_velo($EVENT_ID, $newVelocity)
	
end on

{ ---------------------------------------------------------------------------- }
on release
	
	inc(%Timer[$TIMER_VELOCITY])

	wait($TEMPO_VELOCITY)
	dec(%Timer[$TIMER_VELOCITY])
	if( %Timer[$TIMER_VELOCITY]=0 )
		$KnobVelocity := 0
	end if
	
end on

{ ---------------------------------------------------------------------------- }
on persistence_changed

	if( $initFlag = $TRUE )
	
		call PerformPostInit
	
		@strVar0 := "Init [Done]"	
		
		$initFlag := $FALSE
		
	else
	
		@strVar0 := "Snapshot [Loaded] "
	
	end if
	
	call PrintInfos
	
end on

{ ---------------------------------------------------------------------------- }
