%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The goal of onset detection is to detect the precise moment in time, a
% note is hit. We derive a so called novelty curve, the peaks of which
% indicate likely note onset candidates.
% We assume that something is changing at onset positions and thus, want to
% derive transient signal components from the spectrogram, while
% suppressing steady harmonic parts.
%
% alpha: peak values : the higher the better
% beta: non-peak values: the smaller the better
% gamma: alpha/beta -> the higher the better
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
close all

addpath([pwd '/Toolbox/'])

parameter.DirWAV = 'onsetTestfiles/';
parameter.DirAnnotation = 'onsetAnnotations/';

Testfiles = {
    'BM02A_meinard_se.wav';
    'Beethoven_5_bernstein_0_22.wav';
    'Brahms_Op39-05_0-15.wav';
    'HappyBirthday_V1_Chor.wav';
    'OGL25009_22050_mono.wav';
    'RM-C003_Beethoven_0-22.wav';
    'RWC-MDB-C-2001-M03_tr04_Borodin_Sec1-39.wav';
    'Shostakovich_JazzSuite2_35-65.wav';
    'cello1.wav';
    'clarinet1.wav';
    'classic2.wav';
    'classic3.wav';
    'distguit1.wav';
    'guitar2.wav';
    'guitar3.wav';
    'jazz2.wav';
    'jazz3.wav';
    'piano1.wav';
    'pop1.wav';
    'rock1.wav';
    'sax1.wav';
    'trumpet1.wav';
    'violin2.wav';
    };


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameter.visualize = 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameter (fixed)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameter.win_len = 2048;
parameter.hop_size = 1024;
parameter.F = 0;
parameter.F_max = 8000;
parameter.logCompression = 1000;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




for n = 2:2;%23;
    
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % compute spectrogram
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    [X,F,T,parameter] = filename_to_spectrogram( Testfiles{n},parameter);
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % do something fancy
    % here we want to derive vertical structures in the spectrogram,
    % indicating transient signal parts
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    currentFile = Testfiles{n}(1:end-4);
    anumber = 0;
    params = '';
    
    anumber = anumber + 1;
    printResult(strcat('onsetResults/',currentFile,'-',num2str(anumber)));
    
    argsc = [1 0.5 5 0.001 0.25 1000 20 0];
    argse = [1 0.5 0.25 10 1.5 0];
    argsdil = [0.707 5];
    argsero = [0.707 10];
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 1
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 1
        argse = [1 0.5 0.25 5000 1 0];
        [tempdiff,params] = eed(X,argse,zeros(size(X)));
        temp = im2bw(tempdiff, graythresh(tempdiff));
        params = [params, '\n\n', 'im2bw(result_eed, graythresh(result_eed))'];
        Y = bwmorph(temp,'clean',Inf);
        params = [params, '\n\n', 'bwmorph(result_im2bw, ''clean'', Inf)'];
    end

    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 2
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 2
%         argsc = [1 0.5 5 0.001 0.25 10000 1 0];
%         [Y,params] = ced(X,argsc);
        argse = [1 0.5 0.25 20000 1 0];
        [tempdiff,params] = eed(X,argse,zeros(size(X)));
%         temp = im2bw(tempdiff, graythresh(tempdiff));
%         temp = bwmorph(temp,'clean',Inf);
        Y = tempdiff;
    end

    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 3
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 3
        argsc = [1 0.5 5 0.001 0.25 1000 20 0];
        [Y,params] = ced(X,argsc);
    end

    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 4
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 4
        argsc = [1 0.5 5 0.001 0.25 10000 20 0];
        [tempdiff,params] = ced(X,argsc);
        bwthreshold = 0.41;
        params = [params, '\n\n', 'bwthreshold: ', num2str(bwthreshold)];
        temp = im2bw(tempdiff,bwthreshold);
        params = [params, '\n', 'im2bw(result_ced, bwthreshold)'];
        Y = bwmorph(temp,'clean',Inf);
        params = [params, '\n\n', 'bwmorph(result_im2bw, ''clean'', Inf)'];
    end

    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 5
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 5
        Y = X;
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % parameters for example 7
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if n == 7
        argsc = [1 0.5 5 0.001 0.25 1000 20 0];
        [tempdiff,params] = ced(X,argsc);
        tempedge = double(edge(tempdiff,'sobel',[],'vertical'));
        params = [params, '\n\n', 'double(edge(result_ced, ''sobel'', [], ''vertical''))'];
        Y = bwmorph(tempedge,'skel',Inf);
        params = [params, '\n\n', 'bwmorph(result_edge, ''skel'', Inf)'];
    end
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % show what happend
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    if parameter.visualize
        figure; imagesc(T, F, Y); colormap(hot); colorbar; axis xy;
        title('Modified Spectrogram'); xlabel('Time (sec)'); ylabel('F')
    end
    
    anumber = anumber + 1;
    printResult(strcat('onsetResults/',currentFile,'-',num2str(anumber)));
    
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % compute and evaluate novelty Curve
    % we basically compute the first order difference, discard negative (decreasing energy)
    % values and sum up all coefficients in a frame to obtain a single
    % 1-dimensional function
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    [alpha, beta, gamma, P, R, F] = evaluateNoveltyCurve(X, Testfiles{n}, parameter);
    params = [params, '\n\n', 'Modified file\n', 'alpha: ', num2str(alpha), ' beta: ', num2str(beta), ' gamma: ', num2str(gamma), ' P: ', num2str(P), ' R: ', num2str(R), ' F: ', num2str(F)];

    fprintf('%-30s| ',Testfiles{n}(1:min(length(Testfiles{n}),25)));
    [alpha, beta, gamma, P, R, F] = evaluateNoveltyCurve(Y, Testfiles{n}, parameter);    
    
    params = [params, '\n\n', 'Modified file\n', 'alpha: ', num2str(alpha), ' beta: ', num2str(beta), ' gamma: ', num2str(gamma), ' P: ', num2str(P), ' R: ', num2str(R), ' F: ', num2str(F)];
    
    anumber = anumber + 1;
    printResult(strcat('onsetResults/',currentFile,'-',num2str(anumber)));
    
    writeParameters(strcat('onsetResults/',currentFile,'.txt'),params);
    
    
end



