
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Harmonic analysis: Template based chord recognition
%
%
% Precision:
% Recall:
% Fmeasure:
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


clear
close all

addpath([pwd '/Toolbox'])

parameter.dirFeatures  = 'features/';
parameter.directoryLab = 'chordAnnotations/';


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fileList = {
    'chordTestfiles/','Beatles_AllMyLoving_Beatles_1963-WithTheBeatles-03_0-25.wav';
    'chordTestfiles/','Beatles_AllYouNeedIsLove_Beatles_1967-MagicalMysteryTour-11_163-193.wav';
    'chordTestfiles/','Beatles_AndILoveHer_Beatles_1964-AHardDaysNight-05_0-30.wav';
    'chordTestfiles/','Beatles_BabysInBlack_Beatles_1964-BeatlesForSale-03_0-37.wav';
    'chordTestfiles/','Beatles_BackInTheUSSR_Beatles_1968-TheBeatlesTheWhiteAlbumDisc1-01_10-40.wav';
    'chordTestfiles/','Beatles_CryBabyCry_Beatles_1968-TheBeatlesTheWhiteAlbumDisc2-11_30-60.wav';
    'chordTestfiles/','Beatles_HelterSkelter_Beatles_1968-TheBeatlesTheWhiteAlbumDisc2-06_0-30.wav';
    'chordTestfiles/','Beatles_IAmTheWalrus_Beatles_1967-MagicalMysteryTour-06_0-30.wav';
    'chordTestfiles/','Beatles_ImOnlySleeping_Beatles_1966-Revolver-03_0-35.wav';
    'chordTestfiles/','Beatles_LetItBe_Beatles_1970-LetItBe-06_120-146.wav';
    'chordTestfiles/','Beatles_SavoyTruffle_Beatles_1968-TheBeatlesTheWhiteAlbumDisc2-10_0-30.wav';
    'chordTestfiles/','Beatles_TellMeWhy_Beatles_1964-AHardDaysNight-06_0-30.wav';
    'chordTestfiles/','Beatles_YouCantDoThat_Beatles_1964-AHardDaysNight-12_0-30.wav';
    };




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameter.visualize = 1;
parameter.ChordRec = 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameter.featureType
%   1 linear f spectrogram
%   2 log f spectrogram
%   3 pitch representation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameter.featureType = 3;
parameter.logCompression = 10;



for n = 4:4;%13;
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % load Spectrogram
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    [X,F,T,parameter] = load_spectrogram(fileList{n,2},parameter);
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % do smoething fancy
    % here, the goal is enhance horizontal structures in the spectrogram,
    % caused by harmonic parts of the signal
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    currentFile = fileList{n,2}(1:end-4);
    anumber = 0;
    params = '';
    
    anumber = anumber + 1;
    printResult(strcat('chordResults/',currentFile,'-',num2str(anumber)));
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 1
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 1
        SE = strel('line',15,0);
        Y = imopen(X,SE);
        params = 'Method: Opening\n\n';
        params = [params, 'Structuring element: line, length 15, 0 degrees'];
%         argsc = [1 0.5 5 0.001 0.25 90 0 20];
%         [Y,params] = ced(Y,argsc);
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 2
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 2
%         argsc = [1 0.5 5 0.001 0.25 300 0 20];
%         [Y,params] = ced(X,argsc);
        argse = [1 0.5 0.25 300 0 1];
        [Y,params] = eed(X,argse,zeros(size(X)));
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 3
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 3
%         argsc = [1 0.5 5 0.001 0.25 200 0 20];
%         [Y,params] = ced(X,argsc);
        argse = [1 0.5 0.25 80 0 1];
        [Y,params] = eed(X,argse,zeros(size(X)));
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 4
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 4
%         argsc = [1 0.5 5 0.001 0.25 190 0 20];
%         [Y,params] = ced(X,argsc);
        argse = [1 0.5 0.25 100 0 1];
        [Y,params] = eed(X,argse,zeros(size(X)));
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 5
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 5
        argsc = [1 0.5 5 0.001 0.25 100 0 20];
        [Y,params] = ced(X,argsc);
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 6
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 6
        argsc = [1 0.5 5 0.001 0.25 160 0 20];
        [Y,params] = ced(X,argsc);
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 7
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 7
        [Y,params] = NLmeans(X,[100 1],[2 1],1,'n');
    end
    
    if parameter.visualize
        figure;imagesc(T, F, Y); colormap(hot); colorbar; axis xy; title('Modified Spectrogram'); xlabel('Time (sec)'); ylabel('F');
        if parameter.logF; set(gca,'YScale','log'); end
    end
    
    anumber = anumber + 1;
    printResult(strcat('chordResults/',currentFile,'-',num2str(anumber)));
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % evaluate
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    [f_chroma] = TFR_to_chroma(X,F,parameter);
    if parameter.visualize
        visualize_chroma(f_chroma,parameter);
        title('CHROMA');
    end
    anumber = anumber + 1;
    printResult(strcat('chordResults/',currentFile,'-',num2str(anumber)));
    
    [precision,recall,f_measure] = evaluateChordAnnotation(X,F,T, fileList{n,2}, parameter);
    anumber = anumber + 1;
    printResult(strcat('chordResults/',currentFile,'-',num2str(anumber)));
    
    params = [params, '\n\n', 'Original file\n', 'P: ', num2str(precision), ' R: ', num2str(recall), ' F: ', num2str(f_measure)];

    [f_chroma] = TFR_to_chroma(Y,F,parameter);
    if parameter.visualize
        visualize_chroma(f_chroma,parameter);
        title('CHROMA');
    end
    anumber = anumber + 1;
    printResult(strcat('chordResults/',currentFile,'-',num2str(anumber)));
    
    [precision,recall,f_measure] = evaluateChordAnnotation(Y,F,T, fileList{n,2}, parameter);
    anumber = anumber + 1;
    printResult(strcat('chordResults/',currentFile,'-',num2str(anumber)));
    
    params = [params, '\n\n', 'Modified file\n', 'P: ', num2str(precision), ' R: ', num2str(recall), ' F: ', num2str(f_measure)];
    
    writeParameters(strcat('chordResults/',currentFile,'.txt'),params);
    
end
