function dlxexp6

% DLXEXP6  Run dyslexia experiment 6.
%
% Script written by Stephen Wilson <stephenw@ucla.edu>, 2002-03.

% variables
soundvolume = 1; % you can change the sound volume here
bbox = 1; % set this to 1 if there's a button box, 0 if there isn't

% specific initialization
numpractice = 4;
numtargets = 12;
numtotal = numpractice + numtargets;
thispath = which('dlxexp6');
[pathstr name ext versn] = fileparts(thispath);
cd(pathstr);

% list = textread('dlxexp6-list.txt', '%s', 'delimiter', '\n', 'whitespace', '');
list = mytextread('dlxexp6-list.txt'); % Matlab version 5 does not have textread

targetorder = [1:numpractice randperm(numtargets) + 4];
targetreps = [1 1 1 1 2 2 2 2 3 3 3 3];
targetreps = targetreps(randperm(length(targetreps)));
targetreps = [2 3 1 2 targetreps]
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('dlxexp6');
if outfid == -1
    return;
end

fprintf(outfid, 'trial\ttarget\tword\trt\tkey\tbbox\tcorr\n');

screen('CloseAll');
grey = 100;
[w, wRect] = screen(0, 'OpenWindow', grey, [], 8);
screen_clut = [[0:255]', [0:255]', [0:255]'];
screen(w, 'SetClut', screen_clut);
hidecursor;
accuratewait(3);

% main loop
for target = 1:numtotal
   
    testwords = {};
    rem = list{targetorder(target)};
    while ~isempty(rem)
    [next, rem] = strtok(rem);
        testwords = [testwords {next}];
    end
    targetword = testwords{1};
    testorder = [ones(1, targetreps(target)) 2:length(testwords)]
    numtestwords = length(testorder);
    testorder = testorder(randperm(length(testorder)))
    
    % display picture
    p = imread(fullfile('dlxexp6-pics', [targetword '.bmp']));
    p = double(p);
    p = 255 * (1 - p);
    screen(w, 'PutImage', p);
    
    [wav, samprate, bits] = wavread(fullfile('dlxexp6-wavs', [targetword '.wav']));
    wav = wav';
    snd('Play', wav * soundvolume, samprate, bits);
    
    while snd('IsPlaying'), end
    accuratewait(2); % wait 2 seconds from end of target word
    while kbcheck, end
    
    for test = 1:numtestwords
        trialnum = trialnum + 1;
        [wav, samprate, bits] = wavread(fullfile('dlxexp6-wavs', [testwords{testorder(test)} '.wav']));
        wav = wav'; % this is necessary to run on mac
        snd('Play', wav * soundvolume, samprate, bits);
        [keydown, keycode, button, rt] = getresponse(bbox, port);
        snd('Quiet');
        
        match = testorder(test) == 1;
        
        if match == 0 & (keycode == 'n' | button == 1)
            corr = 1;
        elseif match == 1 & (keycode == 'y' | button == 4)
            corr = 1;
        else
            corr = 0;
        end
        
        fprintf(outfid, '%d\t%s\t%s\t%.3f\t%s\t%d\t%d\n', trialnum, targetword, ...
            testwords{testorder(test)}, rt, keycode, button, corr);
        while kbcheck
        end
        accuratewait(0.5);
    end
    screen(w, 'FillRect', grey);
    accuratewait(3);
    
    if target == numpractice
        screen(w, 'DrawText', 'You''ve finised the practice items. Press any key when you''re ready to start.', 200, 200, 255);
        accuratewait(1);
        kbwait;
        screen(w, 'FillRect', grey);
        accuratewait(3);
    end
end

% finish up
snd('Close');
fclose(outfid);
screen(w, 'DrawText', 'You''ve finished! Press any key to continue.', 200, 200, 255);
accuratewait(1);
kbwait;
screen('CloseAll');
