function [TargetSound, NoTargetSound,TSquintOnset] = CreateStimuli(currCon, JND,TS)


% include variable length of sequence

fs = 44100;

rhythm = 2;
ISI= 1000/rhythm; %in ms
ISIsamp = zeros(1,ceil((ISI/1000)*fs));
f = 1100;
TSinS = JND; 
stimLength = 6; %length in s 
TS = TS*rhythm; 

% quick and dirty fix:  if TS  = 0 index runs out, however, TS is relevant
% only for TS trials therefore it does not matter what happens with TS in
% TS 0 trials thus change it for simplicity to 4 and it runs 
% however, does highlight a generalization issue of my stimulus script
if TS == 0
    TS  = 4 ;
end 

% create quintet and quintet with target
[quintet,quintetTarg] = quintets(f,TSinS,fs);



%%  rhythmic sequence
    
    if currCon == 1
        
        periodicISI = repelem(ISI,(stimLength*rhythm)-1);
        rStim = [];
        % burst without target
        % build sound sequence of quintets and ISIs
        for idx = 1:length(periodicISI)
            curr = periodicISI(idx);
            sil = zeros(1,ceil((curr/1000)*fs)-length(quintet));
            stim = [quintet, sil];
            rStim = [rStim, stim];
            stim = [];
        end
        rStim = [ rStim, quintet];
        
        NoTargetSound = rStim; 
        %burst with target
        
        burst = quintet;
        rStimTarg = [];
        % build sound sequence of quintets and ISIs
        for idx = 1:length(periodicISI)
            curr = periodicISI(idx);
            if idx == TS % insert target quintet as burst
                burst = quintetTarg;
            elseif idx == TS+1  % insert regular quintet as burst following target
                burst = quintet;
            end
            
            sil = zeros(1,ceil((curr/1000)*fs)-length(quintet));
            stim = [burst, sil];
            rStimTarg = [rStimTarg, stim];
            stim = [];
        end
        rStimTarg = [ rStimTarg, quintet];
        TargetSound = rStimTarg; 

        % target position 
        prevISI = periodicISI(1:TS-1);
%         TSquintOnset =(TS-1)*(length(quintet)/fs)+ sum(prevISI)/1000;
        TSquintOnset =sum(prevISI)/1000;
            
        figure 
        plot(rStimTarg)
        sound(rStimTarg,fs)

    end 
%% %% jittered sequence
    
    if currCon == 2 
        %without target
        
        % constant set of 'arrhythmic' ISIs (in deviations from rhythmic ISI in ms)
%         jitInt = ISI+[- 300 -150 0 150 300]
%         
        %% 
        
        % to make it easier for myself these add up to an ISI of 500 
        x = [110 130 210 230 280 445 740 700 965 1190];
        
        jitInt = [x ISI];

%         jitInt = repmat(jitInt,1,rhythm);
        % add 1 ISI distance to keep similar to target sound 
%         jitInt = [jitInt ISI]; 
        % permute order of ISIs
        temp = randperm(length(jitInt));
        jittStim = [];
        
        % build sound sequence of quintets and ISIs
        for idx = 1:length(jitInt)
            curr = jitInt(temp(idx));
            % create silence/IQI by subtracting length of quintet from
            % current randIQI 
            sil = zeros(1,ceil((curr/1000)*fs)-length(quintet));
            stim = [quintet, sil];
            
            jittStim = [jittStim, stim];
            stim = [];
        end
        
        jittStim = [ jittStim, quintet];
        NoTargetSound = jittStim;
%         
        figure
        plot(jittStim)
%         title([' no target sound'])

        
        % with target isoIQI precedes TSquintet
        
    
        jitIntTarg = x; %change here

        temp = randperm(length(jitIntTarg));
        randISI = jitIntTarg(temp); %ensures randomized order of ISIs
        jittStimTarg = [];
        burst = quintet;
        for idx = 1:length(jitIntTarg)+1 % 1 more quintet than IQIs
            
            if idx == TS-1 %insert isoIQI in fornt of target quintet 
                randISI = [randISI(1:idx-1) ISI randISI(idx:end)];
            elseif idx == TS % insert target quintet as burst
                burst = quintetTarg;
                prevISI = randISI(1:TS-1);
                % target position 
%                 TSquintOnset =(TS-1)*(length(quintet)/fs)+ sum(prevISI)/1000;
                TSquintOnset =sum(prevISI)/1000;

            elseif idx == TS+1  % insert regular quintet as burst following target
                burst = quintet;
            end
            curr = randISI(idx);
            
            %build stimulus element of burst and ISI
%             stimTarg = [burst, zeros(1,ceil((curr/1000)*fs))];
            sil = zeros(1,ceil((curr/1000)*fs)-length(quintet));
            stimTarg = [burst, sil];
            %combine stimulus element with previous elements
            jittStimTarg = [jittStimTarg, stimTarg];
            stimTarg = [];
             
           
        end
        
        %end burst at end of stimulus
        jittStimTarg = [ jittStimTarg, quintet];
        TargetSound = jittStimTarg; 
        
%         figure
%         plot(jittStimTarg)
%         title(['target sound'])
% %         sound(jittStimTarg,fs)
    end
    
    %% cued interval

    if currCon == 3
        
        cueTarg = [quintet, zeros(1,(ISI/1000)*fs),quintetTarg];
        TargetSound = cueTarg; 
        cueQuint = [quintet, zeros(1,(ISI/1000)*fs),quintet];
        NoTargetSound = cueQuint; 

        % target position
        TSquintOnset = ISI/1000;
        figure
        plot(cueQuint)
    end
