function dlxexp5sh2

% DLXEXP5sh1  Run dyslexia experiment 5, second shorter version, multispeaker then 1 speaker
%
% Script written by Stephen Wilson <stephenw@ucla.edu>, 2002-05.

% variables
soundvolume = 4; % you can change the sound volume here
% probably don't go over about 4 or there will be clipping
bbox = 0; % set this to 1 if there's a button box, 0 if there isn't

% specific initialization
thispath = which('dlxexp5sh2');
[pathstr name ext versn] = fileparts(thispath);
cd(pathstr);

list = mytextread('dlxexp5sh-list2.txt');
fieldsperitem = 20; %number of lines including blank for each trial in list file, set to max needed and pad all to match
nitems = length(list) / fieldsperitem; % n.b. last item is dummy to show text, since it's functioning like a trial it needs %the full number of lines ='fieldsperitem'
trialnum = 0;

snd('Open');
rand('state', sum(100*clock));

if bbox
    port = psychserial('Open', '.Bin', '.Bout', 19200);	
    a0 = psychserial('Read', port, 0.020); % I don't know what this does
else
    port = 0;
end

% generic initialization
outfid = dlxexpinit('dlxexp5sh2');
if outfid == -1
    return;
end

fprintf(outfid, 'item\ttest\ttrial\ttarget\tword\trt\tkey\tbbox\tcorr\n');

screen('CloseAll');
grey = 100;
[w, wrect] = screen(0, 'OpenWindow', [grey grey grey], [], 32);
screen(w, 'TextFont', 'Helvetica');
screen(w, 'TextFont', 'Arial');
screen(w, 'TextStyle', 1);
screen(w, 'TextSize', 42);
%screen_clut = [[0:255]', [0:255]', [0:255]'];
%screen(w, 'SetClut', screen_clut);
hidecursor;
centerrect = [wrect(3) / 2 - 100, wrect(4) / 2 - 100, wrect(3) / 2 + 100, wrect(4) / 2 + 100];

% wrect is size of screen; wrect(3) is width; wrect(4) is height
barwidth = 400;
barheight = 100;
outerbarrect = [wrect(3) / 2 - barwidth / 2, wrect(4) / 2 - barheight / 2, ...
        wrect(3) / 2 + barwidth / 2, wrect(4) / 2 + barheight / 2];
greybarrect = insetrect(outerbarrect, 30, 30);
barrect = insetrect(greybarrect, 2, 2); % for actually filling in

accuratewait(0.5);

% main loop
for item = 1:nitems

    offset = (item - 1) * fieldsperitem; 
    targetword = list{offset + 4}; %targetword is on 4th line
    ntesttrials = str2num(list{offset + 3}) - 1; %number is on 3rd line
 
    % display entertaining image if specified on line 2 of item
    entimgname = list{offset + 2};
    if ~isempty(entimgname)    
        img = imread(fullfile('dlxexp5-pics', entimgname));
        if size(img, 1) > wrect(4) - 300
            scalefactor = (wrect(4) - 300) / size(img, 1);
            imgrect = [wrect(3) / 2 - round(size(img, 2) / 2 * scalefactor), ...
                wrect(4) / 2 - round(size(img, 1) / 2 * scalefactor), ...
                wrect(3) / 2 + round(size(img, 2) / 2 * scalefactor), ...
                wrect(4) / 2 + round(size(img, 1) / 2 * scalefactor)];
            screen(w, 'PutImage', img, imgrect);
        else
            scalefactor = 1;
            screen(w, 'PutImage', img);
        end
        text = list{offset + 1}; %header text if any on line 1
        width = screen(w, 'TextWidth', text);
        screen(w, 'DrawText', text, wrect(3) / 2 - width / 2, ...
            wrect(4) / 2 + size(img, 1) * scalefactor / 2 + 60, [255 255 255]);
        while kbcheck, end
        while ~kbcheck, end
        screen(w, 'FillRect', grey);
        while kbcheck, end
    end

    if ntesttrials > 0

        img = imread(fullfile('dlxexp5-pics', 'ear.jpg'));
        screen(w, 'PutImage', img);
        accuratewait(0.5);
     
        [wav, samprate, bits] = wavread(fullfile('dlxexp5sh-wavs', [list{offset + 4} '.wav']));
        wav = wav';
        if item <= 16  %prompts get loudness equalized
            wav = normalize(wav);
        end
        snd('Play', wav * soundvolume, samprate, bits); %plays the prompt in a trial

        while snd('IsPlaying'), end
        accuratewait(1); % wait 1 second from end of prompt
        
        [wav, samprate, bits] = wavread(fullfile('dlxexp5sh-wavs', list{offset + 5}));
        wav = wav';
        if item <= 16  %targets get loudness equalized
            wav = normalize(wav);
        end
        snd('Play', wav * soundvolume, samprate, bits); %play the target in a trial

        while snd('IsPlaying'), end
        accuratewait(1); % wait 1 second from end of target word
        
        screen(w, 'FillRect', grey);
        accuratewait(1);
        while kbcheck, end
	
        for t = 1:ntesttrials
            trialnum = trialnum + 1;
            testword = list{offset + 4 + t * 2};
            [wav, samprate, bits] = wavread(fullfile('dlxexp5sh-wavs', list{offset + 5 + t * 2}));
            wav = wav';
            if item <= 8  %probes get loudness equalized
                wav = normalize(wav);
            end
                        
            % screen(w, 'DrawText', '?', wrect(3) / 2, wrect(4) / 2, [255 255 255]);

            screen(w, 'FillRect', [255 255 255], outerbarrect); % draw white rectangle
            screen(w, 'FrameRect', grey, greybarrect); % draw grey outline inside
        
            snd('Play', wav * soundvolume, samprate, bits); %plays the probes in a trial
            [keydown, keycode, button, rt] = getresponsewithbar(bbox, port, w, barrect);
            if keycode == 'q'
                snd('Close');
				fclose(outfid);
				screen('CloseAll');
                error('Quit key pressed.');
            end
            snd('Quiet');
            screen(w, 'FillRect', grey);
            
            match = strcmp(targetword, testword) | testword(end) == '*';
            
            if match == 1 & (keycode == 'y' | button == 1 | button == 8)
                corr = 1;
            elseif match == 0 & (keycode == 'n' | button == 4 | button == 16)
                corr = 1;
            else
                corr = 0;
            end
            
            fprintf(outfid, '%d\t%d\t%d\t%s\t%s\t%.3f\t%s\t%d\t%d\n', item, t, trialnum, targetword, ...
                testword, rt, keycode, button, corr);
            while kbcheck, end
            accuratewait(0.5);
        end
    end
    screen(w, 'FillRect', grey);
    accuratewait(1);
end

% finish up
snd('Close');
fclose(outfid);
screen('CloseAll');


function y = normalize(wav)

sortwav = sort(abs(wav));
newmax = sortwav(floor(0.99 * length(sortwav))); % 99th percentile
y = wav / newmax * 0.2; % scale to 20%

function [keydown, keycode, button, rt] = getresponsewithbar(bbox, port, w, barrect)

trialstarttime = getsecs;
button = [];
keydown = 0;
barsdone = 0;

while isempty(button) & keydown == 0
    
    if getsecs - trialstarttime > (barsdone + 1) * 0.1 & barsdone < 20
        barsdone = barsdone + 1;
        if barsdone <= 15
            color = [0 160 0];
        elseif barsdone <= 19
            color = [255 150 0];
        elseif barsdone == 20
            color = [255 0 0];
        end
        growingbarrect = [barrect(1) + (barrect(3) - barrect(1)) * (barsdone - 1) / 20, barrect(2), ...
                barrect(1) + (barrect(3) - barrect(1)) * barsdone / 20 - 1, barrect(4)];
        % growingbarrect = [barrect(1), barrect(2), barrect(1) + (barrect(3) - barrect(1)) * barsdone / 20, barrect(4)];
        screen(w, 'FillRect', color, growingbarrect);
    end
    
    if bbox
        button = psychserial('Read', port, 0.001);
        if ~isempty(button)
            button = double(button(1));
            if button ~= 1 & button ~= 2 & button ~=4
                button = [];
            end
        end
    else
        button = [];
    end

    [keydown, secs, keys] = kbcheck;
end

trialendtime = getsecs;
rt = trialendtime - trialstarttime;

if keydown
    keycode = kbname(keys);
else
    keycode = 0;
end

if isempty(button)
    button = 0;
end

