
%% 

% filename = '../testfiles/Beethoven_5_bernstein_0_22.wav';


%% read wav file

% [x,Fs] = wavread(filename);
% 
% % stereo2mono
% if size(x,2)>1
%     x = mean(x,2);
% end
% 
% if Fs ~= 22050
%     x = resample(x,Fs,22050);
% end
% 
% 
% t = (0:length(x)-1)./Fs;
% %figure; plot(t,x); xlim([0 t(end)])
% 
% %% compute spectrogram
% 
% win_len = 1024;
% hopsize = 512;
% nFFT = 1024;
% 
% 
% [X,F,T] = spectrogram(x,hann(win_len),win_len-hopsize,nFFT,Fs);
% 
% % magnitude! no phase X = angle(X);
% X = abs(X);
% 
% %figure; imagesc(T,F,log(1+X)); axis xy; colorbar; colormap(hot)
% 
% 
% %tic
% %Y = nlmeans(X);
% %toc

clear
close all

addpath([pwd '/../evaluationStrategies']);
addpath([pwd '/../evaluationStrategies/Toolbox']);

parameterC.dirFeatures  = '../evaluationStrategies/features/';
parameterC.directoryLab = '../evaluationStrategies/chordAnnotations/';


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

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




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

parameterC.visualize = 1;
parameterC.ChordRec = 1;

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

parameterC.featureType = 3;
parameterC.logCompression = 10;

parameterO.DirWAV = '../evaluationStrategies/onsetTestfiles/';
parameterO.DirAnnotation = '../evaluationStrategies/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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameterO.visualize = 1;

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

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

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


n = 1;

% for n=1:13
% 
% close all

% [X,F,T,parameterC] = load_spectrogram(fileList{n,2},parameterC); %% chord recognition

% for n=1:23
% 
% close all

[X,F,T,parameterO] = filename_to_spectrogram( Testfiles{n},parameterO); %% onset detection

% temp = eed(X,[1 0.5 0.25 20 1 0],zeros(size(X)));
% Y = skeleton(temp,[0.1 0.1],0.01);
% Y = NLmeansFast(X,[5 5],[1 5],1,'n',0.5,1.5);
Y = NLmeans(X,[10 10],[1 10],1,'n');

figure; imagesc(T,F,Y); axis xy; colorbar; colormap(hot); title('Modified Spectrogram'); xlabel('Time (sec)'); ylabel('F');
% if parameterC.logF
%     set(gca,'YScale','log');
% end


%%
% evaluate Chord Recognition
%%
% [f_chroma] = TFR_to_chroma(X,F,parameterC);
% if parameterC.visualize
%     visualize_chroma(f_chroma,parameterC);
%     title('CHROMA');
% end
% [f_chroma] = TFR_to_chroma(Y,F,parameterC);
% if parameterC.visualize
%     visualize_chroma(f_chroma,parameterC);
%     title('CHROMA');
% end
% evaluateChordAnnotation(X,F,T, fileList{n,2}, parameterC);
% [precision,recall,f_measure] = evaluateChordAnnotation(Y,F,T, fileList{n,2}, parameterC);

%%
% evaluate Onset Detection
%%

evaluateNoveltyCurve(X, Testfiles{n}, parameterO);
[alpha, beta, gamma, P, R, F] = evaluateNoveltyCurve(Y, Testfiles{n}, parameterO);

% end