% WIP: convert MAT output to XML file for compatibility with Web Audio Evaluation Tool

% CONFIGURATION: 
location = 'CIRMMT Critical Listening Lab'; 

% IMPORT FILE
matfile = load('george.mat');
name = fieldnames(matfile);
test = matfile.(char(name));

docNode = com.mathworks.xml.XMLUtils.createDocument('browserevaluationresult');
docRootNode = docNode.getDocumentElement;

% PRETEST
pretest = docNode.createElement('pretest');
docRootNode.appendChild(pretest);
    sessionID_comment = docNode.createElement('comment');
    pretest.appendChild(sessionID_comment);
    sessionID_comment.setAttribute('id', 'SessionID');
    sessionID_comment.appendChild(docNode.createTextNode(test.id));

    location_comment = docNode.createElement('comment');
    pretest.appendChild(location_comment);
    location_comment.setAttribute('id', 'location');
    location_comment.appendChild(docNode.createTextNode(location));

    
% AUDIOHOLDER
% for each song in the test:
for song = 1:length(test.tstDat)/2 % 2 structs for every song
    song_id = song*2; % every second field corresponds with a song
    % create audioholder
    audioholder = docNode.createElement('audioholder');
    docRootNode.appendChild(audioholder);
    % save song name
    songname = test.tstDat{1,song_id-1}.tstFile(2:end-4); % chop off '_*.txt'
    audioholder.setAttribute('id', songname);
    % add pretest (empty)
    pretest = docNode.createElement('pretest');
    audioholder.appendChild(pretest);
    % add posttest (empty)
    posttest = docNode.createElement('posttest');
    audioholder.appendChild(posttest);
    % add metric
    metric = docNode.createElement('metric');
    audioholder.appendChild(metric);
    metricresult = docNode.createElement('metricresult');
        metric.appendChild(metricresult);
        metricresult.setAttribute('id', 'testTime');
        testduration = num2str(test.tstDat{1,song_id}.rsl.duration);
        metricresult.appendChild(docNode.createTextNode(testduration));
    
    % go over audio files in the order they were presented
    permVec = test.tstDat{1,song_id}.rsl.permVec;
    for index=1:length(permVec) % ('index' is presented number)
        % create audioelement and attach to audioholder
        audioelement = docNode.createElement('audioelement');
        audioholder.appendChild(audioelement);
        % save mix name
        mixname = test.tstDat{1,song_id-1}.rsl.sounds(permVec(index)).name;
        audioelement.setAttribute('id', strcat('McG-', mixname));
        % add comment
        comment = docNode.createElement('comment');
        audioelement.appendChild(comment);
            % add comment question and response
            question = docNode.createElement('question');
            comment.appendChild(question);
            str = sprintf('Comment on track %i', index);
            question.appendChild(docNode.createTextNode(str));
            response = docNode.createElement('response');
            comment.appendChild(response);
            str = char(test.tstDat{1,song_id}.rsl.hcom(permVec(index)));
            response.appendChild(docNode.createTextNode(str));
        % add rating
        value = docNode.createElement('value');
        audioelement.appendChild(value);
        rating = test.tstDat{1,song_id}.rsl.mat(permVec(index));
        value.appendChild(docNode.createTextNode(num2str(rating)));
        % add metric
        % FROM HERE
    end
end
    
xmlFileName = ['test.xml'];
xmlwrite(xmlFileName,docNode);
type(xmlFileName);


% final script features: 
% read in all .mat files from folder
% make replacements (Fei -> McG-B etc.)
