clear all;
close all hidden;

addpath([pwd '/featureExtractor']);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File handling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
directoryGlobal = 'I:\__BeatlesData\';
directoryWAV = strcat(directoryGlobal,'\wav_44100_stereo\');
directorySTMSP = strcat(directoryGlobal,'data_features-STMSP\');
directoryLab = strcat(directoryGlobal,'chord_transcriptions_1-1\');
directoryPar = strcat(directoryGlobal,'chord_parameters\');

files=dir(strcat(directoryWAV,'*.wav'));
[names,ind] = sort(lower({files.name}));
files = files(ind);
filenames = {files.name}';

%selectedFiles = [1:180];
selectedFiles = [99 141 166 154 11 24 179 38];
filenames = filenames(selectedFiles);
numFiles = size(filenames,1);

%filenames = {'Beatles_ADayInTheLife_Beatles_1967-SgtPeppersLonelyHeartsClubBand-13.wav'};
%filenames = {'Beatles_AHardDaysNight_Beatles_1964-AHardDaysNight-01.wav'};
%filenames = {'Beatles_ATasteOfHoney_Beatles_1963-PleasePleaseMe-12'};

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File handling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f_measure_all = zeros(numFiles,1);
f_measure_all_noTuning = zeros(numFiles,1);

fprintf('-----------------------------------------------------------------------------\n');
for n=1:numFiles
    filename = filenames{n};
    parfile = strcat(directoryPar,filename(1:end-3),'par');
    parameter.max = 1;
    [q,shiftFB,circShift,f_measure_old] = readParameterSortedList(parfile,parameter);
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Tuning 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    parameter = [];
    parameter.circShift = circShift;
    parameter.shiftFB = shiftFB;       
    parameter.stat_window_length_array = 11;
    parameter.dirname = directorySTMSP;
    %parameter.strategy = 1;
    %parameter.chordThresholdDistance = 0.2;
    %parameter.templateMajor = [1 0 0 0 1 0 0 1 0 0 0 0];
    %parameter.templateMinor = [1 0 0 1 0 0 0 1 0 0 0 0];
    [chordMatrix_computed,f_feature] = compute_chordAnnotation(filename,parameter);
    
    %visualize_chordMatrix(chordMatrix_computed);
    %set(gcf,'Position',[1605 718 1592 420]);
    
    labfile = strcat(directoryLab,filename(1:end-3),'lab');   
    segmentation = readSegmentationAnnotation(labfile);
    
    labels = {segmentation.label}';
    parameter.chordClass = 0;
    labels_out = discard_chord_annotations(labels,parameter);
    discard = strcmp(labels_out,'N');
    segmentation = segmentation(~discard);
    
    labels = {segmentation.label}';
    labels_simplified = simplify_chord_annotations(labels);
    
    [labels_indices,chordLabelsVis] = chordLabel_to_chordIndex(labels_simplified);
    
    numLabels = size(labels,1);
    for k=1: numLabels
        segmentation(k).label = labels_indices(k);
    end
    
    parameter.numSamples = size(chordMatrix_computed,2);
    parameter.numChords = 24;
    parameter.featureRate = 10;
    
    chordMatrix_groundTruth = segmentation_to_matrix(segmentation,parameter);
    %visualize_chordMatrix(chordMatrix_computed,chordMatrix_groundTruth);
    %set(gcf,'Position',[1605 718 1592 420]);
    %chordMatrix_groundTruth = pickMiddleEntryInRun(chordMatrix_groundTruth);
    
    paramter = [];
    parameter.fprintf = 0;
    parameter.neglectNotAnnotated = 1;
    parameter.visualize = 0;
    parameter.position = [1605 718 1592 420];
    [precision,recall,f_measure] = evaluate_matrix1_matrix2(chordMatrix_computed,chordMatrix_groundTruth,parameter);    
    f_measure_all(n) = f_measure;
    semitoneFrac = 0;
    switch shiftFB
        case 0
            semitoneFrac = 0;
        case 1
            semitoneFrac = 0.25;
        case 2
            semitoneFrac = 0.33;
        case 3
            semitoneFrac = 0.50;
        case 4
            semitoneFrac = 0.67;
        case 5
            semitoneFrac = 0.75;
    end
    semitoneShift = circShift + semitoneFrac;
    if semitoneShift > 6
        semitoneShift = semitoneShift - 12;
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % No Tuning 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    parameter.circShift = 0;
    parameter.shiftFB = 0;       
    [chordMatrix_computed,f_feature] = compute_chordAnnotation(filename,parameter);
    [precision,recall,f_measure] = evaluate_matrix1_matrix2(chordMatrix_computed,chordMatrix_groundTruth,parameter);
    f_measure_all_noTuning(n) = f_measure;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Output
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    %fprintf('%4i %40s %3i %3i %3i %6.3f  %6.3f %6.3f %6.3f\n', n, filename(1:min(length(filename),40)),q,shiftFB,circShift,f_measure_old,precision,recall,f_measure_all(n)); 
    if semitoneShift ~= 0
        semitoneShift = -semitoneShift;
    end
    fprintf('%4i %-50s %6.2f %6.3f %6.3f\n', selectedFiles(n), filename(1:min(length(filename),50)),semitoneShift,f_measure_all_noTuning(n),f_measure_all(n)); 
end

f_measure_average = sum(f_measure_all)/numFiles;
f_measure_average_noTuning = sum(f_measure_all_noTuning)/numFiles;
fprintf('     %-50s        %6.3f %6.3f\n', 'Average',f_measure_average_noTuning,f_measure_average);



% %%
% chord = 2;
% 
% pos = chordMatrix_groundTruth(chord,:);
% chroma_chord = f_feature(:,find(pos==1));
% figure; imagesc(chroma_chord);
% chroma_chord_average = sum(chroma_chord,2)/size(chroma_chord,2);
% chroma_chord_average = chroma_chord_average/norm(chroma_chord_average,2);
