### Experiment 3 - TU Delft
### Executed by Alexandre Fieno
### Source-code from Mylene Farias
###                  Judith Redi

scenario = "packet_error_strength";
no_logfile = true;
active_buttons = 2;
button_codes = 1,2;
default_background_color = 127, 127, 127;
default_text_color = 255, 255, 255;
default_font_size = 18;

begin;

### Screen for participant name ###
picture {
   text {
      font_size = 24;
      caption = "Please type in your name and press return";
   };
   x = 0; y = 100;
   text {
      font_size = 24;
      caption = " ";
   }response_text;
   left_x = -200;
   y = 0;
} participant_name;
### Screen for participant name ###

### Eye-tracker calibration screen ###
picture {
	background_color = 127, 127, 127;
	box {
		height = 10; width = 10;
		color = 0, 0, 0;
		}box1;
		x=0; y=0;
	} et_calibration;
### Eye-tracker calibration screen ###

### Give pic to rego to center ###
trial {    
	trial_duration = 2000;
		picture{
		background_color = 127,127,127;
	}infra_video;
} trial_recenter;
### Give pic to rego to center ###

trial {
   picture {
				text { caption = " place_holder "; font_size = 24;} instruction_text;
      x = 0; y = 30;
   };
   time = 0;
   duration = response;
} instruction_trial;

picture {
   text {
      caption = "Please left-click your mouse to go to the scoring screen";
   };
   x = 0; y = 100;
} after_video_screen;

trial {
	trial_duration = stimuli_length;
	trial_type = fixed;
   video { filename = "blkblr_v1_1280x720_0.avi"; 
   speed_type = video_speed_hz;
   speed = 50;
   use_audio = false;
   } vid;     
} video_trial;

### Choosing Score ###
array {
		text { caption = "0"; } text0;    
		text { caption = "1"; } text1;    
		text { caption = "2"; } text2;
		text { caption = "3"; } text3;
		text { caption = "4"; } text4;
		text { caption = "5"; } text5;
		text { caption = "6"; } text6;
		text { caption = "7"; } text7;
		text { caption = "8"; } text8;
		text { caption = "9"; } text9;
		text { caption = "10"; } text10;
		text { caption = "Very Low";} textPoor;
		text { caption = "Very High";} textExcellent;
	} numbers;
### Choosing Score ###	

trial
{
   picture {   
		### the line ###
		box { color = 255,255,255; height = 3; width = 500; };
		x = 0; y = -20;  
		### scale marks ###
		box { color = 255,255,255; height = 10; width = 3; };
		x = -250; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = -200; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = -150; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = -100; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = -50; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = 0; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = 50; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = 100; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = 150; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = 200; y = -20;  
		box { color = 255,255,255; height = 10; width = 3; };
		x = 250; y = -20; 
		### the cursor ###
		box { color = 255,0,0; height = 20; width = 7; };
		x = 0; y = -20;
		### the text ###
		text { caption = "\n\nMove the mouse to control the score slider.\nLeft-click to choose your score"; } cap_text;
		x = 0; y = -150;
		text text0; 
		x = -250; y = 10;     
		text text1; 
		x = -200; y = 10;     
		text text2; 
		x = -150; y = 10;
		text text3; 
		x = -100; y = 10;
		text text4; 
		x = -50; y = 10;
		text text5; 
		x = 0; y = 10;
		text text6; 
		x = 50; y = 10;
		text text7; 
		x = 100; y = 10;
		text text8; 
		x = 150; y = 10;
		text text9; 
		x = 200; y = 10;  
		text text10; 
		x = 250; y = 10;
		text { caption = "Imperceptible"; } poor_text;
		x = -250; y = -50;
		text { caption = "Very Annoying"; } excellent_text;
		x =  250; y = -50;
	} slider_pic;
} slider_trial;

box {
height = 10; 
width = 10;
}border_box;

array {
	LOOP $i 20; 
	text {caption = " "; background_color = 127, 127, 127 ;};
	ENDLOOP;
}menu_text;

text{
	caption = "mii"; 
	font_size = 7; 
	background_color = 255,255,255; 
}cursor;

text{
	caption = " "; 
	font_size = 36; 
	background_color = 127,127,127;
}titletext;

trial{
	picture{
	background_color = 127,127,127;
	}pic;
}menu_screen;

###########
### PCL ###
###########

begin_pcl;

### global variables ###
array< string > VideoNames[0][0];
int iExpCount = 0;
### set depending on how many sessions ###
int sessions = 1; 
string file_name;
string input_file_name;
int iCount = 1;
int max_x = display_device.width() / 2;
int min_x = -max_x;
int max_y = display_device.height() / 2;
int min_y = -max_y;
int bottom_max_y = 100; ### for restricting mouse to bottom half
int bottom_min_y = min_y + 40; ### for restricting mouse to bottom half

string instructions;

### mouse stuff ###
mouse mse = response_manager.get_mouse( 1 );
mse.set_min_max( 1, -250, 250 );
int iCursor_x_delta; 
int resp_count_pre;
### mouse stuff ###

### files ###
input_file filevideoNames = new input_file;
output_file fExpRes = new output_file;
output_file newFileList = new output_file;
### files ###

int start_time;

### eye tracker stuff ###
	int Caltype = 13; ### >>>> set the strenght of calibration (5,9,13)
	eye_tracker tracker = new eye_tracker( "{F62A86B9-6F75-49B7-944F-2B4DECA92F48}" );
	string cmd_et_bmp = "ET_BMP " + "c:\\packet_loss\\";                                                            
	string cmd_et_sav = "ET_SAV " + "c:\\packet_loss\\";
### eye tracker stuff ###


											###################
#################################### Subroutines ########################################
											###################

### 1. Utility subroutines ###
### EYE TRACKER CALIBRATION ###
	sub
		calibration 
	begin
		if (Caltype == 5)
		then tracker.calibrate( et_calibrate_default, 5.0, 0.0, 0.0 );
		elseif (Caltype == 9)
		then tracker.calibrate( et_calibrate_default, 9.0, 0.0, 0.0 );
		elseif (Caltype == 13)
		then tracker.calibrate( et_calibrate_default, 13.0, 0.0, 0.0 );
		end;
end;
###

### mouse activity ###
sub bool resp (int button, int current_count)
begin
	if response_manager.total_response_count(button) > current_count then
		return true
	end;
	return false;
end;

### mouse activity2 ###
sub int current_button(array<int,2>& locations, int x, int y)
begin
	loop
		int i = 1
	until
		i > locations.count()
	begin
		if ( (x >= locations[i][1]) && (x <= locations[i][2]) &&
			  (y <= locations[i][3]) && (y >= locations[i][4]) ) then
			return i
		end;
		i = i + 1
	end;
	return 0
end;

### display menu ###
sub int menu (array<string, 1> & options, string title)
begin
	mse.set_min_max( 1, min_x, max_x );
	mse.set_min_max( 2, min_y, max_y );
	mse.set_restricted( 1, true );
	mse.set_restricted( 2, true );

	### adjustable parameters for menu ###
	int border_width = 5;
	int box_dist = 20;

	array<int> locations[options.count()][4];
	int number = options.count();
	int max_width = 0;

	### set the text captions into the text array and find the max_width ###
	loop int i = 1 until i > number begin
		menu_text[i].set_height(0);
		menu_text[i].set_width(0);
		menu_text[i].set_caption(options[i]);
		menu_text[i].set_font_size(30);
		menu_text[i].redraw();
		if int(menu_text[i].width()) > max_width then
			max_width = int(menu_text[i].width());
		end;
		i = i + 1;
	end;

	### make the proper number of button boxes and border boxes with the right dimensions and colors ###
	int text_height = int(menu_text[number].height()) + 30;
	int box_height = text_height + border_width * 2;
	int text_width = max_width + 30;
	int box_width = text_width + border_width * 2;
	border_box.set_width( box_width);
	border_box.set_height( box_height );
	border_box.set_color( 0, 0, 0);
	loop int i = 1 until i > number 
	begin
		menu_text[i].set_width( text_width );
		menu_text[i].set_height( text_height );
		menu_text[i].set_background_color( 160, 160, 160 );
		menu_text[i].redraw();
		i = i + 1;
	end;

	### make array of locations ###
	int in_col = 0;
	int last_left_edge = -box_width/2;
	int last_right_edge = box_width/2;
	int last_top_edge = box_height/2;
	int last_bottom_edge = -box_height/2;

	### special case of only one choice ###
	if number == 1 then 
		locations[number][1] = last_left_edge; 
		locations[number][2] = last_right_edge;
		locations[number][3] = last_top_edge;
		locations[number][4] = last_bottom_edge;
		else
		
		### decide how many buttons per column ###
		if mod(number, 2) == 1 then
			in_col = number - 1;
			else 
			in_col = number;
		end;

		### how many rows ###
		int rows = in_col / 2;

		### put all edge locations in location array ###
		loop int i = 1;
			int left_edge = -(box_width + box_dist);
			until i > 2 
			begin
				int top_edge = (box_height + box_dist)*rows/2;
				loop int j = 1 until j > rows 
				begin
					int index = ((i-1) * rows) + j;
					int right_edge = (left_edge + box_width);
					int bottom_edge = top_edge - box_height;
					locations[index][1] = left_edge;
					locations[index][2] = right_edge;
					locations[index][3] = top_edge;
					locations[index][4] = bottom_edge;
					top_edge = top_edge - (box_height + box_dist);
					j = j + 1;
				end;
			left_edge = (left_edge + box_width + 2*box_dist);
			i = i + 1;
		end;
		if mod (number,2) == 1 then
			last_top_edge = (locations[number - 1][4] - box_dist);
			last_bottom_edge = last_top_edge - box_height;
			locations[number][1] = last_left_edge; 
			locations[number][2] = last_right_edge;
			locations[number][3] = last_top_edge;
			locations[number][4] = last_bottom_edge;
		end;
	end;

	### add buttons to picture pic ###
	loop int i = 1 until i > options.count()
	begin
		pic.add_part(border_box, locations[i][1] + box_width/2, locations[i][3] - box_height/2);
		pic.add_part(menu_text[i], locations[i][1] + box_width/2, locations[i][3] - box_height/2);
		i = i + 1;
	end;

	### add title to picture pic ###
	titletext.set_caption(title);
	titletext.redraw();
	pic.add_part(titletext, 0, 300);

	### add cursor to picture pic ###
	pic.add_part(cursor, 0, 0);
	int cursor_index = pic.part_count();
	mse.set_xy( 0, 0 );
	
	int last_location = 1;
	loop  
		int responses = response_manager.total_response_count(1);
	until false
	begin
		pic.present();
		mse.poll();
		int x = mse.x();
		int y = mse.y();	
 
		### display cursor at coordinates ###
		pic.set_part_x( cursor_index, x );
		pic.set_part_y( cursor_index, y ); 

		### change colors if necessary ###
		int location = current_button(locations, x, y);
		if location != last_location then
			if (location > 0) then
				menu_text[location].set_background_color(255, 45, 0 );
				menu_text[location].redraw();
			end;
			if (last_location > 0) then
				menu_text[last_location].set_background_color(180, 180, 180 );
				menu_text[last_location].redraw();
			end;
			last_location = location;
		end;
		if (resp(1, responses)) then
			if (location > 0) then 
				### menu_text[last_location].set_background_color(44, 91, 143 );
				### menu_text[last_location].redraw();				
				pic.clear();
				return location 
			end;
			responses = response_manager.total_response_count(1)
		end;
	end;
	return 0;
end;

#######################################
### 2. experiment-specific routines ###
#######################################

### Records name of participant ###
sub
	string get_response
	begin
		### Accept capital letter input ###
		system_keyboard.set_case_mode( 3 ); 
		system_keyboard.set_max_length( 1 );
		### start the loop ###
		loop until false begin  
			response_text.set_caption( " " + file_name );
			response_text.redraw();
			participant_name.present();
			string letter = system_keyboard.get_input(); 
			if (system_keyboard.last_input_type() == keyboard_delimiter) then
				break 
			end;
			### Check if you were pressing backspace and there are still letters in the string ###
			if (letter == "=" && file_name.count( ) != 0) then 
				int Str_lngth = file_name.count( );
				Str_lngth = Str_lngth - 1;
				file_name.resize( Str_lngth );
			end; 
			if (letter != "=") then     
				file_name = file_name + letter 
			end;  
		end;
		### end the loop ###
		return file_name
	end; 
### Records name of participant ###

### shows videos ###
sub
	int showVideo(string filename)
	begin
		vid.set_filename(filename);
		vid.prepare();
		### recentering ###
		trial_recenter.present();
		fExpRes.print( (clock.time()-start_time) ); 
		fExpRes.print("\t");
		### sending image name to eye tracker ###
		tracker.send_command(cmd_et_bmp + "1024bg.bmp");
		video_trial.present();
		fExpRes.print( (clock.time()-start_time) ); 
		fExpRes.print("\t");
		return 1;
	end;
### shows videos ###

### allows entering scores ###
sub 
	int score_video
	begin
		int resp_count = response_manager.response_data_count( );
		term.print_line (resp_count);
		loop
			mse.set_xy(0,0);
			mse.poll();
			int iCursor_x_initial = mse.x();
			int iCursor_x_current = mse.x();
			iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
		until 
			resp(1,resp_count)
		begin
			mse.poll();
			iCursor_x_current = mse.x();
			iCursor_x_delta = iCursor_x_current - iCursor_x_initial;
			if (iCursor_x_delta > 250) then 
				iCursor_x_delta = 250
			elseif (iCursor_x_delta < -250) then 
				iCursor_x_delta = -250
			end;
			slider_pic.set_part_x( 13, iCursor_x_delta );
			slider_trial.present();
		end;
		int score = (iCursor_x_delta+250)/5;
		return score;	
	end;
### allows entering scores ###

### Sets New Instruction Screen ###
sub
	instruction_screen(string instr)
	begin
		instruction_text.set_caption(instr); 
		instruction_text.redraw(); 
		instruction_trial.present();
	end;
### Sets New Instruction Screen ###

### Loads the video lists for the current task ###
sub
	loadvideos (string filename, int nSess)
	begin
		filevideoNames.open( filename );
		VideoNames.resize(nSess);
		loop 
			int iNameCount = 1;
			string strFileName = "";
		until filevideoNames.end_of_file() ### parse text file ###
		begin
			loop
				int j = 1;
			until j > nSess
			begin
				strFileName = filevideoNames.get_line();
				if(strFileName !="")
				then
					VideoNames[j].resize( iNameCount );
					VideoNames[j] [iNameCount] = strFileName;
				end;
				j = j+1;
			end;
			iNameCount = iNameCount + 1;
		end;
		filevideoNames.close();	
	
		loop
			int j = 1;
		until j > nSess
		begin
			VideoNames[j].shuffle();
			j = j+1;
		end;
	end;
### Loads the video lists for the current task ###	


                                ####################
################################### MAIN ROUTINE ######################################
                                ####################

file_name = get_response();

instructions = "Welcome! Thank you for participating in this experiment.\n\n" +
					" The experiment is divided in 5 sessions: \n \n" +
					" (1) Equipment Calibration, \n \n" +
					" (2) Free Viewing, \n \n" +
					" (3) Training, \n \n" + 
					" (4) Practice Trials \n \n" +
					" (5) Main Experimental Session. \n\n\n" +
					" At the beginning of each session, I will briefly  \n" + 
					" explain what you are required to do at each stage.\n" +
					" \n\n\n\nLeft-click to continue";
instruction_screen(instructions);

### eyetracking calibration ###
instructions = "The distance from the monitor to your eyes is \n" +
					"very important during the presentation.\n\n" +
					"Please put your head on the CHIN-REST in front of you. \n\n" +
					"Try not to lean backward. \n" +
					" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION\n\n\n" +
					"Please, wait for a few seconds while I adjust the system.\n\n" +
					"When the system is ready, I will ask you to continue.\n\n" +
					" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION\n\n\n" +
					"You're now going to see a series of small black SQUARES on the screen.\n\n"+
					"At each screen, one black square will appear at a different position.\n\n" +
					"Please, keep your eyes fixated on each of these squares.\n"+
					"\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

### Tracker comment out ###  
tracker.start_tracking(); 	
string cal = "cal.bmp";
string idf_file_name = file_name; 	
### naming the IDF file using the name of the test participant
idf_file_name.append ("_Trial_"+ string(iExpCount));
idf_file_name.append (".idf");
string idf = file_name + "S1.idf";   
calibration();                           
tracker.send_command( "ET_CLR" );
tracker.set_recording( true );
### Tracker comment out ###  

start_time = clock.time();
instructions = "(1) CALIBRATION SESSION\n\n\n" +
					"Thank you!\n\n The calibration is now complete.\n\n"+
					"\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 
### eyetracking calibration ###

### VIDEO FREE-LOOKING ###
instructions = "(2) FREE VIEWING SESSION\n\n\n" +
               "You are now going to watch a series of 7 videos. \n\n"+
					"\nPlease, watch them as if you were at home watching TV.\n\n"+
					"\n\n\n Whenever you are ready to start, please left-click to continue";
instruction_screen(instructions); 

### timing output file ###
string file_name_txt = file_name;
file_name_txt.append ( "_FL_timing.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\n");

input_file_name = "originals\\originals_list.txt";
loadvideos(input_file_name, 1);

loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[1].count();
until j > nVid
begin
	vfn = VideoNames[1][j];
	showVideo(vfn);
	fExpRes.print( vfn ); 
   fExpRes.print("\n");
	j = j+1;
end;
fExpRes.close();
### VIDEO FREE-LOOKING ###


##########################
### TRAINING -- PART 1 ###
########################################################
### Part 1: show videos with impairments, no scoring ###
########################################################

instructions = "(3) TRAINING SESSION\n\n\n" +
					"This study is concerned with defects or impairments in video images and\n"+
					"their effect on human viewers.  We are not concerned with the content of\n"+
					"the videos.  We are interested in whether or not you see any defects or\n"+
					"impairments in the videos that we will show, and if so, how annoying defect is.\n"+
					"\n\n\n Left-click to continue";
instruction_screen(instructions);

instructions = "(3) TRAINING SESSION\n\n\n" +
					"Here is how you will determine the annoyance value. I am about\n" +
					"to show you a set of sample clips. The sample clips include \n" +
					"two sets of videos. The first set has 4 high quality videos.\n"+
					"\n\n\n Left-click to play the set of high quality videos.";
instruction_screen(instructions);

### Loading training high quality videos filename ###
file_name_txt = file_name;
file_name_txt.append ( "_training_ORG_timing.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\n");
input_file_name = "training\\training_ORG_list.txt";
loadvideos(input_file_name, 1);
### Loading training high quality videos filename ###

### Playing high quality videos ###
loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[1].count();
until j > nVid
begin
	vfn = VideoNames[1][j];
	showVideo(vfn);
	fExpRes.print( vfn ); 
	fExpRes.print("\n");
	j = j+1;
end;
fExpRes.close();
### Playing high quality videos ###

##########################
### TRAINING -- PART 2 ###
####################################################
### Part 2: show videos with impairments scoring ###
####################################################

instructions = "(3) TRAINING SESSION\n\n\n" +
					"The second set has 4 degraded videos. They will give you an\n"+
					"idea of the range of quality that you will be seeing.\n\n\n"+
					"You are to assign an ANNOYANCE value of 10 to the most\n"+
					"annoying video among the sample clips. \n\n\n"+
					"\n\n\n Left-click to play the set of impaired videos.";
instruction_screen(instructions);

### Loading degraded quality videos filename ###
file_name_txt = file_name;
file_name_txt.append ( "_training_LQ_timing.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\n");
input_file_name = "training\\training_LQ_list.txt";
loadvideos(input_file_name, 1);
### Loading degraded quality videos filename ###

### Play degraded videos ###
loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[1].count();
until j > nVid
begin
	vfn = VideoNames[1][j];
	showVideo(vfn);
	fExpRes.print( vfn ); 
	fExpRes.print("\n");
	j = j+1;
end;
fExpRes.close();
### Play degraded videos ###

instructions = "(3) TRAINING SESSION\n\n\n" +
					"Did you see any defects? \n\n"+
					"Remember that the most annoying defect that you have seen\n"+ 
					"is to have a value of 10.\n\n"+
					"If the annoyance value of a defect in the experiment\n"+
					"is half of the worst sample clip, give it a 5; if it \n"+
					"is 1/10th as bad, give it a 1; \n" +
					"If the defect did not annoy you at all, call it zero.\n\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);
### Part 1: show videos with impairments, no scoring ###

######################
### PRACTICE TRIAL ###
######################
instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"Before we start the experiment, you will have 4 practice trials\n"+
					"to be sure that you understand the task. You will be asked to \n"+
					"perform the exact same tasks you will perform in the main experiment.\n"+
					"You will respond in these trials just like you will in the main experiment.\n"+
					"We will not use the data from the practice trials, so don't be concerned \n"+
					"if you make a mistake here.\n\n"+
					"If you have any questions or concerns, feel free to ask me.\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"You will be presented with one video clip on each trial.\n"+
					"Each clip will last 10 seconds and will be played once.\n"+ 
					"After the clip is played, questions will appear on the monitor.\n"+
					"The same questions will be asked after every trial. Do not\n"+ 
					"spend a lot of time thinking about your responses. We want\n"+
					"to know your initial impressions.\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"You will be asked to estimate the annoyance of defects or\n"+
					"impairments in the video. The defects can be found in any \n"+
					"region of video and at any time during the clip. After the \n"+
					"clip is played, you will be asked: Did you see any defect or impairment?\n\n"+ 
					"If you did not see a defect, answer NO. Then, Left-click to play the next clip.\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"For those clips in which you do detect a defect or impairment,\n"+
					"answer YES. You will be then asked to indicate the annoyance of \n"+
					"the defect you saw using a scale with values ranging from 0 and 10.\n\n"+
					"You are to assign an annoyance value of 10 to the most annoying video.\n"+
					"If the annoyance value of a defect in the experiment is half of the \n"+
					"worst sample clip, give it a 5; if it is 1/10th as bad, give it a 1.\n"+
					"If the defect did not annoy you at all, call it zero.\n\n"+ 
					"You should enter the scores using the mouse to LEFT-click \n"+
					"on the desired value.\n\n"+  
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);					

instructions = "(4) PRACTICE TRIAL SESSION \n\n\n" +
					"After you finished entering your choice of number,\n"+
					"Left-click to play the next video.\n\n"+
					"Do you have any questions?\n\n"+
					"\n\n\n Left-click to start PRACTICE TRIALS.";
instruction_screen(instructions);					

### Loading training list filename ###
file_name_txt = file_name;
file_name_txt.append ( "_training_scores.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\t\Impairments_perceived\tScore\n");
input_file_name = "training\\training_list.txt";
loadvideos(input_file_name, 1);
### Loading training list filename ###

### Play training videos ###
loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[1].count();
	int t_score;
until j > nVid
begin
	vfn = VideoNames[1][j];
	showVideo(vfn);
	fExpRes.print( vfn ); 
	fExpRes.print("\n");
#	showVideo(vfn);
	array<string>main_menu_options[2] = {"Yes", "No"};
	int main_menu_choice = menu(main_menu_options, "\n\n Did you perceive any impairments \n or defects in the video?");
	if main_menu_choice == 1
	then
		t_score = score_video();
		fExpRes.print( vfn ); 
		fExpRes.print("\tyes\t");
		fExpRes.print(t_score);
		fExpRes.print("\n");
		### Shot the screen image
		### display_device.screenshot("slider.bmp");
	else
		fExpRes.print( vfn ); 
		fExpRes.print("\tno\n");
	end;
	j = j+1;
end;
fExpRes.close();
### Play degraded videos ###

#####################
## MAIN EXPERIMENT ##
#############################################################
### Part 1: show videos with/without impairments, scoring ###
#############################################################

instructions = "(5) MAIN EXPERIMENT SESSION \n\n\n" +
					"The experiment is divided in 3 parts. Each part should\n"+
					"last approximately 15 minutes. A break will be given after\n"+
					"the first part of the experiment is finished.\n\n"+
					"If you need to take a small break at any time, enter\n"+ 
					"your answers for the most recent video, but wait to Left-click\n"+
					"until you are ready to go on.  You should stop if you are confused \n"+
					"about what to do. If you realize you have entered data incorrectly,\n"+
					"tell me and I will go back and fix it later.\n\n"+
					"\n\n\n Left-click to continue.";
instruction_screen(instructions);

instructions = "(5) MAIN EXPERIMENT SESSION  -- PART I \n\n\n" +
					"Do you have any questions?\n\n"+
					"\n\n\n Left-click to start PART I.";
instruction_screen(instructions);

### Loading video list filename ###
file_name_txt = file_name;
file_name_txt.append ( "_S1.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\t\Impairments_perceived\tScore\n");
input_file_name = "maintest\\video_list.txt";
loadvideos(input_file_name,3);
### Loading video list filename ###

loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[1].count();
	int t_score;
until j > nVid
begin
	vfn = VideoNames[1][j];
	### shows video twice ###
	showVideo(vfn);
	fExpRes.print( vfn ); 
	fExpRes.print("\n");
#	showVideo(vfn);
   ### shows video twice ###
	
	### scoring ###
	array<string>main_menu_options[2] = {"Yes", "No"};
	int main_menu_choice = menu(main_menu_options, "\n\n Did you perceive any impairments \n or defects in the video?");
	if main_menu_choice == 1
	then
		t_score = score_video();
		fExpRes.print( vfn ); 
		fExpRes.print("\tyes\t");
		fExpRes.print(t_score);
		fExpRes.print("\n");
	else
		fExpRes.print( vfn ); 
		fExpRes.print("\tno\n");
	end;
	j = j+1;
end;
fExpRes.close();
### Tracker comment out####
tracker.set_recording( false ); 
tracker.send_command( cmd_et_sav + "roi_exp_01\\" + idf );
tracker.stop_tracking();
### Tracker comment out####

instructions = "\n\n\nThis is the end of the first part of the experiment.\n"+
					"\n\n\nPlease Left-click whenever you are ready to resume the experiment. \n";
					instruction_screen(instructions);

### Part 1: show videos with/without impairments, scoring ###
 
#############################################################
### Part 2: show videos with/without impairments, scoring ###
#############################################################

instructions = "(1) CALIBRATION SESSION -- PART II\n\n\n" +
					"You are now going to perform the calibration session again.\n"+
					"Please, wait for a few seconds while the experimenter adjusts the system.\n\n" +
					"When the system is ready, the experimenter will ask you to continue.\n\n" +
					" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION  -- PART II \n\n\n" +
					"You're now going to see a series of small black SQUARES on the screen.\n\n"+
					"At each screen, one black square will appear at a different position.\n\n" +
					"Please, keep your eyes fixated on each of these squares.\n"+
					"\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

### Tracker comment out ###
### Eye tracker code ###
tracker.start_tracking(); 		
cal = "cal.bmp";
### naming the IDF file using the name of the test participant ###
# string idf_file_name = file_name;
idf_file_name.append ("_Trial_"+ string(iExpCount));
idf_file_name.append (".idf");
idf = file_name + "S2.idf";    
calibration();                           
tracker.send_command( "ET_CLR" );
tracker.set_recording( true );
### Tracker comment out ###

start_time = clock.time();
instructions = "Thank you!\n\n The calibration is now complete.\n\n"+
					"Left-click to start the Part II of the experiment."+
					"\n\n\n\n Left-click to continue";
instruction_screen(instructions); 

### Timing list filename ###
file_name_txt = file_name;
file_name_txt.append ( "_S2.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\t\Impairments_perceived\tScore\n");
### Timing list filename ###

### Loading video list filename ###

loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[2].count();
	int t_score;
until j > nVid
begin
	vfn = VideoNames[2][j];
	
	### shows video twice ###
	showVideo(vfn);
	fExpRes.print( vfn ); 
	fExpRes.print("\n");
#	showVideo(vfn);
   ### shows video twice ###
	
	### scoring ###
	array<string>main_menu_options[2] = {"Yes", "No"};
	int main_menu_choice = menu(main_menu_options, "\n\n Did you perceive any impairments \n or defects in the video?");
	if main_menu_choice == 1
	then
		t_score = score_video();
		fExpRes.print( vfn ); 
		fExpRes.print("\tyes\t");
		fExpRes.print(t_score);
		fExpRes.print("\n");
	else
		fExpRes.print( vfn ); 
		fExpRes.print("\tno\n");
	end;
	j = j+1;
end;
fExpRes.close();
### Tracker comment out####
tracker.set_recording( false ); 
tracker.send_command( cmd_et_sav + "roi_exp_01\\" + idf );
tracker.stop_tracking();
### Tracker comment out####

instructions = "\n\n\nThis is the end of the second part of the experiment.\n"+
					"\n\n\nPlease Left-click whenever you are ready to resume the experiment. \n";
					instruction_screen(instructions);

### Part 2: show videos with/without impairments, scoring ###
 
#############################################################
### Part 3: show videos with/without impairments, scoring ###
#############################################################

instructions = "(1) CALIBRATION SESSION -- PART III\n\n\n" +
					"You are now going to perform the calibration session again.\n"+
					"Please, wait for a few seconds while the experimenter adjusts the system.\n\n" +
					"When the system is ready, the experimenter will ask you to continue.\n\n" +
					" \n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

instructions = "(1) CALIBRATION SESSION  -- PART III \n\n\n" +
					"You're now going to see a series of small black SQUARES on the screen.\n\n"+
					"At each screen, one black square will appear at a different position.\n\n" +
					"Please, keep your eyes fixated on each of these squares.\n"+
					"\n\n\n\nLeft-click to continue";
instruction_screen(instructions); 

### Tracker comment out ###
### Eye tracker code ###
tracker.start_tracking(); 		
cal = "cal.bmp";
### naming the IDF file using the name of the test participant ###
# string idf_file_name = file_name;
idf_file_name.append ("_Trial_"+ string(iExpCount));
idf_file_name.append (".idf");
idf = file_name + "S2.idf";    
calibration();                           
tracker.send_command( "ET_CLR" );
tracker.set_recording( true );
### Tracker comment out ###

start_time = clock.time();
instructions = "Thank you!\n\n The calibration is now complete.\n\n"+
					"Left-click to start the Part III of the experiment."+
					"\n\n\n\n Left-click to continue";
instruction_screen(instructions); 

### Timing list filename ###
file_name_txt = file_name;
file_name_txt.append ( "_S3.txt");
fExpRes.open( "output\\" + file_name_txt );
fExpRes.print( "T-Start\tT-End\tVideo\t\Impairments_perceived\tScore\n");
### Timing list filename ###

loop
	int j = 1;
	string vfn;
	int nVid = VideoNames[3].count();
	int t_score;
until j > nVid
begin
	vfn = VideoNames[3][j];
	### save order of video in the new video list ###
	### shows video twice ###
	showVideo(vfn);
	fExpRes.print( vfn ); 
	fExpRes.print("\n");
#	showVideo(vfn);
	### shows video twice ###
	
	### scoring ###
	array<string>main_menu_options[2] = {"Yes", "No"};
	int main_menu_choice = menu(main_menu_options, "\n\n Did you perceive any impairments \n or defects in the video?");
	if main_menu_choice == 1
	then
		t_score = score_video();
		fExpRes.print( vfn ); 
		fExpRes.print("\tyes\t");
		fExpRes.print(t_score);
		fExpRes.print("\n");
	else
		fExpRes.print( vfn ); 
		fExpRes.print("\tno\n");
	end;
	j = j+1;
end;

newFileList.open("maintest\\video_list.txt");
loop
	int i = 1;
	int ll = VideoNames[1].count();
	string vfn;
until i >ll
begin
	vfn = VideoNames[1][i];
	term.print_line(vfn);
	newFileList.print(vfn);
	newFileList.print("\n");
	i = i+1;
end;

loop
	int i = 1;
	int ll = VideoNames[2].count();
	string vfn;
until i >ll
begin
	vfn = VideoNames[2][i];
	term.print_line(vfn);
	newFileList.print(vfn);
	newFileList.print("\n");
	i = i+1;
end;
	
loop
	int i = 1;
	int ll = VideoNames[3].count();
	string vfn;
until i > ll
begin
	vfn = VideoNames[3][i];
	newFileList.print(vfn);
	newFileList.print("\n");
	i = i+1;
end;
fExpRes.close();
newFileList.close();
instructions = "This is the end of the experiment.\n\n\n\n\nThank you for participating!";
instruction_screen(instructions);
 
### Tracker comment out ###
tracker.set_recording( false ); 
tracker.send_command( cmd_et_sav + "roi_exp_01\\" + idf );
tracker.stop_tracking();
### Tracker comment out ###