###################################################################################
#
# Study name: An investigation into the timing and rhythm of language processing
# Experiment: 2A - MMN induced to changes in pace, with simple tones
# Description: Oddball paradigm; complex sine waves presented at SOA = 650 ms.  
# Student Investigator: Fareeha S. Rana (PhD thesis project #1)
# Supervised by: Dr. Elisabet Service & Dr. John F. Connolly
#
# Coded by: Fareeha S. Rana, ranafs@mcmaster.ca 
# Based off code from the Todd et al. MMN/P300 experiment
# coded by Rober Boshra (boshrar@mcmaster.ca)
#
# Scenario Name = PMMN_2A_COM_1 (PacingMMN_Experiment 2A_Complex_LIST_1)
# Version 1.0
#
# Last updated 04/03/2020 by Fareeha S. Rana
# Changed SOA from 500 ms to 650 ms
# Blocked off deviants to one deviant per block
# Changed std: deviant ratio from 80:20 to 90:10 
#
#                                                                                                                                                                                                                             
#
##################################################################################

#################
# Header block
#################
	scenario = "PMMN_2A_COM_1";
	
	default_trial_type = fixed;
	scenario_type = trials;

	write_codes = true;
	pulse_width = 10; # parallel port pulse length is 10 ms
	
	code_delay = 0;

# white background black text parameters
	default_text_color = 255, 255, 255; #white
	default_background_color = 0, 0, 0; #black


#############
# SDL block
#############

begin;

# Load required tone(s)
##create an array for the 3 syllables (standards)
array{
	sound {
		wavefile { filename = "complex_ti.wav"; preload = true; }; # comptone 1, standard tone
		default_code =  "11"; # always use this code for tone 1
		default_port_code = 11;		
	} syl1; 
	
	sound {
		wavefile { filename = "complex_te.wav"; preload = true; }; # comptone 1, standard tone
		default_code =  "12"; # always use this code for tone 1
		default_port_code = 12;
	} syl2;
	
	sound {
		wavefile { filename = "complex_tu.wav"; preload = true;}; # comptone 1, frequency deviant
		 default_code = "13";
		 default_port_code = 13;
	} syl3;	
} syllables_std;
	
array{	
	sound {
		wavefile { filename = "complex_ti.wav"; preload = true; }; # comptone 1, standard tone
		default_code =  "27"; # always use this code for tone 1
		default_port_code = 27;		
	} syl10; 
	sound {
		wavefile { filename = "complex_te.wav"; preload = true; }; # comptone 1, standard tone
		default_code =  "28"; # always use this code for tone 1
		default_port_code = 28;
	} syl20;
	sound {
		wavefile { filename = "complex_tu.wav"; preload = true;}; # comptone 1, frequency deviant
		 default_code = "29";
		 default_port_code = 29;
	} syl30;
} syllables_dev;
	
	sound {
		wavefile { filename = "complex_ka.wav"; preload = true;}; # comptone 2, frequency deviant
		 default_code = "freqDev";
		 default_port_code = 20;
	} freqDev;
	
	trial{
		trial_duration = 100;
		
		stimulus_event{
			nothing{};
			port_code = 123;
		} eegHeaderStim;
    } eegHeader;
			

	
	 sound { 
		wavefile { filename = "complex_tu_long.wav"; };
		default_code = "end";
		default_port_code = 99;
	} spltest; 



# Main trial, standard SOA = 500 ms

	trial {
		trial_duration = 500; # SOA = 500 ms
		trial_type = fixed;
		
	/*	stimulus_event {
			picture {
				text {	
					caption = "+";
					font_size = 42;} togo;
					x = 0; y = 0; # presented in centre of the screen
				}fixationCross_pic;
				duration = 1000; # duration fixation cross (picture) is presented on screen
				time = 0;
				code = "fixCross";
			} fixationCross_trial; */
			
			stimulus_event {
				sound syl1;
				time = 0;
				#code = "syllable";
				duration = 150; 
			} toneEvent;
				
	}firstTrial;
	
	
	 ## SPL TEST TRIAL
	
	trial {
	#trial_duration = 3000; 
	trial_type = fixed;
			
			stimulus_event {
			sound spltest;
			time = 0;
			code = "start";
			port_code = 01; };
	}splTrial; 


## MARKER object

trial {
	stimulus_event {
		picture {
				} 	;
			time = 100;
			port_code = 50;	
			code = "marker";} markerEvent; 
		}markerTrial;

## PAUSE object

trial {
	stimulus_event {
		picture {
			text {
				caption = "END OF BLOCK" ;
				font_size = 42;
				};
				x = 0; y = 0;
				} pause_pic;
				duration = 2000;
				time = 0;
				code = "pause";
				port_code = 88;
				} ; 
		}pauseTrial;
		
#### START EXPERIMENT object

trial {
	trial_duration = 3000; 
	trial_type = fixed;
		
	stimulus_event {
			picture {
				text {	
					caption = "+";
					font_size = 42; }togoSt # bigger than default
				;
				x = 0; y = 0; # presented in centre of the screen
			} fixationCross_picSt;
			duration = 3000; # duration fixation cross (picture) is presented on screen
			time = 0;
			code = "fixCrossSt";
			} fixationCross_St;
			
			/*stimulus_event {
			sound start;
			time = 0;
			code = "start";
			port_code = 01; };*/
	}startTrial;
	
	
#### END EXPERIMENT object

trial {
	trial_duration = 3000; 
	trial_type = fixed;
		
	stimulus_event {
			picture {
				text {	
					caption = "+";
					font_size = 42; }togoEnd # bigger than default
				;
				x = 0; y = 0; # presented in centre of the screen
			} fixationCross_picEnd;
			duration = 3000; # duration fixation cross (picture) is presented on screen
			time = 0;
			code = "fixCrossEnd";
			} fixationCross_End;
			
			/*stimulus_event {
			sound end;
			time = 0;
			code = "end";
			port_code = 99; };*/
	}endTrial;
	
	
#############
# PCL block
#############

begin_pcl;

## EEG HEADER ##
    ## output date code and flanker
    eegHeaderStim.set_event_code("Date");
    eegHeaderStim.set_port_code(241);
    eegHeader.present();
    eegHeaderStim.set_port_code(int(date_time("yy")));
    eegHeader.present();
    eegHeaderStim.set_port_code(int(date_time("mm")));##FIXME
    eegHeader.present();
    eegHeaderStim.set_port_code(int(date_time("dd")));
    eegHeader.present();

    ## output time code and flanker
    eegHeaderStim.set_event_code("Start Time");
    eegHeaderStim.set_port_code(242);
    eegHeader.present();
    eegHeaderStim.set_port_code(int(date_time("hh")));
    eegHeader.present();
    eegHeaderStim.set_port_code(int(date_time("nn")));
    eegHeader.present();
    eegHeaderStim.set_port_code(int(date_time("ss")));
    eegHeader.present();
	
	## output scenario codes and flankers
    eegHeaderStim.set_event_code("Scenario Code");
    eegHeaderStim.set_port_code(243);
    eegHeader.present();
    eegHeaderStim.set_port_code(01);
    eegHeader.present();

##### Experiment Parameters#############

##### Experiment Parameters#############

############ SET UP TRIALS
# number of trials in  block
int numTrials = 1000;

# Hold stimulus order
array<int> stimOrder[numTrials];

	
# 1a: standard1, 1b: std2, 1c: std3 , 4: freqDev,
# Fill stim array 2 for Block ratio 90:10
	array<int> counts3[] = {300, 300, 300, 100};
	loop int i = 1 until i > numTrials
	begin
		 if( i <= counts3[1] ) then
			  stimOrder[i] = 1;
		 elseif ( i <= counts3[2] + counts3[1] ) then
			  stimOrder[i] = 2;
		 elseif ( i <= counts3[3] + counts3[2] + counts3[1] ) then
			  stimOrder[i] = 3;
		 elseif ( i <= counts3[4] + counts3[3] + counts3[2] + counts3[1] ) then
			  stimOrder[i] = 4;
		 end;
		i = i + 1;
	end;


####################### Start experiment ##############################

int stim;
int k;

#splTrial.present();

#### MAIN EXPERIMENT ####
	
startTrial.present();
markerEvent.set_port_code(60);
markerTrial.present(); 

#### SHUFFLE STIM ORDER FOR BLOCK, min 2 standards, max 4 standards
stimOrder.shuffle();
count = -15;# number of standard stim. in a row presented at the start of the experiment
#array<int> numStandards[] = {1, 2, 3};
loop int i = 1 until i > numTrials
begin
	current = stimOrder[i];
	if ( current > 3 && count < 2 ) then # increase count < "2" if need to increase min. number of standards in a row
		loop int j = i+1 until j > numTrials #until count < 2, stimOrder[j] = stimOrder[i]
		begin
			if ( stimOrder[j] == 1 ) then
				stimOrder[j] = current;
				stimOrder[i] = 1;
				count = count + 1;
				break;
			end;
			j = j + 1;
		end;
	elseif ( current <= 3 && count >= 4 ) then # increase count >= "4" to increase max. number of standards in a row
		loop int j = i+1 until j > numTrials
		begin
			if ( stimOrder[j] > 1 ) then
				stimOrder[i] = stimOrder[j];
				stimOrder[j] = current;
				count = 0;
				break;
			end;
			j = j + 1;
		end;
	elseif ( current > 3 && count >= 2 ) then # also change this "2" to reflect min. number of standards in a row
		count = 0; 
	else
		count = count + 1;
	end;
	term.print("count is ");
	term.print( count );
	term.print(" using stim ");
	term.print_line( stimOrder[i] );
	i = i + 1;
end;		

# PRESENT BLOCK with freqDev 
loop int i = 1 until i > numTrials
begin    
    stim = stimOrder[i];
	 if (i > 2) then
		k = stimOrder[i-2];
	 else
		k = -1;
	 end;
    if ( stim == 1 ) then # first stim is the standard
        toneEvent.set_stimulus(syl1);
		  firstTrial.set_duration(635);
		  #toneEvent.set_event_code("standard");
		  if i == 1 && stimOrder[i] == 1 then 
			  toneEvent.set_event_code("10"); #standard1 = syllable 1 
			  toneEvent.set_port_code(10);
		  elseif i > 1 && stimOrder[i - 1] == 4  then 
			  toneEvent.set_event_code("21");
			  toneEvent.set_port_code(21);
		  else
			  toneEvent.set_event_code("10");
		     toneEvent.set_port_code(10);
		  end;
	 elseif ( stim == 2 ) then # first stim is the standard
        toneEvent.set_stimulus(syl2);
		  firstTrial.set_duration(635);
		  #toneEvent.set_event_code("standard");
		  if i == 1 && stimOrder[i] == 2 then 
			  toneEvent.set_event_code("10"); #standard1 = syllable 1 
			  toneEvent.set_port_code(10);
		  elseif i > 1 && stimOrder[i - 1] == 4  then 
			  toneEvent.set_event_code("21");
			  toneEvent.set_port_code(21);
		  else
			  toneEvent.set_event_code("10");
		     toneEvent.set_port_code(10);
		  end;
	 elseif ( stim == 3 ) then # first stim is the standard
        toneEvent.set_stimulus(syl3);
		  firstTrial.set_duration(635);
		  #toneEvent.set_event_code("standard");
		  if i == 1 && stimOrder[i] == 3 then 
			  toneEvent.set_event_code("10"); #standard1 = syllable 1 
			  toneEvent.set_port_code(10);
		  elseif i > 1 && stimOrder[i - 1] == 4  then 
			  toneEvent.set_event_code("21");
			  toneEvent.set_port_code(21);
		  else
			  toneEvent.set_event_code("10");
		     toneEvent.set_port_code(10);
		  end;
    elseif ( stim == 4 ) then # second stim is the freqDev
		  toneEvent.set_stimulus(freqDev);
		  firstTrial.set_duration(635);
		  toneEvent.set_event_code("freqDev");
		  toneEvent.set_event_code("20");
		  toneEvent.set_port_code(20);
    end;


#	togo.set_caption(string(i)+"/"+string(numTrials));
#	togo.unload();
#	togo.load();
	 
	firstTrial.present();				

	i = i + 1;
		
end;

markerEvent.set_port_code(65);
markerTrial.present();

endTrial.present();

## WRITE SCE END CODE
	eegHeaderStim.set_event_code("Scenario Complete");
	eegHeaderStim.set_port_code(02);
	eegHeader.present();










	