ita_listeningtest_AB_comparison_mar.m 25.6 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
%% Main Function

% <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>

function ita_listeningtest_AB_comparison_mar(ltData)
%ITA_LISTENINGTEST_AB_COMPARISON - gui for complete one-sided AB comparison
%  This function creates a GUI that can be used for complete one-sided
%  AB comparisons with a user definable number of control pairs
%
%  Syntax:
%   ltResult = ita_listeningtest_AB_comparison(ltData)
%
%   Please call this function with a struct 'ltData' that contains all
%   input parameters for the listening test. A guideline for the creation
%   of the ltData struct can be found in the file
%   ltData = ita_generate_input_for_lt_AB_comparison()
%
%  Example:
%   ltResult = ita_listeningtest_AB_comparison(ltData)
%
%  See also: ita_listeningtest_AB_cmp_generate_input
%   
%
%   Reference page in Help browser 
%        <a href="matlab:doc ita_listeningtest_AB_comparison">doc ita_listeningtest_AB_comparison</a>
%
% Author: Martin Guski -- Email: mgu@akustik.rwth-aachen.de
% Created:  04-Mar-2011 
% Edited:   mar 30-May-2011
%

%%% store ltData in handles structure
h.ltData                    = ltData;

%%% check data in ltData
[y1, h.data.samplingRate] = wavread(ltData.soundList{1});
h.data.nChannels = size(y1,2);
h.data.nSamples  = size(y1,1);

for iSound = 1:ltData.nSounds
    [y, fs] = wavread(ltData.soundList{iSound});
    if ~isequal(h.data.samplingRate, fs)
        error('Different sampling rates in wav files')
    elseif ~isequal(h.data.nChannels, size(y,2)) 
        error('Different number of channels in wav files')
    elseif ~isequal(h.data.nSamples, size(y,1)) 
        error('Different number of samples in wav files')
    end
end

if ltData.useAttributes 
    if ~iscell(ltData.attributes) || ~ischar(ltData.attribQuestion)
        error('Inconsistencies in ltData struct found: useAttributes = true but ltData.attribQuestion or ltData.attributes wrong.')
    end
end

if ~exist(ltData.savePath, 'dir')
    mkdir(ltData.savePath)
end

clear y y1 fs iSound

%%% generate fade windows according to fadeType
switch ltData.fadeType
    case 'inoutFade'
        winLen          = ltData.frameSize * ltData.nFadeFrames;
        hanningWindow   = hanning(winLen);
        zeroVec         = zeros(winLen,1);
        h.data.fadeInWindow  = zeroVec;
        h.data.fadeOutWindow = zeroVec;
        h.data.fadeInWindow( (winLen/2 +1) : end ) = hanningWindow( 1 : winLen/2 );
        h.data.fadeOutWindow( 1 : winLen/2 )       = hanningWindow( (winLen/2 +1) : end );
    case 'xFade'
        winLen          = ltData.frameSize * ltData.nFadeFrames;
        hanningWindow   = hanning(2*winLen);
        h.data.fadeInWindow  = hanningWindow( 1 : winLen );
        h.data.fadeOutWindow = hanningWindow( winLen+1 : end );
    otherwise
        error('Invalid fade type!');
end

%%% get name of person
cl = clock;
h.data.testPersonName = cell2mat(inputdlg( 'Bitte geben Sie Ihren Namen ein.','Name', 1));
if isempty(h.data.testPersonName )
    error('Name is empty')
end
h.data.runIdentifier = sprintf( '%04i%02i%02i_%02i%02i_%s', cl(1), cl(2), cl(3), cl(4), cl(5),genvarname(h.data.testPersonName));
    
%%% initialize log files
if ltData.writeLogFile 
    h.data.fid = fopen(fullfile(ltData.savePath , ['LOG_' h.data.runIdentifier '.txt']), 'wt');
    
    % falls Ausgabe der LOG Datei in der Konsole erfolgen soll
    %   handles.data.fid = 1; fprintf('Dateiname: %s\n\n', handles.data.LogFileName);
    
    header = 'Hrversuch LOG Datei \n';
    header = [header, sprintf( ' Datum:                 %02i.%02i.%04i %02i:%02i Uhr\n', cl(3), cl(2), cl(1), cl(4), cl(5))];
    header = [header, sprintf( ' Versuchsperson:        %s \n',h.data.testPersonName)];
    fprintf( h.data.fid, header );
    fprintf( h.data.fid, '\n\n\n=================================================================\n' );

end

%%% generate GUI with all neccessary uicontrols
h.data.figSize           = [ 600 500 ]; % [width, height] in pixels

h.f = figure('Visible','off','NumberTitle', 'off', 'Position',[1,1, h.data.figSize], 'Name',ltData.figureName,'MenuBar', 'none','CloseRequestFcn', {@CloseRequestFcn});
h.data.backgroundcolor =  get(h.f , 'Color');
movegui(h.f,'center');

% Introduction text and button
h.introButton   = uicontrol('Style','pushbutton', 'String',ltData.introButtonString,'Position',getPos(50,20,50,10,'c',h.data.figSize), 'Callback', {@continueAfterIntro});
h.introText     = uicontrol('Style','text','String',ltData.introText, 'Position', getPos(50,70,80,40,'c',h.data.figSize), 'fontsize', 11); 
h.hIntroArray   = [h.introButton , h.introText];

% Practice Run text and buttons
h.practiceRunText      = uicontrol('Style','text','String',ltData.practiceRunText, 'Position', getPos(50,70,80,40,'c',h.data.figSize), 'fontsize', 11);
h.endPracticeRunButton = uicontrol('Style','pushbutton', 'String','Training beenden und Hrversuch starten','Position',getPos(50,15,70,8,'c',h.data.figSize), 'Callback', {@GUI_next});
h.playAllPracRunButton = uicontrol('Style','pushbutton', 'String','Alle abspielen','Position',getPos(50,42,70,8,'c',h.data.figSize), 'Callback', {@playAllPractice});

startPos = 50 - 10*ltData.nSounds/2 - 5;
for k=1:ltData.nSounds
    h.practiceRunPlayButtons(k)  = uicontrol('Style','toggleButton','String',num2str(k), 'Position', getPos(startPos + 10*k ,30,8,8,'c',h.data.figSize), 'Callback', {@practiceRunButtons});
end
h.hPracticeRunArray = [h.practiceRunText, h.endPracticeRunButton, h.playAllPracRunButton, h.practiceRunPlayButtons];

% Selection text and button
h.cmpText          = uicontrol('Style','text','String',ltData.compareQuestion, 'BackgroundColor', h.data.backgroundcolor, 'FontWeight', 'bold', 'Position', getPos(50,90,80,5,'c',h.data.figSize) );
h.selectionButtonA = uicontrol('Style','togglebutton', 'String','Version A','Position',getPos(30,80,20,8,'c',h.data.figSize), 'Callback', {@chooseSound});
h.selectionButtonB = uicontrol('Style','togglebutton', 'String','Version B','Position',getPos(70,80,20,8,'c',h.data.figSize), 'Callback', {@chooseSound});

% Play options
h.currentPlayText   = uicontrol('Style','text','String','Aktuelle Wiedergabe:', 'BackgroundColor', h.data.backgroundcolor, 'Position', getPos(35,22,20,4,'c',h.data.figSize) );
h.sliderText        = uicontrol('Style','text','String','Start und Ende der Wiedergabe ber Slider ndern', 'BackgroundColor', h.data.backgroundcolor, 'Position', getPos(70,22,50,4,'c',h.data.figSize) );
h.toggleAtEndButton = uicontrol('Style','togglebutton', 'Value', 1, 'String','AutoToggle@End','Position', getPos(15,32.5,20,5,'c',h.data.figSize), 'Callback', {@toggleAtEnd});
h.repeatButton      = uicontrol('Style','togglebutton', 'Value', 1, 'String','Repeat','Position', getPos(15,27.5,20,5,'c',h.data.figSize), 'Callback', {@repeat});
h.curPlayAButton    = uicontrol('Style','togglebutton', 'String','A','Position', getPos(30,30,10,10,'c',h.data.figSize), 'Callback', {@playSelection});
h.curPlayBButton    = uicontrol('Style','togglebutton', 'String','B','Position', getPos(40,30,10,10,'c',h.data.figSize), 'Callback', {@playSelection});
h.bckgrdTextBox     = uicontrol('Style','text', 'String','', 'BackgroundColor', 'w', 'Position', getPos(49.5,28,41,4,'lb',h.data.figSize));
h.progressTextBox   = uicontrol('Style','text', 'String','', 'BackgroundColor', 'r', 'Position', getPos(49.5,28,0.1,4,'lb',h.data.figSize));
h.startSlider       = uicontrol('Style','slider'      , 'Value' , 0 ,'Position', getPos(70,33,45,3,'c',h.data.figSize), 'Callback', {@startSlider});
h.endSlider         = uicontrol('Style','slider'      , 'Value' , 1 ,'Position', getPos(70,27,45,3,'c',h.data.figSize), 'Callback', {@endSlider});

% store selection and continue button
h.nextButton   = uicontrol('Style','pushbutton', 'String','Auswahl speichern und weiter','Position', getPos(50,13,60,8,'c',h.data.figSize), 'Callback', {@GUI_next});

h.hArray = [ h.cmpText h.selectionButtonA  h.selectionButtonB h.currentPlayText h.sliderText h.toggleAtEndButton h.repeatButton h.curPlayAButton h.curPlayBButton h.bckgrdTextBox h.progressTextBox h.startSlider h.endSlider h.nextButton];

% show progress in listening test
if ltData.showProgress
    h.progress  =   uicontrol('Style','text','String',' 0 / 0', 'Position', getPos(50,4,20,4,'c',h.data.figSize));
    h.hArray    = [h.hArray  h.progress];
end

% user attributes checkboxes and text
if ltData.useAttributes 
    h.attribText        = uicontrol('Style','text','String',ltData.attribQuestion, 'BackgroundColor', h.data.backgroundcolor, 'Position', getPos(50,68,80,5,'c',h.data.figSize));
    for k=1:numel(ltData.attributes)
        h.attribCbs(k)  = uicontrol('Style','checkbox','String',ltData.attributes{k}, 'BackgroundColor', h.data.backgroundcolor, 'Position', getPos(50,(70-5*k),70,5,'c',h.data.figSize) );
    end
    h.hArray = [h.hArray h.attribText h.attribCbs ];
end

% global online parameters for playback
global onlinePar
onlinePar.track       = 0;
onlinePar.isPlaying   = 0;
onlinePar.stopIt      = 0;
onlinePar.toggleAtEnd = 0;
onlinePar.repeat      = 0;
onlinePar.startSample = 1;
onlinePar.endSample   = h.data.nSamples;

% define color change for pressed buttons
h.data.defaultbuttonColor =  get(h.selectionButtonA , 'BackgroundColor');
h.data.choosedbuttonColor =  'r';

% Initialize visibility of GUI at start
set(h.hArray, 'Visible', 'off')
set(h.hPracticeRunArray, 'Visible', 'off')
set(h.hIntroArray , 'Visible', 'on')

%%% Initialize Pair Data
allCombinations             = nchoosek(1:ltData.nSounds,2);
nSets                       = size(allCombinations,1);

% generate random order of sets
[del randIDX]               = sort( rand( nSets, 1 ) );  %#ok<ASGLU>
playlist                    = allCombinations(randIDX,:);

% generate random order of signal A and B in each Set
swapAB = logical(round(rand(10,1)));
for k=1:nSets
    if swapAB(k)
        playlist(k,:) = playlist(k,[2,1]);
    end
end

h.data.currentLT.nSets      = nSets;
h.data.currentLT.playlist   = playlist;
h.data.currentLT.currentSet = 0;
h.data.currentLT.prefMat    = zeros(ltData.nSounds); % prefMat(i,j) = 1 => i is prefered over j. prefMat(i,j) = 0 => j is prefered over i.
if ltData.useAttributes
    h.data.currentLT.selectedAttributes   = zeros(ltData.nSounds,ltData.nSounds,numel(ltData.attributes)); % 3D array for selected attributes  
end

%%% Make GUI visible
guidata(h.f, h)
set(h.f,'Visible','on') 

end %function

%% Callback functions
function chooseSound(s,e)%#ok<INUSD>
h = guidata(s);

if get(s,'value') 
    set([h.selectionButtonA h.selectionButtonB] ,'Value', 0)
    set([h.selectionButtonA h.selectionButtonB],'BackgroundColor', h.data.defaultbuttonColor);
    set(s,'Value', 1)
    set(s,'BackgroundColor', h.data.choosedbuttonColor);
else
    set(s,'BackgroundColor', h.data.defaultbuttonColor);   
end

end %function

function continueAfterIntro(s,e)

h = guidata(s);

set(h.hIntroArray , 'Visible', 'off');

if h.ltData.doPracticeRun
       
    set(h.practiceRunPlayButtons ,'Value', 0);
    set(h.practiceRunPlayButtons ,'Enable', 'off');
    set(h.endPracticeRunButton ,'Enable', 'off');
    set(h.playAllPracRunButton, 'Enable', 'on');

    set(h.hPracticeRunArray, 'Visible', 'on');
    guidata(h.f, h);
else
    GUI_next(s,e);
end

end %function

function playAllPractice(s,e)

h = guidata(s);

set(s ,'Enable', 'off');
set(h.practiceRunPlayButtons ,'Value', 0);
set(h.practiceRunPlayButtons ,'Enable', 'off');
set(h.endPracticeRunButton ,'Enable', 'off');

pause(0.5);

for k=1:h.ltData.nSounds
    curSig = wavread(h.ltData.soundList{k});
    set(h.practiceRunPlayButtons(k) ,'Value', 1);
    set(h.practiceRunPlayButtons(k) ,'BackgroundColor', h.data.choosedbuttonColor);
    pause(0.01)
    wavplay(curSig, h.data.samplingRate);
    set(h.practiceRunPlayButtons(k) ,'Value', 0);
    set(h.practiceRunPlayButtons(k) ,'BackgroundColor', h.data.defaultbuttonColor);
    pause( 0.5 );
end

set(h.practiceRunPlayButtons ,'Enable', 'on');
set(h.endPracticeRunButton, 'Enable', 'on');
set(s ,'String', 'Zum nochmaligen Abspielen einzelner Samples auf Buttons klicken');

end %function

function practiceRunButtons(s,e)

h = guidata(s);

if get(s,'value') 
    set(h.practiceRunPlayButtons ,'Value', 0);
    set(h.practiceRunPlayButtons,'BackgroundColor', h.data.defaultbuttonColor);
    set(s,'Value', 1);
    set(s,'BackgroundColor', h.data.choosedbuttonColor);
    set(h.practiceRunPlayButtons ,'Enable', 'off');
    pause(0.01);
else 
    return;
end

for k=1:h.ltData.nSounds
    if s == h.practiceRunPlayButtons(k) 
        curSig = wavread(h.ltData.soundList{k});
        break;
    end
end
wavplay(curSig, h.data.samplingRate);

set(h.practiceRunPlayButtons ,'Enable', 'on');
set(s,'BackgroundColor', h.data.defaultbuttonColor);
set(s,'Value', 0);

end %function

function toggleAtEnd(s,e)%#ok<INUSD>

global onlinePar
onlinePar.toggleAtEnd = get(s,'Value');

end %function

function playSelection(s,e)%#ok<INUSD>

global onlinePar

h = guidata(s);

if ~get(s,'Value') % wenn Track gerade ausgeschaltet wurde => beenden
    onlinePar.track  = 0;
    onlinePar.stopIt = 1;
else
    if s == h.curPlayAButton
        onlinePar.track = 1;
    else % s == h.curPlayBButton
        onlinePar.track = 2;
    end
    
    if ~onlinePar.isPlaying  %gerade wird nichts abgespielt => start wiedergabe
        play(h);
        pause(0.1);
    end
end 

end %function

function repeat(s,e)%#ok<INUSD>

global onlinePar
onlinePar.repeat = get(s,'Value');

end %function

function startSlider(s,e)

global onlinePar

h = guidata(s);
onlinePar.startSample = max( 1 , min( round(get(s,'Value')*h.data.nSamples) , (onlinePar.endSample - h.ltData.frameSize + 1) ) );
set(s,'Value', onlinePar.startSample/h.data.nSamples);

end %function

function endSlider(s,e)

global onlinePar

h = guidata(s);
onlinePar.endSample = max( round(get(s,'Value')*h.data.nSamples) , (onlinePar.startSample + h.ltData.frameSize - 1) );
set(s,'Value', onlinePar.endSample/h.data.nSamples);

end %function

function GUI_next(s,e)%#ok<INUSD>

global onlinePar

h = guidata(s);
set(h.hPracticeRunArray, 'Visible', 'off');
set(h.hArray, 'Visible', 'on');

pause(0.01); % to update global vars

% interrupt playback if still playing
if onlinePar.isPlaying
    onlinePar.isPlaying   = 0;
    onlinePar.stopIt      = 1;
    d = warndlg('Wiedergabe wird zuerst beendet... bitte anschlieend nochmals den WEITER Button bettigen');
    waitfor(d)
    return;
end

%%% Save selection from last set, if this ain't not the first set at all
if (h.data.currentLT.currentSet>0)
    
    idx2button = h.data.currentLT.playlist(h.data.currentLT.currentSet,:);
    
    if get(h.selectionButtonA, 'value')==1 && get(h.selectionButtonB, 'value')==0
        winner = 'A';
    elseif get(h.selectionButtonA, 'value')==0 && get(h.selectionButtonB, 'value')==1
        winner = 'B';
    elseif get(h.selectionButtonA, 'value')==0 && get(h.selectionButtonB, 'value')==0
        d = warndlg('Bitte eine Version auswhlen.');
        waitfor(d)
        return
    elseif get(h.selectionButtonA, 'value')==1 && get(h.selectionButtonB, 'value')==1
        error('You selected A and B, how is this even possible??? There appears to be some error in the gui processing.');
    end
    
    if ~any(cell2mat(get(h.attribCbs,'Value')))
        d = warndlg('Bitte mindestens ein Diskriminierungsmerkmal auswhlen.');
        waitfor(d)
        return
    end        
    
    switch winner
        case 'A'
            idxWinner = idx2button(1) ;
            idxLooser = idx2button(2);
        case 'B'
            idxWinner = idx2button(2) ;
            idxLooser = idx2button(1);
    end
    
    h.data.currentLT.prefMat(idxWinner, idxLooser) = 1; % prefMat(i,j) = 1 => i is prefered over j ; prefMat(i,j) = 0 => j is prefered over i
    h.data.currentLT.prefMat(idxLooser, idxWinner) = 0;
    if h.ltData.useAttributes
        attributeStringList= [];
        for k=1:numel(h.ltData.attributes)
            pairAttribVec(k) = get(h.attribCbs(k), 'Value');
            if pairAttribVec(k)
                attributeStringList = [attributeStringList, '  ', h.ltData.attributes{k}];
            end
        end
        h.data.currentLT.selectedAttributes(idx2button(1),idx2button(2),:)  = pairAttribVec; % selectedAttributes(i,j,k) = 1 ; attrib number k is important for discrimination of pair (i,j)
        h.data.currentLT.selectedAttributes(idx2button(2),idx2button(1),:)  = pairAttribVec; % selectedAttributes(i,j,k) = 1 ; attrib number k is important for discrimination of pair (i,j)
    end
    if h.ltData.writeLogFile
        if h.ltData.useAttributes
            fprintf(h.data.fid, '  Auswahl: %s\n Unterschiedsmerkmale fr Paar AB:%s\n', winner, attributeStringList);
        else
            fprintf(h.data.fid, '  Auswahl: %s\n', winner);
        end
    end

end

%%% Go to next set, if if previous set was not the last one
if (h.data.currentLT.currentSet<h.data.currentLT.nSets) 
    
    h.data.currentLT.currentSet = h.data.currentLT.currentSet + 1;
    guidata(h.f, h);
    set(h.progress, 'string', sprintf('Set %i von %i', h.data.currentLT.currentSet, h.data.currentLT.nSets ))
    
    % reset GUI
    set([h.selectionButtonA h.selectionButtonB h.toggleAtEndButton h.repeatButton h.curPlayAButton h.curPlayBButton ], 'value', 0);
    set([h.selectionButtonA h.selectionButtonB h.curPlayAButton h.curPlayBButton],'BackgroundColor', h.data.defaultbuttonColor);
    set(h.startSlider, 'value', 0);
    set(h.endSlider, 'value', 1);
    set(h.progressTextBox, 'Position', getPos(49.5,28,0.1,4,'lb',h.data.figSize));
    
    if h.ltData.useAttributes
        set(h.attribCbs , 'Value', 0);
    end
    set(h.hArray, 'Visible', 'on', 'Enable', 'off');
    
    onlinePar.toggleAtEnd = 0;
    onlinePar.repeat      = 0;
    pause(1);
    
    % Play complete signal for A and B with initial number of repetitions
    buttonHandles = [h.curPlayAButton, h.curPlayBButton];
    
    fileNumbers      = h.data.currentLT.playlist(h.data.currentLT.currentSet,:);
    if h.ltData.writeLogFile
        fprintf(h.data.fid, '%i. Set\n  Sound A: %s\n  Sound B: %s\n', h.data.currentLT.currentSet,h.ltData.soundList{fileNumbers(1)}, h.ltData.soundList{fileNumbers(2)} );
    end
    
    for k=1:h.ltData.ABrepetitions
        set(buttonHandles(1), 'Value', 1);
        playSelection(buttonHandles(1),e);
        set(buttonHandles(1), 'Value', 0);
        pause(h.ltData.pauseBetween);
        set(buttonHandles(2), 'Value', 1);
        playSelection(buttonHandles(2),e);
        set(buttonHandles(2), 'Value', 0);
    end
    
    set(h.hArray, 'Enable', 'on');
    
else %%% We arrived at the end of the listening test
    set(h.hArray, 'Visible', 'off');
    set(h.introText,'String', h.ltData.endText);
    set(h.introButton, 'String', 'Fertig', 'Callback', {@CloseRequestFcn});
    set(h.hIntroArray , 'Visible', 'on');
    
    % save as mat files
    if h.ltData.useAttributes
        ltResult = struct('testPersonName', h.data.testPersonName, 'preferenceMatrix' , h.data.currentLT.prefMat, 'selectedAttributes', h.data.currentLT.selectedAttributes, 'attributeList', {h.ltData.attributes}, 'listeningTestData', h.ltData); %#ok<NASGU>
    else
        ltResult = struct('testPersonName', h.data.testPersonName, 'preferenceMatrix' , h.data.currentLT.prefMat, 'listeningTestData', h.ltData); %#ok<NASGU>
    end
    save(fullfile(h.ltData.savePath, ['result_'  h.data.runIdentifier '.mat',]), 'ltResult')
    
    
    if h.ltData.writeLogFile
        fprintf(h.data.fid, '\n\nTest completed successfully.');
        fclose(h.data.fid);
    end
end

guidata(h.f, h)

end %function

function CloseRequestFcn(s,e) %#ok<INUSD>
h = guidata(s);
if h.data.currentLT.currentSet == h.data.currentLT.nSets    % test finished
    delete(h.f)
else
    btn = questdlg('Wollen Sie wirklich den Test beenden?', 'Test abbrechen', 'Ja', 'Nein', 'Nein');
    
    if strcmp(btn, 'Ja')
        delete(h.f)
        if h.ltData.writeLogFile
            fprintf(h.data.fid, '\n\nCanceled by user. \n\n');
            fclose(h.data.fid);
        end
        
    end
end

end %function

%% Auxiliary Functions
%%% Positioning function for GUI Elements
function pos = getPos(relAnchorHor,relAnchorVert,relWidth,relHeight,anchorType,figSize)

width = relWidth/100 * figSize(1);
height = relHeight/100 * figSize(2);

switch anchorType
    case 'lb'
        left   = relAnchorHor/100  * figSize(1);
        bottom = relAnchorVert/100 * figSize(2);
    case 'c'
        left   = (relAnchorHor/100 - 0.5 * relWidth/100) * figSize(1);
        bottom = (relAnchorVert/100 - 0.5 * relHeight/100) * figSize(2);
end

pos = [left bottom width height];

end %function

%%% playback function
function play(h)

global onlinePar

% Init sound device
pageBufCount     = 2;
runMaxSpeed      = true;
Fs               = ita_preferences('samplingRate');
if playrec('isInitialised')
    playrec('reset');
end
playrec('init',Fs, ita_preferences('playDeviceID'), ita_preferences('recDeviceID'));
playrec('delPage');
pageNumList      = repmat(-1, [1 pageBufCount]);
firstTimeThrough = true;

% constant parameters
buttonHandles     = [h.curPlayAButton h.curPlayBButton];
fileNumbers       = h.data.currentLT.playlist(h.data.currentLT.currentSet,:);
nSamples          = h.data.nSamples;
frameSize         = h.ltData.frameSize;
nFadeFrames       = h.ltData.nFadeFrames;
fadeInWindow      = h.data.fadeInWindow;
fadeOutWindow     = h.data.fadeOutWindow;
playBackAmpFactor = h.ltData.playBackAmpFactor;
soundList         = h.ltData.soundList;

% Parameters for while loop
lastTrack = onlinePar.track;
lastFrame = 0;
curEndSample = 0;
onlinePar.isPlaying =1;
onlinePar.startSample = max(round(get(h.startSlider,'Value')*nSamples),1);
onlinePar.endSample = round(get(h.endSlider,'Value')*h.data.nSamples);
fadeFrame = 0;

% Hauptschleife
while ishandle(h.f)
    
    pause(0.00001)       % doof aber notwendig, damit globale var aktualisiert wird
    
    if onlinePar.stopIt % Wiedergabe beenden
        set(buttonHandles, 'Value', 0);     % alle "aus"
        set(buttonHandles,'BackgroundColor', h.data.defaultbuttonColor);
        set(h.progressTextBox, 'Position', getPos(49.5,28,0.1,4,'lb',h.data.figSize));
        break
    end
        
    % check if the audioSignal was changed in another function during playback
    if (onlinePar.track ~= lastTrack)
        fadeFrame = 1;
    end
        
    % get current sample position in track (account for possible changes induced via the start and end sliders)
    curStartSample   = curEndSample+1;
    curEndSample     = curStartSample+frameSize-1;
    
    playBackStartSample = onlinePar.startSample;
    playBackEndSample   = onlinePar.endSample;

    if curStartSample >= playBackEndSample
        curStartSample = playBackStartSample;
        curEndSample   = curStartSample+frameSize-1;
    end
    if curStartSample < playBackStartSample
        curStartSample = playBackStartSample;
        curEndSample   = curStartSample+frameSize-1;
    end
    if (curStartSample+frameSize-1) >= playBackEndSample
        % play rest of sound sample
        if (curStartSample+frameSize-1) <= nSamples;
            curEndSample = curStartSample + frameSize - 1;
        else
            curEndSample = nSamples;
        end
        lastFrame = 1;
    end
    
    Sig{1} = playBackAmpFactor * wavread(soundList{fileNumbers(1)}, [curStartSample curEndSample]);
    Sig{2} = playBackAmpFactor * wavread(soundList{fileNumbers(2)}, [curStartSample curEndSample]);
    
    if (curStartSample==playBackStartSample)  % never fade at beginning of signal
        fadeFrame = 0;
    end
    if fadeFrame
        startSample = (fadeFrame-1)*frameSize+1;
        winLen = (curEndSample-curStartSample)+1;
        fadeIn  = fadeInWindow ( startSample : startSample+winLen-1 );
        fadeOut = fadeOutWindow( startSample : startSample+winLen-1 );
        
        curSig = ( Sig{onlinePar.track} .* [fadeIn, fadeIn] ) + ( Sig{3-onlinePar.track} .* [fadeOut, fadeOut] );    
    else
        curSig = Sig{onlinePar.track};
    end
    
    set(h.progressTextBox, 'Position', getPos(49.5+(playBackStartSample/nSamples*45),28,((curStartSample-playBackStartSample)/nSamples*41)+eps,4,'lb',h.data.figSize));
    set(buttonHandles(onlinePar.track), 'Value', 1);
    set(buttonHandles(onlinePar.track),'BackgroundColor', h.data.choosedbuttonColor);
    
    % Play !!!
    pageNumList = [pageNumList playrec('play',curSig,[1 2])]; %#ok<AGROW>
    
    set(h.progressTextBox, 'Position', getPos(49.5+(playBackStartSample/nSamples*45),28,((curEndSample-playBackStartSample)/nSamples*41)+eps,4,'lb',h.data.figSize));
    pause(0.00001);

    % nicht gesendete Samples anzeigen
    if(firstTimeThrough)
        playrec('resetSkippedSampleCount');
        firstTimeThrough = false;
    else
        if(playrec('getSkippedSampleCount'))
            fprintf('%d samples skipped!!\n', playrec('getSkippedSampleCount'));
            firstTimeThrough = true;
        end
    end
    
    % Puffer
    if pageNumList(1) ~= -1
        if(runMaxSpeed)
            while(playrec('isFinished', pageNumList(1)) == 0)
            end
        else
            playrec('block', pageNumList(1));
        end
    end
    playrec('delPage', pageNumList(1));
    pageNumList = pageNumList(2:end);

    %%% merken, welcher Track gerade abgespielt wurde, und aktualisieren wo wir im Fade sind
    lastTrack = onlinePar.track;
    if fadeFrame
        fadeFrame = mod(fadeFrame+1,nFadeFrames+1);
    end
    
    if lastFrame
        set(buttonHandles, 'Value', 0)     % alle "aus" und nur den gerade gedrckten Button "an"
        set(buttonHandles,'BackgroundColor', h.data.defaultbuttonColor);
        set(h.progressTextBox, 'Position', getPos(49.5,28,0.1,4,'lb',h.data.figSize));
        if ~onlinePar.repeat    
            break
        end
        if onlinePar.toggleAtEnd
            onlinePar.track = (3 - onlinePar.track); % toggle track
        end
        lastFrame = 0;
        curEndSample = onlinePar.startSample-1;
        fadeFrame = 0;
    end

end

onlinePar.isPlaying     = 0;
onlinePar.stopIt        = 0;

end %function