ita_listeningtest_JND.m 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
function ita_listeningtest_JND()


% <ITA-Toolbox>
% This file is part of the application ListeningTests for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>

 soundPath = 'Z:\Stimuli\EDT'; 
% mkdir(soundPath)
% morseNumbers = test_mgu_morseCode();
% for iNumber = 1:10
%     ita_write(morseNumbers(iNumber), fullfile(soundPath, sprintf('Morse Code Number %i.ita', rem(iNumber, 10))));
% end

%%
%%% INITIALIZATION OF GUI INPUT DATA
br = sprintf('\n');

% General parameters
ltData.testName         = 'JND Listening Test EDT';
ltData.introParameter         = [ br br 'Herzlich Willkommen zum Hrversuch' br br ...
                             'Im Rahmen dieses Hrversuches bekommen sie Paare von raumakustischen Stimuli ' ...
                             'vorgespielt,' 'wobei Sie fr jedes Paar entscheiden sollen, welches Stimuli den greren Nachhall hat.' br br ...
                             'Zunchst werden Sie ein ungewertetes Training absolvieren, gefolgt von dem eigentlichen Hrversuch.'...
                             'Im Training bekommen Sie Angaben ber die Richtigkeit ihrer Wahl der Stimuli,' 'in dem Versuch hingegen nicht.' br br...
                             'Viel Spa!' 
                             ];

ltData.introAufgabe         = [ br br 'Definition:' br br ' Nachhall nennt sich der Schall der in einem Raum, der nach dem ein Ton' br ' pltzlich gestoppt hat, weiterhin besteht.' br br...
                             'Je lebendiger und lnger der Schall wirkt, desto grer ist der Nachhall.' br 'Je krzer und trockener der Schall wirkt, desto geringer ist der Nachhall.' br  br br... 
                              'Frage:' br br ' In welchem der beiden Beispiele ist der Nachhall strker ausgeprgt?' br br ...
                             ];
                         
ltData.txtEndeTraining      = [ br 'Das Training ist beendet, der Versuch beginnt jetzt. ' br br ];
                         
ltData.trainingPath          =    soundPath;
ltData.trainingFiles         = {'Var_EDT K03-R05-P12.ita' 'Var_EDT K04-R04-P01.ita'; ...%3
                                'Var_EDT K02-R05-P02.ita' 'Var_EDT K04-R04-P01.ita'; ...%2
                                'Var_EDT K02-R04-P05.ita' 'Var_EDT K04-R04-P01.ita'; ...%1
                                'Var_EDT K03-R05-P12_Tr10.ita' 'Var_EDT K04-R04-P01_Tr10.ita'; ...%3
                                'Var_EDT K02-R05-P02_Tr10.ita' 'Var_EDT K04-R04-P01_Tr10.ita'; ...%2
                                'Var_EDT K02-R04-P05_Tr10.ita' 'Var_EDT K04-R04-P01_Tr10.ita'; ...%1
                                };
%ltData.trainingFiles      = {'female_1.wav' 'female_2.wav'; ...
%                            'female_3.wav' 'female_4.wav'; ...
%                            'female_6.wav' 'female_7.wav'; ...
%                            'female_8.wav' 'female_9.wav'; ...
%                            };            
ltData.trainingCorrectAnswer = [ 1 1 1 1 1 1 ];


ltData.compareQuestion   = [ br 'In welchem der beiden Beispiele ist der Nachhall strker ausgeprgt?'];              


ltData.testPath  =    soundPath;
ltData.testFiles =  {'Var_EDT K02-R05-P02.ita' 'Var_EDT K02-R05-P05.ita'; ...
                     'Var_EDT K01-R05-P02.ita' 'Var_EDT K01-R05-P09.ita'; ...
                     'Var_EDT K02-R04-P05.ita' 'Var_EDT K03-R05-P12.ita'; ...
                     'Var_EDT K03-R05-P12.ita' 'Var_EDT K04-R04-P01.ita'; ...
                     'Var_EDT K02-R05-P02.ita' 'Var_EDT K04-R04-P01.ita'; ...
                     'Var_EDT K02-R04-P05.ita' 'Var_EDT K04-R04-P01.ita'; ...
                     
                     'Var_EDT K02-R05-P02_Tr10.ita' 'Var_EDT K02-R05-P05_Tr10.ita'; ...
                     'Var_EDT K01-R05-P02_Tr10.ita' 'Var_EDT K01-R05-P09_Tr10.ita'; ...
                     'Var_EDT K02-R04-P05_Tr10.ita' 'Var_EDT K03-R05-P12_Tr10.ita'; ...
                     'Var_EDT K03-R05-P12_Tr10.ita' 'Var_EDT K04-R04-P01_Tr10.ita'; ...
                     'Var_EDT K02-R05-P02_Tr10.ita' 'Var_EDT K04-R04-P01_Tr10.ita'; ...
                     'Var_EDT K02-R04-P05_Tr10.ita' 'Var_EDT K04-R04-P01_Tr10.ita'; ...
                     };
%ltData.testFiles = {'female_1.wav' 'female_2.wav'; ...
%                    'female_3.wav' 'female_4.wav'; ...
%                   'female_6.wav' 'female_7.wav'; ...
%                   'female_8.wav' 'female_9.wav'; ...
%                    };                               
% ltData.testFiles =  reshape(ita_sprintf('Morse Code Number %i.ita', 0:9), [], 2);

ltData.pauseBefore = 2;
ltData.pauseBetween = 0.5;

ltData.txtEndeTest  = [br 'Der Hrversuch endent hier, vielen Dank fr Ihre Teilnahme.'];

ltData.resultPath = fullfile(fileparts(ltData.testPath), 'Results');
%%
ita_preferences('portAudioMonitor', 0)
if ~exist(ltData.resultPath, 'dir' )
    mkdir(ltData.resultPath )
end
%%


vpNumber = ita_str2num(inputdlg('Versuchtsperson Nummer:'));

if isempty(vpNumber)
    return
end
%  if ~isnumeric(vpNumber)
%      errordlg(sprintf('no number !'))
%      error(sprintf('no number !'))
%  end

ltData.testPerson.number = vpNumber;
ltData.testPerson.start = now;




%% generate GUI
screenSize = get(0, 'ScreenSize');


h.f = figure('Visible','on','NumberTitle', 'off', 'Position', screenSize, 'Name',ltData.testName  ,'MenuBar', 'none');
movegui(h.f,'center')

h.text          = uicontrol('Style','text','String','', 'Units', 'normalized', 'Position',  [0.1 0.35 0.8 0.55], 'HorizontalAlignment', 'center' , 'fontsize', 20);

h.buttonA    = uicontrol('Style','pushbutton', 'String','A','units', 'normalized', 'Position',[0.3 0.2 0.1 0.1], 'Callback', {@nextStep});
h.buttonB    = uicontrol('Style','pushbutton', 'String','B','units', 'normalized', 'Position',[0.6 0.2 0.1 0.1], 'Callback', {@nextStep});

h.nextButton    = uicontrol('Style','pushbutton', 'String','Weiter','units', 'normalized', 'Position',[0.8 0.1 0.1 0.1], 'Callback', {@nextStep});

% if ltData.showProgress
%     h.progress  =   uicontrol('Style','text','String',' 0 / 0', 'Position',[ layout.defaultSpace  layout.defaultSpace  120 20]);
%     h.hArray    = [h.hArray h.progress ];
% end

%% handles data

gData.ltData       = ltData;
gData.h = h;

gData.nextPhase = 'IntroParameter';



% allCombinations             = nchoosek(1:ltData.nSounds,2);
% h.data.currentLT.nSets      = size(allCombinations,1);
% [del, randIDX]               = sort(rand(h.data.currentLT.nSets,1));  
% h.data.currentLT.playlist   = allCombinations(randIDX,:);
% h.data.currentLT.currentSet = 0;
% h.data.currentLT.prefMat    = zeros(ltData.nSounds);              % prefMat(i,j) = is i prefered over j ?
% if ltData.useAttributes
%     h.data.currentLT.selectedAttribute   = zeros(ltData.nSounds);                % selectedAttribute(i,j) = attrib for i ; selectedAttribute(j,i) = attrib for j  
% end


% set(h.hArray, 'Visible', 'off')
% set([h.bigText h.nextButton] , 'Visible', 'on')


guidata(gData.h.f, gData)
set(gData.h.f,'Visible','on')%, 'CloseRequestFcn', {@CloseRequestFcn}) 

nextStep(gData.h.f, [])


end





function nextStep(obj, ~)
gData = guidata(obj);


switch gData.nextPhase
    
    case 'IntroParameter'
        set([gData.h.buttonA gData.h.buttonB ] , 'visible', 'off');
        set([gData.h.nextButton  gData.h.text ] , 'visible', 'on');
        set(gData.h.text, 'string', gData.ltData.introParameter );
        gData.nextPhase = 'IntroAufgabe';
    case 'IntroAufgabe'
        set([gData.h.buttonA gData.h.buttonB ] , 'visible', 'off');
        set([gData.h.nextButton  gData.h.text ] , 'visible', 'on');
        set(gData.h.text, 'string', gData.ltData.introAufgabe );
        set(gData.h.nextButton, 'string', 'Training starten');
        gData.nextPhase = 'Training';
        gData.currentSet = 0;
    case 'Training'
        
        % letzte auswahl speichern
        if gData.currentSet > 1
            set([gData.h.buttonA gData.h.buttonB ] , 'visible', 'off');
            auswahl = find(strcmpi({'A' 'B'}, get(obj, 'string')));
            gData.output.trainingAuswahl(gData.currentSet-1) = auswahl;
            
            % FEEDBACK
            currCorrectAuswahl = gData.ltData.trainingCorrectAnswer(gData.currentSet-1);
            if gData.changeABorder(gData.currentSet-1)
                currCorrectAuswahl = 3 - currCorrectAuswahl;
            end
            
            if currCorrectAuswahl == auswahl
                set(gData.h.text, 'string',  'Richtig ! ' );
            else
                set(gData.h.text, 'string',  'Falsch ! ' );
            end
            
            pause(2)
            
            
        end
        
        if gData.currentSet  == 0 % vorbereiten
            set(gData.h.nextButton, 'visible', 'off');
             set(gData.h.text  , 'string', 'Lade Daten....');
             pause(0.1)
            [nTrainingSets, nSoundsPerSet] = size(gData.ltData.trainingFiles);
            
            if nSoundsPerSet ~= 2
                error('trainingFiles cell muss nSets x 2 gro sein!')
            end
            
            gData.trainingSounds = itaAudio(nTrainingSets, 2);
            
            for iSound = 1:nTrainingSets*2
               gData.trainingSounds(iSound) = ita_read(fullfile(gData.ltData.trainingPath, gData.ltData.trainingFiles{iSound}));
            end
            set(gData.h.text  , 'visible', 'off');
            set(gData.h.nextButton, 'string', 'Abspielen', 'fontweight', 'bold', 'visible', 'on');
            gData.trainingsSetOrder = randperm(nTrainingSets);
            gData.changeABorder = round(rand(nTrainingSets,1));
            gData.currentSet  = 1;
            gData.output.trainingPlayList = cell(nTrainingSets,2);
            gData.output.trainingAuswahl = zeros(nTrainingSets,1);
        elseif gData.currentSet > size(gData.trainingSounds,1)      % nchste phase
            
            gData.trainingSounds = [];
            
            set([gData.h.buttonA gData.h.buttonB ] , 'visible', 'off');
            set([gData.h.nextButton  gData.h.text ] , 'visible', 'on');
            set(gData.h.text, 'string',  gData.ltData.txtEndeTraining  );
            set(gData.h.nextButton, 'string',  'Hrversuch starten' );
            
            gData.nextPhase = 'ListeningTest';
            gData.currentSet  = 0;
            
            
        else
            set([gData.h.buttonA gData.h.buttonB  gData.h.nextButton  gData.h.text ] , 'visible', 'off');

            currSounds    = gData.trainingSounds(gData.trainingsSetOrder(gData.currentSet),:);
            currFileNames = gData.ltData.trainingFiles(gData.trainingsSetOrder(gData.currentSet),:);
            
            if gData.changeABorder(gData.currentSet)
                currSounds = currSounds(end:-1:1);
                currFileNames = currFileNames(end:-1:1);
            end
%             currFileNames
            gData.output.trainingPlayList(gData.currentSet,:) = currFileNames;
            
            % wait for
            nSteps = 50;
            wbh = waitbar(0, 'Wiedergabe beginnt ...');
            for iStep = 1:nSteps
                waitbar(iStep / nSteps, wbh);
                pause(gData.ltData.pauseBefore / nSteps)
            end
            close(wbh)
            
            
%             % play A
%             currSounds(1).play
%             % pause
%             pause(gData.ltData.pauseBetween)
%             % play B
%             currSounds(2).play
                 
            ita_portaudio_run(currSounds(1), 'OutputChannels', 1:2, 'Block')
            ita_portaudio_run(currSounds(2), 'OutputChannels', 1:2, 'Block')
            
            set([gData.h.buttonA gData.h.buttonB  gData.h.text ] , 'visible', 'on');
            set( gData.h.text , 'string', gData.ltData.compareQuestion)

            gData.currentSet  = gData.currentSet  + 1;
        end
        
%     case 'TxtEndeTraining'
%         set([gData.h.buttonA gData.h.buttonB ] , 'visible', 'off');
%         set([gData.h.nextButton  gData.h.text ] , 'visible', 'on');
%         set(gData.h.text, 'string',  gData.ltData.txtEndeTraining  )
%         set(gData.h.nextButton, 'string',  'Hrversuch starten' );
%         
%         gData.nextPhase = 'ListeningTest';
%         gData.currentSet  = 0;
    case 'ListeningTest'
           % letzte auswahl speichern
        if gData.currentSet > 1
            find(strcmpi({'A' 'B'}, get(obj, 'string')))
            gData.output.testAuswahl(gData.currentSet-1) = find(strcmpi({'A' 'B'}, get(obj, 'string')));
        end
        
        if gData.currentSet  == 0 % vorbereiten
            set(gData.h.nextButton, 'string', 'Abspielen');
            set(gData.h.text  , 'visible', 'off');
            [nSets, nSoundsPerSet] = size(gData.ltData.testFiles);
            
            if nSoundsPerSet ~= 2
                error('testFiles cell muss nSets x 2 gro sein!')
            end
            
            gData.testSounds = itaAudio(nSets, 2);
            
            for iSound = 1:nSets*2
               gData.testSounds(iSound) = ita_read(fullfile(gData.ltData.testPath, gData.ltData.testFiles{iSound}));
            end
            
            gData.testSetOrder = randperm(nSets);
            gData.changeABorder = round(rand(nSets,1));
            gData.currentSet  = 1;
            gData.output.testPlayList = cell(nSets,2);
            gData.output.testAuswahl = zeros(nSets,1);
        elseif gData.currentSet > size(gData.testSounds,1) % nchste phase
            % save data
            gData.testSounds = [];
            save(fullfile(gData.ltData.resultPath, sprintf('VP%02i_%s.mat', gData.ltData.testPerson.number, datestr(now, 'yyyy-mm-dd_HHMM') )), 'gData')

            %
            gData.nextPhase = 'EndOfTest';
            set([gData.h.buttonA gData.h.buttonB ] , 'visible', 'off');
            set([gData.h.nextButton  gData.h.text ] , 'visible', 'on');
            set(gData.h.text, 'string',  gData.ltData.txtEndeTest  )
            set(gData.h.nextButton, 'string',  'Schlieen' );

        else
            
            set([gData.h.buttonA gData.h.buttonB  gData.h.nextButton  gData.h.text ] , 'visible', 'off');

            currSounds    = gData.testSounds(gData.testSetOrder(gData.currentSet),:);
            currFileNames = gData.ltData.testFiles(gData.testSetOrder(gData.currentSet),:);
            
            if gData.changeABorder(gData.currentSet)
                currSounds = currSounds(end:-1:1);
                currFileNames = currFileNames(end:-1:1);
            end
            
            gData.output.testPlayList(gData.currentSet,:) = currFileNames;
            
            % wait for
            nSteps = 50;
            wbh = waitbar(0, 'Wiedergabe beginnt ...');
            for iStep = 1:nSteps
                waitbar(iStep / nSteps, wbh);
                pause(gData.ltData.pauseBefore / nSteps)
            end
            close(wbh)
            
            
%             % play A
%             currSounds(1).play
%             % pause
%             pause(gData.ltData.pauseBetween)
%             % play B
%             currSounds(2).play
            
            ita_portaudio_run(currSounds(1), 'OutputChannels', 1:2, 'Block')
            ita_portaudio_run(currSounds(2), 'OutputChannels', 1:2, 'Block')
            
            
            set([gData.h.buttonA gData.h.buttonB  gData.h.text ] , 'visible', 'on');
            set( gData.h.text , 'string', gData.ltData.compareQuestion)

            gData.currentSet  = gData.currentSet  + 1;
            

        end
        
    case 'EndOfTest'
        close(gData.h.f)
        return
        
    otherwise 
        error('unkown phase: %s', gData.nextPhase)
end



guidata(gData.h.f, gData)
end